@@ -27,7 +27,7 @@ Create a file `hello.gx`:
2727``` go
2828package main
2929
30- component Hello (name: string ) {
30+ func Hello (name: string ) {
3131 Div {
3232 H1 {
3333 ` Hello, {name}!`
@@ -64,7 +64,7 @@ GOOS=js GOARCH=wasm go build -o main.wasm .
6464Components are the building blocks of Guix applications:
6565
6666``` go
67- component Button (label: string , onClick: func (Event)) {
67+ func Button (label: string , onClick: func (Event)) {
6868 Button (OnClick (onClick)) {
6969 Text (label)
7070 }
@@ -99,7 +99,7 @@ btn := NewButton(
9999Use backticks for template strings with embedded expressions:
100100
101101``` go
102- component Counter (count: int ) {
102+ func Counter (count: int ) {
103103 Div {
104104 ` Count: {count}`
105105 }
@@ -111,19 +111,35 @@ component Counter(count: int) {
111111Channels enable reactive, real-time updates:
112112
113113``` go
114- component Counter (counterChannel: <- chan int ) {
115- Div {
116- ` Counter: {<-counterChannel}`
114+ import " strconv"
115+
116+ func Counter (counterChannel: chan int ) {
117+ Div (Class (" counter-display" )) {
118+ Span (Class (" counter-value" )) {
119+ ` Counter: {<-counterChannel}`
120+ }
117121 }
118122}
119123
120- component App () {
121- Div {
122- Input (OnInput (func (ev Event) {
123- // Send to channel
124- counterChan <- parseValue (ev.Target .Value )
125- }))
126- Counter (WithCounterChannel (counterChan))
124+ func App () {
125+ counter := make (chan int , 10 )
126+
127+ Div (Class (" app-container" )) {
128+ H1 {
129+ " Counter Example"
130+ }
131+ Counter (WithCounterChannel (counter))
132+ Div (Class (" input-group" )) {
133+ Input (
134+ Type (" number" ),
135+ Placeholder (" Enter a number" ),
136+ OnInput (func (e: Event) {
137+ value := e.Target .Value
138+ n , _ := strconv.Atoi (value)
139+ counter <- n
140+ })
141+ )
142+ }
127143 }
128144}
129145```
@@ -133,23 +149,23 @@ component App() {
133149Type-safe event handling with Go functions:
134150
135151``` go
136- component Form () {
152+ func Form () {
137153 Div {
138154 Input (
139- OnInput (func (ev Event) {
140- value := ev .Target .Value
155+ OnInput (func (e: Event) {
156+ value := e .Target .Value
141157 // Handle input
142158 }),
143159 Placeholder (" Enter text..." )
144160 )
145161
146162 Button (
147- OnClick (func (ev Event) {
148- ev .Native .Call (" preventDefault" )
163+ OnClick (func (e: Event) {
164+ e .Native .Call (" preventDefault" )
149165 // Handle click
150166 })
151167 ) {
152- " Submit"
168+ Text ( " Submit" )
153169 }
154170 }
155171}
@@ -161,22 +177,24 @@ Common HTML elements with type-safe APIs:
161177
162178``` go
163179Div (Class (" container" )) {
164- H1 { " Title" }
180+ H1 {
181+ Text (" Title" )
182+ }
165183
166184 P (Style (" color: blue;" )) {
167- " Paragraph text"
185+ Text ( " Paragraph text" )
168186 }
169187
170188 Button (
171189 ID (" submit-btn" ),
172190 OnClick (handleSubmit)
173191 ) {
174- " Submit"
192+ Text ( " Submit" )
175193 }
176194
177195 Img (
178196 Src (" /image.png" ),
179- Attr {" alt" , " Description" }
197+ Attr {Key: " alt" , Value: " Description" }
180198 )
181199}
182200```
@@ -248,12 +266,54 @@ The parser (`pkg/parser`) implements:
248266
249267### Counter
250268
251- See ` examples/counter/ ` for a complete counter application with:
269+ See ` examples/counter/ ` for a complete counter application demonstrating:
270+
271+ - Component composition with the ` Counter ` component
272+ - Reactive channel-based state management
273+ - Event handling with ` OnInput `
274+ - Template interpolation with backtick strings
275+ - Type-safe props and option functions
276+
277+ The example consists of two components in ` .gx ` files:
278+
279+ ** ` counter.gx ` ** - Display component that renders channel values:
280+ ``` go
281+ func Counter (counterChannel: chan int ) {
282+ Div (Class (" counter-display" )) {
283+ Span (Class (" counter-value" )) {
284+ ` Counter: {<-counterChannel}`
285+ }
286+ }
287+ }
288+ ```
289+
290+ ** ` app.gx ` ** - Main app component with input handling:
291+ ``` go
292+ import " strconv"
293+
294+ func App () {
295+ counter := make (chan int , 10 )
252296
253- - Component composition
254- - Event handling
255- - State management
256- - Template interpolation
297+ Div (Class (" app-container" )) {
298+ H1 {
299+ " Counter Example"
300+ }
301+ Counter (WithCounterChannel (counter))
302+ Div (Class (" input-group" )) {
303+ Input (
304+ Type (" number" ),
305+ Placeholder (" Enter a number" ),
306+ ID (" counter-input" ),
307+ OnInput (func (e: Event) {
308+ value := e.Target .Value
309+ n , _ := strconv.Atoi (value)
310+ counter <- n
311+ })
312+ )
313+ }
314+ }
315+ }
316+ ```
257317
258318To run:
259319
0 commit comments