JSPM

  • Created
  • Published
  • Downloads 772112
  • Score
    100M100P100Q190886F
  • License MIT

A React component that traps focus.

Package Exports

  • focus-trap-react

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (focus-trap-react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

focus-trap-react Build Status

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 a focus trap.

  • The focus trap automatically activates when mounted (by default, though this can be changed).
  • The focus trap automatically deactivates when unmounted.

Installation

npm install focus-trap-react

React dependency

Version 2+ is compatible with React 0.14.x.

Version 1 is compatible with React 0.13.x.

Browser Support

Basically IE9+. See .zuul.yml for more details.

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

Automated testing is done with zuul and Open Suace.

Usage

Read code in demo/ (it's very simple), and see how it works.

Here's one simple example:

var React = require('react');
var FocusTrap = require('focus-trap-react');

var DemoOne = React.createClass({
  getInitialState: function() {
    return {
      activeTrap: false,
    };
  },

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

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

  render: function() {
    var trap = (this.state.activeTrap) ? (
      <FocusTrap
        onDeactivate={this.unmountTrap}
        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>
      </FocusTrap>
    ) : false;

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

Easy enough.

Props

onDeactivate

Type: Function, required

This function is called when the FocusTrap deactivates. If, for example, a user hits Escape within the FocusTrap, the trap deactivates; and you need to tell it what to do next. The FocusTrap does not manage its own visible/hidden state: you do that.

initialFocus

Type: String, optional

By default, when the FocusTrap activates it will pass focus to the first element in its tab order. If you want that initial focus to pass to some other element (e.g. a Submit button at the bottom of a modal dialog), pass a selector identifying the element that should receive initial focus when the FocusTrap activates. (This will be passed to document.querySelector() to find the element.)

See demo/demo-two.jsx.

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-three.jsx.

className

Type: String, optional

A className for the FocusTrap's DOM node.

id

Type: String, optional

An id for the FocusTrap's DOM node.

tag

Type: String, Default: 'div', optional

An HTML tag for the FocusTrap's DOM node.

style

Type: Object, optional

An inline style object (in React fashion) for the FocusTrap's DOM node.

Contributing & Development

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Lint with npm run lint.

Test with npm run test-dev, which will give you a URL to open in your browser. Look at the console log for TAP output.