Skip to content

Commit 2dfd33a

Browse files
committed
Add radixInvertedColorScheme functionality
1 parent b6e5027 commit 2dfd33a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

Sources/RadixUI/DemoViews/ToastDemoView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ struct ToastDemoView: View {
4444
.buttonStyle(
4545
.radixSolid(
4646
layout: .leading,
47-
radius: .full,
48-
color: .grass
47+
radius: .large
4948
),
5049
isLoading: .constant(false)
5150
)
@@ -55,6 +54,7 @@ struct ToastDemoView: View {
5554
variant: .surface,
5655
position: .bottom,
5756
color: .grass,
57+
isInverted: false,
5858
duration: 3
5959
) {
6060
Label(
@@ -68,6 +68,7 @@ struct ToastDemoView: View {
6868
$presentActionToast,
6969
variant: .soft,
7070
position: .top,
71+
color: .grass,
7172
duration: 0
7273
) {
7374
presentInfoToast = true

Sources/RadixUI/Helpers/Extensions/View+Extenstions.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension View {
2929
variant: RadixToastVariant,
3030
position: RadixToastPosition,
3131
color: RadixAutoColor? = nil,
32+
isInverted: Bool = false,
3233
duration: Int,
3334
toastLabel: @escaping () -> ToastLabel
3435
) -> some View {
@@ -39,18 +40,20 @@ extension View {
3940
variant: variant,
4041
position: position,
4142
color: color,
43+
isInverted: isInverted,
4244
duration: duration,
4345
toastLabel: toastLabel
4446
)
4547
)
4648
}
47-
49+
4850
/// Shows a Toast on any View of your choice with a Custom Button Action, use it on highest container in your view hierarchy, duration == 0 will disable auto dismiss
4951
public func radixActionToast<ButtonLabel: View, ToastLabel: View>(
5052
_ isPresented: Binding<Bool>,
5153
variant: RadixToastVariant,
5254
position: RadixToastPosition,
5355
color: RadixAutoColor? = nil,
56+
isInverted: Bool = false,
5457
duration: Int,
5558
buttonAction: @escaping () -> Void,
5659
buttonLabel: @escaping () -> ButtonLabel,
@@ -63,14 +66,28 @@ extension View {
6366
variant: variant,
6467
position: position,
6568
color: color,
69+
isInverted: isInverted,
6670
duration: duration,
6771
buttonAction: buttonAction,
6872
buttonLabel: buttonLabel,
6973
toastLabel: toastLabel
7074
)
7175
)
7276
}
73-
77+
78+
/// Inverts the colors of a view based on the Environment's ColorScheme value
79+
@ViewBuilder public func radixInvertedColorScheme(_ shouldInvert: Bool) -> some View {
80+
if shouldInvert {
81+
switch Environment(\.colorScheme).wrappedValue {
82+
case .dark: self.environment(\.colorScheme, .light)
83+
case .light: self.environment(\.colorScheme, .dark)
84+
@unknown default: self
85+
}
86+
} else {
87+
self
88+
}
89+
}
90+
7491
}
7592

7693
extension View where Self == Text {

Sources/RadixUI/RadixDesign/Toast/RadixToast+ViewModifier.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct RadixToastModifier<ButtonLabel: View, ToastLabel: View>: ViewModif
1616
private var position: RadixToastPosition
1717
private var color: RadixAutoColor?
1818
private var duration: Int
19+
private var isInverted: Bool
1920
private var buttonAction: (() -> Void)?
2021
private var buttonLabel: (() -> ButtonLabel)?
2122
@ViewBuilder private var toastLabel: () -> ToastLabel
@@ -29,9 +30,9 @@ public struct RadixToastModifier<ButtonLabel: View, ToastLabel: View>: ViewModif
2930
Spacer()
3031
}
3132
if buttonAction != nil && buttonLabel != nil {
32-
actionToast
33+
actionToast.radixInvertedColorScheme(isInverted)
3334
} else {
34-
infoToast
35+
infoToast.radixInvertedColorScheme(isInverted)
3536
}
3637
if position == .top {
3738
Spacer()
@@ -84,6 +85,7 @@ extension RadixToastModifier {
8485
variant: RadixToastVariant,
8586
position: RadixToastPosition,
8687
color: RadixAutoColor?,
88+
isInverted: Bool,
8789
duration: Int,
8890
buttonAction: @escaping () -> Void,
8991
buttonLabel: @escaping () -> ButtonLabel,
@@ -93,6 +95,7 @@ extension RadixToastModifier {
9395
self.variant = variant
9496
self.position = position
9597
self.color = color
98+
self.isInverted = isInverted
9699
self.duration = duration
97100
self.buttonAction = buttonAction
98101
self.buttonLabel = buttonLabel
@@ -104,13 +107,15 @@ extension RadixToastModifier {
104107
variant: RadixToastVariant,
105108
position: RadixToastPosition,
106109
color: RadixAutoColor?,
110+
isInverted: Bool,
107111
duration: Int,
108112
toastLabel: @escaping () -> ToastLabel
109113
) where ButtonLabel == Never {
110114
self._isPresented = isPresented
111115
self.variant = variant
112116
self.position = position
113117
self.color = color
118+
self.isInverted = isInverted
114119
self.duration = duration
115120
self.toastLabel = toastLabel
116121
}

0 commit comments

Comments
 (0)