Litewire 1.1.0

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

AttributeWhat it doesExample
lw-getSends a GET request when triggered.<button lw-get="/search">Search</button>
lw-postSends a POST request with the containing form.<form lw-post="/messages">...</form>
lw-putSends a PUT request with the containing form.<form lw-put="/profiles/42">...</form>
lw-patchSends a PATCH request with the containing form.<form lw-patch="/tasks/42">...</form>
lw-deleteSends a DELETE request when triggered.<button lw-delete="/messages/42">Delete</button>

Request behavior

AttributeWhat it doesExample
lw-triggerChooses 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-targetSelects the element that receives the response.<button lw-get="/panel" lw-target="#panel">Load</button>
lw-swapChooses how response HTML is applied.<section lw-get="/feed" lw-target="#feed" lw-swap="prepend">
lw-selectUses only matching response elements for the main swap.<button lw-get="/dashboard" lw-select="#summary" lw-target="#summary">
lw-swap-oobMarks a response element that updates another target.<span lw-swap-oob="innerHTML:#count">4</span>
lw-indicatorSelects elements that receive the loading class while the request runs.<button lw-get="/feed" lw-indicator="#spinner">Refresh</button>
lw-push-urlPushes the request URL (true) or a custom same-origin URL to history.<button lw-get="/products" lw-push-url="true">Products</button>
lw-credentialsSets the fetch credentials mode for the request.<button lw-get="/account" lw-credentials="include">Load</button>
lw-headersAdds request headers from a JSON object.<button lw-get="/feed" lw-headers='{"Accept":"text/html"}'>Load</button>
lw-timeoutAborts the request after the given milliseconds.<button lw-get="/slow" lw-timeout="5000">Load</button>
lw-retriesRetries retryable failures up to the given count.<button lw-get="/flaky" lw-retries="2">Load</button>
lw-retry-delaySets the delay between retries in milliseconds.<button lw-get="/flaky" lw-retries="2" lw-retry-delay="500">Load</button>
lw-concurrencyChooses 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-jsonSends a non-GET body as JSON instead of FormData; an expression value supplies the payload.<form lw-post="/tasks" lw-json>...</form>

Components

AttributeWhat it doesExample
lw-componentImports and mounts an ES module on the target element.<div lw-component="/widgets/chart.js" data-mode="compact"></div>

State and rendering

AttributeWhat it doesExample
lw:stateCreates local reactive state for the element and its descendants.<section lw:state="{ count: 0 }">
lw:textWrites an expression result as text.<span lw:text="name"></span>
lw:htmlWrites an expression result as HTML; use trusted or sanitized markup only.<div lw:html="trustedHtml"></div>
lw:showSets the element's hidden property from an expression.<p lw:show="visible">Details</p>
lw:classAdds or removes classes from an object expression.<span lw:class="{ active: isActive }">
lw:bindBinds attributes or properties from an object expression.<input lw:bind="{ disabled: saving, value: name }">
lw:forRepeats a template's contents for an iterable.<template lw:for="item of items"><li lw:text="item"></li></template>
lw:keyProvides a key expression for rows created by lw:for.<template lw:for="user of users" lw:key="user.id">
lw:ifInserts a template's contents while an expression is truthy.<template lw:if="open"><p>Details</p></template>

Form models

AttributeWhat it doesExample
lw:modelBinds a control to a model and updates on input.<input lw:model="name">
lw:model.liveAlias of lw:model that updates on input.<input lw:model.live="query">
lw:model.changeUpdates the model on change.<select lw:model.change="country">
lw:model.blurUpdates the model on blur.<input lw:model.blur="email">

Events

AttributeWhat it doesExample
lw:clickEvaluates an expression on click.<button lw:click="count++">Add</button>
lw:inputEvaluates an expression on input.<input lw:input="query = $value">
lw:changeEvaluates an expression on change.<select lw:change="country = $value">
lw:submitEvaluates an expression on submit.<form lw:submit.prevent="saved = true">
lw:keydownEvaluates an expression on keydown.<input lw:keydown="$event.key === 'Enter' && (saved = true)">
lw:keyupEvaluates an expression on keyup.<input lw:keyup="preview = $value">
lw:focusEvaluates an expression on focus.<input lw:focus="focused = true">
lw:blurEvaluates an expression on blur.<input lw:blur="focused = false">