Exhibition is a framework and generator for displaying and debugging a SwiftUI component library.
Inspired by Storybook and Showkase
- Add
https://github.com/mjarvis/Exhibition
to your project via Xcode. - Install Sourcery
- Copy Exhibition.swifttemplate into your project
- Modify your SwiftUI previews to use
ExhibitProvider
import Exhibition struct Foo_Previews: ExhibitProvider, PreviewProvider { static var exhibitName: String = "Foo" static var exhibitSection: String = "Bar" static func exhibitContent(context: Context) -> some View { Foo( title: context.parameter(name: "title", defaultValue: "Title"), content: context.parameter(name: "content") ) } }
- Run
Sourcery
to generate your Exhibition:sourcery --sources Your/Source/Path --templates Exhibition.swifttemplate --output ./Sources/Generated
- Show
Exhibition()
in a swift view
If you would like your exhibit to have some custom layout, there is an optional function in ExhibitProvider
you can implement.
Here is an example of embeding the exhibit within a List
:
static func exhibitLayout(content: AnyView) -> some View {
List {
content
}
}
You can also provide a custom View
here to provide presentation samples:
struct CustomLayout<Content: View>: View {
let content: Content
@State var isPresented: Bool = false
var body: some View {
Button("Open") {
isPresented = true
}
.sheet(isPresented: $isPresented) {
content
}
}
}
static func exhibitLayout(content: AnyView) -> some View {
CustomLayout(content: content)
}
Exhibition supports a number of types in the parameter list of the debug menu.
You can add your own arbitrary types along with a view to modify the parameter, or override the views for existing types.
Conform to ParameterView
, and pass the type in via the .parameterView
modifier on Exhibition
.
struct DoublingStringParameterView: ParameterView {
let key: String
@Binding var value: String
var body: some View {
Button(key) {
value += value
}
}
}
Exhibition()
.parameterView(DoublingStringParameterView.self)