88import SwiftUI
99
1010extension View {
11- func toast(
11+ func toast< Label : View > (
1212 isPresented: Binding < Bool > ,
1313 duration: TimeInterval = 2 ,
14- message: String ,
1514 action: ( ( ) -> Void ) ? = nil ,
16- onDismiss: ( ( ) -> Void ) ? = nil
15+ onDismiss: ( ( ) -> Void ) ? = nil ,
16+ @ViewBuilder label: @escaping ( ) -> Label
1717 ) -> some View {
1818 self
1919 . frame ( maxWidth: . infinity, maxHeight: . infinity)
2020 . overlay ( alignment: . bottom) {
2121 ToastOverlayView (
2222 isPresented: isPresented,
2323 duration: duration,
24- message: message,
2524 action: action,
26- onDismiss: onDismiss
25+ onDismiss: onDismiss,
26+ label: label
2727 )
2828 . padding ( . horizontal, 12 )
2929 }
3030 }
3131}
3232
33- private struct ToastOverlayView : View {
33+ private struct ToastOverlayView < Label : View > : View {
3434 @Binding var isPresented : Bool
3535 let duration : TimeInterval
36- let message : String
3736 let action : ( ( ) -> Void ) ?
3837 let onDismiss : ( ( ) -> Void ) ?
38+ @ViewBuilder let label : ( ) -> Label
3939
4040 @State private var yOffset : CGFloat = 0
4141 @State private var opacityValue : Double = 0
@@ -45,8 +45,8 @@ private struct ToastOverlayView: View {
4545 var body : some View {
4646 if isPresented {
4747 ToastCardView (
48- message ,
49- textColor : action == nil ? . primary : . blue
48+ label ,
49+ color : action == nil ? . primary : . blue
5050 )
5151 . offset ( y: yOffset)
5252 . opacity ( opacityValue)
@@ -106,24 +106,21 @@ private struct ToastOverlayView: View {
106106 }
107107}
108108
109- private struct ToastCardView : View {
110- let message : String
111- let textColor : Color
109+ private struct ToastCardView < Label : View > : View {
110+ @ ViewBuilder let label : Label
111+ let color : Color
112112
113113 init (
114- _ message : String ,
115- textColor : Color = . primary
114+ @ ViewBuilder _ label : @escaping ( ) -> Label ,
115+ color : Color = . primary
116116 ) {
117- self . message = message
118- self . textColor = textColor
117+ self . label = label ( )
118+ self . color = color
119119 }
120120
121121 var body : some View {
122- Text ( message)
123- . font ( . caption)
124- . foregroundStyle ( textColor)
125- . multilineTextAlignment ( . center)
126- . lineLimit ( 3 )
122+ self . label
123+ . foregroundStyle ( color)
127124 . padding ( . vertical, 12 )
128125 . padding ( . horizontal, 14 )
129126 . background {
0 commit comments