pream/vnode
Types
vnode property — either an attribute (any gleam
value, serialized downstream in ffi) or an
event handler. handlers are camelCased and
prefixed with on_ in ffi
pub type Prop {
Attr(key: String, value: dom.Native)
Handler(
event: String,
handle: fn(event.Event(dynamic.Dynamic)) -> Nil,
)
}
Constructors
-
Attr(key: String, value: dom.Native)NOTE: a prop can be any gleam value we’re swallowing the type here and handle it downstram in ffi. this is done to make the render API simple
-
Handler( event: String, handle: fn(event.Event(dynamic.Dynamic)) -> Nil, )
child of a vnode — one of five variants:
static element, static text, reactive element,
reactive text, or component boundary. use the
constructors below (e.g. text, element,
reactive, reactive_text, component)
to build values of this type.
pub type VChild {
Element(VNode)
Text(String)
Reactive(signal.Signal(VNode))
ReactiveText(signal.Signal(String))
Boundary(render: dom.Native)
}
Constructors
-
Element(VNode) -
Text(String) -
Reactive(signal.Signal(VNode)) -
ReactiveText(signal.Signal(String)) -
Boundary(render: dom.Native)
Values
pub fn component(render: fn() -> VNode) -> VChild
Creates a Preact component boundary from a render function. Use this when composing components so that Preact devtools and signal-driven re-rendering work correctly.
Named zero-arg functions can be passed directly: vnode.component(counter)
Functions with arguments need a wrapping closure: vnode.component(fn() { inner(count) })
pub fn fragment() -> VNode
https://npmx.dev/package-docs/preact/v/10.29.2#function-fragment a fragment vnode — used to return multiple sibling children without a wrapper element
pub fn map_signal(
s: signal.Signal(a),
render: fn(a) -> VChild,
) -> VChild
maps a signal through render — each value
is wrapped as a VChild inside a fragment. the
result is itself a reactive VChild.
pub fn new(tag: String) -> VNode
https://npmx.dev/package-docs/preact/v/10.29.2#function-h
creates an empty vnode with the given tag and
no props or children — equivalent to <tag />
pub fn on(
vnode: VNode,
event: String,
handle: fn(event.Event(dynamic.Dynamic)) -> Nil,
) -> VNode
attaches an event handler. the event name is
camelCased and prefixed with on_ in ffi
(e.g. click becomes onClick)
pub fn placeholder(vnode: VNode, text: String) -> VNode
Sets the placeholder attribute on the vnode.
pub fn prop(vnode: VNode, key: String, value: a) -> VNode
sets an attribute on the vnode. any gleam value is accepted — stringification/serialization is deferred to the ffi at render time
pub fn reactive(s: signal.Signal(VNode)) -> VChild
creates a reactive element child from a signal
pub fn reactive_text(s: signal.Signal(String)) -> VChild
creates a reactive text child from a signal
pub fn text_with(content: String, args: List(String)) -> VChild
creates a static text child with positional
formatting — each {} is replaced with the
next value from the list
pub fn unless(condition: Bool, render: fn() -> VChild) -> VChild
renders render when condition is False,
otherwise returns an empty text node
pub fn value_int(vnode: VNode, val: Int) -> VNode
Sets the value attribute on the vnode as an integer.
pub fn value_string(vnode: VNode, val: String) -> VNode
Sets the value attribute on the vnode as a string.
pub fn when(condition: Bool, render: fn() -> VChild) -> VChild
renders render when condition is True,
otherwise returns an empty text node
pub fn when_signal(
s: signal.Signal(Bool),
render: fn() -> VChild,
) -> VChild
renders render when the signal value is True,
otherwise returns an empty fragment. the result
is itself a reactive VChild.
pub fn when_some(
option: option.Option(a),
render: fn(a) -> VChild,
) -> VChild
renders render when the option is Some,
otherwise returns an empty text node