Overview
Provides commands for executing JavaScript utility operations on the client. JS commands support a variety of utility operations for common client-side needs, such as adding or removing CSS classes, setting attributes, showing or hiding content, and transitioning animations. JS commands are DOM-patch aware, so operations applied by the JS APIs will stick to elements across patches from the server.Creating JS Commands
All functions accept an optional%JS{} struct as the first argument for chaining:
Client Utility Commands
show/1
Shows elements with optional transitions.Options:
:to- DOM selector to show (default: interacted element):transition- CSS classes or 3-tuple{transition, start, end}:time- Transition duration in ms (default: 200):display- Display value when showing (default:"block"):blocking- Block UI during transition (default:true)
Updated JS command struct.
phx:show-start- When show beginsphx:show-end- After transition completes
hide/1
Hides elements with optional transitions.Options:
:to- DOM selector to hide (default: interacted element):transition- CSS classes or 3-tuple:time- Transition duration in ms (default: 200):blocking- Block UI during transition (default:true)
phx:hide-start- When hide beginsphx:hide-end- After transition completes
toggle/1
Toggles element visibility based on current state.Options:
:to- DOM selector (default: interacted element):in- CSS classes or 3-tuple for showing:out- CSS classes or 3-tuple for hiding:time- Transition duration in ms (default: 200):display- Display value when showing (default:"block"):blocking- Block UI during transition (default:true)
add_class/1
Adds CSS classes to elements.Space-separated class names to add.
Options:
:to- DOM selector (default: interacted element):transition- CSS classes or 3-tuple:time- Transition duration in ms (default: 200):blocking- Block UI during transition (default:true)
remove_class/1
Removes CSS classes from elements.Space-separated class names to remove.
Same options as
add_class/1.toggle_class/1
Toggles CSS classes based on presence.Space-separated class names to toggle.
Same options as
add_class/1.transition/1
Applies temporary transition to elements for animations.CSS classes or 3-tuple for transition.
Options:
:to- DOM selector (default: interacted element):time- Transition duration in ms (default: 200):blocking- Block UI during transition (default:true)
set_attribute/1
Sets an attribute on elements.Tuple containing attribute name and value.
Options:
:to- DOM selector (default: interacted element)
remove_attribute/1
Removes an attribute from elements.The attribute name to remove.
Options:
:to- DOM selector (default: interacted element)
toggle_attribute/1
Toggles an attribute based on presence.2-tuple to set/remove, or 3-tuple to toggle between two values.
Options:
:to- DOM selector (default: interacted element)
ignore_attributes/1
Marks attributes as ignored, skipping them when patching the DOM.Attribute name(s) to ignore. Use
* as wildcard.Options:
:to- DOM selector (default: interacted element)
Must be used with
phx-mounted binding. Does not affect initial render.dispatch/2
Dispatches a DOM event to elements.The event name to dispatch.
Options:
:to- DOM selector (default: interacted element):detail- Detail map for the event:bubbles- Whether event bubbles (default:true):blocking- Block UI untilevent.detail.done()is called
All events are
CustomEvent except "click" which is MouseEvent.Focus Commands
focus/1
Sends focus to a selector.Options:
:to- DOM selector (default: interacted element)
focus_first/1
Sends focus to the first focusable child.Options:
:to- DOM selector (default: interacted element)
push_focus/1
Pushes focus from the source element to be later popped.Options:
:to- DOM selector (default: interacted element)
pop_focus/0
Focuses the last pushed element.Navigation Commands
push/1
Pushes an event to the server.The event name to push.
Options:
:target- Selector or component ID (overridesphx-target):loading- Selector to apply loading classes:page_loading- Trigger page loading events (default:false):value- Map of values to send (merged withphx-value-*)
navigate/1
Sends a navigation event to the server and updates browser history.The path to navigate to.
Options:
:replace- Replace browser history (default:false)
patch/1
Sends a patch event to the server and updates browser history.The path to patch to.
Options:
:replace- Replace browser history (default:false)
Utility Commands
exec/1
Executes JS commands located in an element’s attribute.The attribute containing the JS command.
Options:
:to- DOM selector (default: interacted element)
concat/2
Combines two JS commands, appending the second to the first.First JS command.
Second JS command.
Combined JS command.
DOM Selectors
All commands accept a:to option with these formats:
String Selectors
Scoped Selectors
Scope type:
:inner- Target element within the interacted element:closest- Target closest element upwards from interacted element:document- Document-level selector (equivalent to string)