Get started
Install
Loock is published to the NPM registry
bash
npm install @chialab/loock
bash
yarn add @chialab/loock
bash
pnpm add @chialab/loock
or use a CDN
You can use DNA via CDN thanks to unpkg:
ts
import { focusTrapBehavior } from 'https://unpkg.com/@chialab/loock?module';
Usage
Loock provides a set of utilities and behaviors to manage focus and keyboard navigation:
focusManager
: manage focusable objects inside a contextfocusTrapBehavior
: trap focus inside a contextfocusEnterBehavior
: handle focus entering or exiting a contextfocusFirstChildBehavior
: focus the first element when entering a contextkeyboardNavigationBehavior
: navigate inside a context using keyboard arrows
In the example below, we create a focus trap for a dialog element.
ts
import { focusTrapBehavior } from '@chialab/loock';
const dialog = document.getElementById('.dialog');
const trap = focusTrapBehavior(dialog, {
inert: true,
});
dialog.addEventListener('open', () => {
trap.connect();
});