Documentation Index
Fetch the complete documentation index at: https://mintlify.com/soyleninjs/swappit/llms.txt
Use this file to discover all available pages before exploring further.
<swappit-instance> is an HTML custom element that creates (or reinitializes) a Swappit instance using only HTML attributes — no JavaScript required. This makes it ideal for server-rendered templates, CMS-driven pages, and any situation where adding a <script> block feels unnecessarily heavy. You configure Swappit the same way you configure everything else in your HTML: with attributes.
Basic usage
The only required attribute isdata-handle. This single line creates a fully functional Swappit instance:
<body> — after the Swappit script tag.
The
swappit.min.js <script> tag must be present in the page — typically at the end of <body>. If the browser parses <swappit-instance> before the script has loaded, it queues the element for upgrade; connectedCallback fires once the script runs and registers the custom element. A safe pattern is to place <swappit-instance> anywhere in <body> and put <script src="swappit.min.js"> at the very end of the body.Available attributes
| Attribute | Type | Description | Default |
|---|---|---|---|
data-handle | string | Instance handle — must be unique (required) | — |
data-log | presence boolean | Enables color-coded console logging | false |
data-update-url | presence boolean | Updates the browser URL bar on each navigation | false |
data-enable-history | presence boolean | Enables back/forward browser navigation | false |
data-preload | string | Default preload mode for managed links: "hover" or "instant" | false |
false:
Behavior
When the custom element connects to the DOM, it checks theSwappit.instances registry:
No existing instance
If no instance with the given
data-handle exists, <swappit-instance> creates a new Swappit instance using the attribute values as options.Instance already exists
If an instance with that handle is already registered,
<swappit-instance> calls reinit() on it with the new attribute values — merging the new options into the existing instance without destroying it. This lets you re-configure a running instance declaratively, for example after a server-side template re-renders the element with different attributes.Full example
A complete HTML page combining update regions, declarative configuration, navigation links, and a JavaScript block that accesses the instance and listens to events:- Your update region elements
- Your
<swappit-instance>element and navigation links <script src="swappit.min.js">— this definesSwappit, registers<swappit-instance>, and triggers the upgrade of any already-parsed<swappit-instance>elements- Any additional inline
<script>blocks that useSwappit.instances.get()