lily/component

Components subscribe to the Store and re-render when their slice of the model changes. Being stateless and focused on rendering keeps them light (lighter than Lustre or LiveView components, for example), and having a lot of components is not an anti-pattern in Lily.

Each component is one of the kinds below plus whatever you pipe onto it, such as a transition or an event listener from event.

There are five kinds, and every one takes a render function that returns your html type:

  1. static renders once and never updates again
  2. dynamic morphs the subtree in place when the slice changes
  3. live applies targeted patches you hand-write
  4. each is a keyed list of rows, and each_live is the same list patched in place
  5. fragment groups other components into one

dynamic is usually best for any component that reacts to changes in the store, and live lets you hand-write patches if you want smoother patching instead of having to run through a large DOM subtree at every frame. Lily doesn’t use a VDOM so these choices are yours to make for your own performance requirements.

A live component’s render must carry the state, not just the layout. It builds the first paint, and it is also what renders on the server and when the component mounts late, meaning a render that ignores the slice shows stale state until the first patch. The patch function only changes what is already there, and it reads the same slice as render.

On top of its kind, a component carries decorations, each applied with a pipe:

  1. transition adds CSS enter/exit classes, holding back removal until the exit animation finishes
  2. event.on and the typed helpers such as event.on_click attach listeners, scoped to the component they are piped onto

A component marks its own root element with data-lily-component, so a <tr> stays in its table. A render with several roots or only text gets a display: contents wrapper instead, which is invalid inside a <table>, <ul> or <select>.

Every render function is handed a nest function as its last argument. Call nest(child_component) wherever you want a child to appear in the parent template, it returns a placeholder of your html type that gets swapped for the rendered child once the parent serialises. Unlike an HTML <slot>, the parent picks the child itself. Nest it as deep as you like:

component.live(
  slice: fn(model) { model.is_active },
  render: fn(is_active, nest) {
    html.section([attribute.classes([#("active", is_active)])], [
      html.h2([], [html.text("Title")]),
      nest(component.each(
        slice: fn(model) { cards_for(model) },
        key: fn(card) { card.id },
        render: fn(card, _) { card_html(card) },
      )),
    ])
  },
  patch: column_patches,
)

When a component has no children, just ignore the parameter:

component.dynamic(
  slice: fn(model) { model.count },
  render: fn(count, _) {
    html.div([], [html.text(int.to_string(count))])
  },
)

A list’s rows are plain markup. The list compares its slice once per update and each row against its previous version by reference, letting an update that leaves a row alone cost almost nothing as long as the rows are kept as records in the model. Row-specific state, like a selection, lives in the rows, and list-wide presentation is CSS on the container. A row that needs a reactive part of its own calls nest for it.

Components work with any HTML library, Lustre or raw strings, whatever you like. The to_html function you pass at mount converts your chosen library’s types into strings, we’d recommend Lustre elements. If you use nesting, also pass to_placeholder, a zero-argument function returning an html placeholder that serialises to <lily-nest></lily-nest>. For Lustre:

component.mount(
  runtime,
  selector: "#app",
  to_html: element.to_string,
  to_placeholder: fn() { element.element("lily-nest", [], []) },
  view: app,
)

Or with raw HTML strings:

component.mount(
  runtime,
  selector: "#app",
  to_html: fn(html) { html },
  to_placeholder: fn() { "<lily-nest></lily-nest>" },
  view: app,
)

Escaping is the to_html function’s job, not Lily’s. The rendered string is written to the DOM verbatim through innerHTML. Lustre’s element.to_string escaping text and attribute values keeps the Lustre path safe. The raw-string to_html: fn(html) { html } above does not escape, making model data (which on a synced app can carry other clients’ input) interpolated straight into that string a stored-XSS vector. When using raw strings, escape any untrusted text yourself. The from_string argument of render_to_string is the same, an unsafe_raw_html style constructor inserts its string without escaping.

Every component declares a slice that pulls just the data it needs out of the model. The runtime caches the last slice and skips rendering when it’s unchanged, comparing structurally (deep equality), meaning a slice that builds a new tuple or record each time still compares equal by value. Keep slices cheap and do the heavy lifting in render, which the comparison gates. Here’s the whole thing end to end:

import lily/client
import lily/component
import lily/event
import lily/store
import lustre/attribute
import lustre/element
import lustre/element/html

fn app(_model: Model) {
  component.dynamic(
    slice: fn(model: Model) { model.count },
    render: fn(count, _) {
      html.div([], [
        html.button(
          [
            attribute.data(
              "lily-message",
              event.encode_message(Decrement),
            ),
          ],
          [html.text("-")],
        ),
        html.p([], [html.text(int.to_string(count))]),
        html.button(
          [
            attribute.data(
              "lily-message",
              event.encode_message(Increment),
            ),
          ],
          [html.text("+")],
        ),
      ])
    },
  )
}

pub fn main() {
  let runtime =
    store.new(init: Model(count: 0), update: update)
    |> client.start

  runtime
  |> component.mount(
    selector: "#app",
    to_html: element.to_string,
    to_placeholder: fn() { element.element("lily-nest", [], []) },
    view: app,
  )
}

With mount itself picking up clicks on an element carrying data-lily-message, the buttons above need no handler. For anything else, pipe event.on or one of its helpers onto the component. A listener fires for events inside that component.

Building components and rendering them to a string with render_to_string work on both targets so that the server can render initial components.

mount and event handling drive a live DOM, meaning they do nothing on Erlang.

Types

A piece of UI that renders part of the model, built with dynamic, live, each, static or fragment.

html is whatever type your HTML library uses for markup.

pub opaque type Component(model, message, html)

The nest function every render receives as its last argument. Call it with a child component where the child should appear.

pub type Nest(model, message, html) =
  fn(Component(model, message, html)) -> html

A targeted DOM update, returned by the patch function of live and each_live.

target is a CSS selector under the component’s root, and an empty string means the root itself.

pub type Patch {
  RemoveAttribute(target: String, name: String)
  SetAttribute(target: String, name: String, value: String)
  SetStyle(target: String, property: String, value: String)
  SetText(target: String, value: String)
}

Constructors

  • RemoveAttribute(target: String, name: String)

    Remove an HTML attribute

  • SetAttribute(target: String, name: String, value: String)

    Set an HTML attribute

  • SetStyle(target: String, property: String, value: String)

    Set a CSS style property. A value that loads from another origin, a url() say, is refused

  • SetText(target: String, value: String)

    Set an element’s textContent (wipes children)

Values

pub fn dynamic(
  slice slice: fn(model) -> a,
  render render: fn(
    a,
    fn(Component(model, message, html)) -> html,
  ) -> html,
) -> Component(model, message, html)

The default component, re-rendered when its slice changes while keeping focus, cursor and in-progress input.

Reach for live only when a large subtree updates so often that this is measurably too slow.

component.dynamic(
  slice: fn(model) { model.count },
  render: fn(count, _) {
    html.div([], [html.text("Count: " <> int.to_string(count))])
  }
)
pub fn each(
  slice slice: fn(model) -> List(item),
  key key: fn(item) -> key,
  render render: fn(
    item,
    fn(Component(model, message, html)) -> html,
  ) -> html,
) -> Component(model, message, html)

A keyed list of rows where only the rows that changed update.

slice returns the list, key identifies a row and render returns its markup. Keep the rows as records in the model so an untouched row is skipped, and call nest in a row for a part that reacts on its own.

component.each(
  slice: fn(model) { model.counters },
  key: fn(counter) { counter.id },
  render: fn(counter, _) {
    html.div([attribute.class("counter")], [
      html.text(int.to_string(counter.value))
    ])
  }
)
pub fn each_live(
  slice slice: fn(model) -> List(item),
  key key: fn(item) -> key,
  render render: fn(
    item,
    fn(Component(model, message, html)) -> html,
  ) -> html,
  patch patch: fn(item) -> List(Patch),
) -> Component(model, message, html)

A keyed list whose changed rows are patched in place, the list version of live.

render builds a row when it first appears, and patch returns the updates for a row that changed.

component.each_live(
  slice: fn(model) { model.boxes },
  key: fn(box) { box.id },
  render: fn(box, _) { box_html(box) },
  patch: fn(box) {
    [SetStyle(".box", "transform", transform(box))]
  },
)
pub fn fragment(
  children: List(Component(model, message, html)),
) -> Component(model, message, html)

Groups several components into one, rendered in order, like Lustre’s element.fragment.

fn app(_model: Model) -> Component(Model, Message, Element(Message)) {
  component.fragment([
    component.static(fn(_) { html.h1([], [html.text("My App")]) }),
    component.dynamic(...),
    component.each(...),
  ])
}
pub fn live(
  slice slice: fn(model) -> a,
  render render: fn(
    a,
    fn(Component(model, message, html)) -> html,
  ) -> html,
  patch patch: fn(a) -> List(Patch),
) -> Component(model, message, html)

Renders once, then applies the Patches you return when its slice changes.

Use it for a hot path where dynamic is measurably too slow, not just to keep focus or input, which dynamic already does.

render must draw the slice’s current state, not only the layout.

component.live(
  slice: fn(model) { model.data },
  render: fn(data, _) {
    html.div([], [
      html.span([attribute.class("value")], [
        html.text(int.to_string(data)),
      ]),
      html.div([attribute.class("bar")], [])
    ])
  },
  patch: fn(data) {
    [
      SetText(".value", int.to_string(data)),
      SetStyle(".bar", "width", int.to_string(data) <> "%"),
    ]
  }
)
pub fn mount(
  runtime: client.Runtime(model, message, mode),
  selector selector: String,
  to_html to_html: fn(html) -> String,
  to_placeholder to_placeholder: fn() -> html,
  view view: fn(model) -> Component(model, message, html),
) -> client.Runtime(model, message, mode)

Renders a view into the element matching selector and keeps it up to date with the store. Does nothing on Erlang.

to_html turns your html type into a string, and to_placeholder returns a placeholder that serialises to <lily-nest></lily-nest>.

Clicks on an element whose data-lily-message comes from event.encode_message are dispatched for you. A message that fails the store’s schema, or an attribute over 16 KiB, is ignored.

Mount other selectors on the same runtime to drive several roots from one model. Mounting the same selector again replaces it.

runtime
|> component.mount(
  selector: "#app",
  to_html: element.to_string,
  to_placeholder: fn() { element.element("lily-nest", [], []) },
  view: app,
)
pub fn render_to_string(
  view view: fn(model) -> Component(model, message, html),
  model model: model,
  to_html to_html: fn(html) -> String,
  from_string from_string: fn(String) -> html,
) -> String

Renders a view to an HTML string without a DOM, for a server or a build step to produce the first paint. Pair with store.embed.

from_string wraps a child’s string back into your html type, such as an unsafe_raw_html constructor for Lustre. Listeners and transitions are skipped.

let html = component.render_to_string(
  view: shared.view,
  model: shared.initial_model(),
  to_html: element.to_string,
  from_string: element.unsafe_raw_html(_, "div", [], _),
)
pub fn static(
  render render: fn(fn(Component(model, message, html)) -> html) -> html,
) -> Component(model, message, html)

Renders once and never updates again, for anything that doesn’t depend on the model.

component.static(fn(_) { html.h1([], [html.text("My App")]) })
pub fn transition(
  component: Component(model, message, html),
  enter enter: String,
  exit exit: String,
) -> Component(model, message, html)

Adds CSS enter and exit classes to a component, letting it animate out before a list or a dynamic re-render removes it.

Removal waits for the CSS animations to end, or happens at once with none. Use forwards on the exit animation, and pass "" for a side you don’t animate.

.panel-enter { animation: fade-in 200ms; }
.panel-exit { animation: fade-out 200ms forwards; }
panel()
|> component.transition(enter: "panel-enter", exit: "panel-exit")
pub fn transition_duration(
  component: Component(model, message, html),
  milliseconds milliseconds: Int,
) -> Component(model, message, html)

Overrides the transition piped before it, as the --lily-transition-duration custom property for your CSS to read:

.panel-exit {
  animation: panel-out var(--lily-transition-duration, 200ms) forwards;
}
panel()
|> component.transition(enter: "panel-enter", exit: "panel-exit")
|> component.transition_duration(milliseconds: 400)
✨ Search Document