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(dom.Event) -> 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(dom.Event) -> Nil)
child of a vnode — one of four variants:
static element, static text, reactive element,
or reactive text. use the constructors below
(e.g. text, element, reactive, reactive_text)
to build values of this type.
pub type VChild {
Element(VNode)
Text(String)
Reactive(signal.Signal(VNode))
ReactiveText(signal.Signal(String))
}
Constructors
-
Element(VNode) -
Text(String) -
Reactive(signal.Signal(VNode)) -
ReactiveText(signal.Signal(String))
Values
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(dom.Event) -> Nil,
) -> VNode
attaches an event handler. the event name is
camelCased and prefixed with on_ in ffi
(e.g. click becomes onClick)
pub fn option_tag() -> 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 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