react-focus-lock
It is a trap! (for a focus)
Last updated 6 years ago by kashey .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install react-focus-lock 
SYNC missed versions from official npm registry.

REACT FOCUS LOCK

it-is-a-trap

The way to manage your focus.
The way to lock it inside.
The way to team up with a11y.

Make you site a better place. For everyone.

CircleCI status npm


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

NPM

This is a small library, but very useful for:

  • Modal dialogs. You can not leave it with "Tab", ie do a "tab-out".
  • Focused tasks. It will aways brings you back, as you can "lock" user inside a component.

You have to lock every modal dialog, that's what a11y is asking for.

And this is most comprehensive focus lock/trap ever built.

Features

  • no keyboard control, everything is done watching a focus behavior, not emulating tabs. Thus works always and everywhere.
  • React Portals support. Even if some data is in outerspace - it is still in lock.
  • Scattered locks, or focus lock groups - you can setup different isolated locks, and tab from one to another.
  • Controllable isolation level.

How to use

Just wrap something with focus lock, and focus will be moved inside on mount.

 import FocusLock from 'react-focus-lock';

 const JailForAFocus = ({onClose}) => (
    <FocusLock>
      You can not leave this form
      <button onClick={onClick} />
    </FocusLock>
 );

Demo - https://codesandbox.io/s/5wmrwlvxv4.

Final piece for a modals

That is actually not enough, - you shall not lock the focus, but also disable page scroll and user iteractions with the rest of a page - "shadow" rest of the page, to make it unclickable or unscrollable. And react-locky is your next component to check.

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.

API

FocusLock has few props to tune behavior

  • disabled, to disable(enable) behavior without altering the tree.
  • returnFocus, to return focus into initial position on unmount(not disable). This is expected behavior for Modals, but it is better to implement it by your self.
  • persistentFocus, default false, requires any element to be focused. This also disables text selections inside, and outside focus lock.
  • autoFocus, default true, enables or disables focusing into on Lock activation. If disabled Lock will blur an active focus.
  • noFocusGuards disabled focus guards - virtual inputs which secure tab index.
  • group named focus group for focus scattering aka combined lock targets
  • whiteList you could whitelist locations FocusLock should carry about. Everything outside it will ignore. For example - any modals.
  • as if you need to change internal div element, to any other
  • lockProps to pass any extra props (except className) to the internal wrapper.

Behavior

  1. It will always keep focus inside Lock.
  2. It will cycle forward then you press Tab.
  3. It will cycle in reverse direction on Shift+Tab.
  4. It will do it using browser tools, not emulation.
  5. It will handle positive tabIndex inside form.
  6. It will prevent any jump outside, returning focus to the last element.

Focusing in OSX (Safary/FireFox) is strange!

By default tabbing in OSX sees only control, but not links or anything else tabbable. This is system settings, and Safary/FireFox obey. Press Option+Tab in Safary to loop across all tabbables, or change the Safary settings. There is no way to fix FireFox, unless change system settings (Control+F7). See this issue for more information.

You can use nested Locks or have more than one Lock on the page. Only last, or deepest one will work. No fighting.

Autofocus

As long you cannot use autoFocus prop - cos "focusing" should be delayed to Trap activation, and autoFocus will effect immediately - Focus Lock provide a special API for it

  • prop data-autofocus on the element.
  • prop data-autofocus-inside on the element to focus on something inside.
  • AutoFocusInside component, as named export of this library.
 import FocusLock, { AutoFocusInside } from 'react-focus-lock';
 
 <FocusLock>
   <button>Click</button>
   <AutoFocusInside>
    <button>will be focused</button>
   </AutoFocusInside>
 </FocusLock>
 // is the same as
 
 <FocusLock>
   <button>Click</button>
    <button data-autofocus>will be focused</button>
 </FocusLock>
 
 <FocusLock as="section">
    <button>Click</button>
    <button data-autofocus>will be focused</button>
 </FocusLock>
 
 <FocusLock as={AnotherComponent} lockProps={{anyProp: 4}}>
    <button>Click</button>
    <button data-autofocus>will be focused</button>
 </FocusLock>

If there is more than one auto-focusable target - the first will be selected. If it is a part of radio group, and rest of radio group element are also autofocusable(just put them into AutoFocusInside) - checked one fill be selected.

AutoFocusInside will work only on Lock activation, and does nothing, then used outside of the lock. You can use MoveFocusInside to move focus inside with or without lock.

 import { MoveFocusInside } from 'react-focus-lock';
    
 <MoveFocusInside>
  <button>will be focused</button>
 </MoveFocusInside>

Unmounting and focus management

  • In case FocusLock has returnFocus enabled, and it's gonna to be unmounted - focus will be returned after zero-timeout.
  • In case returnFocus did not set, and you are going to control focus change by your own - keep in mind

React will first call Parent.componentWillUnmount, and next Child.componentWillUnmount

Thus means - Trap will be still active, be the time you may want move(return) focus on componentWillUnmount. Please deffer this action with a zero-timeout.

How it works

Everything thing is simple - react-focus-lock just dont left focus left boundaries of component, and do something only if escape attempt was succeeded.

It is not altering tabbing behavior at all. We are good citizens.

Not only for React

Uses focus-lock under the hood. It does also provide support for Vue.js and Vanilla DOM solutions

More

Dont forget to lock the scroll to complete the picture. react-scroll-locky - browser scrollbars hiding, you were looking for.

Warning!

Two different focus-lock-managers or even different version of a single one, active simultaneously will FIGHT!

Focus-lock will surrender, as long any other focus management library will not.

Focus fighting

You may wrap some render branch with FreeFocusInside, and react-focus-lock will ignore any focus inside marked node, thus landing a peace.

import { FreeFocusInside } from 'react-focus-lock';

<FreeFocusInside>
 <div id="portal-for-modals">
   in this div i am going to portal my modals, dont fight with them please
 </div>
</FreeFocusInside>

Even the better is to whiteList FocusLock areas - for example "you should handle only React Stuff in React Root"

<FocusLock whiteList={node => document.getElementById('root').contains(node)}>
 ...
</FocusLock>

PS: please use webpack or yarn resolution for force one version of react-focus-lock used

webpack.conf

 resolve: {    
    alias: {
      'react-focus-lock': path.resolve(path.join(__dirname, './node_modules/react-focus-lock'))
 ...

Package size

About 6kb with all dependencies, minified and gzipped.

Licence

MIT

Current Tags

  • 1.17.0                                ...           beta (6 years ago)
  • 2.13.2                                ...           latest (4 months ago)

106 Versions

  • 2.13.2                                ...           4 months ago
  • 2.13.1                                ...           4 months ago
  • 2.13.0 [deprecated]           ...           4 months ago
  • 2.12.1                                ...           8 months ago
  • 2.12.0 [deprecated]           ...           8 months ago
  • 2.11.3                                ...           8 months ago
  • 2.11.2                                ...           10 months ago
  • 2.11.1                                ...           10 months ago
  • 2.11.0                                ...           10 months ago
  • 2.10.1                                ...           10 months ago
  • 2.10.0                                ...           10 months ago
  • 2.9.8                                ...           a year ago
  • 2.9.7                                ...           a year ago
  • 2.9.6                                ...           a year ago
  • 2.9.5                                ...           a year ago
  • 2.9.4                                ...           2 years ago
  • 2.9.3                                ...           2 years ago
  • 2.9.2                                ...           2 years ago
  • 2.9.1                                ...           3 years ago
  • 2.9.0                                ...           3 years ago
  • 2.8.1                                ...           3 years ago
  • 2.8.0                                ...           3 years ago
  • 2.7.1                                ...           3 years ago
  • 2.7.0                                ...           3 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.2                                ...           3 years ago
  • 2.5.1                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.1                                ...           4 years ago
  • 2.4.0                                ...           5 years ago
  • 2.3.1                                ...           5 years ago
  • 2.3.0                                ...           5 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.1                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.5                                ...           5 years ago
  • 2.0.4                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           6 years ago
  • 1.19.1                                ...           6 years ago
  • 1.19.0                                ...           6 years ago
  • 1.18.3                                ...           6 years ago
  • 1.18.2                                ...           6 years ago
  • 1.18.1                                ...           6 years ago
  • 1.18.0                                ...           6 years ago
  • 1.17.7                                ...           6 years ago
  • 1.17.6                                ...           6 years ago
  • 1.17.5                                ...           6 years ago
  • 1.17.4                                ...           6 years ago
  • 1.17.3                                ...           6 years ago
  • 1.17.2                                ...           6 years ago
  • 1.17.1                                ...           6 years ago
  • 1.17.0                                ...           6 years ago
  • 1.16.2                                ...           6 years ago
  • 1.16.1                                ...           6 years ago
  • 1.16.0                                ...           6 years ago
  • 1.15.0                                ...           6 years ago
  • 1.14.1                                ...           6 years ago
  • 1.14.0                                ...           6 years ago
  • 1.13.2                                ...           6 years ago
  • 1.13.1                                ...           6 years ago
  • 1.13.0                                ...           6 years ago
  • 1.12.1                                ...           6 years ago
  • 1.12.0                                ...           6 years ago
  • 1.11.3                                ...           6 years ago
  • 1.11.2                                ...           6 years ago
  • 1.11.1                                ...           7 years ago
  • 1.11.0                                ...           7 years ago
  • 1.10.0                                ...           7 years ago
  • 1.9.1                                ...           7 years ago
  • 1.9.0                                ...           7 years ago
  • 1.8.1                                ...           7 years ago
  • 1.8.0                                ...           7 years ago
  • 1.7.0                                ...           7 years ago
  • 1.6.6                                ...           7 years ago
  • 1.6.5                                ...           7 years ago
  • 1.6.4                                ...           7 years ago
  • 1.6.3                                ...           7 years ago
  • 1.6.2                                ...           7 years ago
  • 1.6.1                                ...           7 years ago
  • 1.6.0                                ...           7 years ago
  • 1.5.5                                ...           7 years ago
  • 1.5.4                                ...           7 years ago
  • 1.5.3                                ...           7 years ago
  • 1.5.2                                ...           7 years ago
  • 1.5.1                                ...           7 years ago
  • 1.5.0                                ...           7 years ago
  • 1.4.9                                ...           7 years ago
  • 1.4.8                                ...           7 years ago
  • 1.4.7                                ...           7 years ago
  • 1.4.6                                ...           7 years ago
  • 1.3.6                                ...           7 years ago
  • 1.3.5                                ...           7 years ago
  • 1.3.4                                ...           7 years ago
  • 1.3.3                                ...           7 years ago
  • 1.3.2                                ...           7 years ago
  • 1.1.2                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.3                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Total 70
Today 0
This Week 0
This Month 68
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)

© 2010 - cnpmjs.org x YWFE | Home | YWFE