Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
focus-lock
Advanced tools
The focus-lock npm package is designed to trap focus within a specified DOM element, typically used in modal dialogs, popups, and similar components to enhance accessibility and user experience. It prevents users from tabbing out of the focused area into other parts of the webpage, ensuring that keyboard and screen reader users have a confined, manageable interaction space.
Focus trapping within a DOM element
This feature allows developers to create a focus trap around a specified DOM element. When activated, all tab and shift+tab keystrokes are confined within the element, enhancing accessibility for modal dialogs.
import { FocusTrap } from 'focus-lock';
const focusTrap = new FocusTrap('#myDialog');
focusTrap.activate();
// When done
focusTrap.deactivate();
Auto focusing on the first focusable element
Automatically focuses the first focusable element within the component when activated. This is useful for ensuring that users start interacting with the most relevant part of a newly opened UI component.
import { AutoFocusInside } from 'focus-lock';
<AutoFocusInside>
<input type='text' />
<button>Click me</button>
</AutoFocusInside>
Return focus to initial element on deactivation
When the focus lock is deactivated, focus can be programmatically moved back to the initially focused element before the focus lock was activated, maintaining a logical flow of interaction.
import { MoveFocusInside } from 'focus-lock';
const initialFocus = document.getElementById('initialFocus');
// After deactivating the focus lock
MoveFocusInside(initialFocus);
A React-specific implementation of focus trapping. It provides similar functionalities as focus-lock but is tailored for React applications, making it easier to integrate with React component lifecycle.
This package provides a lightweight yet robust solution for creating accessible dialog windows. While it includes focus trapping as part of its functionality, it also manages aria attributes and supports multiple dialog instances, offering a broader scope compared to focus-lock.
It is a trap! We got your focus and will not let him out!
This is quite low level API, to be used by final realization. Usually everything can be solved in 3 lines
import moveFocusInside, { focusInside } from 'focus-lock';
if (observed && !focusInside(observed)) {
result = moveFocusInside(observed, lastActiveFocus);
}
#WHY? From MDN Article about accessible dialogs:
This one is about managing the focus.
I'v got a good article about focus management, dialogs and WAI-ARIA.
#Implementations
This is vanilla js base package for:
This is a small, but very useful for:
#API
default(topNode, lastNode)
(aka setFocus), moves focus inside topNode, keeping in mind that last focus inside was - lastNode
MIT
FAQs
DOM trap for a focus
The npm package focus-lock receives a total of 1,722,993 weekly downloads. As such, focus-lock popularity was classified as popular.
We found that focus-lock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.