Skip to content

Commit 0ad32d5

Browse files
committed
chore: Refactor DOM API for typed wrappers
1 parent c3fcd74 commit 0ad32d5

27 files changed

+421
-169
lines changed

docs/articles/api/dom.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
# dom
22

33
Low level DOM helpers used by the framework. Most applications interact
4-
with the DOM indirectly through components, but the following utilities
5-
are available for advanced use:
4+
with the DOM indirectly through components.
5+
6+
## Context
7+
8+
These helpers allow fine-grained control over the browser DOM without
9+
relying on `syscall/js` directly.
10+
11+
## Prerequisites
12+
13+
Use these APIs when components need to manipulate elements that are not
14+
handled by the template system.
15+
16+
## API
17+
18+
Both function helpers and typed wrappers are available. The functions
19+
delegate to the wrappers for backward compatibility:
620

721
| Function | Description |
822
| --- | --- |
23+
| `Doc()` | Returns the global `Document`. |
924
| `CreateElement(tag)` | Returns a new element. |
1025
| `ByID(id)` | Fetches an element by id. |
1126
| `Query(sel)` | Returns the first element matching the CSS selector. |
@@ -17,8 +32,12 @@ are available for advanced use:
1732
| `SetText(el, text)` | Sets an element's text content. |
1833
| `Attr(el, name)` | Retrieves the value of an attribute. |
1934
| `SetAttr(el, name, value)` | Sets the value of an attribute. |
35+
| `SetStyle(el, prop, value)` | Sets an inline style property. |
36+
| `StyleInline(map)` | Builds a style string from CSS properties. |
2037
| `AddClass(el, name)` | Adds a class to an element. |
2138
| `RemoveClass(el, name)` | Removes a class from an element. |
39+
| `HasClass(el, name)` | Reports whether an element has a class. |
40+
| `ToggleClass(el, name)` | Toggles a class on an element. |
2241
| `ScheduleRender(id, html, delay)` | Updates a component after a delay. |
2342
| `UpdateDOM(id, html)` | Patches a component's DOM with raw HTML. |
2443
| `TemplateHook` | Callback invoked after `UpdateDOM`. |
@@ -38,14 +57,36 @@ Go functions to named DOM events.
3857
3958
### Selecting and modifying elements
4059

41-
```go
42-
title := dom.Query("h1")
43-
dom.SetText(title, "Hello")
44-
dom.AddClass(title, "highlight")
45-
```
60+
1. Obtain the document wrapper.
61+
```go
62+
doc := dom.Doc()
63+
```
64+
2. Query and update elements with typed methods.
65+
```go
66+
title := doc.Query("h1")
67+
title.SetText("Hello")
68+
title.AddClass("highlight")
69+
title.SetAttr("lang", "en")
70+
title.SetStyle("color", "red")
71+
72+
box := doc.CreateElement("div")
73+
box.SetAttr("style", dom.StyleInline(map[string]string{"display": "flex", "gap": "4px"}))
74+
75+
items := doc.QueryAll("li")
76+
for i := 0; i < items.Length(); i++ {
77+
items.Index(i).ToggleClass("active")
78+
}
79+
```
4680

4781
The snippet demonstrates direct DOM interactions.
4882

83+
`QueryAll` returns a collection exposing `Length` and `Index` to walk matched elements.
84+
85+
### Migration
86+
87+
Legacy helpers such as `dom.Query` and `dom.SetText` continue to work and
88+
delegate to the new methods for compatibility.
89+
4990
@include:ExampleFrame:{code:"/examples/components/event_component.go", uri:"/examples/event"}
5091

5192
### Delayed updates
@@ -76,16 +117,20 @@ directives. When building elements yourself, call the bind helpers
76117
explicitly:
77118

78119
```go
79-
import "github.com/rfwlab/rfw/v1/state"
120+
import (
121+
"github.com/rfwlab/rfw/v1/dom"
122+
"github.com/rfwlab/rfw/v1/state"
123+
)
80124

81-
el := dom.CreateElement("div")
82-
dom.SetInnerHTML(el, `<input value="@store:user.name:w">`)
83-
dom.BindStoreInputs(el)
125+
doc := dom.Doc()
126+
el := doc.CreateElement("div")
127+
el.SetHTML(`<input value="@store:user.name:w">`)
128+
dom.BindStoreInputs(el.Value)
84129

85130
nameSig := state.NewSignal("")
86131
dom.RegisterSignal("cmp", "name", nameSig)
87-
dom.SetInnerHTML(el, `<input value="@signal:name:w">`)
88-
dom.BindSignalInputs("cmp", el)
132+
el.SetHTML(`<input value="@signal:name:w">`)
133+
dom.BindSignalInputs("cmp", el.Value)
89134
```
90135

91136
### Virtual lists
@@ -104,3 +149,12 @@ list := virtual.NewVirtualList("list", 1000, 24, func(i int) string {
104149
```html
105150
<div id="list" style="height:200px; overflow-y:auto"></div>
106151
```
152+
153+
### Notes and limitations
154+
155+
Only common operations are wrapped. For unsupported features, use the
156+
`dom` helpers or the lower level `js` package cautiously.
157+
158+
### Related links
159+
160+
- [js](js.md)

docs/articles/api/events.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ emit their own notifications – a separate mechanism from DOM events.
2525
`On` attaches a handler and provides a cleanup function:
2626

2727
```go
28-
stop := events.OnClick(dom.ByID("btn"), func(evt js.Value) {
28+
doc := dom.Doc()
29+
stop := events.OnClick(doc.ByID("btn").Value, func(evt js.Value) {
2930
js.Console().Call("log", "clicked")
3031
})
3132
// ...

docs/articles/api/webgl.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ ctx.UseProgram(prog)
8181
### Loading a texture
8282

8383
```go
84-
img := dom.ByID("img")
85-
tex := ctx.LoadTexture2D(img)
84+
doc := dom.Doc()
85+
img := doc.ByID("img")
86+
tex := ctx.LoadTexture2D(img.Value)
8687
ctx.ActiveTexture(webgl.TEXTURE0)
8788
ctx.BindTexture(webgl.TEXTURE_2D, tex)
8889
```
@@ -105,8 +106,9 @@ Use when drawing geometry that shares vertices or requires depth ordering.
105106
4. Render with `Context.DrawElements`.
106107

107108
```go
108-
canvas := dom.ByID("canvas")
109-
ctx := webgl.NewContextFrom(canvas)
109+
doc := dom.Doc()
110+
canvas := doc.ByID("canvas")
111+
ctx := webgl.NewContextFrom(canvas.Value)
110112
ctx.Viewport(0, 0, canvas.Get("width").Int(), canvas.Get("height").Int())
111113
ctx.Enable(webgl.DEPTH_TEST)
112114
ctx.DepthFunc(webgl.LEQUAL)

docs/articles/essentials/event-handling.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Example:
4444
Handlers can also be registered from Go code using the `events` package:
4545

4646
```go
47-
stop := events.OnClick(dom.ByID("save"), func(evt js.Value) {
47+
doc := dom.Doc()
48+
stop := events.OnClick(doc.ByID("save").Value, func(evt js.Value) {
4849
// handle click
4950
})
5051
defer stop()

0 commit comments

Comments
 (0)