Skip to content

Commit

Permalink
feat: Create Toast
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercen-Lee committed Apr 1, 2024
1 parent 191fe34 commit e2e6e0e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Source/DDS/Component/TextField/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public struct DodamTextField: View {
VStack(spacing: 12) {
ZStack(alignment: .leading) {
Text(title)
.scaleEffect(isHighlighted ? 0.75 : 1,
anchor: .topLeading)
.scaleEffect(
isHighlighted ? 0.75 : 1,
anchor: .topLeading
)
.padding(.top, isHighlighted ? -30 : 0)
Group {
if isSecured {
Expand Down
18 changes: 18 additions & 0 deletions Source/DDS/Component/Toast/Extension/Toast+ViewExt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import SwiftUI

@available(macOS 12, iOS 15, *)
public extension View {

@ViewBuilder
func toast<C: View>(
isPresented: Binding<Bool>,
timeout: Double? = nil,
@ViewBuilder content: @escaping () -> C
) -> some View {
DodamToast(
isPresented: isPresented,
timeout: timeout,
content: content
) { self }
}
}
77 changes: 77 additions & 0 deletions Source/DDS/Component/Toast/Toast.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import SwiftUI

@available(macOS 12, iOS 15, *)
public struct DodamToast<C: View, V: View>: View {

@Binding private var isPresented: Bool
private let timeout: Double?
private let content: () -> C
private let view: () -> V

public init(
isPresented: Binding<Bool>,
timeout: Double?,
@ViewBuilder content: @escaping () -> C,
@ViewBuilder view: @escaping () -> V
) {
self._isPresented = isPresented
self.animatedPresentation = isPresented.wrappedValue
self.timeout = timeout
self.content = content
self.view = view
}

@State private var animatedPresentation: Bool

public var body: some View {
ZStack(alignment: .top) {
view()
.frame(maxWidth: .infinity, maxHeight: .infinity)
if animatedPresentation {
content()
.padding(.vertical, 15)
.padding(.horizontal, 40)
.background(Color(.tertiarySystemBackground))
.clipShape(Capsule())
.shadow(color: Color.black.opacity(0.08), radius: 2, y: 1)
.shadow(color: Color.black.opacity(0.08), radius: 5, y: 4)
.transition(
.move(edge: .top)
.combined(with: .opacity)
)
}
}
.onChange(of: isPresented) { newValue in
withAnimation(.spring(duration: 0.2)) {
animatedPresentation = newValue
}
if newValue, let timeout {
let time = 1_000_000_000 * UInt64(timeout)
Task {
try? await Task.sleep(nanoseconds: time)
isPresented = false
}
}
}
}
}

#Preview {
struct DodamToastPreview: View {

@State private var isPresented: Bool = false

var body: some View {
Button("Present") {
isPresented.toggle()
}
.toast(
isPresented: $isPresented,
timeout: 3
) {
Text("Hello, world!")
}
}
}
return DodamToastPreview()
}

0 comments on commit e2e6e0e

Please sign in to comment.