Astro-DX Logo Astro-DX
GitHub
Docs · @astro-dx/events

@astro-dx/events

Event helpers — on, onHover, onKey, onFocus — all with auto cleanup.

on()

typescript
import { on } from '@astro-dx/events';

// Basic click
on('#btn', 'click', () => count.update(v => v + 1));

// Input event — value extracted automatically
on('#search', 'input', (value) => query.set(value as string));

// Pass an element directly
on(document, 'click', (e) => console.log(e));

onHover()

typescript
import { onHover } from '@astro-dx/events';

onHover('#card', {
  enter: () => hovered.set(true),
  leave: () => hovered.set(false),
});

onKey()

typescript
import { onKey } from '@astro-dx/events';

onKey('#search', 'Enter', () => performSearch());
onKey(document, 'Escape', () => modal.set(false));

onFocus()

typescript
import { onFocus } from '@astro-dx/events';

onFocus('#input', {
  focus: () => isFocused.set(true),
  blur: () => isFocused.set(false),
});
These are also available via @astro-dx/dom if you're already importing from there.