Import the utility function:
import rovingTabindex from './rovingTabindex';
Init the function by bassing the required parameter:
rovingTabindex(<container>, 'next event.key name', 'previous event.key name', 'elements classname or any selector')
Where:
type Node
The element containing the consecutive elements that should be navigable by keyboard
type String
The event.key name that navigate to the next element. For example ArrowUp
. More info here.
type String
The event.key name that navigate to the previous element. For example ArrowDown
. More info here.
type StringSelector
The selector that match the navigable list items inside the container.
Taking this HTML structure:
<nav class="MyList">
<h2>Menu</h2>
<a href="#">List item 1</a>
<a href="#">List item 2</a>
<a href="#">List item 3</a>
</nav>
to make li
navigable we can call rovingTabindex()
as follow:
rovingTabindex(document.querySelector('.MyList'), 'ArrowRight', 'ArrowLeft', 'a')