Package detail

react-focus-on

theKashey750.9kMIT3.10.0

The final solution for WAI ARIA compatible modal dialogs or full-screen tasks.

react, modal, focus-management, scroll

readme

👁 React-Focus-On


lock and loaded!
Build status npm downloads bundle size

The final solution for WAI ARIA compatible Modal Dialogs or any full-screen tasks:

Now you could focus on a single task.

This is basically the inert

Minimal size - no more than 2kb, maximal - no more that 6kb. See sidecar example for details.

Example

Code sandbox example - https://codesandbox.io/s/p3vjp8mzw7

import {FocusOn} from 'react-focus-on';

<FocusOn
 onClickOutside={callback}
 onEscapeKey={callback}
 shards={[externalRef]}
>
 content you should be "focused" on
</FocusOn>

API

FocusOn

FocusOn - the focus on component

  • enabled - controls behaviour
  • [shards] - a list of Refs to be considered as a part of the Lock. A way to properly handle portals or scattered lock.


  • [gapMode] - the way removed ScrollBar would be compensated - margin(default), or padding. See scroll-locky documentation to find the one you need.
  • [noIsolation] - disables aria-hidden isolation
  • [inert] - enables pointer-events isolation (☠️ dangerous, use to disable "parent scrollbars", refer to react-remove-scroll documentation)
  • [allowPinchZoom] - enables "pinch-n-zoom" behavior. By default it might be prevented, refer to react-remove-scroll documentation
  • [preventScrollOnFocus] - prevents a side effect of a programatic page scroll caused by focusing elements. Especially useful to address modal animations.

  • [onActivation] - on activation callback
  • [onDeactivation] - on deactivation callback

  • [onClickOutside] - on click outside of "focus" area. (actually on any event "outside")
  • [onEscapeKey] - on Esc key down (and not defaultPrevented)

Additional API

Exposed from React-Focus-Lock

  • AutoFocusInside - to mark autofocusable element
  • MoveFocusInside - to move focus inside a component on mount
  • InFocusGuard - to "guard" a shard node (place an invisible node before and after)

See react-focus-lock documentation for details.

Exposed from React-Remove-Scroll

  • classNames.fullWidth - "100%" width (will not change on scrollbar removal)
  • classNames.zeroRight - "0" right (will not change on scrollbar removal)

See react-remove-scroll for details.

PS: Version 1 used React-scroll-locky which was replaced by remove-scroll.

Size

- (🧩 full) 5.7kb after compression (excluding tslib).

  • (👁 UI) 2kb, visual elements only
  • (🚗 sidecar) 4kb, side effects

Import full

import {FocusOn} from 'react-focus-on';

<FocusOn>
 {content}
</FocusOn>

Import UI only

import {FocusOn} from 'react-focus-on/UI';
import {sidecar} from "use-sidecar";

const FocusOnSidecar = sidecar(  
  () => import(/* webpackPrefetch: true */ "react-focus-on/sidecar")
);

<FocusOn
    sideCar={FocusOnSidecar}
>
 {content}
</FocusOn>

React versions

  • v1 and v2 might work with React 15/16
  • v3 require React 16.8+ (hooks)

Licence

MIT

changelog

3.10.0 (2025-05-18)

Features

  • Support react 19. Bump all dependencies (b2b6c23)

3.9.4 (2024-09-11)

3.9.3 (2024-04-13)

3.9.2 (2024-03-03)

3.9.1 (2023-07-09)

Bug Fixes

  • memoize focusOptions to prevent react-focus-lock Trap resetting focus (ae360d7)
  • prevent focus options shakes sideEffect (458b61a)

3.9.0 (2023-07-05)

3.8.2 (2023-06-25)

3.8.1 (2023-04-30)

Bug Fixes

3.8.0 (2023-03-16)

Bug Fixes

3.7.0 (2022-11-13)

Bug Fixes

3.6.0 (2022-05-01)

Bug Fixes

  • update underlaying packages; brings React 18 and Shadow DOM support (5bd7b5c)

3.5.4 (2021-11-09)

3.5.3 (2021-11-09)

Bug Fixes

3.5.2 (2021-03-28)

3.5.1 (2020-11-27)

3.5.0 (2020-08-13)

Features

  • add 'as' and 'style' props (d1a79e4)
  • expose whitelist prop (fa5fe87)

3.4.1 (2020-04-17)

Bug Fixes

3.4.0 (2020-04-16)

Features

3.3.0 (2019-10-17)

Features

3.2.0 (2019-10-13)

Features

3.1.5 (2019-10-06)

3.1.4 (2019-10-05)

3.1.3 (2019-10-01)

Bug Fixes

3.1.2 (2019-09-28)

3.1.1 (2019-09-21)

Bug Fixes

  • update hooks when users changes callbacks, fixes #17 (127a303)

3.1.0 (2019-09-13)

Features

  • support returnOption=preventScroll FocusLock option (ff2dad3)

3.0.7 (2019-09-11)

3.0.6 (2019-08-15)

Bug Fixes

  • fixes onClickOutside on iOS devices. fixes #13 (96d0756)

3.0.5 (2019-07-16)

Bug Fixes

  • handle Esc in IE compatible way, fixes #10 (f897bea)

3.0.4 (2019-07-16)

Bug Fixes

3.0.3 (2019-07-03)

3.0.1 (2019-07-02)

Bug Fixes

  • aria-hidden broken in v3, fixes #8 (f7570e5)

3.0.0 (2019-06-29)

Features

2.0.2 (2019-05-05)

2.0.1 (2019-04-10)

Bug Fixes

  • dont prevent internal clicks, fixes #3 (229eaad)

2.0.0 (2019-03-11)

Features

  • shards and remove-scroll (dbd4d5f)
  • shards and remove-scroll (0f5d421)

1.3.2 (2019-01-21)

Bug Fixes

  • return focus by default (a525b4c)

1.3.1 (2018-12-31)

Bug Fixes

1.3.0 (2018-11-13)

Features

1.2.1 (2018-11-13)

1.2.0 (2018-11-12)

Features

1.1.0 (2018-11-07)

Features