focus-lock vs focus-trap
Focus Management Libraries Comparison
1 Year
focus-lockfocus-trapSimilar Packages:
What's Focus Management Libraries?

Focus management libraries are essential tools in web development that help manage keyboard focus within web applications, particularly for accessibility purposes. They ensure that users can navigate through interactive elements using keyboard controls, which is crucial for users with disabilities. These libraries provide mechanisms to lock focus within specific elements or areas of the page, preventing focus from moving to unintended elements, thus enhancing the user experience and accessibility compliance.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
focus-lock2,319,562174188 kB12 months agoMIT
focus-trap2,037,0201,397670 kB12 months agoMIT
Feature Comparison: focus-lock vs focus-trap

Focus Locking Mechanism

  • focus-lock:

    Focus Lock provides a straightforward mechanism to lock focus within a specific container or component. It allows users to navigate through interactive elements without the risk of moving focus outside the intended area, making it ideal for modals and pop-ups.

  • focus-trap:

    Focus Trap offers a more advanced focus locking mechanism that not only locks focus within a specified area but also ensures that focus cannot escape until the user completes their interaction. It provides a seamless experience for keyboard users, particularly in complex UI scenarios.

Accessibility Compliance

  • focus-lock:

    Focus Lock enhances accessibility by ensuring that users who rely on keyboard navigation can interact with all elements within the locked area without losing focus. This is crucial for compliance with accessibility standards such as WCAG.

  • focus-trap:

    Focus Trap is designed with accessibility in mind, ensuring that focus management adheres to best practices. It prevents focus from escaping, which is essential for users with disabilities, thereby improving overall accessibility compliance.

Customization Options

  • focus-lock:

    Focus Lock offers basic customization options, allowing developers to specify which elements should be focusable and how focus should be managed within the locked area. This makes it flexible for various use cases.

  • focus-trap:

    Focus Trap provides extensive customization options, including callbacks for focus events, which allow developers to define specific behaviors when focus enters or exits the trap. This level of customization is beneficial for complex applications.

Performance

  • focus-lock:

    Focus Lock is lightweight and optimized for performance, ensuring minimal overhead when managing focus. It is suitable for applications where performance is a critical concern.

  • focus-trap:

    Focus Trap is also designed for performance but may introduce slightly more overhead due to its comprehensive focus management features. However, it is still efficient for most applications.

Ease of Use

  • focus-lock:

    Focus Lock is easy to implement and requires minimal configuration, making it a great choice for developers looking for a quick solution to manage focus without extensive setup.

  • focus-trap:

    Focus Trap may require a bit more setup and understanding of its API, but it provides a powerful tool for developers who need advanced focus management capabilities.

How to Choose: focus-lock vs focus-trap
  • focus-lock:

    Choose Focus Lock if you need a simple and effective solution to manage focus within specific components, such as modals or dialogs, while allowing focus to move freely within the locked area. It is particularly useful for applications that require a straightforward implementation without complex configurations.

  • focus-trap:

    Choose Focus Trap if you require a more robust solution that not only locks focus but also manages focus behavior in a more comprehensive manner, including handling focus restoration and ensuring that focus does not escape from the designated area. It is ideal for complex applications where focus management is critical for accessibility.

README for focus-lock

focus-lock

It is a trap! We got your focus and will not let him out!

NPM

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

Focus-lock implementations

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.

Declarative control

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:

  • control
    • 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.
  • autofocus (via 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'

Additional API

Get focusable nodes

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);

Programmatic focus management

Allows moving back and forth between focusable/tabbable elements

import { focusNextElement, focusPrevElement } from 'focus-lock';
focusNextElement(document.activeElement, {
  scope: theBoundingDOMNode,
}); // -> next tabbable element

Return focus

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

WHY?

From MDN Article about accessible dialogs:

  • The dialog must be properly labeled
  • Keyboard focus must be managed correctly

This one is about managing the focus.

I'v got a good article about focus management, dialogs and WAI-ARIA.

Focus fighting

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.

API

default(topNode, lastNode) (aka setFocus), moves focus inside topNode, keeping in mind that last focus inside was - lastNode

Licence

MIT