Overview
LiveViewJS user events (clicks, etc) typically trigger a server-side event which updates the state of the LiveView and re-renders the HTML. Sometimes you want to update the DOM without (or in addition to) initiating a server round trip. This is where JS Commands come in.
JS Commands support a number of client-side DOM manipulation function that can be used to update the DOM without a server round trip. These functions are:
add_class
- Add css classes to an element including optional transition classesremove_class
- Remove css classes from an element including optional transition classesset_attribute
- Set an attribute on an elementremove_attribute
- Remove an attribute from an elementshow
- Show an element including optional transition classeshide
- Hide an element including optional transition classestoggle
- Toggle the visibility of an elementdispatch
- Dispatch a DOM event from an elementtransition
- Apply transition classes to an element (i.e., animate it)push
- Push an event to the LiveView server (i.e., trigger a server round trip)
JS Command Syntax
JS Commands are used in the render
function of a LiveView or LiveComponent:
import { JS } from 'lieviewjs';
//... render function of a LiveView
render() {
return html`
<div>
<button phx-click="${new JS().toggle({ to: "#toggle" })}">Toggle</button>
<div id="toggle">Toggle Me</div>
</div>
`;
}
"Chainable" (i.e., fluent) Syntax
JS Commands are "chainable" (i.e., fluent) so you can chain multiple commands together as needed and they will be executed in the order they are called:
import { JS } from 'lieviewjs';
//... render function of a LiveView
render() {
// hide the button then push the "increment" event to the server
return html`
<div>
<button phx-click="${new JS().hide().push("increment")}">Increment</button>
</div>
`;
}
Example JS Commands LiveView
See packages/examples/src/liveviews/jsCommands
for a working, complete set of examples of using JS Commands.
More details on each JS Command can be found in the following sections.