React Reference ยท Props and Attributes
React tabIndex prop
How tabIndex works in React: 0 and -1, why positive values hurt accessibility, programmatic focus, and the roving tabindex pattern for toolbars.
tabIndex controls whether an element can receive keyboard focus and where it sits in the Tab order. It is the camelCase form of the HTML tabindex attribute. Links, buttons and form controls are focusable by default; a div is not.
Only three kinds of values are useful: 0 puts a normal element in the natural tab order, -1 makes it focusable by script (and by mouse click) but skips it when tabbing, and leaving the prop off keeps the browser default. Positive numbers reorder the page and almost always cause confusion.
<div tabIndex={0} role="button" onKeyDown={handleKey}>Custom control</div> <div tabIndex={-1} ref={headingRef}>Section to focus from code</div>
Try it
A roving tabindex keeps a group of related controls (a toolbar, tabs) as one Tab stop. Only the current item has tabIndex={0}; the arrow keys move that status.
Syntax
<element tabIndex={number} />
| Name | Description |
|---|---|
tabIndex={0} |
Focusable and part of the normal Tab order (document order). |
tabIndex={-1} |
Focusable with element.focus() or a click, but skipped by Tab. |
tabIndex={n} (n > 0) |
Jumps ahead of every 0 element in the order 1, 2, 3.... Avoid. |
Usage notes
- Use native elements first. A
<button>or<a href>is focusable and handles Enter/Space already. Reach fortabIndexonly for custom widgets and focus targets. - Casing matters.
tabindex(lowercase) in JSX triggers a warning; React expectstabIndex. - Pair
tabIndex={0}with keyboard handling. A focusablediv role="button"also needsonKeyDownfor Enter and Space. - Moving focus after navigation or a dialog opening is a good use of
tabIndex={-1}plusref.current.focus(), so screen-reader and keyboard users land in the right place. - Never remove the focus outline without giving a visible replacement.
inertandhiddenremove an entire subtree from the focus order; seeinert.