hx

hx - Gleam HTMX Bindings

Type-safe Gleam bindings for HTMX attributes and events.

Links

Basic Usage

import hx
import lustre/element/html

html.button([
  hx.get("/api/users"),
  hx.target(hx.CssSelector("#results")),
  hx.swap(hx.InnerHTML),
], [html.text("Load Users")])

Types

Event triggers with optional modifiers.

Events can be combined with modifiers like with_delay, with_throttle, etc.

Official Event Documentation

pub opaque type Event

Modifiers that change event behavior.

Official Trigger Modifiers

pub opaque type EventModifier

Configuration options for the intersect event.

Official Intersect Documentation

pub type IntersectConfig {
  Root(selector: String)
  Threshold(value: Float)
}

Constructors

  • Root(selector: String)

    Specify the root element for intersection observation

  • Threshold(value: Float)

    Intersection threshold (0.0 to 1.0)

Queue behavior for synchronized requests.

Used with hx.sync to control request queuing.

Official Sync Documentation

pub type Queue {
  First
  Last
  All
}

Constructors

  • First

    Process the first request, ignore subsequent

  • Last

    Process the last request, ignore previous

  • All

    Process all requests

Scroll position specification for swap modifiers.

pub type ScrollPosition {
  Top
  Bottom
}

Constructors

  • Top

    Scroll to the top

  • Bottom

    Scroll to the bottom

Scroll target specification for swap modifiers.

pub type ScrollTarget {
  TargetScroll(ScrollPosition)
  ElementScroll(selector: String, position: ScrollPosition)
  WindowScroll(ScrollPosition)
}

Constructors

Extended CSS selectors for targeting elements.

HTMX extends standard CSS selectors with additional keywords for more flexible targeting.

Official Extended CSS Selectors Documentation

pub type Selector {
  Selector(selector: String)
  Document
  Window
  Closest(selector: String)
  Find(selector: String)
  Next(selector: String)
  Previous(selector: String)
  This
}

Constructors

  • Selector(selector: String)

    Standard CSS selector (e.g., “#id”, “.class”, “div > p”)

  • Document

    Target the document

  • Window

    Target the window

  • Closest(selector: String)

    Find the closest ancestor matching the selector

  • Find(selector: String)

    Find the first descendant matching the selector

  • Next(selector: String)

    Find the next sibling matching the selector

  • Previous(selector: String)

    Find the previous sibling matching the selector

  • This

    Target the element itself

Show target specification for swap modifiers.

pub type ShowTarget {
  TargetShow(ScrollPosition)
  ElementShow(selector: String, position: ScrollPosition)
  WindowShow(ScrollPosition)
}

Constructors

Different ways content can be swapped in the DOM.

Controls how the response content replaces existing content.

Official Swap Documentation

pub type Swap {
  InnerHTML
  OuterHTML
  TextContent
  Afterbegin
  Beforebegin
  Beforeend
  Afterend
  Delete
  SwapNone
}

Constructors

  • InnerHTML

    Replace the inner HTML of the target element

  • OuterHTML

    Replace the entire target element

  • TextContent

    Replace the text content of the target element

  • Afterbegin

    Insert inside the target, before its first child

  • Beforebegin

    Insert before the target element (as a sibling)

  • Beforeend

    Insert inside the target, after its last child

  • Afterend

    Insert after the target element (as a sibling)

  • Delete

    Delete the target element

  • SwapNone

    Do not swap content

Options for modifying swap behavior.

These modifiers can be used with swap_with and swap_oob_with.

Official Swap Modifiers Documentation

pub type SwapConfig {
  Transition(Bool)
  SwapTiming(duration.Duration)
  Settle(duration.Duration)
  IgnoreTitle(Bool)
  ScrollTo(ScrollTarget)
  Show(ShowTarget)
  FocusScroll(Bool)
}

Constructors

  • Transition(Bool)

    Enable/disable view transitions

  • SwapTiming(duration.Duration)

    Delay before swapping content

  • Duration to wait after swap before settling

  • IgnoreTitle(Bool)

    Whether to ignore title updates from response

  • ScrollTo(ScrollTarget)

    Scroll configuration

  • Show(ShowTarget)

    Show configuration

  • FocusScroll(Bool)

    Whether to scroll to focused element

Synchronization options for coordinating AJAX requests.

Controls how multiple requests to the same element are handled.

Official Sync Documentation

pub type Sync {
  Default(Selector)
  Drop(Selector)
  Abort(Selector)
  Replace(Selector)
  Queue(Selector, Queue)
}

Constructors

  • Default(Selector)

    Use default behavior (drop new requests if one is in flight)

  • Drop(Selector)

    Explicitly drop (ignore) new requests if one is in flight

  • Abort(Selector)

    Drop new requests OR abort current request if a new one comes in

  • Replace(Selector)

    Abort current request and replace it with the new one

  • Queue(Selector, Queue)

    Queue requests with specified strategy

Values

pub fn blur() -> Event

Creates a blur event.

MDN Reference

pub fn boost(set: Bool) -> attribute.Attribute(a)

Progressive enhancement for links and forms using AJAX.

Official Documentation

pub fn change() -> Event

Creates a change event (fires when an input value changes).

MDN Reference

pub fn click() -> Event

Creates a click event.

MDN Reference

pub fn confirm(confirm_text: String) -> attribute.Attribute(a)

Shows a confirmation dialog before making a request.

Official Documentation

pub fn custom(event_name: String) -> Event

Creates a custom event with the given name.

pub fn delete(url url: String) -> attribute.Attribute(a)

Issues a DELETE request to the given URL when the element is triggered.

Official Documentation

pub fn disable() -> attribute.Attribute(a)

Disables HTMX processing for an element and its children.

Official Documentation

pub fn disable_elt(
  extended_css_selectors: List(Selector),
) -> attribute.Attribute(a)

Disables elements during AJAX requests.

Official Documentation

Examples

// Disable specific buttons
hx.disable_elt([hx.Selector("button")])

// Disable multiple element types
hx.disable_elt([
  hx.Selector("input[type='submit']"),
  hx.Selector("button")
])
pub fn disinherit(
  attributes: List(String),
) -> attribute.Attribute(a)

Controls which attributes are not inherited from parent elements.

Official Documentation

pub fn disinherit_all() -> attribute.Attribute(a)

Prevents inheritance of all HTMX attributes from ancestors.

Official Documentation

pub fn dom_content_loaded() -> Event

Creates a DOMContentLoaded event that fires when the initial HTML document is loaded.

MDN Reference

pub fn encoding(encoding: String) -> attribute.Attribute(a)

Sets the encoding type for the request (e.g., “multipart/form-data”).

Official Documentation

pub fn ext(ext: List(String)) -> attribute.Attribute(a)

Enables HTMX extensions for an element.

Official Documentation

pub fn focus() -> Event

Creates a focus event.

MDN Reference

pub fn get(url url: String) -> attribute.Attribute(a)

Issues a GET request to the given URL when the element is triggered.

Official Documentation

pub fn headers(
  headers: json.Json,
  compute_value: Bool,
) -> attribute.Attribute(a)

Adds custom headers to the AJAX request.

Official Documentation

pub fn history(should_be_saved: Bool) -> attribute.Attribute(a)

Controls if requests from this element update browser history.

Official Documentation

pub fn history_elt() -> attribute.Attribute(a)

Marks the element to snapshot for history restoration.

Official Documentation

pub fn htmx_abort() -> Event

Listener event to cancel ongoing requests.

Official Events Reference

pub fn htmx_after_on_load() -> Event

Triggered after an AJAX onload has finished.

Official Events Reference

pub fn htmx_after_process_node() -> Event

Triggered after processing a DOM node.

Official Events Reference

pub fn htmx_after_request() -> Event

Triggered after an AJAX request completes.

Official Events Reference

pub fn htmx_after_settle() -> Event

Triggered after the settling phase completes.

Official Events Reference

pub fn htmx_after_swap() -> Event

Triggered after content is swapped into the DOM.

Official Events Reference

pub fn htmx_bad_response_url() -> Event

Triggered when a bad response URL is encountered.

Official Events Reference

pub fn htmx_before_cleanup_element() -> Event

Triggered before cleaning up an element.

Official Events Reference

pub fn htmx_before_history_save() -> Event

Triggered before a history save occurs.

Official Events Reference

pub fn htmx_before_history_update() -> Event

Triggered before a history update occurs.

Official Events Reference

pub fn htmx_before_on_load() -> Event

Triggered before any response processing.

Official Events Reference

pub fn htmx_before_process_node() -> Event

Triggered before processing a DOM node.

Official Events Reference

pub fn htmx_before_request() -> Event

Triggered before an AJAX request is made.

Official Events Reference

pub fn htmx_before_send() -> Event

Triggered right before sending the request.

Official Events Reference

pub fn htmx_before_settle() -> Event

Triggered before the settling phase begins.

Official Events Reference

pub fn htmx_before_swap() -> Event

Triggered before content is swapped into the DOM.

Official Events Reference

pub fn htmx_before_transition() -> Event

Triggered before View Transition API wraps a swap.

Official Events Reference

pub fn htmx_config_request() -> Event

Triggered before a request is configured.

Official Events Reference

pub fn htmx_confirm() -> Event

Triggered before every request trigger, allowing cancellation or async confirmation.

Official Events Reference

pub fn htmx_error() -> Event

Triggered on general errors.

Official Events Reference

pub fn htmx_eval_disallowed_error() -> Event

Triggered when eval is disallowed.

Official Events Reference

pub fn htmx_event_filter_error() -> Event

Triggered when an event filter error occurs.

Official Events Reference

pub fn htmx_history_cache_error() -> Event

Triggered when a history cache error occurs.

Official Events Reference

pub fn htmx_history_cache_hit() -> Event

Triggered when a history cache hit occurs.

Official Events Reference

pub fn htmx_history_cache_miss() -> Event

Triggered when a history cache miss occurs.

Official Events Reference

pub fn htmx_history_cache_miss_load() -> Event

Triggered when loading after a history cache miss.

Official Events Reference

pub fn htmx_history_cache_miss_load_error() -> Event

Triggered when a history cache miss load error occurs.

Official Events Reference

pub fn htmx_history_item_created() -> Event

Triggered when a history item is created.

Official Events Reference

pub fn htmx_history_restore() -> Event

Triggered when history is restored.

Official Events Reference

pub fn htmx_invalid_path() -> Event

Triggered when an invalid path is encountered.

Official Events Reference

pub fn htmx_load() -> Event

Triggered when new content is loaded into the DOM.

Official Events Reference

pub fn htmx_on_load_error() -> Event

Triggered when an onload error occurs.

Official Events Reference

pub fn htmx_oob_after_swap() -> Event

Triggered after an out-of-band swap.

Official Events Reference

pub fn htmx_oob_before_swap() -> Event

Triggered before an out-of-band swap.

Official Events Reference

pub fn htmx_oob_error_no_target() -> Event

Triggered when an OOB swap target is not found.

Official Events Reference

pub fn htmx_prompt() -> Event

Triggered when a prompt is shown.

Official Events Reference

pub fn htmx_pushed_into_history() -> Event

Triggered when an element is pushed into history.

Official Events Reference

pub fn htmx_replaced_in_history() -> Event

Triggered when an element is replaced in history.

Official Events Reference

pub fn htmx_response_error() -> Event

Triggered when an HTTP error response is received.

Official Events Reference

pub fn htmx_restored() -> Event

Triggered when the page is restored from history.

Official Events Reference

pub fn htmx_send_abort() -> Event

Triggered when a request is aborted.

Official Events Reference

pub fn htmx_send_error() -> Event

Triggered when a network error occurs.

Official Events Reference

pub fn htmx_session_storage_test() -> Event

Triggered during session storage test.

Official Events Reference

pub fn htmx_swap_error() -> Event

Triggered when an error occurs during swap.

Official Events Reference

pub fn htmx_syntax_error() -> Event

Triggered when a syntax error occurs.

Official Events Reference

pub fn htmx_target_error() -> Event

Triggered when a target error occurs.

Official Events Reference

pub fn htmx_timeout() -> Event

Triggered when a request times out.

Official Events Reference

pub fn htmx_trigger() -> Event

Triggered when a trigger is activated.

Official Events Reference

pub fn htmx_validate_url() -> Event

Validates the URL before making the request.

Official Events Reference

pub fn htmx_validation_failed() -> Event

Triggered when validation fails.

Official Events Reference

pub fn htmx_validation_halted() -> Event

Triggered when validation is halted.

Official Events Reference

pub fn htmx_validation_validate() -> Event

Triggered before validation runs.

Official Events Reference

pub fn htmx_xhr_abort() -> Event

Triggered when an XHR request is aborted.

Official Events Reference

pub fn htmx_xhr_loadend() -> Event

Triggered when an XHR request ends.

Official Events Reference

pub fn htmx_xhr_loadstart() -> Event

Triggered when an XHR request starts.

Official Events Reference

pub fn htmx_xhr_progress() -> Event

Triggered during XHR request progress.

Official Events Reference

pub fn hyper_script(script: String) -> attribute.Attribute(a)

Adds hyperscript behavior to an element using the “_” attribute.

Official Hyperscript Documentation

pub fn include(
  extended_css_selector: Selector,
) -> attribute.Attribute(a)

Includes additional element values in the request.

Official Documentation

pub fn indicator(selector: Selector) -> attribute.Attribute(a)

Shows loading indicators during AJAX requests.

Supports extended CSS selectors including closest, this, etc.

Official Documentation

pub fn inherit(
  attributes: List(String),
) -> attribute.Attribute(a)

Explicitly inherits specific attributes from parent elements.

Official Documentation

pub fn inherit_all() -> attribute.Attribute(a)

Inherits all HTMX attributes from parent elements.

Official Documentation

pub fn input() -> Event

Creates an input event (fires on every character typed).

MDN Reference

pub fn intersect(config: List(IntersectConfig)) -> Event

Creates an intersect event that triggers when the element enters the viewport.

Supports configuration options for root element, threshold, and root margin.

Official Intersect Documentation

Examples

// Basic intersect
hx.intersect([])

// With threshold
hx.intersect([hx.Threshold(0.5)])

// With root element and threshold
hx.intersect([hx.Root("#container"), hx.Threshold(0.75)])
pub fn intersect_once(config: List(IntersectConfig)) -> Event

Creates an intersect event that fires only once when entering the viewport.

Official Intersect Documentation

Examples

// Basic intersect once
hx.intersect_once([])

// With threshold, fires once
hx.intersect_once([hx.Threshold(0.5)])
pub fn keydown() -> Event

Creates a keydown event.

MDN Reference

pub fn keyup() -> Event

Creates a keyup event.

MDN Reference

pub fn load() -> Event

Creates a load event that fires when the element loads.

MDN Reference

pub fn mouseout() -> Event

Creates a mouseout event.

MDN Reference

pub fn mouseover() -> Event

Creates a mouseover event.

MDN Reference

pub fn no_replace_url() -> attribute.Attribute(a)

Prevents URL replacement in the browser history.

Official Documentation

pub fn on(
  event_name: String,
  script: String,
) -> attribute.Attribute(a)

Handles events with inline scripts on elements using the hx-on attribute.

This is an HTMX 2.x feature that allows inline event handling.

Official Documentation

Examples

hx.on("click", "alert('Clicked!')")
hx.on("htmx:afterSwap", "console.log('Swapped')")
pub fn params(params: String) -> attribute.Attribute(a)

Filters which parameters are included in the request.

Official Documentation

pub fn patch(url url: String) -> attribute.Attribute(a)

Issues a PATCH request to the given URL when the element is triggered.

Official Documentation

pub fn post(url url: String) -> attribute.Attribute(a)

Issues a POST request to the given URL when the element is triggered.

Official Documentation

pub fn preserve() -> attribute.Attribute(a)

Preserves element state between requests (e.g., iframes, videos).

Official Documentation

pub fn prompt(prompt_text: String) -> attribute.Attribute(a)

Shows an input prompt before submitting the request.

Official Documentation

pub fn push_url(bool: Bool) -> attribute.Attribute(a)

Pushes a URL into the browser’s location bar and history.

Official Documentation

pub fn put(url url: String) -> attribute.Attribute(a)

Issues a PUT request to the given URL when the element is triggered.

Official Documentation

pub fn replace_url() -> attribute.Attribute(a)

Replaces the current URL in the browser history.

Official Documentation

pub fn replace_url_with(url: String) -> attribute.Attribute(a)

Replaces the current URL with the specified URL.

Official Documentation

pub fn request(request: String) -> attribute.Attribute(a)

Configures the AJAX request (timeout, credentials, etc.).

Official Documentation

pub fn resize() -> Event

Creates a resize event.

MDN Reference

pub fn revealed() -> Event

Creates a revealed event that triggers when the element is scrolled into the viewport.

Official Revealed Documentation

pub fn scroll() -> Event

Creates a scroll event.

MDN Reference

pub fn select(css_selector: String) -> attribute.Attribute(a)

Selects a subset of the server response to process.

Official Documentation

pub fn select_oob(
  css_selectors: List(String),
) -> attribute.Attribute(a)

Selects content from a response to be swapped in out-of-band.

Accepts a comma-separated list of CSS selectors for elements to swap out-of-band.

Official Documentation

pub fn submit() -> Event

Creates a submit event (fires when a form is submitted).

MDN Reference

pub fn swap(swap swap: Swap) -> attribute.Attribute(a)

Controls how content is swapped into the DOM.

Official Documentation

Examples

// Basic swap strategies
hx.swap(hx.InnerHTML)
hx.swap(hx.OuterHTML)
hx.swap(hx.Afterend)
pub fn swap_oob(
  swap swap: Swap,
  css_selector css_selector: option.Option(String),
) -> attribute.Attribute(a)

Allows you to swap content “out of band” - updating other elements in the response.

Official Documentation

pub fn swap_oob_with(
  swap swap: Swap,
  css_selector css_selector: option.Option(String),
  config modifier: SwapConfig,
) -> attribute.Attribute(a)

Allows you to swap content “out of band” with configuration options.

Official Documentation

pub fn swap_with(
  swap swap: Swap,
  config config: SwapConfig,
) -> attribute.Attribute(a)

Controls how content is swapped into the DOM with additional configuration options.

Official Documentation

Examples

// Swap with transition
hx.swap_with(hx.InnerHTML, hx.Transition(True))

// Swap with timing
hx.swap_with(hx.OuterHTML, hx.SwapTiming(duration.milliseconds(500)))
pub fn sync(strategy: Sync) -> attribute.Attribute(a)

Synchronizes AJAX requests between elements.

Official Documentation

Examples

// Use default drop behavior (strategy is optional)
hx.sync(hx.Default(hx.Closest("form")))

// Explicitly drop conflicting requests
hx.sync(hx.Drop(hx.Closest("form")))

// Abort current request if new one comes in
hx.sync(hx.Abort(hx.This))

// Queue requests, keeping only the last
hx.sync(hx.Queue(hx.Selector("#form"), hx.Last))
pub fn sync_option_to_string(sync_option: Sync) -> String
pub fn target(
  extended_css_selector extended_css_selector: Selector,
) -> attribute.Attribute(a)

Specifies the target element to swap content into.

Official Documentation

Examples

// Target specific element
hx.target(hx.Selector("#results"))

// Target parent element
hx.target(hx.Closest(".card"))

// Target the element itself
hx.target(hx.This)
pub fn trigger(events: List(Event)) -> attribute.Attribute(a)

Specifies which events will cause an HTMX request.

Official Documentation

Examples

// Single event
hx.trigger([hx.click()])

// Multiple events
hx.trigger([hx.click(), hx.keyup()])

// Event with modifiers
hx.trigger([hx.with_delay(hx.click(), duration.seconds(2))])
pub fn trigger_polling(
  timing timing: duration.Duration,
  filters filters: option.Option(String),
  on_load on_load: Bool,
) -> attribute.Attribute(a)

Creates a polling trigger that fires at regular intervals.

Official Documentation

Examples

// Poll every 5 seconds
hx.trigger_polling(timing: duration.seconds(5), filters: None, on_load: False)

// Poll with filters
hx.trigger_polling(timing: duration.seconds(10), filters: Some("visible"), on_load: False)

// Start polling immediately and on load
hx.trigger_polling(timing: duration.seconds(2), filters: None, on_load: True)
pub fn validate(bool: Bool) -> attribute.Attribute(a)

Forces validation before making a request.

Official Documentation

pub fn vals(
  json: json.Json,
  compute_value: Bool,
) -> attribute.Attribute(a)

Adds values to be submitted with the request.

Official Documentation

pub fn with_changed(event: Event) -> Event

Only triggers if the element’s value has changed.

Official Trigger Modifiers

pub fn with_consume(event: Event) -> Event

Prevents the event from bubbling up the DOM.

Official Trigger Modifiers

pub fn with_delay(
  event: Event,
  timing: duration.Duration,
) -> Event

Adds a delay before the event triggers.

Official Trigger Modifiers

pub fn with_from(
  event: Event,
  extended_css_selector: Selector,
) -> Event

Listens for the event on a different element.

Official Trigger Modifiers

pub fn with_once(event: Event) -> Event

Makes the event fire only once.

Official Trigger Modifiers

pub fn with_queue(
  event: Event,
  queue: option.Option(Queue),
) -> Event

Queues the event with a specific strategy.

Official Trigger Modifiers

pub fn with_target(event: Event, css_selector: String) -> Event

Filters the event to only trigger when it targets a specific element.

Official Trigger Modifiers

pub fn with_throttle(
  event: Event,
  timing: duration.Duration,
) -> Event

Throttles the event to fire at most once per duration.

Official Trigger Modifiers

Search Document