Gathering detailed insights and metrics for react-focus-rings
Gathering detailed insights and metrics for react-focus-rings
Gathering detailed insights and metrics for react-focus-rings
Gathering detailed insights and metrics for react-focus-rings
A centralized system for displaying and stylizing focus indicators anywhere on a webpage.
npm install react-focus-rings
Typescript
Module System
Node Version
NPM Version
v1.1.0 - Toggle ring visibility through JS
Updated on Sep 19, 2022
v1.0.8 - Fix default package export compatibility
Updated on Sep 15, 2022
v1.0.7
Updated on Sep 14, 2022
v1.0.6 - Add configuration for light/dark ring colors and thresholds
Updated on Sep 14, 2022
v1.0.5 - Fix border radius calculations in Firefox
Updated on Nov 12, 2021
v1.0.4 - Fix rings staying enabled too long
Updated on Oct 08, 2021
TypeScript (96.74%)
HTML (2.06%)
CSS (1.21%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1,136 Stars
47 Commits
26 Forks
162 Watchers
1 Branches
469 Contributors
Updated on Jul 10, 2025
Latest Version
1.2.1
Package Id
react-focus-rings@1.2.1
Unpacked Size
73.73 kB
Size
21.93 kB
File Count
37
NPM Version
10.5.0
Node Version
20.12.2
Published on
Mar 13, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
1
A centralized system for displaying and stylizing focus indicators anywhere on a webpage.
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.
This package is published under
react-focus-rings
and can be installed with any
npm-compatible package manager.
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
.
1function ScopeExample() { 2 const containerRef = React.useRef<HTMLDivElement>(null); 3 return ( 4 <div ref={containerRef} id="main"> 5 <FocusRingScope containerRef={containerRef}>{/* ... */}</FocusRingScope> 6 </div> 7 ); 8}
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.
1function Button(props: ButtonProps) { 2 return ( 3 <FocusRing> 4 <button {...props} /> 5 </FocusRing> 6 ); 7}
FocusRing
has a few props you can use to get the right behavior and alignment. If using TypeScript
the type is exported as FocusRingProps
1import { FocusRingProps } from "react-focus-rings";
within
- acts like
:focus-within
and will render
the focus ring if any descendant is focusedenabled
- controls whether the FocusRing
is being renderedfocused
- controls the focused stateoffset
- lets you adjust the alignment of the focus ring, relative to the focused element. Can
be a number
or
Offset
objectfocusTarget
- 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
.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 "react-focus-rings/src/styles.css";
.
A complete, minimal example might look like this:
1import * as React from "react"; 2import ReactDOM from "react-dom"; 3 4import { FocusRing, FocusRingScope } from "react-focus-rings"; 5import "react-focus-rings/src/styles.css"; 6 7function App() { 8 const containerRef = React.useRef<HTMLDivElement>(null); 9 return ( 10 <div className="app-container" ref={containerRef}> 11 <FocusRingScope containerRef={containerRef}> 12 <div className="content"> 13 <p>Here's a paragraph with some text.</p> 14 <FocusRing offset={-2}> 15 <button onClick={console.log}>Click Me</button> 16 </FocusRing> 17 <p>Here's another paragraph with more text.</p> 18 </div> 19 </FocusRingScope> 20 </div> 21 ); 22} 23 24ReactDOM.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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 8/27 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
23 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More