-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
191fe34
commit e2e6e0e
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |