npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

me-trap-focus

v3.0.2

Published

A small utility script to trap the focus within a container element

Downloads

11

Readme

meTrapFocus

meTrapFocus is a small utility script to trap the focus within a container element.

Trapping the focus is for instance necessary for accessible modal dialogs (see http://www.w3.org/TR/wai-aria-practices/#make_modal). But many dialog-widgets out there don't support keyboard/focus-handling. To enhance the accessibility of those widgets, just initialize meTrapFocus on the container of the dialog (see "Usage").

Usage

1. Include the JavaScript

Bundled & minified versions

meTrapFocus depends on meTools. It also uses Element.prototype.matches, so you need to include a polyfill for IE (see http://caniuse.com/#feat=matchesselector), which you can find at mePolyfills in the sources-folder.

  • Either include all the dependencies yourself and include me-trap-focus.min.js from the dist folder in your HTML page.
  • or use the standalone bundled me-trap-focus.bundle.min.js.

Source versions

You can find the original JavaScript file in the src folder of this package.

AMD

meTrapFocus has AMD support. This allows it to be lazy-loaded with an AMD loader, such as RequireJS.

2. Prepare your HTML

In most cases you won't have to change anything in your HTML.

iFrames

If you have an iFrame as first/last focusable element of the container, tabbing out of the container may not be noticed. In this case add a tab-reachable dummy-element with the class me-tabout as first/last element of the container to prevent tabbing out of the container.

If the iFrame has a cross-domain source it can not be focused via script in FireFox. In this case wrap the iFrame in a focusable wrapper and initialize meTrapFocus with an adjusted focusableSelector option to prevent the cross-domain-iFrame being detected as focusable.

3. Use meTrapFocus

Initialize meTrapFocus

/**
 * Create a new instance
 * @param container mixed; id or element; the container in which the focus should be maintained
 * @param options object; optional; overwrite the default options
 */
var myFocusTrapper = new meTrapFocus(container [,options])

If the focusable elements within the container change (elements are added or removed) you need to update the list of focusable elements. To update the list of focusable elements, call

myFocusTrapper.update();

To get all possibly focusable elements, call

myFocusTrapper.getFocusables();

To get all elements reachable by tab, call

myFocusTrapper.getTabable();

To get all elements reachable by back-tab, call

myFocusTrapper.getTabable(true);

To destroy the instance, call

myFocusTrapper = myFocusTrapper.destroy();

4. Default Options

{
  // selector for elements which are focusable
  focusableSelector: 'a,frame,iframe,input:not([type=hidden]),select,textarea,button,*[tabindex]',
  
  // selector for an optional element placed before the 1st or after the last tabable element to prevent tabbing out of the page directly to the browser-window (e.g. placed after an iframe as last tabable element)
  taboutIndicator: '.me-tabout' 
}

Package managers

You can install meTrapFocus using npm or Bower.

$ npm install me-trap-focus

or

$ bower install me-trap-focus

License

meTrapFocus is licenses under the MIT licence.