Rover
Rover is an abstract component that implements the roving tabindex method to manage focus between items (rovers).
#Installation
npm install reakit
Learn more in Get started.
#Usage
import { useRoverState, Rover } from "reakit/Rover";
import { Group } from "reakit/Group";
import { Button } from "reakit/Button";
function Example() {
const rover = useRoverState();
return (
<Group>
<Rover {...rover} as={Button}>
Button 1
</Rover>
<Rover {...rover} as={Button} disabled>
Button 2
</Rover>
<Rover {...rover} as={Button} disabled focusable>
Button 3
</Rover>
<Rover {...rover} as={Button}>
Button 4
</Rover>
<Rover {...rover} as={Button}>
Button 5
</Rover>
</Group>
);
}
#Loop
If you set loop to true on useRoverState, the roving tabindex will loop from the last item to the first item.
import { useRoverState, Rover } from "reakit/Rover";
function Example() {
const rover = useRoverState({ loop: true });
return (
<>
<Rover {...rover}>Button 1</Rover>
<Rover {...rover}>Button 2</Rover>
<Rover {...rover}>Button 3</Rover>
<Rover {...rover}>Button 4</Rover>
<Rover {...rover}>Button 5</Rover>
</>
);
}
#Accessibility
Roverhastabindexset to0if it's the current element. Otherwisetabindexis set to-1.- Pressing ↑ moves focus to the previous
Roveriforientationisverticalor not defined. - Pressing ↓ moves focus to the next
Roveriforientationisverticalor not defined. - Pressing → moves focus to the next
Roveriforientationishorizontalor not defined. - Pressing ← moves focus to the previous
Roveriforientationishorizontalor not defined. - Pressing Home or PageUp moves focus to the first
Rover. - Pressing End or PageDown moves focus to the last
Rover.
Learn more in Accessibility.
#Composition
Roveruses Clickable.
Learn more in Composition.
#Props
#useRoverState
-
baseIdstringID that will serve as a base for all the items IDs.
-
orientation"horizontal" | "vertical" | undefinedDefines the orientation of the rover list.
-
currentIdstring | nullThe current focused element ID.
-
loopbooleanIf enabled:
- Jumps to the first item when moving next from the last item.
- Jumps to the last item when moving previous from the first item.
#Rover
-
disabledboolean | undefinedSame as the HTML attribute.
-
focusableboolean | undefinedWhen an element is
disabled, it may still befocusable. It works similarly toreadOnlyon form elements. In this case, onlyaria-disabledwill be set. -
idstring | undefinedSame as the HTML attribute.
-
stopIdstring | undefinedElement ID.
12 state props
These props are returned by the state hook. You can spread them into this component (
{...state}) or pass them separately. You can also provide these props from your own state logic.
-
baseIdstringID that will serve as a base for all the items IDs.
-
orientation"horizontal" | "vertical" | undefinedDefines the orientation of the rover list.
-
unstable_movesnumberStores the number of moves that have been made by calling
move,next,previous,firstorlast. -
currentIdstring | nullThe current focused element ID.
-
stopsStop[]A list of element refs and IDs of the roving items.
-
register(id: string, ref: RefObject<HTMLElement>) => voidRegisters the element ID and ref in the roving tab index list.
-
unregister(id: string) => voidUnregisters the roving item.
-
move(id: string | null, unstable_silent?: boolean |...Moves focus to a given element ID.
-
next() => voidMoves focus to the next element.
-
previous() => voidMoves focus to the previous element.
-
first() => voidMoves focus to the first element.
-
last() => voidMoves focus to the last element.