Skip to content

Commit 90e1cb3

Browse files
Merge pull request #19 from shortcut/feature/overlay-with-preseting-state
Feature/overlay with preseting state
2 parents ffab3eb + 3fbaaa0 commit 90e1cb3

File tree

5 files changed

+104
-29
lines changed

5 files changed

+104
-29
lines changed

Sources/ShortcutUI/Extensions/String+Extension.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import SwiftUI
1010

1111
public extension String {
12-
13-
var text: SwiftUI.Text { SwiftUI.Text(self) }
12+
var text: SwiftUI.Text {
13+
SwiftUI.Text(self)
14+
}
1415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// View+AnyView.swift
3+
// ShortcutUI
4+
//
5+
// Created by Sheikh Bayazid on 2022-12-15.
6+
//
7+
8+
import SwiftUI
9+
10+
public extension View {
11+
func eraseToAnyView() -> AnyView {
12+
AnyView(self)
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// View+CornerRadius.swift
3+
// ShortcutUI
4+
//
5+
// Created by Sheikh Bayazid on 2022-12-15.
6+
//
7+
8+
import SwiftUI
9+
10+
#if !os(macOS)
11+
private struct RoundedCorner: Shape {
12+
var radius: CGFloat = .infinity
13+
var corners: UIRectCorner = .allCorners
14+
15+
func path(in rect: CGRect) -> Path {
16+
let path = UIBezierPath(
17+
roundedRect: rect,
18+
byRoundingCorners: corners,
19+
cornerRadii: CGSize(width: radius, height: radius)
20+
)
21+
return Path(path.cgPath)
22+
}
23+
}
24+
#endif
25+
26+
public extension View {
27+
#if !os(macOS)
28+
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
29+
clipShape(RoundedCorner(radius: radius, corners: corners))
30+
}
31+
#endif
32+
}

Sources/ShortcutUI/Extensions/View+Extension.swift Sources/ShortcutUI/Extensions/View/View+ModalView.swift

+1-27
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111
import UIKit
1212
#endif
1313

14-
/// Fixes issues related to having multiple .sheet() or .fullScreenCover() functions in the same view hierarcy that SwiftUI cant handle
14+
/// Fixes issues related to having multiple .sheet() or .fullScreenCover() functions in the same view hierarchy that SwiftUI can't handle
1515

1616
public extension View {
1717
func sheetWithoutConflicts<Item, Content>(item: Binding<Item?>,
@@ -62,29 +62,3 @@ public extension View {
6262
}
6363
#endif
6464
}
65-
66-
public extension View {
67-
func eraseToAnyView() -> AnyView {
68-
AnyView(self)
69-
}
70-
71-
#if !os(macOS)
72-
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
73-
clipShape(RoundedCorner(radius: radius, corners: corners))
74-
}
75-
#endif
76-
}
77-
78-
#if !os(macOS)
79-
private struct RoundedCorner: Shape {
80-
var radius: CGFloat = .infinity
81-
var corners: UIRectCorner = .allCorners
82-
83-
func path(in rect: CGRect) -> Path {
84-
let path = UIBezierPath(roundedRect: rect,
85-
byRoundingCorners: corners,
86-
cornerRadii: CGSize(width: radius, height: radius))
87-
return Path(path.cgPath)
88-
}
89-
}
90-
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// View+Overlay.swift
3+
// ShortcutUI
4+
//
5+
// Created by Sheikh Bayazid on 2022-12-15.
6+
//
7+
8+
import SwiftUI
9+
10+
public extension View {
11+
/// Presents an overlay when a Boolean value that you provide is true.
12+
///
13+
/// Use this method when you want to present an overlay view to the
14+
/// user when a Boolean value you provide is true. The example
15+
/// below displays an overlay view when the user toggles the
16+
/// `isShowingOverlay` variable by clicking or tapping on
17+
/// the "Show overlay" button:
18+
///
19+
/// struct Example: View {
20+
/// @State private var isShowingOverlay = false
21+
///
22+
/// var body: some View {
23+
/// VStack(spacing: 50) {
24+
/// Button("Show overlay") {
25+
/// isShowingOverlay.toggle()
26+
/// }
27+
///
28+
/// RoundedRectangle(cornerRadius: 12)
29+
/// .fill(Color.blue)
30+
/// .overlay(isPresented: isShowingOverlay) {
31+
/// Text("Hello, world!")
32+
/// }
33+
/// }
34+
/// }
35+
/// }
36+
///
37+
/// - Parameters:
38+
/// - alignment: The alignment of the overlay content that you create in the modifier's
39+
/// `content` closure.. The default is `Alignment/center`.
40+
/// - isPresented: A Boolean value that determines whether
41+
/// to present the overlay that you create in the modifier's `content` closure.
42+
/// - content: A closure that returns the content of the overlay.
43+
@ViewBuilder
44+
func overlay<Content: View>(
45+
alignment: Alignment = .center,
46+
isPresented: Bool,
47+
@ViewBuilder content: () -> Content
48+
) -> some View {
49+
overlay(
50+
isPresented ? content() : nil,
51+
alignment: alignment
52+
)
53+
}
54+
}

0 commit comments

Comments
 (0)