focus-trap-js

WebJar for focus-trap-js

License

License

MIT
Categories

Categories

JavaScript Languages
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

focus-trap-js
Last Version

Last Version

1.0.8
Release Date

Release Date

Type

Type

jar
Description

Description

focus-trap-js
WebJar for focus-trap-js
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/alexandrzavalii/focus-trap-js

Download focus-trap-js

How to add to project

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

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

focus-trap-js

Trap focus inside specified HTML element. Vanilla JS with size <0.5kb. No dependencies.

Browser Support: IE 10, Firefox, Chrome.

Note

No event listeners, it is just handling the current event, and traps the focus if it is neccessary.

Instalation

npm i focus-trap-js

Usage

import focusTrap from "focus-trap-js";

const popupContainer = document.getElementById("popupId");

document.addEventListener("keydown", event => {
  focusTrap(event, popupContainer);
});

The method focusTrap accepts two parameters, the event and HTML element container in which you want to trap your focus.

Usage in React

import React from "react";
import focusTrap from "focus-trap-js";

const Container = () => {
  const contRef = React.useRef();

  React.useEffect(() => {
    const handleKeyEvent = event => {
      focusTrap(event, contRef.current);
    };
    document.addEventListener("keydown", handleKeyEvent);
    return () => {
      document.removeEventListener("keydown", handleKeyEvent);
    };
  }, [contRef]);

  return <div ref={contRef}></div>;
};

List of Tabbable Elements

const tabbableQuery = [
  "input",
  "select",
  "textarea",
  "a[href]",
  "button",
  "[tabindex]", //tabIndex > 0
  "audio[controls]",
  "video[controls]",
  '[contenteditable]:not([contenteditable="false"])'
];

Versions

Version
1.0.8