Giter Club home page Giter Club logo

react-focus-trap's Introduction

NPM


Circle CI


A generic focus management tool for components such as dialogs and dropdowns.

focus

Usage

React Focus Trap is a container element that will manage focus for its children.

let Modal = React.createClass({
  render() {
    return (
      <FocusTrap onExit={ this._onExit } active={ this.props.active }>
        Amazing stuff goes here
      </FocusTrap>
    )
  }
})

When Focus Trap is active, it will do several things:

  1. Ensure focus remains on its content
  2. Exits when clicks outside of the container occur
  3. Exits when the escape key is pressed

Props

Name Default Description
active true Should the FocusTrap render?
className 'focus-trap' The class of the inner container that maintains focus
onExit null Callback when escape or an outside click occurs
element 'div' The tag name of the inner container
role 'dialog' The aria role for the inner container

Code At Viget

Visit code.viget.com to see more projects from Viget.

react-focus-trap's People

Contributors

cwmanning avatar kyleamathews avatar nhunzaker avatar styfle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-focus-trap's Issues

undefined import

I'm working on a typescript react project where I've installed react-focus-trap via yarn yarn add react-focus-trap and I can see into the package.json "react-focus-trap": "^2.7.1",.

In my code I'm using it:

// ...
import FocusTrap from 'react-focus-trap';
// ...

class Modal extends React.Component<ModalProps, any> {
	// ...

	render() {
		return <FocusTrap className='modal'><div /></FocusTrap>
	}
}

Debugging it, I can see that FocusTrap is undefined and react is generating this error:

warning.js?85a7:33 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `Modal`.

Any idea on this error?

Focus trap focuses `focus-trap`

Focus trap focuses the focus-trap div after I press tab on the last element. I do not need that focus, I want it to jump from the last to the first item.

Focus trap should not be forced to 'unpause' when another trap is disabled

Found an issue where one trap is incorrectly forced to unpause when another trap is disabled/destroyed.

Here is simple example:

Steps to reproduce:

  1. Click on 'btn 2' to remove second trap.

Result:
Trap 1 (with child 'btn 1') becomes active preventing 'btn 3' from being focused or clicked.

Expectation:

  • Trap 1 (with child 'btn 1') should remain paused due <FocusTrap paused={true}>.
  • 'btn 3' should be focusable and clickable.

Warning: componentWillMount has been renamed

When dynamically rendering a component coning a FocusTrap-component this warning gets logged to the console:

Warning: componentWillMount has been renamed, and is not recommended for use. See https://fb.me/react-async-component-lifecycle-hooks for details.

* Move code with side effects to componentDidMount, and set initial state in the constructor.
* Rename componentWillMount to UNSAFE_componentWillMount to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.

Please update the following components: FocalPoint

Unknown prop `onExit` on <div> tag

Not sure if I'm just doing something really wrong here but this is my code:

return <FocusTrap active={this.props.menuVisible} onExit={this.escape}> 
...

Which fires a React error:
Warning: Unknown prop onExit on

tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop

Add dropdown example

This project can also be used for dropdowns. It would be great to have an example of this.

Listener memory leak

I've just spent a while hunting down a little memory leak that we traced to this library. The event handlers on inputs that are left focussed when we unmounted a route in our app were being held and causing a leak. Have you come across this before? Is there anyway to ensure these are released from within focus-trap-react?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.