Actions & events
How a DOM event becomes a server action: `action` declares what the client may invoke, `on(...)` wires an element to it, a param schema coerces the input, and event/timing modifiers cover the page-level patterns that would otherwise need a hand-written Stimulus controller.
Declaring an action, binding an event#
A reactive action is a two-step contract. action :name on the class declares the method the client may invoke — default-deny, so a public method with no action line is unreachable. on(:name) on an element wires a DOM event to that action: it emits the signed data-reactive-* attributes the one generic Stimulus controller reads.
class Counter < ApplicationComponent
include Phlex::Reactive::Component
reactive_record :counter
action :increment # declared → invokable
action :decrement
def increment = @counter.increment!(:value)
def decrement = @counter.decrement!(:value)
def view_template
div(**reactive_root) do
button(**on(:decrement)) { "−" }
span { @counter.value }
button(**on(:increment)) { "+" }
end
end
endon(:action, event: "click") defaults to a click and adds type="button" (so a bare button inside a <form> can't submit it). The event flows client → endpoint → action → re-render: the click POSTs the signed token, the endpoint verifies it and runs the declared method, and the component re-renders into its own #id. No routes, no partial, no hand-picked Turbo target.
on(...) returns a data: hash. Merging it with another data:/ class: with a bare ** clobbers the data-action the descriptor rides on. Use Phlex's mix to deep-merge:
button(**mix(on(:increment), class: "btn", data: { testid: "inc" })) { "+" }Passing input — the param schema#
An action that takes input declares its params. Only declared keys reach the method, each coerced to its declared type; anything not in the schema is dropped before your method runs (no raw mass assignment). The schema is compiled once at declaration — a typo'd type symbol raises Phlex::Reactive::UnknownParamType at class load, not silently at click time.
action :rename, params: { title: :string }
def rename(title:)
authorize! @todo, :update? # the signature is not authorization
@todo.update!(title:)
end| Type | Coercion | Dropped when |
|---|---|---|
:string | the default; used raw | — |
:integer | to_i: blank and "abc" are 0, "12abc" is 12 | never |
:float | to_f: blank and "abc" are 0.0, "12abc" is 12.0 | never |
:strict_integer | Integer(value, 10) | blank, non-numeric, a fraction |
:strict_float | decimal notation → Float | blank, non-numeric, hex, out of range |
:boolean | truthy string → true/false | — |
:date | ISO8601 → Date | won't parse |
:datetime | ISO8601 → Time | won't parse |
:decimal | BigDecimal(value) | non-numeric |
:file | ActionDispatch::Http::UploadedFile, untouched | a non-file value |
Drop, don't fabricate. A value that won't coerce is dropped, so the method receives its keyword default. Give every declared param a default (def rename(title: nil)) so a dropped value has somewhere to land.
:integer and :float are the exception: they never drop. to_i and to_f read a cleared number field as 0 and "12abc" as 12, so an action can't tell a blank field from a typed 0. When 0 means something (a quantity, a cap, a price), declare the strict type:
action :save, params: { quantity: :strict_integer, weight: :strict_float }
def save(quantity: nil, weight: nil) # nil when blank or unparsable; a typed 0 stays 0
authorize! @widget, :update?
@widget.update!(quantity:, weight:)
end:strict_integer reads base 10 only, so "0x10" drops and "010" is 10. A fraction drops even when it is whole, so a JSON 1.0 is dropped too. :strict_float drops hex and a string too large or too small for a Float ("1e400", "1e-400"). Both accept a JSON number as well as a string, and both ignore surrounding whitespace. The one limit: a bare 1e-400 in a JSON body is already 0.0 when the parser hands it over, so it arrives as zero. A form field's value is a string, so it drops as usual.
Nested and array params compose. A params: { invoice: { date: :string } } matches bracketed field names (invoice[date]) after the endpoint expands the brackets — so the schema must mirror the field names, not the conceptual params. A flat { date: :string } against invoice[date] inputs matches nothing and the value is silently dropped.
Checkbox groups. Controls sharing a name that ends in [] are collected as an array of the chosen values: a ticked box contributes its value, an unticked one nothing, a <select multiple> its selected options. Declare it as an array type.
action :save, params: { features: [:string] }
# <input type="checkbox" name="features[]" value="news">Nothing ticked is an empty array, not a missing key, so an action can tell a cleared group from one that never rendered. That holds over a form body too, which the client uses as soon as a file input holds a file: an empty array can't be written there, so the client names the cleared group in empty_groups[] and the endpoint sets it to []. This works for params declared as an array at the top level, by their plain name or with the component's reactive_scope. A group declared one level down keeps its keyword default. The README has the details.
Three shapes keep their own meaning: a lone checkbox without [] stays the yes/no boolean, a radio group keeps its single checked value with or without [], and a hidden input sharing a name with a checkbox is that box's companion — Rails' check_box emits one carrying the unchecked_value, "0" by default — and contributes nothing. A hidden input without a same-named checkbox is an ordinary value, the usual shape for a list JS maintains.
Declare :file (or [:file] for multiple) to accept an upload in a reactive action. When the root holds a populated <input type="file">, the client sends the action as multipart FormData instead of JSON — same bracket-expanded field shape, so both bodies coerce identically. See the uploads example for the full walkthrough.
Custom param types (Phlex::Reactive.param_type)#
Register your own type in an initializer. The block receives the raw client value and returns the coerced value, or Phlex::Reactive::ParamSchema::DROP to reject it (the keyword default then applies — the same drop-don't-fabricate contract as :date or :strict_integer).
# config/initializers/phlex_reactive.rb
Phlex::Reactive.param_type(:money) do |value|
if /\A\d+(\.\d{1,2})?\z/.match?(value.to_s)
BigDecimal(value)
else
Phlex::Reactive::ParamSchema::DROP
end
end
# then, in any component:
action :charge, params: { amount: :money }
def charge(amount:) = @invoice.charge!(amount) # a BigDecimal, or unsetA schema referencing a registered type is validated at declaration like any built-in.
The type registry is frozen after initialization. A runtime param_type call raises — register every custom type from an initializer, never lazily from a component.
Running without the endpoint transaction — transaction: false#
The endpoint runs every action inside a database transaction, so a broadcast or enqueue deferred to commit never fires for a rolled-back change. An action that waits on something slow that is not the database, such as a call to a remote HTTP API, would hold that transaction (and its connection) open for the whole wait. Declare that one action transaction: false:
action :refresh, transaction: false
action :rename, params: { title: :string } # unchanged: in a transaction
action :sync, params: { force: :boolean }, transaction: false
def refresh
authorize! @widget, :update? # before any side effect
data = RemoteService.fetch(@widget) # no transaction open while it waits
@widget.update!(synced_at: Time.current, data:)
endtransaction: defaults to true, takes only true or false (anything else raises ArgumentError at class load), and a subclass inherits it with the action. Every other action, on the same component and on the same page, keeps the transaction.
What changes for that action:
- No endpoint transaction. Only the endpoint's own wrapper is skipped (and an overridden
transaction_wrapperis not called for it). A transaction opened further out, by a transactional test or anaround_actionwrapper, is not affected. The action may open its own, shorter transactions around its writes. - The endpoint rolls nothing back.
verify_authorizedstill raisesAuthorizationNotVerifiedafter an action that never authorized, and the request still fails with no reply, but the writes it already made stay (unless a transaction opened outside the endpoint rolls them back). An error the action raises likewise rolls back only what its own transactions cover. Authorize before any side effect. - Commit-deferred work fires at the action's own commit, or at once when it has none open. "Never fires for a rolled-back change" is then the action's job: wrap the write and its broadcast or enqueue in one
ActiveRecord::Base.transaction.
Phlex::Reactive.around_action wrappers, reply.defer and reply.pending behave as for any action, and run_reactive in the test helpers skips the transaction for the same actions the endpoint does. The text inventory (phlex_reactive:actions) marks them (no endpoint transaction), and FORMAT=json and the MCP tools report transaction: false for them.
Keyboard triggers (event:)#
event: is interpolated straight into the Stimulus action descriptor, so any Stimulus event string works — including its native keyboard filters. Pass event: "keydown.enter" to fire only on Enter, event: "keydown.esc" for Escape — the classic "Enter adds the row", "Escape cancels the edit" interactions, with no event.key check of your own.
# Enter in the composer adds the todo (same action as the Add button).
input(**mix(on(:add, event: "keydown.enter"), name: "title", placeholder: "New todo…"))
# Inline editor: Enter saves; a separate control cancels on Escape.
input(**mix(on(:save, event: "keydown.enter"), name: "title", value: @todo.title))
button(**on(:cancel, event: "keydown.esc")) { "Cancel" }The filter tokens are Stimulus's own (enter, esc, space, up, down, a bare letter, …). A keyboard trigger isn't a click, so it gets no forced type="button". Folding the key into event: keeps key free as an ordinary action-param name.
A list row drawn as one link should answer Space as well as Enter. Space does not follow a link natively, so bind it to the js.click client op (#471): HTMLElement#click() on the link, the browser's own activation, so Turbo Drive makes the visit with the link's own data-turbo-* attributes. The binding prevents the keydown's default, so the page does not scroll. A held Space repeats its keydown, and each repeat clicks again.
a(**mix(on_client("keydown.space", js.click), href: item_path(item))) { item.name }Each trigger element carries a single reactive action, so you can't put on(:save, event: "keydown.enter") and on(:cancel, event: "keydown.esc") on the same input — the second overwrites the first. Bind each key trigger to its own element.
Timing — debounce: and throttle:#
Two mutually-exclusive rate limiters shape how rapid events reach the server. Passing both raises ArgumentError.
debounce: 300(trailing-edge) coalesces rapid events into one round trip fired after the quiet period — live-as-you-type without a POST per keystroke. A blur flushes a pending dispatch so the last edit is never dropped.throttle: 250(leading-edge) fires the first event immediately, then drops further events until the window elapses — for a hot trigger likescrollormousemove.
# Recompute a total live as the user types, without hammering the endpoint.
input(**mix(on(:update, event: "input", debounce: 300), name: "quantity", value: @item.quantity))Page-level modifiers — window:, outside:, once:#
Three modifiers (issue #80) cover the page-level trigger patterns that otherwise need a hand-written Stimulus controller:
window: truebinds the trigger to the window (Stimulus's native@window) for page-level events likescroll/resize. A window-bound trigger is neverpreventDefault-ed — a mounted dropdown must not kill link clicks elsewhere — and skips the forcedtype="button". An element-bound trigger is prevented unless it saysprevent: false(below).outside: truefires the action only for events whose target is outside this component's root — the close-a-dropdown-on-outside-click pattern. An event inside the root is a complete client-side no-op. Implieswindow: true.once: truefires at most once, then unbinds (Stimulus's:once).
# A dropdown that closes itself on any click outside — no Stimulus controller.
div(**mix(reactive_root, on(:close_menu, outside: true))) do
button(**on(:toggle_menu)) { "Menu" }
ul { menu_items } if @open
end
# Throttled page-scroll tracking.
div(**mix(reactive_root, on(:track, event: "scroll", window: true, throttle: 500)))Page-level shortcuts — shortcut:#
Undo, redo, save and a command palette bind with one keyword on on(...) or on_client(:keydown, …) (issue #357):
div(**mix(reactive_root, on_client(:keydown, js.toggle("#palette"), shortcut: "mod+k"))) do
span(**on(:undo, shortcut: "mod+z")) # one reactive action per element
span(**on(:redo, shortcut: "mod+shift+z"))
textarea(name: "notes")
endA shortcut is a window keydown binding with four rules a plain event: "keydown.meta+z", window: true does not have:
modis ⌘ on Apple platforms and Ctrl elsewhere. The helper renders a meta twin and a ctrl twin; the client runs the one for its platform and the other does nothing, so Ctrl+Z on a Mac stays the browser's. A chord withoutmod("esc","alt+n") renders one descriptor.- Typing is left alone. With focus in an
input,textarea,selector anythingcontenteditable(includingreactive_editable), the shortcut does nothing and the browser's own handling (text undo) runs. Opt in with the Hash form:shortcut: { keys: "esc", in_editable: true }. - A key something else handled is left to it. When a handler inside the page already called
preventDefaulton the keydown — Escape cancelling a drag, an element-boundon/on_clientbinding onkeydown.esc, your own listener — the shortcut does nothing (issue #429). Another shortcut'spreventDefaultdoes not count: two shortcuts on one chord both run. To run regardless, use the Hash form:shortcut: { keys: "esc", handled: true }. One exception: while a root with a shortcut on that chord has not connected yet,phlex/reactive/earlyprevents the key for it first, so a drag's Escape then still runs the shortcuts on it. - The chord is prevented, synchronously and before any
confirm:dialog, somod+sdoes not also open the save dialog — including a chord pressed before the controller connects, withphlex/reactive/early. Other window bindings are still never prevented.
shortcut: decides the event and the window binding: with event:, window: or outside: it raises ArgumentError. The key must be in Stimulus's default key map (a letter, a digit, enter, tab, esc, space, the arrows, home, end, page_up, page_down) or be delete or backspace; an unknown key raises ArgumentError. On on_client, the event must be bare keydown (:keydown or "keydown").
delete and backspace are matched by the library, not Stimulus, whose default key map lacks them: such a shortcut binds a bare keydown@window (no key filter, no meta/ctrl twin) and the client matches the chord, so it works even on an app with a custom Stimulus schema. The rules above all hold — Delete typed in a search box stays the field's. once: raises on these two keys: a bare binding would be spent by the first key pressed of any kind.
span(**on(:remove_selected, shortcut: "delete")) # remove the selected row
span(**on_client(:keydown, js.hide("#preview"), shortcut: "mod+backspace"))Keep the native default — prevent: false#
An element-bound trigger (on or on_client) calls preventDefault() before its ops or action run, so a bare button inside a <form> cannot submit it. (Once the controller is connected, a binding whose key filter, detail: or spent once: skips the event leaves it alone; before that, phlex/reactive/early prevents every firing that matches a descriptor.) Some listeners only observe an event and need the browser to carry on: a drop zone built from a native <input type="file"> stretched over an area gets the dropped file only when nothing cancelled the drop. prevent: false (#433) skips the preventDefault for that trigger, and its ops or action still run:
div(id: "zone", class: "relative", **mix(
on_client(:dragenter, js.add_class("#zone", "is-over")),
on_client(:dragleave, js.remove_class("#zone", "is-over")),
on_client(:drop, js.remove_class("#zone", "is-over"), prevent: false) # keeps the drop
)) do
input(type: "file", name: "file", class: "absolute inset-0 opacity-0")
plain "Drop a file here"
end
div(**on(:track, event: "drop", prevent: false)) # the same keyword on a server trigger- The default,
prevent: true, renders no attribute: existing markup is byte-identical. - It belongs to its own binding: a sibling
on_clienton another event (thedragenterabove) is still prevented, and so is the event when another binding on the same element and event does not opt out. - A
window:oroutside:trigger never prevents, soprevent: falsechanges nothing there; withshortcut:(a chord the page claims) it raisesArgumentError. Anything buttrue/falseraises too. - A click trigger keeps its forced
type="button"; a button that should submit its form natively says so withmix(on(...), type!: "submit"). - With
confirm:, this trigger does not cancel the native default, whatever the dialog answers: the dialog gates only its ops or action. phlex/reactive/earlyfollows the same rule before the controller connects.
Reserved keyword names#
These keywords on on(...) are reserved — they configure the trigger and are no longer usable as free action-param names. Everything not on this list flows through as an action param (so on(:switch, key: "pgbus") still passes key as a param):
| Keyword | Purpose |
|---|---|
event: | the DOM event / Stimulus keyboard filter ("click", "keydown.enter", …) |
debounce: | trailing-edge coalescing (ms) |
throttle: | leading-edge rate limit (ms) |
window: | bind to the window |
outside: | fire only outside the root (implies window:) |
once: | fire at most once |
prevent: | false keeps an element-bound trigger's native default (#433) |
shortcut: | a page-level keyboard chord ("mod+z"): platform mod, skips typing and a key already handled, prevented |
confirm: | gate behind a confirmation dialog — a String (always), or a Hash (conditional, #179) |
optimistic: | reversible cosmetic hint applied before the round trip |
busy: | declarative pending state on the trigger |
A misspelled action surfaces early: with Phlex::Reactive.verbose_errors on (the default in development and test), on(:typo) raises at render time listing the declared actions, instead of an opaque 403 on click. Production keeps the permissive emit so a stale page after a deploy that removed an action never 500s on render.
Combobox keyboard navigation is no longer an on(...) keyword — it's the standalone reactive_listnav helper, composed via mix, e.g. mix(on(:search, event: "input"), reactive_listnav).
reactive_listnav(focus: true) (#271) is the roving-focus variant for a role="menu" container: Arrow Down/Up move real focus among the [role=menuitem] items (wrapping), Home/End jump to the first/last. Enter/Space stay the focused item's native activation, and Escape is yours to bind with on_client("keydown.esc", …) on the same element:
ul(role: "menu", **mix(reactive_listnav(focus: true),
on_client("keydown.esc", js.hide("#menu").focus("#trigger"))))Conditional confirm (#179). confirm: also takes a Hash to warn only when the field values look suspect — soft-validation before submit, evaluated client-side over the same collected fields, no bespoke handler:
# Declarative — reuses reactive_show's conditions language (scalar = equals,
# Range = threshold, Array = membership). Prompts only when it MATCHES.
on(:save, confirm: { when: { total: 0 }, message: "Total is 0 — continue?" })
on(:save, confirm: { when: { qty: 100.. }, message: "Large order — sure?" })
# Named predicate — for multi-field logic. Register a pure function at boot
# (the setComputeReducer twin); it runs over the collected fields.
# setConfirmPredicate("end_before_start", ({ starts_at, ends_at }) => ends_at < starts_at)
on(:save, confirm: { predicate: "end_before_start", message: "End precedes start — continue?" })The Hash form works on on_client(...) too. It is soft-validation UX, not authorization — a user can bypass any client predicate and the action still hits the endpoint's real authorize! / default-deny. Never let a predicate stand in for a server-side check.
on_client — the same modifiers, zero round trips#
on_client(:event, ops) binds a DOM event to a chain of declared DOM operations the controller applies locally — no token, no params, no POST, ever. It's the zero-round-trip sibling of on(...), for purely presentational state (tabs, a dropdown, an accordion).
div(**mix(reactive_root, on_client(:click, js.hide("#menu"), outside: true))) do
button(**on_client(:click, js.show("#menu"))) { "Menu" }
div(id: "menu", hidden: true) { menu_items }
endwindow:, once:, and outside: compose exactly as they do on on(...): the menu above closes on any outside click, and the window-bound trigger never preventDefaults (an element-bound one does, unless it says prevent: false). Reach for on_client when the interaction is presentational; reach for a signed action (a token
- POST) only when the server must decide or persist something. See the client-only ops example for the live version.
The vocabulary includes submit (#226): on_client(:change, js.submit("form")) on a select is the one-line autosubmit filter — requestSubmit() fires a real submit event, so a native/Turbo form navigates normally and an on(:save, event: "submit") interception turns it into a signed action instead. Actor-only like focus: refused in broadcasts.
Several bindings on one element (#271). Each on_client call carries its own descriptor, flags and confirm:, so two calls compose through mix — the root below closes the menu on an outside click and on Escape, and the outside guard of the first never gates the second:
div(**mix(reactive_root,
on_client(:click, js.hide("#menu", expanded: "#trigger"), outside: true),
on_client("keydown.esc", js.hide("#menu", expanded: "#trigger").focus("#trigger"))))The one thing that does not compose is two element-bound :click bindings on the same <button> (both force type="button") — chain the ops into one call instead.
And paste_into (#228): on_client(:click, js.paste_into("[name=code]")) reads the clipboard into a field on the user's gesture and feeds it through the normal input pipeline — the explicit paste affordance for a field whose real input is visually hidden (an OTP cell UI). Author the trigger hidden and the controller reveals it only where the clipboard API exists. Actor-only too.