T1 · Toggle a modal
-
Author the markup
Two elements: a trigger (the button) and a target (the modal). The modal carries the
idthe wire will resolve against; theis-openclass is what we flip.<button class="btn" id="open-modal">Open modal</button> <div class="modal-backdrop" id="site-modal" role="dialog" aria-modal="true"> <div class="modal-card"> <h3>It works.</h3> <button class="btn btn--ghost" id="close-modal">Close</button> </div> </div> -
Save the wire
In the Behaviours panel, select the button and add this wire. The wire lives on the button — its
trigger.hostis the button itself, andtargetpoints at the modal by id.{ "trigger": { "event": "click" }, "action": { "kind": "class:toggle", "params": { "name": "is-open" }, "target": { "kind": "by-id", "id": "site-modal" } } }Why
by-idand notself? The action runs against the modal, not the button — and you want the wire to keep working even if the button moves somewhere else in the tree. -
Try it
Click the button below. The modal toggles. Click outside the card or press
Escto close (those are two more wires — see Variations).trigger button target #tut-modal
Variations
-
Close on Escape. Add a second wire whose
host is the document — same target, different trigger.
{ "trigger": { "event": "keydown", "key": "Escape" }, "action": { "kind": "class:remove", "params": { "name": "is-open" }, "target": { "kind": "by-id", "id": "site-modal" } } } -
Close on backdrop click. Put a third wire
on the backdrop itself with
target: { kind: 'self' }andaction: 'class:remove'. -
Track open count. Add a sibling action on
the same trigger:
state:incwith keymodal.opens. Two actions, one trigger.