Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support #Preview parsing (iOS 17+ only) #50

Merged
merged 8 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 110 additions & 5 deletions Development/Demo/MyBook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,114 @@ let myBook = Book.init(
}
)

#Preview("Simple Sample") {
Text("Yo there!")
.foregroundStyle(Color.green)
}

struct StorybookTrait: PreviewModifier {
func body(content: Content, context: Void) -> some View {
content
}
}

@available(iOS 18.0, *)
extension PreviewTrait where T == Preview.ViewTraits {

@MainActor public static var storybook: PreviewTrait<Preview.ViewTraits> {
return .init(.modifier(StorybookTrait()))
}
}

struct Storybook: View {

let content: AnyView
init<Content: View>(
@ViewBuilder content: () -> Content
) {
self.content = .init(content())
}

var body: some View {
content
}
}

@available(iOS 18.0, *)
#Preview(traits: .storybook) {
Text("My Component")
}

#Preview {
Text("My Component")
}

#Preview {
Text("NO TITLE!")
.foregroundStyle(Color.purple)
}

@available(iOS 17.0, *)
#Preview("States Sample") {
@Previewable @State var state: Int = 1

VStack {
Text("Aloha \(state)")
Button(
action: { state += 1 },
label: { Text("+") }
)
}
}

@available(iOS 17.0, *)
#Preview("UIView Sample") {
{
var state: Int = 1

let label = UILabel()
label.text = "Test \(state)"

let button = UIButton(
configuration: .bordered(),
primaryAction: .init { _ in
state += 1
label.text = "Test \(state)"
}
)
button.setTitle("+", for: .normal)
let stack = UIStackView(arrangedSubviews: [label, button])
stack.spacing = 8
return stack
}()
}

@available(iOS 17.0, *)
#Preview("UIViewController Sample") {
{
let controller = UIViewController()
controller.view.backgroundColor = .systemMint
var state: Int = 1

let label = UILabel()
label.text = "Test \(state)"

let button = UIButton(
configuration: .bordered(),
primaryAction: .init { _ in
state += 1
label.text = "Test \(state)"
}
)
button.setTitle("+", for: .normal)
let stack = UIStackView(arrangedSubviews: [label, button])
stack.spacing = 8
stack.frame = controller.view.bounds
controller.view.addSubview(stack)
return controller
}()
}

#StorybookPage(title: "UILabel updating text") {
BookPreview { context in
let label = UILabel()
Expand Down Expand Up @@ -201,10 +309,10 @@ let myBook = Book.init(
}
}

#Preview("Some title 2") {
#Preview("Title") {
#StorybookPreview<MyLabel> {
BookPreview { _ in
MyLabel(title: "MyLabel 2")
MyLabel(title: "Test")
}
}
}
Expand All @@ -213,9 +321,6 @@ let myBook = Book.init(
BookPreview { _ in
MyLabel(title: "Test")
}
BookPreview { _ in
MyLabel(title: "Test")
}
}

#StorybookPage<MyLabel> {
Expand Down
8 changes: 8 additions & 0 deletions Development/Demo/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ struct RootView: View {
Book.allStorybookPages()
.map({ $0.bookBody })
}

if #available(iOS 17.0, *) {
if let nodes = Book.allBookPreviews() {
Book(title: "#Preview macro") {
nodes
}
}
}
}
)
)
Expand Down
4 changes: 2 additions & 2 deletions Development/Storybook.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -344,7 +344,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "f5a894bbdc3287a91c8c33f864cfb447314305f7fc66cabf1c369e8c3a67521d",
"pins" : [
{
"identity" : "descriptors",
Expand Down Expand Up @@ -32,26 +33,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing.git",
"state" : {
"branch" : "main",
"revision" : "15916c0c328339f54c15d616465d79700e3f7de8"
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
"version" : "0.5.2"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "e7b77228b34057041374ebef00c0fd7739d71a2b",
"version" : "1.15.3"
"revision" : "7b0bbbae90c41f848f90ac7b4df6c4f50068256d",
"version" : "1.17.5"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d",
"version" : "509.1.1"
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
},
{
Expand Down Expand Up @@ -86,19 +87,19 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/FluidGroup/TextureBridging.git",
"state" : {
"branch" : "main",
"revision" : "4383f8a9846a0507d2c22ee9fac22153f9b86fed"
"revision" : "4383f8a9846a0507d2c22ee9fac22153f9b86fed",
"version" : "3.2.1"
}
},
{
"identity" : "textureswiftsupport",
"kind" : "remoteSourceControl",
"location" : "https://github.com/FluidGroup/TextureSwiftSupport.git",
"state" : {
"branch" : "main",
"revision" : "5bae50cab3798dccb8b98c3ffbc70320ae66b45a"
"revision" : "fb748d6a9d0a2dca0635227e1db0360fd26e0e24",
"version" : "3.20.1"
}
}
],
"version" : 2
"version" : 3
}
Loading
Loading