Skip to content

Commit eb29c74

Browse files
committed
docs: Improve docs about Composition APIs
1 parent f3c0f86 commit eb29c74

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

docs/articles/essentials/composition.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ cmp := composition.Wrap(core.NewComponent("Counter", nil, nil))
6161
cmp.On("save", func() { /* handle save */ })
6262
```
6363

64+
If the component isn't wrapped with `composition.Wrap`, attach listeners
65+
directly via the `dom` package:
66+
67+
```go
68+
btn := dom.ByID("save")
69+
stop := btn.On("click", func(dom.Event) { /* handle click */ })
70+
defer stop()
71+
```
72+
6473
## Example
6574

6675
```go

docs/articles/essentials/template-syntax.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,18 @@ Example:
115115
<button @on:click.once:launch>Launch once</button>
116116
```
117117

118-
Event handlers are registered with `dom.RegisterHandlerFunc` on the Go side.
118+
Event handlers are registered on the Go side. When a component is wrapped
119+
with the Composition API (`composition.Wrap`), register them by name using
120+
`cmp.On`. Plain `*core.HTMLComponent`s attach listeners directly through the
121+
`dom` package:
122+
123+
```go
124+
btn := dom.ByID("save")
125+
stop := btn.On("click", func(dom.Event) {
126+
// handle click
127+
})
128+
defer stop()
129+
```
119130

120131
## Conditionals
121132

docs/articles/getting-started/introduction.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Here is a minimal counter component:
1515
```rtml
1616
<root>
1717
<button @on:click:increment>
18-
Count is: {count}
18+
Count is: @signal:count
1919
</button>
2020
</root>
2121
```
@@ -24,28 +24,29 @@ Here is a minimal counter component:
2424
package counter
2525

2626
import (
27-
"github.com/rfwlab/rfw/v1/core"
28-
"github.com/rfwlab/rfw/v1/dom"
27+
"github.com/rfwlab/rfw/v1/composition"
28+
core "github.com/rfwlab/rfw/v1/core"
2929
"github.com/rfwlab/rfw/v1/router"
30+
"github.com/rfwlab/rfw/v1/state"
3031
)
3132

3233
//go:embed counter.rtml
3334
var tpl []byte
3435

3536
type Counter struct {
36-
*core.HTMLComponent
37-
Count int
37+
*composition.Component
38+
count *state.Signal[int]
3839
}
3940

4041
func New() *Counter {
41-
c := &Counter{}
42-
c.HTMLComponent = core.NewComponentWith("Counter", tpl, nil, c)
43-
dom.RegisterHandlerFunc("increment", func() { c.Count++ })
42+
cmp := composition.Wrap(core.NewComponent("Counter", tpl, nil))
43+
c := &Counter{Component: cmp, count: state.NewSignal(0)}
44+
cmp.Prop("count", c.count)
45+
cmp.On("increment", func() { c.count.Set(c.count.Get() + 1) })
4446
return c
4547
}
4648

4749
func main() {
48-
core.SetDevMode(true)
4950
router.RegisterRoute(router.Route{
5051
Path: "/",
5152
Component: func() core.Component { return New() },

0 commit comments

Comments
 (0)