Skip to content

Commit

Permalink
✨ Refactor toast animations and transitions
Browse files Browse the repository at this point in the history
- Set toast view z-index based on toast id
- Add transition animation for toast removal
- Simplify toast removal logic
  • Loading branch information
WhiteHyun committed Mar 24, 2024
1 parent 95d7e6b commit 09e9ef5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DesignSystemApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ContentView: View {
var body: some View {
VStack {
Button("Toast!") {
Toast.shared.present(title: "Hello", symbol: "globe")
Toast.shared.present(title: "Airpods Pro", symbol: "airpodspro", isUserInteractionEnabled: true)
}
}
.padding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct ToastGroup: View {
ToastView(size: size, item: toast)
.scaleEffect(scale(toast))
.offset(y: offsetY(toast))
.zIndex(Double(model.toasts.firstIndex(where: { $0.id == toast.id }) ?? 0))
}
}
.padding(.bottom, safeArea.top == .zero ? 15 : 10)
Expand Down
20 changes: 3 additions & 17 deletions Shared/Sources/DesignSystem/Components/Toast/View/ToastView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,18 @@ struct ToastView: View {
}
}
)
.offset(y: animateIn ? 0 : 150)
.offset(y: !animateOut ? 0 : 150)
.task {
guard !animateIn else { return }
withAnimation(.snappy) {
animateIn = true
}

try? await Task.sleep(for: .seconds(item.duration.rawValue))

removeToast()
}
// Limiting Size
.frame(maxWidth: size.width * 0.7)
.transition(.offset(y: 150))
}

private func removeToast() {
guard !animateOut else { return }
withAnimation(.snappy, completionCriteria: .logicallyComplete) {
animateOut = true
} completion: {
removeToastItem()
withAnimation(.snappy ) {
Toast.shared.toasts.removeAll(where: { $0.id == item.id })
}
}

private func removeToastItem() {
Toast.shared.toasts.removeAll(where: { $0.id == item.id })
}
}

0 comments on commit 09e9ef5

Please sign in to comment.