lustre/attribute

Types

Attributes are attached to specific elements. They’re either key/value pairs or event handlers.

pub opaque type Attribute(msg)

Functions

pub fn accept(types: List(String)) -> Attribute(a)
pub fn accept_charset(types: List(String)) -> Attribute(a)
pub fn alt(text: String) -> Attribute(a)
pub fn attribute(name: String, value: String) -> Attribute(a)

Lustre does some work internally to convert common Gleam values into ones that make sense for JavaScript. Here are the types that are converted:

  • List(a) -> Array(a)
  • Some(a) -> a
  • None -> undefined
pub fn autocomplete(name: String) -> Attribute(a)
pub fn autofocus(should_autofocus: Bool) -> Attribute(a)
pub fn autoplay(should_autoplay: Bool) -> Attribute(a)
pub fn checked(is_checked: Bool) -> Attribute(a)
pub fn class(name: String) -> Attribute(a)
pub fn classes(names: List(#(String, Bool))) -> Attribute(a)
pub fn cols(val: Int) -> Attribute(a)
pub fn controls(visible: Bool) -> Attribute(a)
pub fn disabled(is_disabled: Bool) -> Attribute(a)
pub fn download(filename: String) -> Attribute(a)
pub fn for(id: String) -> Attribute(a)
pub fn height(val: Int) -> Attribute(a)
pub fn href(uri: String) -> Attribute(a)
pub fn id(name: String) -> Attribute(a)
pub fn loop(should_loop: Bool) -> Attribute(a)
pub fn map(attr: Attribute(a), f: fn(a) -> b) -> Attribute(b)
pub fn max(val: String) -> Attribute(a)
pub fn min(val: String) -> Attribute(a)
pub fn msg(uri: String) -> Attribute(a)
pub fn name(name: String) -> Attribute(a)
pub fn on(name: String, handler: fn(Dynamic) -> Option(a)) -> Attribute(
  a,
)

Attach custom event handlers to an element. A number of helper functions exist in this module to cover the most common events and use-cases, so you should check those out first.

If you need to handle an event that isn’t covered by the helper functions, then you can use on to attach a custom event handler. The callback is given the event object as a Dynamic.

As a simple example, you can implement on_click like so:

import gleam/option.{Some}
import lustre/attribute.{Attribute}
import lustre/event

pub fn on_click(msg: msg) -> Attribute(msg) {
  use _ <- event.on("click")
  Some(msg)
}

By using gleam/dynamic you can decode the event object and pull out all sorts of useful data. This is how on_input is implemented:

import gleam/dynamic
import gleam/option.{None, Some}
import gleam/result
import lustre/attribute.{Attribute}
import lustre/event

pub fn on_input(msg: fn(String) -> msg) -> Attribute(msg) {
  use event, dispatch <- on("input")
  let decode = dynamic.field("target", dynamic.field("value", dynamic.string))

  case decode(event) {
    Ok(value) -> Some(msg(value))
    Error(_) -> None
  }
}

You can take a look at the MDN reference for events here to see what you can decode.

Unlike the helpers in the rest of this module, it is possible to simply ignore the dispatch function and not dispatch a message at all. In fact, we saw this with the on_input example above: if we can’t decode the event object, we simply return None and emit nothing.

Beyond ignoring errors, this can be used to perform side effects we don’t need to observe in our main application loop, such as logging…

import gleam/io
import gleam/option.{None}
import lustre/attribute.{Attribute}
import lustre/event

pub fn log_on_click(msg: String) -> Attribute(msg) {
  use _ <- event.on("click")
  io.println(msg)
  None
}
pub fn pattern(regex: String) -> Attribute(a)
pub fn placeholder(text: String) -> Attribute(a)
pub fn property(name: String, value: a) -> Attribute(b)
pub fn readonly(is_readonly: Bool) -> Attribute(a)
pub fn rel(relationship: String) -> Attribute(a)
pub fn required(is_required: Bool) -> Attribute(a)
pub fn rows(val: Int) -> Attribute(a)
pub fn selected(is_selected: Bool) -> Attribute(a)
pub fn src(uri: String) -> Attribute(a)
pub fn step(val: String) -> Attribute(a)
pub fn style(properties: List(#(String, String))) -> Attribute(a)
pub fn target(target: String) -> Attribute(a)
pub fn to_string(attr: Attribute(a)) -> String
pub fn to_string_builder(attr: Attribute(a)) -> StringBuilder
pub fn type_(name: String) -> Attribute(a)
pub fn value(val: Dynamic) -> Attribute(a)
pub fn width(val: Int) -> Attribute(a)
pub fn wrap(mode: String) -> Attribute(a)
Search Document