Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
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!
Important - this is a low level package to be used in order to create "focus lock". It does not provide any "lock" capabilities by itself, only helpers you can use to create one
This is a base package for:
The common use case will look like final realization.
import { moveFocusInside, focusInside } from 'focus-lock';
if (someNode && !focusInside(someNode)) {
moveFocusInside(someNode, lastActiveFocus /* very important to know */);
}
note that tracking
lastActiveFocus
is on the end user.
focus-lock
provides not only API to be called by some other scripts, but also a way one can leave instructions inside HTML markup
to amend focus behavior in a desired way.
These are data-attributes
one can add on the elements:
data-focus-lock=[group-name]
to create a focus group (scattered focus)data-focus-lock-disabled="disabled"
marks such group as disabled and removes from the list. Equal to removing elements from the DOM.data-no-focus-lock
focus-lock will ignore/allow focus inside marked area. Focus on this elements will not be managed by focus-lock.moveFocusInside(someNode, null)
)
data-autofocus
will autofocus marked element on activation.data-autofocus-inside
focus-lock will try to autofocus elements within selected area on activation.data-no-autofocus
focus-lock will not autofocus any node within marked area on activation.These markers are available as import * as markers from 'focus-lock/constants'
Returns visible and focusable nodes
import { expandFocusableNodes, getFocusableNodes, getTabbleNodes } from 'focus-lock';
// returns all focusable nodes inside given locations
getFocusableNodes([many, nodes])[0].node.focus();
// returns all nodes reacheable in the "taborder" inside given locations
getTabbleNodes([many, nodes])[0].node.focus();
// returns an "extended information" about focusable nodes inside. To be used for advances cases (react-focus-lock)
expandFocusableNodes(singleNodes);
Allows moving back and forth between focusable/tabbable elements
import { focusNextElement, focusPrevElement } from 'focus-lock';
focusNextElement(document.activeElement, {
scope: theBoundingDOMNode,
}); // -> next tabbable element
Advanced API to return focus (from the Modal) to the last or the next best location
import { captureFocusRestore } from 'focus-lock';
const restore = captureFocusRestore(element);
// ....
restore()?.focus(); // restores focus the the element, or it's siblings in case it no longer exists
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.
It is possible, that more that one "focus management system" is present on the site. For example, you are using FocusLock for your content, and also using some Modal dialog, with FocusTrap inside.
Both system will try to do their best, and move focus into their managed areas. Stack overflow. Both are dead.
Focus Lock(React-Focus-Lock, Vue-Focus-Lock and so on) implements anti-fighting protection - once the battle is detected focus-lock will surrender(as long there is no way to win this fight).
You may also land a peace by special data attribute - data-no-focus-lock
(constants.FOCUS_ALLOW). It will
remove focus management from all nested elements, letting you open modals, forms, or
use any third party component safely. Focus lock will just do nothing, while focus is on the marked elements.
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,129,302 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 0 open source maintainers 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.