Getting Started
Install Litewire, initialize it in your app, and see why it makes reactive interfaces straightforward.
Installation
Use Litewire as an ES module directly, or install the npm package and import it from your application's entry point.
Load from your application
<script type="module" src="/litewire.js"></script>
Install with npm
npm install litewire
Then import it in your JavaScript entry point: import "litewire";
Instantiation
When using Zap PHP, Litewire is initialized automatically when the DOM is ready. It reads the base URL from base-url meta tag or window.AppConfig.baseUrl. Create an instance yourself when you need custom configuration:
window.litewire = {};
const { Litewire } = await import("/litewire.js");
window.litewire = new Litewire({
baseUrl: "/app",
loadingClass: "is-loading",
expressionMode: "legacy",
});
The placeholder prevents automatic initialization from running before your configured instance is assigned.
Why Litewire?
HTML-driven
Describe requests, bindings, and components directly in markup, adding JavaScript only where you need it.
A focused toolkit
Bring reactive state, DOM updates, HTTP, and component lifecycles together in one small library.
Adopt at your pace
Litewire 1.1.0 preserves 1.0.x behavior by default, while new capabilities and safe expressions are opt-in.
Directive overview
Litewire recognizes 41 attribute spellings: 20 lw-* request and component attributes, and 21 lw:* state and event attributes. Each guide covers the details; this table is a quick map. Event attributes also accept modifiers such as .prevent, .once, .debounce.300ms, and .throttle.300ms; those are not separate directives.
Requests
| Attribute | What it does | Example |
|---|---|---|
lw-get | Sends a GET request when triggered. | <button lw-get="/search">Search</button> |
lw-post | Sends a POST request with the containing form. | <form lw-post="/messages">...</form> |
lw-put | Sends a PUT request with the containing form. | <form lw-put="/profiles/42">...</form> |
lw-patch | Sends a PATCH request with the containing form. | <form lw-patch="/tasks/42">...</form> |
lw-delete | Sends a DELETE request when triggered. | <button lw-delete="/messages/42">Delete</button> |
Request behavior
| Attribute | What it does | Example |
|---|---|---|
lw-trigger | Chooses the event that sends the request; defaults to click, or submit on forms. load sends on scan and every 5s polls. | <input lw-get="/search" lw-trigger="keyup"> |
lw-target | Selects the element that receives the response. | <button lw-get="/panel" lw-target="#panel">Load</button> |
lw-swap | Chooses how response HTML is applied. | <section lw-get="/feed" lw-target="#feed" lw-swap="prepend"> |
lw-select | Uses only matching response elements for the main swap. | <button lw-get="/dashboard" lw-select="#summary" lw-target="#summary"> |
lw-swap-oob | Marks a response element that updates another target. | <span lw-swap-oob="innerHTML:#count">4</span> |
lw-indicator | Selects elements that receive the loading class while the request runs. | <button lw-get="/feed" lw-indicator="#spinner">Refresh</button> |
lw-push-url | Pushes the request URL (true) or a custom same-origin URL to history. | <button lw-get="/products" lw-push-url="true">Products</button> |
lw-credentials | Sets the fetch credentials mode for the request. | <button lw-get="/account" lw-credentials="include">Load</button> |
lw-headers | Adds request headers from a JSON object. | <button lw-get="/feed" lw-headers='{"Accept":"text/html"}'>Load</button> |
lw-timeout | Aborts the request after the given milliseconds. | <button lw-get="/slow" lw-timeout="5000">Load</button> |
lw-retries | Retries retryable failures up to the given count. | <button lw-get="/flaky" lw-retries="2">Load</button> |
lw-retry-delay | Sets the delay between retries in milliseconds. | <button lw-get="/flaky" lw-retries="2" lw-retry-delay="500">Load</button> |
lw-concurrency | Chooses how duplicate requests behave: cancel skips the new one, replace aborts the previous one, and any other value runs them in parallel. | <button lw-get="/search" lw-concurrency="replace">Search</button> |
lw-json | Sends a non-GET body as JSON instead of FormData; an expression value supplies the payload. | <form lw-post="/tasks" lw-json>...</form> |
Components
| Attribute | What it does | Example |
|---|---|---|
lw-component | Imports and mounts an ES module on the target element. | <div lw-component="/widgets/chart.js" data-mode="compact"></div> |
State and rendering
| Attribute | What it does | Example |
|---|---|---|
lw:state | Creates local reactive state for the element and its descendants. | <section lw:state="{ count: 0 }"> |
lw:text | Writes an expression result as text. | <span lw:text="name"></span> |
lw:html | Writes an expression result as HTML; use trusted or sanitized markup only. | <div lw:html="trustedHtml"></div> |
lw:show | Sets the element's hidden property from an expression. | <p lw:show="visible">Details</p> |
lw:class | Adds or removes classes from an object expression. | <span lw:class="{ active: isActive }"> |
lw:bind | Binds attributes or properties from an object expression. | <input lw:bind="{ disabled: saving, value: name }"> |
lw:for | Repeats a template's contents for an iterable. | <template lw:for="item of items"><li lw:text="item"></li></template> |
lw:key | Provides a key expression for rows created by lw:for. | <template lw:for="user of users" lw:key="user.id"> |
lw:if | Inserts a template's contents while an expression is truthy. | <template lw:if="open"><p>Details</p></template> |
Form models
| Attribute | What it does | Example |
|---|---|---|
lw:model | Binds a control to a model and updates on input. | <input lw:model="name"> |
lw:model.live | Alias of lw:model that updates on input. | <input lw:model.live="query"> |
lw:model.change | Updates the model on change. | <select lw:model.change="country"> |
lw:model.blur | Updates the model on blur. | <input lw:model.blur="email"> |
Events
| Attribute | What it does | Example |
|---|---|---|
lw:click | Evaluates an expression on click. | <button lw:click="count++">Add</button> |
lw:input | Evaluates an expression on input. | <input lw:input="query = $value"> |
lw:change | Evaluates an expression on change. | <select lw:change="country = $value"> |
lw:submit | Evaluates an expression on submit. | <form lw:submit.prevent="saved = true"> |
lw:keydown | Evaluates an expression on keydown. | <input lw:keydown="$event.key === 'Enter' && (saved = true)"> |
lw:keyup | Evaluates an expression on keyup. | <input lw:keyup="preview = $value"> |
lw:focus | Evaluates an expression on focus. | <input lw:focus="focused = true"> |
lw:blur | Evaluates an expression on blur. | <input lw:blur="focused = false"> |