Repository navigation
Expand file tree
/
Copy pathinterface.dom.go
More file actions
88 lines (70 loc) · 2.8 KB
/
Copy pathinterface.dom.go
File metadata and controls
88 lines (70 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package dom
import "webtyp.com/fmt"
// DOM is the main entry point for interacting with the browser.
// It is designed to be injected into your components.
type DOM interface {
// Render injects a component into a parent element.
// 1. Calls component.Init(ctx) if present (exactly once)
// 2. Calls component.Render() to obtain the element tree
// 3. Injects the resulting HTML and wires bindings and events
Render(parentID string, component Component) error
// Append injects a component AFTER the last child of the parent element.
// Useful for dynamic lists.
Append(parentID string, component Component) error
// OnHashChange registers a listener for changes in the URL hash.
OnHashChange(handler func(hash string))
// OnScrollCapture registers a scroll listener in capture phase on the
// document: fires for any scroller on the page. See the package-level
// function of the same name.
OnScrollCapture(handler func(scrollTop float64))
// OnUserActivity registers document-level presence listeners in the capture
// phase: the handler is called when the user performs an action anywhere on
// the page. See the package-level function of the same name.
OnUserActivity(handler func())
// GetHash returns the current URL hash (e.g., "#help").
GetHash() string
// SetHash updates the URL hash.
SetHash(hash string)
// GetQuery returns the value of key in the page URL's query string.
GetQuery(key string) (string, bool)
// ClearQuery removes the query string from the address bar.
ClearQuery()
// NavigatePost leaves the page by submitting a POST form.
NavigatePost(url string, fields []fmt.KeyValue)
// Navigate leaves the page and opens url with a plain GET.
Navigate(url string)
// Get retrieves an element by ID.
Get(id string) (Reference, bool)
// Log provides logging functionality using the log function passed to New.
Log(v ...any)
}
// Component is the minimal interface for components.
// All components must implement this for both SSR (backend) and WASM (frontend).
//
// NOTE: If your struct embeds Element, embed it as a VALUE, not a pointer:
//
// type MyComponent struct {
// Element // ✅ Correct — never nil
// // NOT: *Element // ❌ Wrong — nil pointer causes panic in renderToHTML
// }
//
// This is because renderToHTML calls GetID() on every Component child before checking ViewRenderer.
type Component interface {
GetID() string
SetID(id string)
String() string
Children() []Component
}
// ViewRenderer returns a Node tree for declarative UI.
type ViewRenderer interface {
Render() *Element
}
// Ctx is handed to the Init hook. Register teardown for async resources (timers, websockets).
type Ctx interface {
OnCleanup(fn func())
}
// eventHandler represents a DOM event handler in the declarative builder.
type eventHandler struct {
Name string
Handler func(Event)
}