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

@meitner/react-focus-rings

v1.0.8

Published

A centralized system for displaying and stylizing focus indicators anywhere on a webpage.

Downloads

51

Readme

This is a fork of discord/focus-rings

This package is a fork of discord/focus-rings. It has been extended with :focus-visible functionality using the focus-visible polyfill and also added the borderColor & borderWidth props to FocusRingScope

Focus Rings

A centralized system for displaying and stylizing focus indicators anywhere on a webpage.

a gif showing focus rings in action

Motivation

When working on creating a complete keyboard navigation experience for Discord, we ran into a number of problems with rendering clean and consistent focus rings and were unable to find any suitable native alternatives. After a lot of trial and error, we landed on this system as a way to meet all of our needs. You can read more about the process we went through in this blog post.

Installation

This package is published under react-focus-rings and can be installed with any npm-compatible package manager.

Usage

This library is composed of two components: FocusRing and FocusRingScope.

FocusRingScope

FocusRingScope is responsible for providing a frame of reference for calculating the position of any FocusRing instances it contains. The containerRef prop takes a React.Ref that references the DOM element that should be used for these position calculations. You'll want to include a FocusRingScope instance at the top level of your application. If a component creates a new scroll container, or is absolutely positioned within the viewport, you should add a new FocusRingScope.

function ScopeExample() {
  const containerRef = React.useRef<HTMLDivElement>(null);
  return (
    <div ref={containerRef} id="main">
      <FocusRingScope borderColor={"red"} borderWidth={4} containerRef={containerRef}>{/* ... */}</FocusRingScope>
    </div>
  );
}

Keep in mind that the element provided to containerRef must be styled with position: relative or else the alignment calculations will be incorrect. If you find that FocusRing isn't being rendered at all or its positioning is wrong, verify that you have a FocusRingScope in the correct places and that the containerRef element has the position: relative style.

FocusRing

The FocusRing is the main show. You can wrap any focusable element with a FocusRing and it will add the required focus/blur listeners and magically render the focus ring when the element receives focus. We recommend integrating this at the design primitive level, in custom components like Button or Link, so you get the focus ring behavior across your application with minimal effort.

function Button(props: ButtonProps) {
  return (
    <FocusRing>
      <button {...props} />
    </FocusRing>
  );
}

Props

FocusRing has a few props you can use to get the right behavior and alignment. If using TypeScript the type is exported as FocusRingProps

import { FocusRingProps } from "react-focus-rings";
  • within - acts like :focus-within and will render the focus ring if any descendant is focused
  • enabled - controls whether the FocusRing is being rendered
  • focused - controls the focused state
  • offset - lets you adjust the alignment of the focus ring, relative to the focused element. Can be a number or Offset object
  • focusTarget - lets you choose a different element to act as the focus target for the ring. Must be used with ringTarget.
  • ringTarget - lets you choose a different element to render the ring around. Must be used with focusTarget.
  • focusWithinClassName - lets you apply a CSS class to the focused element when a descendant is focused. Like :focus-within.

Default Styling

The focus ring also relies on some default CSS styles in order to render properly. To make this work in your project, be sure to import the styles separately somwhere within your app with import "focus-rings/src/styles.css";.

Example

A complete, minimal example might look like this:

import * as React from "react";
import ReactDOM from "react-dom";

import { FocusRing, FocusRingScope } from "react-focus-rings";
import "focus-rings/src/styles.css";

function App() {
  const containerRef = React.useRef<HTMLDivElement>(null);
  return (
    <div className="app-container" ref={containerRef}>
      <FocusRingScope containerRef={containerRef}>
        <div className="content">
          <p>Here's a paragraph with some text.</p>
          <FocusRing offset={-2}>
            <button onClick={console.log}>Click Me</button>
          </FocusRing>
          <p>Here's another paragraph with more text.</p>
        </div>
      </FocusRingScope>
    </div>
  );
}

ReactDOM.render(<App />, document.body);

You can find a more complete example in the examples directory of this repository. You can find a hosted version of the example application here.