Skip to content

Commit e31f24a

Browse files
authored
Update BookPage (#55)
- Display a content as full screen - Move info into top-right button - Automatically push the last shown item on launch ![CleanShot 2025-04-11 at 14 58 21@2x](https://github.com/user-attachments/assets/1df28406-bdc2-4072-a63e-7eaa07a26ab4)
1 parent 1db2a49 commit e31f24a

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,24 @@ struct PreviewRegistryWrapper: Comparable {
8989
case "DeveloperToolsSupport.DefaultPreviewSource<SwiftUI.ViewPreviewBody>": // iOS 18
9090
let makeBody: MakeFunctionWrapper<any SwiftUI.View> = .init(source["structure", "singlePreview", "makeBody"])
9191
return {
92-
VStack {
93-
if let title, !title.isEmpty {
94-
Text(title)
95-
.font(.system(size: 17, weight: .semibold))
96-
}
92+
VStack {
9793
AnyView(makeBody())
98-
Text("\(fileID):\(line)")
99-
.font(.caption.monospacedDigit())
100-
BookSpacer(height: 16)
101-
}
94+
.toolbar {
95+
ToolbarItem(placement: .topBarTrailing) {
96+
Menu {
97+
Button {
98+
UIPasteboard.general.string = "\(fileID):\(line)"
99+
} label: {
100+
Text("\(fileID):\(line)")
101+
.font(.caption.monospacedDigit())
102+
}
103+
} label: {
104+
Image(systemName: "info.circle")
105+
}
106+
107+
}
108+
}
109+
}
102110
}
103111

104112
case "DeveloperToolsSupport.DefaultPreviewSource<__C.UIView>": // iOS 18

Sources/StorybookKit/Primitives/BookPage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public struct BookPage: BookView, Identifiable {
8787
destination
8888
}
8989
} else {
90-
destination
90+
destination
9191
}
9292
}
9393
.listStyle(.plain)

Sources/StorybookKit/StorybookDisplayRootView.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,36 @@ public struct BookActionHosting<Content: View>: View {
4242
}
4343

4444
struct BookContainer: View {
45-
45+
46+
struct UniqueBox<T>: Hashable {
47+
48+
static func == (lhs: UniqueBox<T>, rhs: UniqueBox<T>) -> Bool {
49+
lhs.uuid == rhs.uuid
50+
}
51+
52+
func hash(into hasher: inout Hasher) {
53+
hasher.combine(uuid)
54+
}
55+
56+
let uuid: UUID = .init()
57+
let value: T
58+
59+
init(value: T) {
60+
self.value = value
61+
}
62+
}
63+
4664
// MARK: Properties
4765

4866
@ObservedObject private var store: BookStore
4967

50-
@State private var lastUsedItem: BookPage?
68+
@State private var lastUsedItem: UniqueBox<BookPage>?
5169
@State private var query: String = ""
5270
@State private var result: [BookPage] = []
5371
@State private var currentTask: Task<Void, Error>?
5472

73+
@State var path: NavigationPath = .init()
74+
5575
// MARK: Initializers
5676

5777
@MainActor
@@ -65,7 +85,7 @@ struct BookContainer: View {
6585

6686
public var body: some View {
6787

68-
NavigationStack {
88+
NavigationStack(path: $path) {
6989
List {
7090

7191
if result.isEmpty == false {
@@ -91,15 +111,23 @@ struct BookContainer: View {
91111
}
92112
.navigationTitle(store.title)
93113
.searchable(text: $query, prompt: "Search")
94-
114+
.navigationDestination(for: UniqueBox<BookPage>.self) { page in
115+
page
116+
.value
117+
.destination
118+
.environment(\.bookContext, store)
119+
}
95120
}
121+
96122
.tabItem {
97123
Image(systemName: "list.bullet")
98124
Text("List")
99125
}
100126
.environment(\.bookContext, store)
101127
.onAppear {
102-
lastUsedItem = store.historyPages.first
128+
if let value = store.historyPages.first {
129+
path.append(UniqueBox(value: value))
130+
}
103131
}
104132
.onChange(of: query, perform: { value in
105133

@@ -123,13 +151,7 @@ struct BookContainer: View {
123151
self.result = result
124152
}
125153

126-
})
127-
.sheet(item: $lastUsedItem) { item in
128-
ScrollView {
129-
item.destination
130-
.padding(.vertical, 24)
131-
}
132-
}
154+
})
133155
}
134156

135157
}

0 commit comments

Comments
 (0)