focus-trap-react

WebJar for focus-trap-react

License

License

MIT
Categories

Categories

React User Interface Web Frameworks
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

focus-trap-react
Last Version

Last Version

6.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

focus-trap-react
WebJar for focus-trap-react
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/davidtheclark/focus-trap-react

Download focus-trap-react

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/focus-trap-react/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>focus-trap-react</artifactId>
    <version>6.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/focus-trap-react/
implementation 'org.webjars.npm:focus-trap-react:6.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/focus-trap-react/
implementation ("org.webjars.npm:focus-trap-react:6.0.0")
'org.webjars.npm:focus-trap-react:jar:6.0.0'
<dependency org="org.webjars.npm" name="focus-trap-react" rev="6.0.0">
  <artifact name="focus-trap-react" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='focus-trap-react', version='6.0.0')
)
libraryDependencies += "org.webjars.npm" % "focus-trap-react" % "6.0.0"
[org.webjars.npm/focus-trap-react "6.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : focus-trap jar [4.0.2,5)

Project Modules

There are no modules declared in this project.

focus-trap-react CI license

All Contributors

A React component that traps focus.

This component is a light wrapper around focus-trap, tailored to your React-specific needs.

You might want it for, say, building an accessible modal?

What it does

Check out the demo.

Please read the focus-trap documentation to understand what a focus trap is, what happens when a focus trap is activated, and what happens when one is deactivated.

This module simply provides a React component that creates and manages a focus trap.

  • The focus trap automatically activates when mounted (by default, though this can be changed).
  • The focus trap automatically deactivates when unmounted.
  • The focus trap can be activated and deactivated, paused and unpaused via props.

Installation

npm install focus-trap-react

dist/focus-trap-react.js is the Babel-compiled file that you'll use.

React dependency

React >= 16.0.0.

Browser Support

Basically IE9+.

Why? Because this module's core functionality comes from focus-trap, which uses a couple of IE9+ functions.

Usage

You wrap any element that you want to act as a focus trap with the <FocusTrap> component. <FocusTrap> expects exactly one child element which can be any HTML element or other React component that contains focusable elements.

For example:

<FocusTrap>
  <div id="modal-dialog" className="modal" >
    <button>Ok</button>
    <button>Cancel</button>
  </div>
</FocusTrap>
<FocusTrap>
  <ModalDialog okButtonText="Ok" cancelButtonText="Cancel" />
</FocusTrap>

You can read further code examples in demo/ (it's very simple), and see how it works.

Here's one more simple example:

const React = require('react');
const ReactDOM = require('react-dom');
const FocusTrap = require('../../dist/focus-trap-react');

const container = document.getElementById('demo-one');

class DemoOne extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      activeTrap: false
    };

    this.mountTrap = this.mountTrap.bind(this);
    this.unmountTrap = this.unmountTrap.bind(this);
  }

  mountTrap() {
    this.setState({ activeTrap: true });
  }

  unmountTrap() {
    this.setState({ activeTrap: false });
  }

  render() {
    const trap = this.state.activeTrap
      ? <FocusTrap
          focusTrapOptions={{
            onDeactivate: this.unmountTrap
          }}
        >
          <div className="trap">
            <p>
              Here is a focus trap
              {' '}
              <a href="#">with</a>
              {' '}
              <a href="#">some</a>
              {' '}
              <a href="#">focusable</a>
              {' '}
              parts.
            </p>
            <p>
              <button onClick={this.unmountTrap}>
                deactivate trap
              </button>
            </p>
          </div>
        </FocusTrap>
      : false;

    return (
      <div>
        <p>
          <button onClick={this.mountTrap}>
            activate trap
          </button>
        </p>
        {trap}
      </div>
    );
  }
}

ReactDOM.render(<DemoOne />, container);

Props

focusTrapOptions

Type: Object, optional

Pass any of the options available in focus-trap's createOptions.

active

Type: Boolean, optional

By default, the FocusTrap activates when it mounts. So you activate and deactivate it via mounting and unmounting. If, however, you want to keep the FocusTrap mounted while still toggling its activation state, you can do that with this prop.

See demo/demo-special-element.js.

paused

Type: Boolean, optional

If you would like to pause or unpause the focus trap (see focus-trap's documentation), toggle this prop.

containerElements

Type: Array of HTMLElement, optional

If passed in, these elements will be used as the boundaries for the focus-trap, instead of the child. These get passed as arguments to focus-trap's updateContainerElements() method.

Note that when you use containerElements, the need for a child is eliminated as the child is always ignored when the prop is specified, even if the prop is [] (an empty array). Also note that if the refs you're putting into the array like containerElements={[ref1.current, ref2.current]} and one or both refs aren't resolved yet, resulting in [null, null] for example, the trap will not get created. The array must contain at least one HTMLElement in order for the trap to get updated.

If containerElements is subsequently updated (i.e. after the trap has been created) to an empty array (or an array of falsy values like [null, null]), the trap will still be active, but the TAB key will do nothing because the trap will not contain any tabbable groups of nodes. At this point, the trap can either be deactivated manually or by unmounting, or an updated set of elements can be given to containerElements to resume use of the TAB key.

Using containerElements does require the use of React refs which, by nature, will require at least one state update in order to get the resolved elements into the prop, resulting in at least one additional render. In the normal case, this is likely more than acceptable, but if you really want to optimize things, then you could consider using focus-trap directly (see Trap2.js).

Contributing

See CONTRIBUTING.

Contributors

In alphabetical order:


David Clark

๐Ÿ’ป ๐Ÿ› ๐Ÿš‡ โš ๏ธ ๐Ÿ“– ๐Ÿšง

Stefan Cameron

๐Ÿ’ป ๐Ÿ› ๐Ÿš‡ โš ๏ธ ๐Ÿ“– ๐Ÿšง

Marais Rossouw

๐Ÿš‡

Nate Liu

โš ๏ธ

Sean McPherson

๐Ÿ’ป

Dependabot

๐Ÿšง

Rivaldo Junior

๐Ÿšง

Kathleen McMahon

๐Ÿšง

Scott Rippey

๐Ÿ’ป ๐Ÿ›

Daniel

๐Ÿšง โš ๏ธ

Benjamin Koltes

๐Ÿ›

Benjamin Tan

๐Ÿ“–

Slapbox

๐Ÿ“–

Jonathan Suzuki

๐Ÿ›

krikienoid

๐Ÿ›

Clint Goodman

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก โš ๏ธ

Tyler Hawkins

๐Ÿš‡

Versions

Version
6.0.0