Gathering detailed insights and metrics for react-arrow-navigation
Gathering detailed insights and metrics for react-arrow-navigation
Gathering detailed insights and metrics for react-arrow-navigation
Gathering detailed insights and metrics for react-arrow-navigation
@arrow-navigation/react
A light and performant React implementation for @arrow-navigation/core package.
react-arrow-key-navigation-hook
A React hook & associated functions to implement arrow key navigation through a component
react-input-suggestions
A React input component with pluggable suggestions and autocomplete
react-arrow-nav
ArrowKey Navigation for react elements
npm install react-arrow-navigation
Typescript
Module System
Min. Node Version
Node Version
NPM Version
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
28
A react library for managing navigation with arrow keys
1npm install --save react-arrow-navigation
Mount ArrowNavigation
in your app, and register components that receive navigation state with the useArrowNavigation
hook. It takes two arguments: an x and y navigation index.
1import React from 'react' 2import { ArrowNavigation, useArrowNavigation } from 'react-arrow-navigation' 3 4const NavigationChild = ({ xIndex, yIndex }) => { 5 const { selected, active } = useArrowNavigation(xIndex, yIndex) 6 return ( 7 <div className={selected && active ? 'child selected' : 'child'}> 8 {`Index [${xIndex}, ${yIndex}]`} 9 </div> 10 ) 11} 12 13const MyApp = () => ( 14 <ArrowNavigation className="nav"> 15 <NavigationChild xIndex={0} yIndex={0} /> 16 <NavigationChild xIndex={0} yIndex={1} /> 17 <NavigationChild xIndex={0} yIndex={2} /> 18 </ArrowNavigation> 19)
useArrowNavigation
returns two values that represent the navigation state:
selected
: whether this index is currently selectedactive
: whether this ArrowNavigation
component is currently focusedThis gives you flexibilty when implementing navigable components. For example:
select
callbackYou may also want to be able to update the navigation state when a component is clicked. For example:
When this happens you want the navigation index to be updated on click, so when they use the arrow keys again the index is correct.
To achieve this we can use the select
callback provided by useArrowNavigation
:
1const ButtonGroupButton = ({ xIndex }) => { 2 const { selected, select } = useArrowNavigation(xIndex, 0) 3 return ( 4 <div 5 className={selected ? 'bg-button selected' : 'bg-button'} 6 onClick={() => { 7 // Click handler logic goes here 8 select() // Then call the `select` callback 9 }} 10 > 11 {`Option ${xIndex + 1}`} 12 </div> 13 ) 14}
ArrowNavigation
focus stateTo toggle the active
state, ArrowNavigation
returns a containing <div>
, and listens to the onFocus
, and onBlur
events. It updates active
to be true
when the <div>
is focused, and false
when it is not. It also switches active
to false
when the Escape key is pressed.
Additional props passed to ArrowNavigation
are spread on to the <div>
. This includes support for a ref
prop, implemented with React.forwardRef.
If you want to opt-out and manage the active
state yourself, use BaseArrowNavigation
. Its active
state is determined by its active
prop. It does not insert a containing <div>
.
Sometimes you will want navigable components to be focused when they are selected. There are behaviors built into browsers you might want to leverage (onClick
being fired when the user hits the Enter key), and it's also good for acessibility: screen readers rely on the focus state.
To enable this there is a hook: useArrowNavigationWithFocusState
. It returns an additional value: focusProps
, which is spread onto the navigable component. focusProps
is comprised of tabIndex
, onClick
, and ref
. In more complex cases you may want to access these props directly: e.g. you need to do something else in the click handler.
Here is a dropdown menu implemented with it:
1import React, { useState, useRef, useEffect } from 'react' 2import { ArrowNavigation, useArrowNavigationWithFocusState } from 'react-arrow-navigation' 3 4const DropdownMenuItem = ({ index, label, closeMenu }) => { 5 const { 6 focusProps: { ref, tabIndex, onClick }, 7 } = useArrowNavigationWithFocusState(0, index) 8 9 return ( 10 <button 11 className="menu-item" 12 ref={ref} 13 tabIndex={tabIndex} 14 onClick={() => { 15 onClick() 16 alert(`Clicked: "${label}"`) 17 closeMenu() 18 }} 19 > 20 {label} 21 </button> 22 ) 23} 24 25const DropdownMenu = ({ label, itemLabels }) => { 26 const [open, setOpen] = useState(false) 27 const navRef = useRef() 28 29 useEffect(() => { 30 if (open) { 31 navRef.current && navRef.current.focus() 32 } 33 }, [open]) 34 35 return ( 36 <div> 37 <button 38 className="dropdown-button" 39 onClick={() => { 40 setOpen(!open) 41 navRef.current && navRef.current.focus() 42 }} 43 > 44 {open ? 'Close the menu' : 'Open the menu'} 45 </button> 46 {open && ( 47 <ArrowNavigation className="menu" ref={navRef} initialIndex={[0, 0]}> 48 {itemLabels.map((itemLabel, index) => ( 49 <DropdownMenuItem 50 index={index} 51 label={itemLabel} 52 closeMenu={() => setOpen(false)} 53 key={index} 54 /> 55 ))} 56 </ArrowNavigation> 57 )} 58 </div> 59 ) 60} 61 62const MyApp = () => ( 63 <DropdownMenu itemLabels={['Navigate through', 'The menu items', 'With arrow keys']} /> 64)
useArrowNavigationWithFocusState
has to interact with the focus state of ArrowNavigation
, so it is not compatible with BaseArrowNavigation
.
useArrowNavigation
retrieves the navigation state from ArrowNavigation
using the context API, so navigable components can be arbitrarly nestedArrowNavigation
manages the selection state of the navigation indexes, and its active state based on if it is focused.
Props:
children
: React.Node
initialIndex
: [number, number]
(optional)
An index to be selected when ArrowNavigation
is first focused
mode
: 'roundTheWorld' | 'continuous' | 'bounded'
(optional)
The edge mode of the navigation: what happens when a user goes over the edges of the x and y indexes. The options are:
'roundTheWorld'
(this is the default)
'continuous'
'bounded'
reInitOnDeactivate
: true | false
(optional)
Resets the indexes when ArrowNavigation
deactivates
...divProps
All other props passed to ArrowNavigation
are passed onto the div
it returns. This includes support for the ref
prop.
The useArrowNavigation
hook takes two arguments: an x and y navigation index.
Returned values:
selected
: true | false
Whether this index is currently selected
active
: true | false
Whether the navigation component (ArrowNavigation
or BaseArrowNavigation
) is active. Active means it is responding to keypresses.
select
: () => void
A callback that updates the selected index to this one
The useArrowNavigation
hook takes two arguments: an x and y navigation index. It is not compatible with BaseArrowNavigation
.
Returned values:
selected
, active
, and select
are the same as for useArrowNavigation
focusProps
: { ref, tabIndex, onClick }
focusProps
should be spread onto the navigable component. They will:
tabIndex
to 0
if it is selected and -1
otherwiseactive
is true
In complex cases you may want to access these props directly, e.g. if you need to do another thing in the component's click handler:
1 onClick={() => { 2 // Click handler logic goes here 3 onClick() // Then call the onClick callback from `focusProps` 4 }}
BaseArrowNavigation
works in a similar way to ArrowNavigation
, except it does not return a containing div, and
does not manage it's active state. This is now passed in with a prop.
Props:
children
, initialIndex
, mode
, and reInitOnDeactivate
are the same as for ArrowNavigation
active
: true | false
Whether the component should update the selected index in response to keypresses
MIT © Jack Aldridge
No vulnerabilities found.
No security vulnerabilities found.