Skip to content

Commit c490aaf

Browse files
committed
Add sheet-based previews
1 parent 31d55e8 commit c490aaf

File tree

4 files changed

+102
-36
lines changed

4 files changed

+102
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ScrollKit is a SwiftUI SDK that adds powerful scroll features, like offset track
2020
<img src="Resources/Demo.gif" width=425 />
2121
</p>
2222

23-
ScrollKit works on all major Apple platforms and is designed to be easy to use. It current doesn't use the new `ScrollView` APIs for OS backwards compatibility reasons, but will eventually do so.
23+
ScrollKit works on all major Apple platforms and is designed to be easy to use. It doesn't use the new `ScrollView` APIs for backwards compatibility reasons, but will eventually do so.
2424

2525

2626

Sources/ScrollKit/Examples/Spotify+PreviewScreen.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public extension Spotify {
1919

2020
private var info: PreviewInfo
2121

22-
@State
23-
private var scrollOffset: CGPoint = .zero
22+
@Environment(\.dismiss)
23+
private var dismiss
2424

2525
@State
26-
private var headerVisibleRatio: CGFloat = 1
26+
private var headerVisibleRatio = 1.0
2727

28-
@Environment(\.dismiss)
29-
private var dismiss
28+
@State
29+
private var scrollOffset = CGPoint.zero
3030

3131
public var body: some View {
3232
ScrollViewWithStickyHeader(
@@ -90,7 +90,7 @@ private extension View {
9090
}
9191
}
9292

93-
#Preview {
93+
#Preview("Navigation") {
9494

9595
NavigationView {
9696
#if os(macOS)
@@ -104,3 +104,25 @@ private extension View {
104104
.navigationViewStyle(.stack)
105105
#endif
106106
}
107+
108+
#Preview("Sheet") {
109+
110+
struct Preview: View {
111+
112+
@State var isPresented = false
113+
114+
var body: some View {
115+
Button("Present") {
116+
isPresented.toggle()
117+
}
118+
.sheet(isPresented: $isPresented) {
119+
NavigationView {
120+
Spotify.PreviewScreen(info: .regina)
121+
}
122+
.navigationViewStyle(.stack)
123+
}
124+
}
125+
}
126+
127+
return Preview()
128+
}

Sources/ScrollKit/ScrollKit.docc/ScrollKit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ScrollKit is a Swift SDK that lets adds powerful scroll features to SwiftUI.
1010

1111
ScrollKit is a SwiftUI SDK that adds powerful scroll features, like offset tracking and a header view that stretches & transforms as you pull down, and sticks to the top when you scroll.
1212

13-
ScrollKit works on all major Apple platforms and is designed to be easy to use. It current doesn't use the new `ScrollView` APIs for OS backwards compatibility reasons, but will eventually do so.
13+
ScrollKit works on all major Apple platforms and is designed to be easy to use. It doesn't use the new ScrollView APIs for backwards compatibility reasons, but will eventually do so.
1414

1515

1616

Sources/ScrollKit/ScrollViewWithStickyHeader.swift

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,42 +145,58 @@ private extension ScrollViewWithStickyHeader {
145145
}
146146
}
147147

148-
#Preview {
148+
private struct Preview: View {
149149

150-
struct Preview: View {
151-
152-
@State
153-
var selection = 0
154-
155-
func header() -> some View {
156-
#if canImport(UIKit)
157-
TabView {
158-
Color.red.tag(0)
159-
Color.green.tag(1)
160-
Color.blue.tag(2)
150+
@State
151+
var headerVisibleRatio = 0.0
152+
153+
@State
154+
var scrollOffset = CGPoint.zero
155+
156+
func header() -> some View {
157+
#if canImport(UIKit)
158+
TabView {
159+
Color.red.tag(0)
160+
Color.green.tag(1)
161+
Color.blue.tag(2)
162+
}
163+
.tabViewStyle(.page)
164+
.overlay {
165+
VStack {
166+
Text("Offset: \(scrollOffset.y)")
167+
Text("Ratio: \(headerVisibleRatio)")
161168
}
162-
.tabViewStyle(.page)
163-
#else
169+
}
170+
#else
171+
VStack {
172+
Color.blue
164173
Color.red
165-
#endif
166174
}
167-
168-
var body: some View {
169-
ScrollViewWithStickyHeader(
170-
.vertical,
171-
header: header,
172-
headerHeight: 250,
173-
headerMinHeight: 150,
174-
showsIndicators: false
175-
) {
176-
LazyVStack {
177-
ForEach(1...100, id: \.self) {
178-
Text("\($0)")
179-
}
175+
#endif
176+
}
177+
178+
var body: some View {
179+
ScrollViewWithStickyHeader(
180+
.vertical,
181+
header: header,
182+
headerHeight: 250,
183+
headerMinHeight: 150,
184+
showsIndicators: false,
185+
onScroll: { offset, headerVisibleRatio in
186+
self.scrollOffset = offset
187+
self.headerVisibleRatio = headerVisibleRatio
188+
}
189+
) {
190+
LazyVStack {
191+
ForEach(1...100, id: \.self) {
192+
Text("\($0)")
180193
}
181194
}
182195
}
183196
}
197+
}
198+
199+
#Preview("Navigation") {
184200

185201
return NavigationView {
186202
#if os(macOS)
@@ -195,7 +211,35 @@ private extension ScrollViewWithStickyHeader {
195211
#endif
196212
}
197213

214+
#Preview("Sheet") {
215+
216+
struct SheetPreview: View {
217+
218+
@State var isPresented = true
219+
220+
var body: some View {
221+
Button("Present") {
222+
isPresented.toggle()
223+
}
224+
.sheet(isPresented: $isPresented) {
225+
Preview()
226+
}
227+
}
228+
}
229+
230+
return SheetPreview()
231+
}
232+
198233
private extension View {
234+
235+
func isInSheet(in geo: GeometryProxy) -> Bool {
236+
#if os(iOS)
237+
guard UIScreen.main.traitCollection.userInterfaceIdiom == .phone else { return false }
238+
return geo.safeAreaInsets.top == 0
239+
#else
240+
return false
241+
#endif
242+
}
199243

200244
@ViewBuilder
201245
func prefersNavigationBarHidden() -> some View {

0 commit comments

Comments
 (0)