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

tolefocus

v0.3.1

Published

Agnostic Framework Focus Manager

Downloads

6

Readme

ToleFocus

NPM version Dependency Status devDependency Status

JavaScript Framework agnostic Focus Manager coded in TypeScript

ToleFocus is a small library to manage focus on JavaScript web applications. ToleFocus use ES6 methods, like array.findIndex, and require some polyfill like core-js in order to work in old browsers, like IE.

ToleFocus is highly inspired on Angular Focus Manager coded by Rob Tylor.

Installation

npm install tolefocus --save

Usage

To enable the focus manager you have to import focusManager from tolefocus module and call enable method.

import {focusManager} from "tolefocus";
focusManager.enable();

This load the content of body tag searching for ToleFocus attributes.

You can enable the built in DOM observer witch use MutationObserver in order to detect the DOM changes.

import {focusManager, focusObserver} from "tolefocus";
focusManager.enable();
focusObserver.enable();

ToleFocus handles by default the TAB key in the common focusables controls and sends focus to the next or previous control.

Setting focus order

You can set the focus order:

<input /> <!-- first control in focus order -->
<input /> <!-- third control in focus order -->
<input focus-order="1" /> <!-- second control in focus order -->

Focus order is zero based, you can set negative focus order to any element, the negative focus order are pushed to the first positions.

<input id='i1'/> <!-- second order -->
<input id='i2' focus-order='-2' /> <!-- first order -->
<input id='i3' /> <!-- third order -->
<input id='i4' focus-order='2' /> <!-- fourth order -->
<input id='i5' /> <!-- fifth order -->
<input id='i6' /> <!-- seventh order -->
<input id='i7' focus-order='4' /> <!-- sixth order -->
<input id='i8' /> <!-- eighth order -->

Focus groups

You can create focus groups where each element can to have his own focus-order in the group.

<div focus-group>
    <input />
    <div focus-group focus-order="1">
        <input /> 
        <input /> 
        <input />
    </div>
    <input />
    <input focus-order="2" />
</div>
<div focus-group focus-order="1">
    <input /> 
    <input /> 
    <input />
</div>

Focus loopback control

You can control the group loopback, you can set the head and tail behaivor. When behaivor is "stop" the focus doesn't go to the next element, the foucus stops in the last or first element. When behaivor is "loop" the focus go from last to first element in the group or from the first to the end element.

<div focus-group="loop"> <!-- head and tail behaivor to "loop". The focus from first to last or reverse. -->
    <input /> 
    <input /> 
    <input />
</div>
<div focus-group="stop"> <!-- head and tail behaivor to "stop". The focus stops in the last or the first element -->
    <input /> 
    <input /> 
    <input />
</div>
<div focus-group="stop loop"> <!-- head behaivor set to stop and tail behaivor is "loop". The focus stops in the first element and will go from the last to the first -->
    <input /> 
    <input /> 
    <input />
</div>

Non-selectable elements

You can send focus to other controls like div or spans, you have to set focus-order attribute.

<div focus-order>
</di>
<input />
<div focus-order="2">
</di>

Autofocus

You can set an element as autofocus to set focus on it automatically. If you have the focusObserver enabled the element will focus when it's added to the DOM.

<input />
<input />
<input autofocus /> <!-- gets the focus automatically -->

If you want to hide a element and get focused when it becomes visible you can set observe value to autofocus attribute.

<input />
<input />
<input autofocus="observe" style="display: none" />  <!-- gets the focus automatically when become visible -->
<button onclick="showHiddenInput()">Show</button>

Custom focusable elements

You can add selectors to handle focusable elements, for example you can add elements with class select2-selection as focusable elements, then tolefocus detect this elements as focusables.

focusManager.addCustomSelector(".select2-selection");

Decide if element can get the focus

When tolefocus going to set focus to an element, it ask if the element can get focus, by default it look if element is visible and not disabled this way:

    const visible = element.offsetParent !== null;
    const disable = !!element["disabled"];
    return visible && !disable;

You can set a handler to decide if element can get focus, for example you can do that elements with class select2-hidden-accessible don't get the focus:

    focusManager.setCanElementGetFocusHandler((element) => {
        if (element.classList.contains("select2-hidden-accessible")) {
            return false;
        }
    });

If handler don't return a value tolefocus decide via visible and disabled.

Select2 support

Select2 hide the <select> element and redirect focus to a self created <span>, tolefocus can't detect this behivor without help, you have to mark the span as focusable and disable the select to get focus, you can do it this way:

    // Select2 Focus management.
    focusManager.addCustomSelector(".select2-selection");
    focusManager.setCanElementGetFocusHandler((element) => {
        if (element.classList.contains("select2-hidden-accessible")) {
            return false;
        }
    });
    // end select2 Focus management

    // Enable tolefocus.
    focusManager.enable();
    focusObserver.enable();

SystemJS config

To use using SystemJS you have to map tolefocus to the distributed bundle:

System.config({
    ...
    ...
    map: {
      tolefocus: './node_modules/tolefocus/dist/bundles/tolefocus.bundle.umd.js'
      ...
      ...
    },
    ...
    ...
})

Build

Pull request are welcome, to build it:

git clone https://github.com/tolemac/tolefocus.git
cd tolefocus
npm install
npm run build

You can launch test suite using:

npm run test