https://github.com/fluidgroup/swiftui-functional-component-macro
A Swift macro library that enables to make SwiftUI stateful component from function declaration
https://github.com/fluidgroup/swiftui-functional-component-macro
Last synced: 8 months ago
JSON representation
A Swift macro library that enables to make SwiftUI stateful component from function declaration
- Host: GitHub
- URL: https://github.com/fluidgroup/swiftui-functional-component-macro
- Owner: FluidGroup
- License: apache-2.0
- Created: 2025-04-07T14:04:20.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T02:27:56.000Z (about 1 year ago)
- Last Synced: 2025-04-08T03:28:47.076Z (about 1 year ago)
- Language: Swift
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swiftui-functional-component
A Swift macro library that enables to make SwiftUI stateful component from function declaration. It automatically converts a function with parameters into a SwiftUI View struct with proper state management (@State, @Binding) and initialization.
## Overview
```swift
@ViewComponent
func hoge(arg1: Int, arg2: String, arg3: @escaping () -> Void) -> some View {
@State var count: Int = 0
VStack {
Text("\(count), \(arg1), \(arg2)")
Button("Click me") {
count += 1
}
}
}
```
Macro expands above into this:
```swift
func hoge(arg1: Int, arg2: String, arg3: @escaping () -> Void) -> some View {
struct Component: View {
let arg1: Int
let arg2: String
let arg3: () -> Void
@State var count: Int = 0
init(arg1: Int, arg2: String, arg3: @escaping () -> Void) {
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
}
var body: some View {
VStack {
Text("\(count), \(arg1), \(arg2)")
Button("Click me") {
count += 1
}
}
}
}
return Component(arg1: arg1, arg2: arg2, arg3: arg3)
}
```