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

@michaeldrotar/focus-tracker-js

v0.3.0

Published

Enhances focus tracking with transitions and customization to make the experience more accessible for users and easier for developers.

Downloads

449

Readme

focus-tracker-js

NPM Version NPM License NPM Downloads

Typescript-friendly Focus Tracker library for use with browsers or frameworks.

Enhances focus tracking with transitions and customization to make the experience more accessible for users and easier for developers.

See the video demonstration below.

Demonstration video]

Installation

Install with any package manager. The focus tracker is a visual element that runs in your application so should usually be a production dependency.

npm install @michaeldrotar/focus-tracker-js
pnpm add @michaeldrotar/focus-tracker-js
bun add @michaeldrotar/focus-tracker-js

Access

Import into your code

import { focusTracker } from '@michaeldrotar/focus-tracker-js'

or add the dist/index.js file to your scripts and access from the window object.

<script src="/path/to/node_modules/@michaeldrotar/focus-tracker-js/dist/index.js"></script>
<script>
  window.focusTracker
</script>

Usage

FocusTrackerConfiguration

The configuration object allows customization of how your focus tracker looks. The options are limited to allow branding while focusing on the importance of following established best practices for a recognizable focus indication.

| Property | Type | Description | | --------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | boxShadow | string | Provides access to the box-shadow css property so multiple inner and outer shadows can simply be defined. Use currentColor to refer to the configuration.color property for better re-use. | | color | string | Sets the main color of the the focus tracker. By default, uses the browser's default focus styles. Any valid css value may be used, including helpers like color-mix(). | | offset | string \| number | Sets an offset distance. By default, the offset is 0 so that the tracker tightly wraps the edges of the target element. A negative offset will move the tracker inside the bounds of the element. A string may be used to include css units like "0.1rem" or "3px". | | target | 'self' \| 'target' \| HTMLElement | Sets where the focus tracker's indicator will be positioned. By default, uses 'target' as the target of the focus event. May use 'self' for nested configurations so children show focus on itself, or may pass any other element. | | thickness | string \| number | Sets how thick the focus tracker's outline should be. By default, uses the browser's default thickness. A string may be used to include css units like "0.1rem" or "3px". |

register(element: HTMLElement, configuration: FocusTrackerConfiguration)

For most users, register the document.body element and use the configuration to customize how your focus tracker should look.

focusTracker.register(document.body, {
  color: '#0ea5e9',
  thickness: 3,
})

Check advanced use cases for more.

unregister(element: HTMLElement)

Elements may be unregistered to return to using default focus.

If an element is removed from the DOM, it should be unregistered to clean up the event handlers and memory usage.

Advanced Use Cases

The registration method allows for more advanced use cases to support sparse registrations and nested registrations. Below are some common examples.

Compound Form Fields

If you have a compound form field like a search box where you want the outline around the input and its label, register the element with an overriding configuration to set its target.

<div data-search-container class="rounded-md border">
  <label for="search-input">Search</label>
  <input id="search-input" />
</div>
focusTracker.register(
  document.querySelector('[data-search-container]', { target: 'self' }),
)

Handling color contrast with nested configurations

This library comes preconfigured with a configuration that should match the browser's default appearance with the added benefit of the built-in transitions.

Most users will want to put their own branding on it. But if your brand is green and you have some elements with a green background then you'll lose contrast. Nested configurations allow making these changes more intuitive.

<body>
  <div data-hero-banner style="background-color: green">
    <h1>My Hero</h1>
    <button>Read the docs</button>
  </div>
</body>
focusTracker.register(document.body, {
  color: 'green',
  offset: 1,
  thickness: 3,
})
focusTracker.register(document.querySelector('[data-hero-banner]'), {
  color: 'white',
})

Now when focus moves to the [data-hero-banner] element or one of its children, the color will transition from green to white.