11# dom
22
33Low 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
4781The 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
76117explicitly:
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
85130nameSig := state.NewSignal (" " )
86131dom.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 )
0 commit comments