Events

Events in Hologram are user interactions that can trigger actions or commands. They are the primary way to make your application interactive and responsive to user input.

Event Types

Hologram supports various event types that you can bind to elements in your templates (more are coming soon):

Event Data

When an event occurs, Hologram provides event data that you can access in the action or command that was bound to handle that event. This data is available in the params argument under the :event key. The event data structure varies depending on the event type:

Change Event

The change event includes:

Pointer Events

Pointer events (like click, pointerdown, pointerup) include:

Pointer events are ignored in the following cases:

This behavior prevents interference with browser's built-in functionality like opening links in new tabs, selecting text, or using browser extensions.

Select Event

The select event includes:

Submit Event

The submit event includes form field values directly under the :event key. For example, if your form has fields named "email" and "password", the event data will look like:

%{email: "user@example.com", password: "secret"}

Event Binding Syntax

When binding events to elements in your templates, prefix the event name with a dollar sign ($). You can bind events using several syntax options. Note that text and shorthand syntax are only available for actions. To trigger a command, you must use the longhand syntax with the command: key.

Text Syntax (Actions Only)

The simplest way to bind an event to an action:

<button $click="my_action">Click me</button>

Expression Shorthand Syntax (Actions Only)

Use this syntax when you need to pass parameters to the action:

<button $click={:my_action, param_1: 1, param_2: 2}>Click me</button>

Expression Longhand Syntax

For complete control, including targeting specific components and triggering commands:

<button $click={action: :my_action, target: "other_component", params: %{key: value}}>Click me</button>

To trigger a command, use the command: key instead of action:

<button $click={command: :my_command, params: %{key: value}}>Click me</button>

Event Targets

By default, events are handled by the component that contains them. If the component is stateless (doesn't have a CID specified), the closest stateful component higher in the hierarchy is used. You can specify a different component for handling the event using the target parameter:

<button $click={action: :my_action, target: "other_component"}>Click me</button>

Valid targets include:

Event Flow

When an event occurs, it can trigger either an action or a command:

Best Practices

When working with events in Hologram, consider these best practices:

Previous
← Layouts