Toggler
A button that switches something on and off: a star, a setting, a compact table, a filter panel. Add data-momo-toggle; momo.js flips it, keeps screen readers told, and can remember it.
A button that switches itself
On its own, the button switches itself. Put both looks inside: .toggler-off shows while it is off and .toggler-on while it is on. A button without them simply looks pressed (the Bold button).
<div class="cluster">
<button class="btn btn-icon btn-ghost" type="button" data-momo-toggle aria-label="Star" id="tg-star"><i class="mi mi-star toggler-off"></i><i class="mi mi-star-filled toggler-on"></i></button>
<button class="btn btn-icon btn-ghost" type="button" data-momo-toggle aria-label="Like" id="tg-like"><i class="mi mi-heart toggler-off"></i><i class="mi mi-heart-filled toggler-on"></i></button>
<button class="btn btn-icon btn-ghost" type="button" data-momo-toggle aria-label="Save for later" aria-pressed="true"><i class="mi mi-bookmark toggler-off"></i><i class="mi mi-bookmark-filled toggler-on"></i></button>
<button class="btn btn-icon" type="button" data-momo-toggle aria-label="Bold" id="tg-bold"><i class="mi mi-bold"></i></button>
<button class="btn" type="button" data-momo-toggle id="tg-follow">
<span class="toggler-off"><i class="mi mi-plus"></i>Follow</span>
<span class="toggler-on"><i class="mi mi-check"></i>Following</span>
</button>
</div><button class="btn btn-icon btn-ghost" data-momo-toggle aria-label="Star">
<i class="mi mi-star toggler-off"></i>
<i class="mi mi-star-filled toggler-on"></i>
</button>
<button class="btn" data-momo-toggle>
<span class="toggler-off"><i class="mi mi-plus"></i>Follow</span>
<span class="toggler-on"><i class="mi mi-check"></i>Following</span>
</button>Screen readers hear "pressed" or "not pressed" (aria-pressed). Write aria-pressed="true" to start switched on, like the bookmark above.
Switch a class on something
data-momo-toggle="#target" with data-toggle-class adds and removes that class: a compact table, a collapsed sidebar, a wide layout. data-persist="name" remembers it on this device: turn on "Dense rows", reload the page, and the rows stay dense.
| Order | Customer | Status | Total |
|---|---|---|---|
| #10421 | Ana Kim | Paid | $129.00 |
| #10420 | Marco Rossi | Pending | $48.50 |
| #10419 | Lena Fischer | Refunded | $310.20 |
| #10418 | Sam Okafor | Paid | $76.00 |
<div class="cluster mb-3">
<button class="btn btn-sm" type="button" data-momo-toggle="#tg-orders" data-toggle-class="table-sm" data-persist="docs-dense"><i class="mi mi-rows-3"></i> Dense rows</button>
<button class="btn btn-sm" type="button" data-momo-toggle="#tg-orders" data-toggle-class="table-striped"><i class="mi mi-list-collapse"></i> Stripes</button>
</div>
<div class="table-wrap">
<table class="table" id="tg-orders">
<thead><tr><th>Order</th><th>Customer</th><th>Status</th><th class="text-end">Total</th></tr></thead>
<tbody>
<tr><td>#10421</td><td>Ana Kim</td><td><span class="badge badge-green-soft">Paid</span></td><td class="text-end">$129.00</td></tr>
<tr><td>#10420</td><td>Marco Rossi</td><td><span class="badge badge-yellow-soft">Pending</span></td><td class="text-end">$48.50</td></tr>
<tr><td>#10419</td><td>Lena Fischer</td><td><span class="badge badge-red-soft">Refunded</span></td><td class="text-end">$310.20</td></tr>
<tr><td>#10418</td><td>Sam Okafor</td><td><span class="badge badge-green-soft">Paid</span></td><td class="text-end">$76.00</td></tr>
</tbody>
</table>
</div><button class="btn btn-sm" data-momo-toggle="#orders" data-toggle-class="table-sm" data-persist="dense">Dense rows</button>
<table class="table" id="orders">…</table>Every button aimed at the same thing stays in step, so the same switch can sit in a toolbar and in a menu.
Show and hide
Aimed at something with no class given, it shows and hides it, and the button says so to screen readers (aria-expanded). Start with hidden on the target. For a smooth height animation, use Collapse instead.
Changing this breaks old links.
<div class="w-96 max-w-full">
<div class="form-field">
<label class="form-label" for="tg-name">Project name</label>
<input class="form-control" id="tg-name" value="Spring catalog">
</div>
<button class="btn btn-sm btn-ghost" type="button" data-momo-toggle="#tg-advanced" id="tg-adv-btn"><span class="toggler-swap"><i class="mi mi-plus toggler-off"></i><i class="mi mi-minus toggler-on"></i></span> Advanced options</button>
<div class="mt-3" id="tg-advanced" hidden>
<div class="form-field">
<label class="form-label" for="tg-slug">Web address</label>
<input class="form-control" id="tg-slug" value="spring-catalog">
<p class="form-hint">Changing this breaks old links.</p>
</div>
<div class="check-group">
<label class="check"><input type="checkbox" checked> Show in search results</label>
<label class="check"><input type="checkbox"> Ask for a password</label>
</div>
</div>
</div><button class="btn btn-sm btn-ghost" data-momo-toggle="#advanced">Advanced options</button>
<div id="advanced" hidden>…</div>Switch an attribute
data-toggle-attr adds and removes an attribute on the target: open on a <details>, inert, or one of your own with a value (data-view=grid) that your CSS reads. Attributes that could run code or load something (onclick, href, src, style) are refused.
- Momo Mug$24.00
- Canvas Tote$29.00
- Dot Notebook$14.00
- Field Watch$120.00
<div class="cluster mb-3">
<button class="btn btn-sm" type="button" data-momo-toggle="#tg-view" data-toggle-attr="data-view=grid" id="tg-view-btn"><span class="toggler-swap"><i class="mi mi-layout-grid toggler-off"></i><i class="mi mi-layout-list toggler-on"></i></span><span class="toggler-off">Show as grid</span><span class="toggler-on">Show as list</span></button>
</div>
<ul class="list-group w-96 max-w-full" id="tg-view" style="gap: .5rem">
<li class="list-group-item"><b>Momo Mug</b><small class="list-group-end">$24.00</small></li>
<li class="list-group-item"><b>Canvas Tote</b><small class="list-group-end">$29.00</small></li>
<li class="list-group-item"><b>Dot Notebook</b><small class="list-group-end">$14.00</small></li>
<li class="list-group-item"><b>Field Watch</b><small class="list-group-end">$120.00</small></li>
</ul>
<style>#tg-view[data-view="grid"] { display: grid; grid-template-columns: 1fr 1fr; }</style><button data-momo-toggle="#products" data-toggle-attr="data-view=grid">Show as grid</button>
<ul id="products">…</ul>
#products[data-view="grid"] { display: grid; grid-template-columns: 1fr 1fr; }Icons that turn into each other
Wrap two Moey icons in .toggler-swap and one turns into the other: a menu into a close mark, a plus into a minus, play into pause.
<div class="cluster">
<button class="btn btn-icon" type="button" data-momo-toggle aria-label="Menu" id="tg-menu"><span class="toggler-swap"><i class="mi mi-menu toggler-off"></i><i class="mi mi-x toggler-on"></i></span></button>
<button class="btn btn-icon" type="button" data-momo-toggle aria-label="Play"><span class="toggler-swap"><i class="mi mi-play toggler-off"></i><i class="mi mi-pause toggler-on"></i></span></button>
<button class="btn btn-icon" type="button" data-momo-toggle aria-label="Mute"><span class="toggler-swap"><i class="mi mi-volume-2 toggler-off"></i><i class="mi mi-volume-x toggler-on"></i></span></button>
<button class="btn" type="button" data-momo-toggle><span class="toggler-swap"><i class="mi mi-bell toggler-off"></i><i class="mi mi-bell-off toggler-on"></i></span><span class="toggler-off">Mute alerts</span><span class="toggler-on">Alerts muted</span></button>
</div><button class="btn btn-icon" data-momo-toggle aria-label="Menu">
<span class="toggler-swap"><i class="mi mi-menu toggler-off"></i><i class="mi mi-x toggler-on"></i></span>
</button>From code
Momo.toggle(button); // true / false: is it on?
Momo.toggle(button, true); // turn it on
button.addEventListener('momo:toggle', (e) => {
if (e.detail.on && !signedIn) e.preventDefault(); // cancel it: nothing changes
else save(e.detail.on);
});Reference
| Class / attribute | What it does |
|---|---|
data-momo-toggle | The button switches itself on and off (aria-pressed) |
data-momo-toggle="#sel" + data-toggle-class="name" | Adds and removes that class on everything the selector finds |
data-momo-toggle="#sel" | Shows and hides it (aria-expanded) |
data-persist="name" | Remembers the state on this device |
.toggler-on, .toggler-off | Shown only while it is on, only while it is off |
.toggler-swap | Two icons in one place; one turns into the other |
aria-pressed="true" | Start switched on |
Momo.toggle(button, on?), event momo:toggle | Read or set from code; the event can be cancelled, detail: { on, targets } |