0.2.1 • Published 5 years ago

react-tab-controller v0.2.1

Weekly downloads
22
License
-
Repository
github
Last release
5 years ago

React Tab Controller

Simple roving tabIndex for React.

Motivation

A roving tabIndex is an a11y technique used for making sure only one element in a list or group of items is focusable at a time, so the user can quickly skip a possible large list of items without having to tab through all elements.

This is accomplished by setting only one children in the group of items with a tabIndex of 0, which is the current selected item, and all other items with a value of -1. Then, to change the selected item, you can use the keyboard arrow keys to jump through the items in the group.

Installation

yarn add react-tab-controller
# or
npm install react-tab-controller

Usage

You can also try out the live demo at CodeSandbox!

Edit radio-example

import React, { useRef } from 'react'
import {
  TabController,
  useControlledTabIndex,
} from 'react-tab-controller'

const MyRadioGroup = () => {
  return (
    <TabController>
      <RadioButton />
      <RadioButton />
      <RadioButton />
    </TabController>
  )
}

const RadioButton = ({ label }) => {
  const buttonRef = useRef(null)

  const { onKeyDown, tabIndex } = useControlledTabIndex(buttonRef, label)

  return (
    <div role="radio" tabIndex={tabIndex} onKeyDown={onKeyDown}>
      {label}
    </div>
  )
}
0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago