Skip to content

Commit 7a11d04

Browse files
committed
Various Changes and New Things
- Folder Structure - Codes Generalization - Introducing RadixToast - Added ToastDemoView - Introducing `SwipeToDismiss` Modifier - Added ShapeFillApplier & ShapeBorderApplier Modifiers - `ToggleType == Switch` New Styles - Related Changes for ToggleDemoView
1 parent e2aef90 commit 7a11d04

24 files changed

+974
-267
lines changed

Sources/RadixUI/DemoViews/PickerDemoView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ struct PickerDemoView: View {
1818
)
1919
}
2020

21-
@State private var selected: RadixToggleVariant = .switch
21+
@State private var selected: RadixToggleType = .switch
2222

2323
var body: some View {
2424
Picker(String(""), selection: $selected) {
25-
Text("Checkbox").tag(RadixToggleVariant.checkbox)
26-
Text("Switch").tag(RadixToggleVariant.switch)
27-
Text("Toggle").tag(RadixToggleVariant.toggle)
25+
Text("Checkbox").tag(RadixToggleType.checkbox)
26+
Text("Switch").tag(RadixToggleType.switch)
27+
Text("Toggle").tag(RadixToggleType.toggle)
2828
}
2929
.pickerStyle(.segmented)
3030
.padding()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// ToastDemoView.swift
3+
// RadixUI
4+
//
5+
// Created by Amir Mohammadi on 11/27/1403 AP.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ToastDemoView: View {
11+
12+
@State private var presentInfoToast = false
13+
@State private var presentActionToast = false
14+
15+
var body: some View {
16+
VStack(spacing: 25) {
17+
Button {
18+
presentInfoToast = true
19+
} label: {
20+
Label(
21+
"Present Bot Toast",
22+
imageName: "vercel-logo",
23+
imageSize: 20,
24+
bundle: .module
25+
)
26+
}
27+
.buttonStyle(
28+
.radixSolid(
29+
layout: .leading,
30+
radius: .full,
31+
color: .grass
32+
),
33+
isLoading: .constant(false)
34+
)
35+
Button {
36+
presentActionToast = true
37+
} label: {
38+
Label(
39+
"Present Top Toast",
40+
imageName: "vercel-logo",
41+
imageSize: 20,
42+
bundle: .module
43+
)
44+
}
45+
.buttonStyle(
46+
.radixSolid(
47+
layout: .leading,
48+
radius: .full,
49+
color: .grass
50+
),
51+
isLoading: .constant(false)
52+
)
53+
}
54+
.radixInfoToast(
55+
isPresented: $presentInfoToast,
56+
variant: .surface,
57+
position: .bottom,
58+
color: .grass,
59+
duration: 3
60+
) {
61+
Label(
62+
"This is a Radix Info Toast",
63+
imageName: "vercel-logo",
64+
imageSize: 20,
65+
bundle: .module
66+
)
67+
}
68+
.radixActionToast(
69+
isPresented: $presentActionToast,
70+
variant: .soft,
71+
position: .top,
72+
color: .grass,
73+
duration: 0
74+
) {
75+
presentInfoToast = true
76+
} buttonLabel: {
77+
Label(
78+
"Ignored Text",
79+
imageName: "avatar",
80+
imageSize: 20,
81+
bundle: .module
82+
)
83+
} toastLabel: {
84+
Label(
85+
"Button Presents Info Toast",
86+
imageName: "vercel-logo",
87+
imageSize: 20,
88+
bundle: .module
89+
)
90+
}
91+
}
92+
93+
}
94+
95+
#Preview {
96+
ToastDemoView()
97+
}

Sources/RadixUI/DemoViews/ToggleDemoView.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import SwiftUI
1010
struct ToggleDemoView: View {
1111

1212
@State private var checkbox = false
13-
@State private var `switch` = false
13+
@State private var switchSurface = false
14+
@State private var switchSoft = false
1415
@State private var toggle = false
1516

1617
private var radixColor = RadixAutoColor.self
@@ -35,12 +36,24 @@ struct ToggleDemoView: View {
3536
)
3637
)
3738
}
38-
Toggle(isOn: $switch) {
39+
Toggle(isOn: $switchSurface) {
3940
Text("Label Text")
4041
.foregroundStyle(radixColor.grass.text2)
4142
}
4243
.toggleStyle(
4344
.radixSwitch(
45+
style: (variant: .surface, radius: .full),
46+
onColor: .grass,
47+
thumbColor: .whiteA
48+
)
49+
)
50+
Toggle(isOn: $switchSoft) {
51+
Text("Label Text")
52+
.foregroundStyle(radixColor.grass.text2)
53+
}
54+
.toggleStyle(
55+
.radixSwitch(
56+
style: (variant: .soft, radius: .full),
4457
onColor: .grass,
4558
thumbColor: .whiteA
4659
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// SwipeToDismiss.swift
3+
// RadixUI
4+
//
5+
// Created by Amir Mohammadi on 11/25/1403 AP.
6+
//
7+
8+
import SwiftUI
9+
10+
public struct RadixSwipeToDismiss: ViewModifier {
11+
12+
@Binding var isPresented: Bool
13+
var direction: DismissSwipeDirection
14+
15+
init(
16+
isPresented: Binding<Bool>,
17+
direction: DismissSwipeDirection
18+
) {
19+
self._isPresented = isPresented
20+
self.direction = direction
21+
}
22+
23+
@State private var dragOffset: CGFloat = 0
24+
private var dismissThreshold: Double = 60.0
25+
26+
public func body(content: Content) -> some View {
27+
content
28+
.offset(y: direction == .down ? dragOffset : -dragOffset)
29+
.opacity(max(1 - (abs(dragOffset) / dismissThreshold), 0))
30+
.gesture(
31+
32+
DragGesture()
33+
.onChanged { value in
34+
let dragAmount = value.translation.height
35+
if direction == .down, dragAmount > 0 {
36+
dragOffset = dragAmount
37+
} else if direction == .up, dragAmount < 0 {
38+
dragOffset = abs(dragAmount)
39+
}
40+
}
41+
.onEnded { value in
42+
withAnimation {
43+
if dragOffset > dismissThreshold {
44+
isPresented = false
45+
}
46+
dragOffset = 0
47+
}
48+
}
49+
50+
)
51+
}
52+
53+
}

Sources/RadixUI/Helpers/Enums.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
//
77

88
import Foundation
9+
import SwiftUI
10+
11+
// MARK: -
12+
public enum DismissSwipeDirection {
13+
case up, down
14+
}
915

1016
// MARK: - Globally used Enums in RadixUI Elements
1117
public enum RadixElementShapeRadius {
@@ -34,12 +40,32 @@ public enum RadixSliderVariant {
3440
case soft, surface
3541
}
3642

43+
// MARK: - Enums used in RadixToast
44+
public enum RadixToastPosition {
45+
case top, bottom
46+
47+
var edge: Edge {
48+
switch self {
49+
case .top: .top
50+
case .bottom: .bottom
51+
}
52+
}
53+
}
54+
55+
public enum RadixToastVariant {
56+
case soft, surface
57+
}
58+
3759
// MARK: - Enums used in TextFieldStyle
3860
public enum RadixTextFieldVariant {
3961
case surface, soft
4062
}
4163

4264
// MARK: - Enums used in ToggleStyle
43-
public enum RadixToggleVariant {
65+
public enum RadixToggleType {
4466
case checkbox, `switch`, toggle
4567
}
68+
69+
public enum RadixSwitchVariant {
70+
case surface, soft
71+
}

Sources/RadixUI/Helpers/Extensions/Shape+Extensions.swift

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,19 @@
88
import SwiftUI
99

1010
//MARK: - Slider Thumb Shape Modifiers
11-
internal extension Shape {
11+
extension Shape {
1212

13-
func thumbBorderShapeModifier(size: CGSize) -> some View {
13+
public func radixShapeBorderApplier(color: Color, width: CGFloat?, height: CGFloat?) -> some View {
1414
self
15-
.stroke(RadixAutoColor.gray.border3, lineWidth: 2)
15+
.stroke(color, lineWidth: 1.25)
1616
.background(.clear)
17-
.frame(width: size.width, height: size.height)
17+
.frame(width: width, height: height)
1818
}
1919

20-
func thumbActiveShapeModifier(size: CGSize) -> some View {
21-
self
22-
.fill(RadixAutoColor.blackA.border2)
23-
.frame(width: size.width, height: size.height)
24-
}
25-
26-
func thumbShapeModifier(size: CGSize) -> some View {
27-
self
28-
.fill(RadixAutoColor.whiteA.text2)
29-
.frame(width: size.width, height: size.height)
30-
}
31-
32-
}
33-
34-
// MARK: - Slider Track Shape Modifiers
35-
internal extension Shape {
36-
37-
func trackShapeBorderModifier(size: CGFloat) -> some View {
38-
self
39-
.stroke(RadixAutoColor.gray.border2, lineWidth: 1)
40-
.background(.clear)
41-
.frame(height: size)
42-
}
43-
44-
func trackShapeBaseModifier(size: CGFloat, color: Color) -> some View {
45-
self
46-
.fill(color)
47-
.frame(height: size)
48-
}
49-
50-
func trackShapeFillModifier(_ color: Color, _ percentage: CGFloat, _ size: CGFloat) -> some View {
20+
public func radixShapeFillApplier(color: Color, width: CGFloat?, height: CGFloat?) -> some View {
5121
self
5222
.fill(color)
53-
.frame(width: percentage, height: size)
23+
.frame(width: width, height: height)
5424
}
5525

5626
}

Sources/RadixUI/Helpers/Extensions/Toggle+Extensions.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@ extension ToggleStyle where Self == RadixToggleStyle {
1515
boxSize: CGFloat? = nil
1616
) -> Self {
1717
.init(
18-
variant: .checkbox,
18+
type: .checkbox,
19+
switchStyle: nil,
1920
backgroundColor: bgColor,
2021
foregroundColor: iconColor,
2122
size: boxSize
2223
)
2324
}
2425

2526
public static func radixSwitch(
27+
style: (
28+
variant: RadixSwitchVariant,
29+
radius: RadixElementShapeRadius
30+
),
2631
onColor: RadixAutoColor? = nil,
2732
thumbColor: RadixAutoColor? = nil
2833
) -> Self {
2934
.init(
30-
variant: .switch,
35+
type: .switch,
36+
switchStyle: style,
3137
backgroundColor: onColor,
3238
foregroundColor: thumbColor,
3339
size: nil
@@ -40,7 +46,8 @@ extension ToggleStyle where Self == RadixToggleStyle {
4046
boxSize: CGFloat? = nil
4147
) -> Self {
4248
.init(
43-
variant: .toggle,
49+
type: .toggle,
50+
switchStyle: nil,
4451
backgroundColor: bgColor,
4552
foregroundColor: iconColor,
4653
size: boxSize

0 commit comments

Comments
 (0)