eure/swift-storybook
Über uns
A platform that works with an agent to create UIs using the #Preview macro.
Web- und App-EntwicklungGemessener Sternzuwachs
- in 24h
- -2
- in 7 days
- -2
- in 15 days
- -2
- in 1 month
- Verlauf wird gesammelt
Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.
README.md
OriginalStorybook for iOS and macOS
Turn the #Preview declarations already in your app into an in-app component catalog.
Add Storybook() once, then keep writing previews as usual. Storybook discovers them at runtime and adds them to the catalog automatically—there is no separate registration step.
#Preview("Circle") {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
#Preview("Circle2") {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
#Preview {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
#Preview {
UISwitch()
}
#Preview {
UIColorPickerViewController()
}
Motivation
Xcode previews can become difficult to keep working reliably in very large projects, especially when the build is complex. When that happens, existing #Preview declarations often stop contributing to the development loop even though they still describe valuable component states.
Storybook was created to preserve that investment. It discovers previews from a regular app build and presents them as a catalog, allowing teams to reuse their preview code and maintain a smooth UI development loop through the build and runtime path their project already relies on.
Even when Xcode previews work well, running Storybook as part of an app provides additional benefits:
- Open previews directly on a physical device.
- Keep the catalog available in the installed app across launches, independently of an Xcode preview session.
- Include a Storybook entry point in debug or internal builds and open it from within the app whenever it is needed.
From #Preview to Storybook
#Preview("Circle") {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
The preview automatically appears in Storybook:
The same declaration continues to work as an Xcode preview, so your preview code becomes the source of truth for both development and the in-app catalog.
The included swift-storybook-visual-check skill also turns that catalog into a repeatable visual-check workflow for coding agents.
Quick start
Add the package and link the
StorybookKitproduct to your app.Put
Storybookat the entry point of your catalog app or debug-only route.
import StorybookKit
import SwiftUI
struct ContentView: View {
var body: some View {
Storybook()
}
}
- Add
#Previewdeclarations anywhere in the linked modules. No Storybook-specific registration is needed.
#Preview("Circle") {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
Native macOS host
Native macOS hosting requires macOS 14 or later. Link StorybookKit into a
SwiftUI or AppKit app and embed Storybook() in its window, using
NSHostingController when the host uses AppKit. SwiftUI, NSView, and
NSViewController previews are discovered from the app and its linked dynamic
frameworks. Controller previews open as native sheets.
The macOS catalog uses a resizable sidebar for preview folders, history, and search, with the selected preview in the detail pane. The toolbar can hide or show the sidebar. An exact-page launch selects and reveals its catalog row.
import AppKit
import StorybookKit
import SwiftUI
@main
struct CatalogApp: App {
private let launchRequest = StorybookLaunchRequest(
arguments: ProcessInfo.processInfo.arguments
) ?? .catalog
var body: some Scene {
WindowGroup {
Storybook(launchRequest: launchRequest)
}
}
}
#Preview("Native button") {
NSButton(title: "Continue", target: nil, action: nil)
}
Open Development/Storybook.xcodeproj, select the SwiftUIDemoApp scheme, and
choose My Mac to run the demo as a native macOS app. The same scheme also
runs on iOS. SwiftUI previews are shared, and the Mac catalog includes native
AppKit counters and a dismissible controller sheet.
The smaller macOS host remains available as a standalone SwiftPM executable package that uses the repository as a local dependency:
swift build --package-path Development/MacDemo
Book, BookPage, BookPageSelector, and StorybookLaunchRequest use the same
API on both platforms, including exact-page arguments and accessibility
identifiers. Native preview and image-export types follow the platform:
| Export content | iOS | macOS |
|---|---|---|
| SwiftUI | StorybookViewportRenderer |
StorybookViewportRenderer |
| Native view | StorybookUIViewRenderer / StorybookUIView |
StorybookNSViewRenderer / StorybookNSView |
| Presented controller | StorybookPresentedViewControllerRenderer / UIViewController |
StorybookPresentedViewControllerRenderer / NSViewController |
| Exported image | StorybookExportImage.image: UIImage |
StorybookExportImage.image: NSImage |
| Safe-area insets | UIEdgeInsets |
NSEdgeInsets |
Native view export requires a host attached to a live window; the preview factory returns an unattached view. Controller export captures the presented window's content area, without the window's title bar. On macOS, image export preserves the visible viewport of native scroll views; it does not expand their documents into a full-content image. The included agent visual-check and viewport-artifact workflow below targets the iOS Simulator; use the macOS demo and native macOS window tooling for Mac checks.
Preview discovery
Storybook automatically discovers previews in the app executable and linked dynamic frameworks.
Each catalog entry currently renders one preview. Parameterized or grouped preview declarations show an unsupported-content diagnostic.
[!IMPORTANT] Previews in a static library may require the
-all_loadlinker flag. Without it, the linker can remove preview symbols that appear unused, preventing Storybook from discovering them.
Agentic Coding
Storybook provides a deterministic bridge from UI code to visual verification in Simulator. The included swift-storybook-visual-check skill guides a coding agent through the complete check:
- Select an exact
#PrevieworBookPageby name. - Disambiguate duplicate names with the source file and declaration line when needed.
- Launch the host app directly into that page.
- Verify its source-qualified accessibility identifier and rendered UI.
- Prefer a host viewport artifact when available: it excludes Storybook chrome,
expands root scroll content, and captures
UIViewControllerpreviews as a full presented window. Verify its manifest selector, render mode, appearance, dimensions, and SHA-256 throughsimctl get_app_container. - Otherwise, capture a clean simulator screenshot only after hierarchy verification.
This makes UI validation reproducible without navigating the catalog by hand or adding a separate route for every component.
To onboard another app, use the create-storybook-project-skill skill. It connects StorybookLaunchRequest to the host's real startup lifecycle, maps project-specific environment variables onto the standard launch arguments, and generates a repository-local visual-check skill with the exact scheme, bundle identifier, runner, and simulator workflow.
[!NOTE] The host app must first connect
StorybookLaunchRequestto its startup lifecycle and includeStorybookKitin the selected build configuration. The package defines the launch contract but cannot replace an app's root UI on its own.
Programmable launch
Storybook can parse a standard process argument and open its catalog or an exact page immediately. The host app must check the request before starting its normal UI; this package cannot intercept or replace an app's startup path by itself.
import Foundation
import StorybookKit
import SwiftUI
@main
struct ExampleApp: App {
private let storybookLaunchRequest: StorybookLaunchRequest? = .init(
arguments: ProcessInfo.processInfo.arguments
)
var body: some Scene {
WindowGroup {
if let storybookLaunchRequest {
Storybook(launchRequest: storybookLaunchRequest)
} else {
AppRootView()
}
}
}
}
Use the same request with a custom book:
@MainActor
func makeCustomStorybook(
launchRequest: StorybookLaunchRequest
) -> some View {
let book = Book(title: "Design System") {
BookPage(title: "Primary button") {
Button("Continue") {}
}
}
return StorybookDisplayRootView(
bookStore: BookStore(book: book),
launchRequest: launchRequest
)
}
The standard arguments are:
--storybook
--storybook <page-name>
--storybook --storybook-name=<page-name>
--storybook <page-name> --storybook-fullscreen
--storybook <page-name> --storybook-file <module/file.swift>
--storybook <page-name> --storybook-file <module/file.swift> --storybook-line <line>
--storybook <page-name> --storybook-fullscreen --storybook-file <module/file.swift> --storybook-line <line>
--storybook opens the catalog. Supply each token above as a distinct process argument; a page name containing spaces remains one argument. The positional form is convenient for ordinary names; use the single argument --storybook-name=<page-name> when the exact name is empty or begins with -. A name-only page request succeeds only when exactly one page has that name. Add the compiler-emitted #fileID, then the declaration line when necessary, to disambiguate duplicate names. Supplied page names and file identifiers are matched by their original UTF-8 bytes, without trimming, Unicode normalization, case folding, fuzzy search, or order-dependent fallback. Not-found and ambiguous requests remain visible in Storybook with candidate source locations and reusable launch-argument entries instead of opening another page. Pages that share the same name, file ID, and line must be given a unique declaration name or source location; Storybook does not choose between indistinguishable declarations.
Add --storybook-fullscreen after a page name to open that page without Storybook's navigation presentation. On iPhone, iPad, and Mac Catalyst, this uses a full-screen cover and displays the preview without Storybook's NavigationStack, title, appearance toolbar, or outer scrolling container; a preview-owned NavigationStack then supplies its own navigation and toolbar. Existing page requests continue to use push navigation. On native macOS, the request selects the page in the existing sidebar detail view rather than opening a separate cover.
On iPhone, the fullscreen preview has a draggable circular control near the lower trailing edge, with a glass effect on iOS 26 and later. Drag it to move, release to spring toward a horizontal edge based on release velocity, or catch it mid-spring to drag again. Tap it to open a SwiftUI Menu with Return to Catalog; dismissing the menu without choosing the action preserves the preview. The control is hosted alongside the preview inside the same fullscreen view controller, and touches outside the visible circle reach the preview. A sheet or controller presented by the preview can cover the control; close that presentation first to return. The control avoids a docked keyboard but does not move for a floating or undocked keyboard.
On iPad, Mac Catalyst, and native macOS, there is no floating control. Add .commands { StorybookCatalogCommands() } to your host's SwiftUI scene to expose View > Return to Catalog and the shortcut ⌘⌥⇧R, as the demo does. On iPadOS versions without a menu bar, use the hardware keyboard shortcut. On native macOS, the command clears the current detail selection.
From the catalog on iPhone, iPad, or Mac Catalyst, open a page row's context menu and choose Open Fullscreen. The catalog, search text, and navigation state remain underneath the cover. Opening from the catalog ends active catalog text editing without clearing the query or resetting the scroll position.
The iOS demo includes #Preview("Navigation Stack Fullscreen") in Development/SwiftUIDemoApp/Component.swift for checking a preview-owned stack and toolbar. Launch it with these distinct arguments:
--storybook
Navigation Stack Fullscreen
--storybook-fullscreen
--storybook-file
SwiftUIDemoApp/Component.swift
--storybook-line
58
The expected page identifier is storybook.page|27:Navigation Stack Fullscreen|30:SwiftUIDemoApp/Component.swift|58.
The input and sheet fixture lets you check preview-owned text input, child presentations, and the floating control.
Two colored fixtures isolate safe-area rendering. Fullscreen Safe Area Plain checks edge-to-edge indigo behind content positioned inside the safe area. Fullscreen Safe Area Navigation Stack uses teal on the root and pink on a pushed detail, with safe-area markers and toolbar actions on both. Their selectors and page markers are:
Fullscreen Safe Area Plain:storybook.page|26:Fullscreen Safe Area Plain|30:SwiftUIDemoApp/Component.swift|203Fullscreen Safe Area Navigation Stack:storybook.page|37:Fullscreen Safe Area Navigation Stack|30:SwiftUIDemoApp/Component.swift|207
Use --storybook-fullscreen with either selector, inspect the top and bottom color edges and safe-area labels, then push the detail in the NavigationStack fixture and repeat the check.
After a page opens, its root exposes a deterministic accessibility identifier in this form:
storybook.page|<name-byte-count>:<name>|<fileID-byte-count>:<fileID>|<line>
The counts are UTF-8 byte counts. This lets automation prove that a qualified request opened the intended page before taking a screenshot.
Appearance
Each page has a Light / Dark toggle in its navigation bar. A page opens in the host's appearance, and a tap switches only that page, including UIKit views inside it, without relaunching the app. The catalog and other pages keep the host's appearance.
The toggle sets the SwiftUI color scheme of the page's content. A SwiftUI sheet presented from the page inherits it for its content, but its background keeps the host's appearance, and alerts keep the host's appearance. To check presented UI, change the host's appearance instead.
The toggle exposes the accessibility identifier storybook.appearance.toggle.
Maintainers
License
Storybook-ios is released under the MIT license.
Ähnliche Projekte entdecken
Wird geladen…