Toggler
A button that switches something on and off — a star, a setting, a compact layout, a filter panel. Add data-moey-toggle; moey.js flips it, keeps screen readers told, and can remember it.
<div class="flex flex-wrap gap-2 items-center">
<button class="btn btn-icon btn-ghost btn-amber" data-moey-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 btn-icon btn-ghost btn-red" data-moey-toggle aria-label="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" data-moey-toggle aria-label="Bold">
<i class="mi mi-bold"></i>
</button>
<button class="btn btn-default" data-moey-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>
</div>
On its own, the button switches itself: aria-pressed tells screen readers, and it looks pressed while it's on. Put both looks inside — .toggler-off shows while it's off, .toggler-on while it's on.
Switch a class on something
data-moey-toggle="#target" with data-moey-toggle-class adds and removes that class — compact tables, a collapsed sidebar, a dark section. Every button aimed at the same thing stays in step.
| Order | Customer | Total |
|---|---|---|
| #10421 | Ana Kim | $129.00 |
| #10420 | Marco Rossi | $48.50 |
| #10419 | Lena Fischer | $310.20 |
<div class="flex flex-wrap gap-2 mb-3">
<button class="btn btn-sm" data-moey-toggle="#tg-orders" data-moey-toggle-class="table-sm" data-moey-persist="docs-dense"><i class="mi mi-rows-3"></i>Dense rows</button>
</div>
<div class="card"><div class="card-body card-flush table-wrap">
<table class="table" id="tg-orders">
<thead>
<tr><th>Order</th><th>Customer</th><th class="text-end">Total</th></tr>
</thead>
<tbody>
<tr><td>#10421</td><td>Ana Kim</td><td class="text-end">$129.00</td></tr>
<tr>
<td>#10420</td>
<td>Marco Rossi</td>
<td class="text-end">$48.50</td>
</tr>
<tr>
<td>#10419</td>
<td>Lena Fischer</td>
<td class="text-end">$310.20</td>
</tr>
</tbody>
</table>
</div></div>
data-moey-persist="name" remembers it on this device — reload the page and the rows stay dense.
Show and hide
Aimed at something with no class given, it shows and hides it, with aria-expanded on the button. For a smooth height animation, use Collapse instead.
<button class="btn btn-sm" data-moey-toggle="#tg-filters"><span class="toggler-icon-plus" aria-hidden="true"></span>Filters</button>
<div class="mt-3" id="tg-filters" hidden>
<div class="chip-group" data-moey-chips="multiple" aria-label="Status">
<button class="chip chip-pill" aria-pressed="true">Paid</button>
<button class="chip chip-pill">Pending</button>
<button class="chip chip-pill">Refunded</button>
</div>
</div>
Animated icons
.toggler-icon is a menu icon that turns into an ×; .toggler-icon-plus a + that becomes a −. They're drawn in the text color and sized like an icon.
<div class="flex flex-wrap gap-2 items-center">
<button class="btn btn-icon btn-ghost" data-moey-toggle aria-label="Menu">
<span class="toggler-icon"></span>
</button>
<button class="btn btn-icon btn-ghost" data-moey-toggle aria-label="Add">
<span class="toggler-icon-plus"></span>
</button>
<button class="btn btn-icon btn-default" data-moey-toggle aria-label="Menu">
<span class="toggler-icon"></span>
</button>
<button class="btn btn-lg" data-moey-toggle><span class="toggler-icon"></span>Menu</button>
</div>
From code
Moey.toggle(button); // true / false — is it on?
Moey.toggle(button, true); // turn it on
button.addEventListener('moey:toggle', (e) => e.detail.on); // cancelable
| Class / attribute | What it does |
|---|---|
data-moey-toggle | The button switches itself on and off (aria-pressed) |
data-moey-toggle="#sel" + data-moey-toggle-class="name" | Adds / removes that class on #sel |
data-moey-toggle="#sel" | Shows / hides #sel (aria-expanded) |
data-moey-persist="name" | Remembers the state on this device |
.toggler-on · .toggler-off | Shown only while it's on · off |
.toggler-icon · .toggler-icon-plus | Menu → × · + → − |
aria-pressed="true" | Start switched on |