lustre/stylish/keyed

Keyed layout elements for efficient list rendering.

When rendering dynamic lists that can be reordered, added to, or removed from, using keyed elements helps the rendering engine efficiently update only what changed.

Each child is associated with a unique string key. When the list changes, elements with matching keys are preserved and reused, avoiding unnecessary re-renders.

When to Use Keys

When NOT to Use Keys

Example

import lustre/stylish as el
import lustre/stylish/keyed
import gleam/list

pub fn todo_list(todos: List(Todo)) {
  let keyed_todos = list.map(todos, fn(todo) {
    #(todo.id, el.row([], [
      el.text(todo.title),
      delete_button(todo.id),
    ]))
  })
  
  keyed.column([], keyed_todos)
}

Values

pub fn column(
  attrs: List(@internal Attribute(@internal Aligned, msg)),
  children: List(#(String, stylish.Element(msg))),
) -> stylish.Element(msg)

A keyed column that lays out children vertically.

let items = list.index_map(todos, fn(todo, idx) {
  #(int.to_string(idx), todo_view(todo))
})
keyed.column([el.spacing(5)], items)
pub fn el(
  attrs: List(@internal Attribute(@internal Aligned, msg)),
  child: #(String, stylish.Element(msg)),
) -> stylish.Element(msg)

A keyed element that wraps a single child.

keyed.el([], #("item-1", child_element))
pub fn row(
  attrs: List(@internal Attribute(@internal Aligned, msg)),
  children: List(#(String, stylish.Element(msg))),
) -> stylish.Element(msg)

A keyed row that lays out children horizontally.

let items = [
  #("a", el.text("First")),
  #("b", el.text("Second")),
  #("c", el.text("Third")),
]
keyed.row([el.spacing(10)], items)
Search Document