Subtlyv0.1.1
Framework guides

Alpine.js

Declarative events and component cleanup.

Install

Use your existing Alpine instance; with Livewire do not start a second Alpine instance.

npm install subtly

Initialize

resources/js/app.js

import Alpine from 'alpinejs';
import { createInteractions } from 'subtly';
const ix = createInteractions();
Alpine.start(); // only if Alpine is not already started by your host
ix.start();
if (import.meta.hot) import.meta.hot.dispose(() => ix.stop());

Use a behavior

<div data-ix="hover-intent" data-ix-hover-delay="150">
  <button type="button">Preview</button>
</div>

Use the shared HTML attributes, or import programmatic helpers inside your framework lifecycle. State and visual rendering stay in your component.

Lifecycle and accessibility

For individual programmatic utilities, initialize in init() and clean up in destroy(). Use $nextTick for newly rendered panels.

Use a single runtime per root; do not overlap roots. Keep native button semantics, focus and click support. For cleanup, call each utility’s returned callback or destroy method.

Try the same behavior

Live example

Preview closed. Hover, focus or click to open.

<div id="your-component">
<div class="preview-project">
          <button class="project-trigger" aria-expanded="false" aria-controls="project-preview">Website redesign <span class="muted">Project preview</span></button>
          <article id="project-preview" class="project-preview" hidden><span class="sample-art" aria-hidden="true"></span><strong>Website redesign</strong><p>A clearer home for your next release.</p><span class="tag">In progress</span></article>
        </div><p class="demo-status" role="status">Preview closed. Hover, focus or click to open.</p>
</div>
import * as kit from 'subtly';

const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
  el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

function setup() {
  const trigger = el.querySelector('button');
          const preview = el.querySelector('.project-preview');
          function show(open) {
            preview.hidden = !open;
            trigger.setAttribute('aria-expanded', String(open));
            status(open ? 'Project preview open' : 'Project preview closed');
          }
          const stop = kit.hoverIntent(trigger, {
            delay: 150,
            onEnter: () => show(true),
            onLeave: () => { if (document.activeElement !== trigger) show(false); }
          });
          trigger.addEventListener('focus', () => show(true), { signal });
          trigger.addEventListener('blur', () => show(false), { signal });
          trigger.addEventListener('click', () => show(true), { signal });
          trigger.addEventListener('keydown', event => {
            if (event.key === 'Escape') show(false);
          }, { signal });
          return stop;
}
const stop = setup();

// Call when this component is removed.
function cleanup() {
  listeners.abort();
  stop?.();
}

The live example uses native JavaScript; the framework snippets are integration recipes, not separately compiled framework applications.

Listen to an intent event

<div x-data="{ active: false }" data-ix="hover-intent"
     @ix:hover-enter="active = true" @ix:hover-leave="active = false">
  <button @click="active = !active">Preview</button>
  <span x-text="active ? 'Active' : 'Idle'"></span>
</div>

Search Subtly documentation

Popular pages
↑↓ to navigate↵ to openESC to close