Documentation
¶
Index ¶
- func Append(parentID string, component Component) error
- func GetDocumentAttr(attr string) string
- func GetHash() string
- func LocalStorageAvailable() bool
- func LocalStorageClear() error
- func LocalStorageDel(key string) error
- func LocalStorageGet(key string) (string, error)
- func LocalStorageSet(key, value string) error
- func Log(v ...any)
- func OnHashChange(handler func(hash string))
- func Render(parentID string, component Component) error
- func SetDocumentAttr(attr, value string)
- func SetHash(hash string)
- func SetLog(log func(v ...any))
- func Update(component Component)
- type Component
- type DOM
- type Element
- func (b *Element) Add(children ...any) *Element
- func (b *Element) Attr(key, val string) *Element
- func (b *Element) Children() []Component
- func (b *Element) Class(class ...string) *Element
- func (b *Element) For(other *Element) *Element
- func (b *Element) GetID() string
- func (b *Element) ID(id string) *Element
- func (b *Element) NoCloseTag() *Element
- func (b *Element) On(t string, h func(Event)) *Element
- func (b *Element) Render(parentID string) error
- func (b *Element) SetID(id string)
- func (b *Element) String() string
- func (b *Element) Text(text string) *Element
- func (b *Element) Update()
- type Event
- type Mountable
- type Reference
- type Unmountable
- type Updatable
- type ViewRenderer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶ added in v0.2.0
Append injects a component AFTER the last child of the parent element.
func GetDocumentAttr ¶ added in v0.9.0
GetDocumentAttr reads an attribute from document.documentElement. Returns "" if the attribute is absent.
func LocalStorageAvailable ¶ added in v0.9.0
func LocalStorageAvailable() bool
LocalStorageAvailable reports whether localStorage is accessible in the current browser context. Returns false when blocked by iframe sandbox, privacy settings, or private mode. Used internally by all LocalStorage* functions and available as a public check.
func LocalStorageClear ¶ added in v0.9.0
func LocalStorageClear() error
LocalStorageClear removes all keys. Returns error if storage unavailable.
func LocalStorageDel ¶ added in v0.9.0
LocalStorageDel removes a key. Returns error if storage unavailable.
func LocalStorageGet ¶ added in v0.9.0
LocalStorageGet retrieves a value from window.localStorage. Returns ("", nil) — key absent, storage is functional. Returns ("", error) — storage unavailable.
func LocalStorageSet ¶ added in v0.9.0
LocalStorageSet writes a key-value pair. Returns error if: storage unavailable, value > lsMaxValue, or budget exceeded.
func OnHashChange ¶ added in v0.0.11
func OnHashChange(handler func(hash string))
OnHashChange registers a hash change listener.
func SetDocumentAttr ¶ added in v0.9.0
func SetDocumentAttr(attr, value string)
SetDocumentAttr sets an attribute on document.documentElement (<html>). value=="" removes the attribute — consistent with GetDocumentAttr returning "" for absent attributes.
Types ¶
type Component ¶
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 DOM ¶
type DOM interface {
// Render injecta un componente en un elemento padre.
// 1. Llama a componente.Render() (si es ViewRenderer) o componente.RenderHTML()
// 2. Establece el contenido del elemento padre (buscado por parentID)
// 3. Llama a componente.OnMount() para enlazar eventos
Render(parentID string, component Component) error
// Append injecta un componente DESPUÉS del último hijo del elemento padre.
// Útil para listas dinámicas.
Append(parentID string, component Component) error
// OnHashChange registra un listener para cambios en el hash de la URL.
OnHashChange(handler func(hash string))
// GetHash devuelve el hash actual de la URL (ej. "#help").
GetHash() string
// SetHash actualiza el hash de la URL.
SetHash(hash string)
// Update re-renderiza el componente en su posición actual en el DOM.
Update(component Component)
// 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)
}
DOM is the main entry point for interacting with the browser. It is designed to be injected into your components.
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element represents a DOM element in the fluent Element API.
func NewElement ¶ added in v0.10.1
NewElement creates an Element with the given HTML tag. Used by tinywasm/html, tinywasm/svg, tinywasm/image to build elements.
func (*Element) Add ¶ added in v0.2.3
Add adds one or more children or attributes to the element. Children can be *Element, Component, string, or fmt.KeyValue.
func (*Element) Children ¶ added in v0.2.3
Children returns the component's children (components only).
func (*Element) For ¶ added in v0.8.0
For sets the for= attribute pointing to other's ID, auto-generating other's ID if it has none. Use for label/input pairing and aria-* references.
func (*Element) NoCloseTag ¶ added in v0.10.1
NoCloseTag marks the element as self-closing (no closing tag rendered). Use for void HTML elements: br, hr, img, input, link, meta, etc.
func (*Element) Render ¶ added in v0.2.3
Render renders the element to the parent. This is a terminal operation.
func (*Element) String ¶ added in v0.10.0
String serializes the element tree to its string representation.
type Event ¶
type Event interface {
// PreventDefault prevents the default action of the event.
PreventDefault()
// StopPropagation stops the event from bubbling up the DOM tree.
StopPropagation()
// TargetValue returns the value of the event's target element.
// Useful for input, textarea, and select elements.
TargetValue() string
// TargetID returns the ID of the event's target element.
TargetID() string
// TargetChecked returns the checked status of the event's target element.
// Useful for checkbox and radio input elements.
TargetChecked() bool
}
Event represents a DOM event.
type Mountable ¶
type Mountable interface {
OnMount()
}
Mountable is an optional interface for components that need initialization logic.
type Reference ¶ added in v0.2.3
type Reference interface {
// GetAttr retrieves an attribute value.
GetAttr(key string) string
// Value returns the current value of an input/textarea/select.
Value() string
// SetValue sets element.value (inputs, textarea, select).
SetValue(value string)
// SetAttr calls element.setAttribute(key, value).
// Use empty string for boolean attributes (e.g., SetAttr("disabled", "")).
SetAttr(key, value string)
// RemoveAttr calls element.removeAttribute(key).
RemoveAttr(key string)
// SetText sets element.textContent.
// Safe for plain text — does not parse HTML.
SetText(text string)
// Checked returns the current checked state of a checkbox or radio button.
Checked() bool
// On registers a generic event handler (e.g., "click", "change", "input", "keydown").
On(eventType string, handler func(event Event))
// Focus sets focus to the element.
Focus()
}
Reference represents a reference to a DOM node. It provides methods for reading and interaction.
type Unmountable ¶ added in v0.2.0
type Unmountable interface {
OnUnmount()
}
Unmountable is an optional interface for components that need cleanup logic.
type Updatable ¶ added in v0.2.0
type Updatable interface {
OnUpdate()
}
Updatable is an optional interface for components that need update logic.
type ViewRenderer ¶ added in v0.2.0
type ViewRenderer interface {
Render() *Element
}
ViewRenderer returns a Node tree for declarative UI.