diff --git a/.changeset/esm-only.md b/.changeset/esm-only.md
new file mode 100644
index 0000000000..012773b788
--- /dev/null
+++ b/.changeset/esm-only.md
@@ -0,0 +1,7 @@
+---
+'react-tabs': major
+---
+
+`react-tabs` now ships a single native ES module build. Import components from `react-tabs` and stylesheets from `react-tabs/style/`. Replace deep imports from `react-tabs/lib/` or `react-tabs/esm/` with imports from the package root.
+
+If your CommonJS app uses `require('react-tabs')`, use a Node version with synchronous ESM support, such as Node 22.12 or newer, or load the package with dynamic `import()`.
diff --git a/.changeset/hidden-inactive-panels.md b/.changeset/hidden-inactive-panels.md
new file mode 100644
index 0000000000..0dbe1f7dd3
--- /dev/null
+++ b/.changeset/hidden-inactive-panels.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': major
+---
+
+Inactive tab panels now use the HTML `hidden` attribute, so they stay hidden without a stylesheet. `forceRender` and `forceRenderTabPanel` still keep their contents mounted. See the [migration guide](https://github.com/reactjs/react-tabs/blob/main/MIGRATION.md#account-for-hidden-panels-in-exit-animations) for custom styles and exit animations.
diff --git a/.changeset/keep-disabled-tab-selection.md b/.changeset/keep-disabled-tab-selection.md
new file mode 100644
index 0000000000..0028fe3e7e
--- /dev/null
+++ b/.changeset/keep-disabled-tab-selection.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Keep the current selection and skip `onSelect` when Home or End is pressed and all tabs are disabled.
diff --git a/.changeset/keyed-tabs.md b/.changeset/keyed-tabs.md
new file mode 100644
index 0000000000..b9f49bfa7e
--- /dev/null
+++ b/.changeset/keyed-tabs.md
@@ -0,0 +1,24 @@
+---
+'react-tabs': major
+---
+
+Tabs and panels now pair by a shared `itemKey` instead of their positions. The key can be a string or a finite number. Give each item a stable key so selection survives reorder, insertion, and removal of another tab. Keyboard navigation still follows the tab list's rendered order.
+
+For uncontrolled tabs, replace `defaultIndex` with `defaultKey`. An explicit initial key is required, even if you previously relied on the first tab being selected automatically.
+
+```tsx
+
+
+ Overview
+ Billing
+
+ Overview content
+ Billing content
+
+```
+
+For controlled tabs, replace `selectedIndex={index}` with `selectedKey={key}`. Keep `onSelect`, but update handlers to receive keys instead of positions. For example, use `` and give the matching tab and panel the same key. TypeScript state can use the exported `TabKey` type.
+
+If an uncontrolled selection disappears, the group selects the first enabled tab with a matching panel and calls `onSelect` with an undefined event. Controlled callers choose their own replacement. Returning `false` still cancels mouse and keyboard selections.
+
+`TabsProps` is now a union. If your TypeScript wrapper used `interface CustomProps extends TabsProps`, use `type CustomProps = TabsProps & { … }` instead.
diff --git a/.changeset/numeric-tab-index.md b/.changeset/numeric-tab-index.md
new file mode 100644
index 0000000000..75cbea0955
--- /dev/null
+++ b/.changeset/numeric-tab-index.md
@@ -0,0 +1,7 @@
+---
+'react-tabs': major
+---
+
+`Tab` now uses React's numeric `tabIndex` type. Replace string values such as `Overview` with numbers such as `Overview`.
+
+When `tabIndex` is omitted, the selected tab still gets `0`. If there is no selection, the first enabled tab with a matching panel gets `0` so keyboard users can enter the group.
diff --git a/.changeset/react-server-components.md b/.changeset/react-server-components.md
new file mode 100644
index 0000000000..8430fc913f
--- /dev/null
+++ b/.changeset/react-server-components.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Mark the `react-tabs` entry point with `'use client'` so React Server Components can import it directly. Put event handlers such as `onSelect` in a Client Component.
diff --git a/.changeset/restore-tab-focus.md b/.changeset/restore-tab-focus.md
new file mode 100644
index 0000000000..14b1d05994
--- /dev/null
+++ b/.changeset/restore-tab-focus.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Restore keyboard focus to the replacement when a focused tab is removed and selection changes. `defaultFocus` now waits for a selected tab to become available, including when an invalid default key falls back to the first enabled tab.
diff --git a/.changeset/skip-unrelated-tab-events.md b/.changeset/skip-unrelated-tab-events.md
new file mode 100644
index 0000000000..e7c8364644
--- /dev/null
+++ b/.changeset/skip-unrelated-tab-events.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Skip tab collection scans for clicks and key presses outside the group's tabs, and for keys that the group doesn't handle. Consumer event handlers still receive these events.
diff --git a/.changeset/skip-unused-tab-diagnostics.md b/.changeset/skip-unused-tab-diagnostics.md
new file mode 100644
index 0000000000..266b0589ed
--- /dev/null
+++ b/.changeset/skip-unused-tab-diagnostics.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Skip collecting developer warnings when scanning tabs for interactions, focus, or production rendering.
diff --git a/.changeset/skip-unused-tab-fallback.md b/.changeset/skip-unused-tab-fallback.md
new file mode 100644
index 0000000000..2264084cf1
--- /dev/null
+++ b/.changeset/skip-unused-tab-fallback.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Avoid an extra render when the first enabled tab changes and the current selection still has a matching tab and panel.
diff --git a/.changeset/standard-keyboard-events.md b/.changeset/standard-keyboard-events.md
new file mode 100644
index 0000000000..b95c478109
--- /dev/null
+++ b/.changeset/standard-keyboard-events.md
@@ -0,0 +1,7 @@
+---
+'react-tabs': patch
+---
+
+Use the standard `KeyboardEvent.key` value for tab navigation and activation, including when a key has been remapped.
+
+This removes the fallback for legacy browsers that only report `keyCode`. Every browser version listed in the README’s compatibility table already supports `KeyboardEvent.key`, so the documented browser support stays the same. [Browser compatibility for `KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#browser_compatibility)
diff --git a/.changeset/tab-panel-class-defaults.md b/.changeset/tab-panel-class-defaults.md
new file mode 100644
index 0000000000..d94383b53c
--- /dev/null
+++ b/.changeset/tab-panel-class-defaults.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+Apply the default `TabPanel` classes when `className` or `selectedClassName` is explicitly `undefined`.
diff --git a/.changeset/tabs-event-handlers.md b/.changeset/tabs-event-handlers.md
new file mode 100644
index 0000000000..c659f5b6ef
--- /dev/null
+++ b/.changeset/tabs-event-handlers.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': patch
+---
+
+`Tabs` now calls the provided `onClick` and `onKeyDown` handlers before its own handlers. Calling `event.preventDefault()` skips the built-in tab handling for that event.
diff --git a/.changeset/tabs-item-context.md b/.changeset/tabs-item-context.md
new file mode 100644
index 0000000000..b1f489f29b
--- /dev/null
+++ b/.changeset/tabs-item-context.md
@@ -0,0 +1,39 @@
+---
+'react-tabs': major
+---
+
+Tab and panel state comes from React context. Custom wrappers render the built-in component and forward `itemKey`. Remove the old `tabsRole` assignment and use ordinary React component types in place of `ReactTabsFunctionComponent`.
+
+If your wrapper reads `selected` to apply a CSS class, move that class to the built-in `selectedClassName` prop.
+
+**Before**
+
+```jsx
+import { Tab } from 'react-tabs';
+
+function CustomTab({ selected, ...props }) {
+ return (
+
+ );
+}
+
+CustomTab.tabsRole = 'Tab';
+```
+
+**After**
+
+```tsx
+import { Tab } from 'react-tabs';
+import type { TabProps } from 'react-tabs';
+
+function CustomTab(props: TabProps) {
+ return (
+
+ );
+}
+```
+
+The built-in `Tab` applies `is-active` when selected. If your custom UI needs the selection in JavaScript, control `` with `selectedKey` and `onSelect`.
diff --git a/.changeset/tabs-rendered-direction.md b/.changeset/tabs-rendered-direction.md
new file mode 100644
index 0000000000..985596048a
--- /dev/null
+++ b/.changeset/tabs-rendered-direction.md
@@ -0,0 +1,7 @@
+---
+'react-tabs': major
+---
+
+Left and right arrow navigation now follows the tab group's rendered direction, including the HTML `dir` attribute and inherited direction.
+
+If your group needs to stay left-to-right on an RTL page, set `dir="ltr"` on `Tabs` and make sure any CSS `direction` rule agrees.
diff --git a/.changeset/typed-tabs-callbacks.md b/.changeset/typed-tabs-callbacks.md
new file mode 100644
index 0000000000..c70db65564
--- /dev/null
+++ b/.changeset/typed-tabs-callbacks.md
@@ -0,0 +1,7 @@
+---
+'react-tabs': major
+---
+
+Build `react-tabs` with TypeScript and generate its declarations from source.
+
+The callback types now match their runtime values. For user interactions, `onSelect` receives a React mouse or keyboard event. Use `event.nativeEvent` for the underlying DOM event. Automatic recovery in an uncontrolled group reports an undefined event. `domRef` receives the root `HTMLDivElement` or `null`, so typed callbacks must handle `null` on unmount.
diff --git a/.changeset/v7-migration-guide.md b/.changeset/v7-migration-guide.md
new file mode 100644
index 0000000000..a99d1b5d03
--- /dev/null
+++ b/.changeset/v7-migration-guide.md
@@ -0,0 +1,5 @@
+---
+'react-tabs': major
+---
+
+Upgrading from v6? See the [v6 to v7 migration guide](https://github.com/reactjs/react-tabs/blob/main/MIGRATION.md#v6-to-v7) for the breaking changes and migration examples.
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 667843702d..245a0ba907 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -58,4 +58,15 @@ jobs:
run: pnpm install --frozen-lockfile
- name: Lint
- run: pnpm lint
+ run: pnpm run lint
+
+ - name: Type check
+ run: pnpm run typecheck
+
+ - name: Build library
+ run: pnpm run build
+
+ - name: Check native Node entry points
+ run: |
+ node --input-type=module -e "import { Tabs } from 'react-tabs'; if (typeof Tabs !== 'function') process.exit(1)"
+ node --input-type=commonjs -e "if (typeof require('react-tabs').Tabs !== 'function') process.exit(1)"
diff --git a/MIGRATION.md b/MIGRATION.md
new file mode 100644
index 0000000000..26aebfd2f4
--- /dev/null
+++ b/MIGRATION.md
@@ -0,0 +1,216 @@
+# Migration guide
+
+## v6 to v7
+
+### Match each tab and panel with `itemKey`
+
+**What changed**
+
+v6 matched tabs and panels by render order. v7 matches them by an explicit `itemKey`, so panels can render in a different order from their tabs.
+
+**How to migrate**
+
+Give each `` and its `` the same `itemKey`. Use a unique key per pair within the group. Old indexes work as numeric keys for a fixed list. For a dynamic list, use a stable item ID so the key stays with the item through reordering or removal.
+
+```tsx
+import { Tab, TabList, TabPanel, Tabs } from 'react-tabs';
+
+function AccountTabs() {
+ return (
+
+
+ Overview
+ Billing
+
+ Account overview
+ Billing settings
+
+ );
+}
+```
+
+Keys can be nonempty strings or finite numbers. Match the type on both sides, since `1` and `'1'` are different keys. Keep the panel element mounted even if its contents load on demand.
+
+### Account for hidden panels in exit animations
+
+**What changed**
+
+In v6, CSS controlled whether an inactive panel was visible. Inactive panels now get the HTML `hidden` attribute as soon as selection changes, even with `forceRender` or `forceRenderTabPanel`. The default stylesheet no longer sets panel visibility.
+
+**How to migrate**
+
+Check custom panel styles that set `display` so they respect `[hidden]`. For example, apply `display: grid` only to `.my-panel:not([hidden])`.
+
+If you animate an outgoing panel, keep `forceRender` or `forceRenderTabPanel` enabled for its contents and override `display` on that panel's `[hidden]` element only while its exit animation runs. Mark the outgoing panel `inert` during that interval to keep it out of keyboard navigation and the accessibility tree. Remove the display override when the animation ends.
+
+### Replace `defaultIndex` with `defaultKey`
+
+**What changed**
+
+In v6, uncontrolled tabs used `defaultIndex` and selected the first tab when it was omitted. In v7, they require an explicit `defaultKey`.
+
+**How to migrate**
+
+Set `defaultKey` to the `itemKey` of the tab you want to open first. Add it even if you previously omitted `defaultIndex`.
+
+```diff
+-
++
+```
+
+### Replace `selectedIndex` with `selectedKey`
+
+**What changed**
+
+In v6, `selectedIndex` identified the controlled selection by position. In v7, `selectedKey` identifies it by `itemKey`.
+
+**How to migrate**
+
+Store the active `itemKey` in state, pass it as `selectedKey`, and update it in `onSelect`. If you remove the selected tab, set a replacement key yourself. The group shows no selection while the key is missing.
+
+```diff
+-
++
+```
+
+### Use keys in `onSelect`
+
+**What changed**
+
+The first two `onSelect` arguments changed from the next and previous numeric indexes to the next and previous `itemKey` values.
+
+**How to migrate**
+
+Update handlers that use the arguments as array indexes. For example, replace `tabs[nextIndex]` with `tabs.find((tab) => tab.id === nextKey)` if you use each item's `id` as its `itemKey`. Import `TabKey` from `react-tabs` for explicit TypeScript annotations.
+
+### Use `nativeEvent` when you need a DOM event
+
+**What changed**
+
+v6 declared the `onSelect` event as a DOM `Event`. v7 types user interactions as the React mouse or keyboard events the callback receives at runtime.
+
+**How to migrate**
+
+Let TypeScript infer the event type in your handler. If a helper expects a DOM `Event`, guard against `undefined` and pass `event.nativeEvent`.
+
+### Handle automatic selection when a tab is removed
+
+**What changed**
+
+In v6, removing a tab could shift the selection to another item at the same index. In v7, removing another tab preserves the selected key. If you remove the selected tab from an uncontrolled group, v7 selects the first enabled tab with a matching panel and calls `onSelect` with `undefined` as the event.
+
+**How to migrate**
+
+Check that the event exists before reading it in `onSelect`. Returning `false` still cancels mouse and keyboard selections. The callback reports an automatic replacement after it has already been selected.
+
+### Match horizontal arrow navigation to the tab group's direction
+
+**What changed**
+
+In v6, left and right arrow keys used the `direction` prop and defaulted to LTR, even when the tab group displayed right-to-left. In v7, the keys follow the group's rendered direction, which can come from HTML or CSS. A group that inherits RTL from the page now also uses RTL arrow navigation.
+
+**How to migrate**
+
+If horizontal navigation should follow the page, let the group inherit its direction. If the group should stay left-to-right on an RTL page, set `dir="ltr"` on `` and make sure any CSS `direction` rule on the group also uses `ltr`.
+
+### Pass a number for the HTML `tabindex` attribute
+
+**What changed**
+
+`Tab`'s `tabIndex` prop changed from a string in v6 to a number in v7. It sets the HTML `tabindex` attribute, which controls keyboard tab order.
+
+**How to migrate**
+
+Replace string values with numbers. Keep the same numeric value to preserve the tab order.
+
+```diff
+- tabIndex="0"
++ tabIndex={0}
+```
+
+### Handle `null` in `domRef`
+
+**What changed**
+
+v6 typed the `domRef` argument as an optional `HTMLElement`. v7 types it as `HTMLDivElement | null`, including the `null` React passes on unmount.
+
+**How to migrate**
+
+Check for `null` before using the element. If you annotate the callback parameter, use `HTMLDivElement | null`.
+
+### Remove `tabsRole` assignments
+
+**What changed**
+
+v6 used the `tabsRole` static property to recognize custom wrappers. v7 removed it.
+
+**How to migrate**
+
+Delete assignments such as `CustomTab.tabsRole = 'Tab'`. Render the built-in component inside your wrapper and forward the props it needs, including `itemKey` for tabs and panels.
+
+### Replace `ReactTabsFunctionComponent`
+
+**What changed**
+
+v7 removed the `ReactTabsFunctionComponent` type, which v6 used to describe components with a `tabsRole` static.
+
+**How to migrate**
+
+Remove the type import and annotate your wrapper's props with the type of the component it renders.
+
+```tsx
+import { Tab } from 'react-tabs';
+import type { TabProps } from 'react-tabs';
+
+function CustomTab(props: TabProps) {
+ return ;
+}
+```
+
+### Replace reads of private wrapper props
+
+**What changed**
+
+v6 passed private props such as `selected`, `focus`, and `tabRef` to custom wrappers. In v7, the built-in components get their state from context.
+
+**How to migrate**
+
+Remove reads of those private props. Use `selectedClassName` for styling and let the built-in `Tab` handle focus. If custom logic needs to know which tab is active, read the key from your controlled `Tabs` state.
+
+### Use a type intersection to extend `TabsProps`
+
+**What changed**
+
+`TabsProps` changed from an interface to a union of controlled and uncontrolled props.
+
+**How to migrate**
+
+Replace `interface TrackedTabsProps extends TabsProps` with a type alias that uses `&` to add your own props.
+
+```tsx
+import type { TabsProps } from 'react-tabs';
+
+type TrackedTabsProps = TabsProps & { analyticsName: string };
+```
+
+### Switch to the ESM package entry point
+
+**What changed**
+
+v6 shipped a CommonJS build and an ESM build for bundlers. v7 ships a single native ES module build and declares its public entry points with `exports`.
+
+**How to migrate**
+
+Import components from `react-tabs`. If you used a deep import from `react-tabs/lib/` or `react-tabs/esm/`, switch to the package root. The documented stylesheet paths, such as `react-tabs/style/react-tabs.css`, still work.
+
+If your CommonJS app calls `require('react-tabs')`, use a Node version with synchronous ESM support, such as Node 22.12 or newer. On older versions, load the package with dynamic `import('react-tabs')`.
+
+### Import types from the package root
+
+**What changed**
+
+v6 shipped an `index.d.ts` at the package root. v7 generates its declarations alongside the ESM build and points TypeScript to them through the package metadata.
+
+**How to migrate**
+
+If you imported `react-tabs/index.d.ts` directly, change the import to `import type { TabsProps } from 'react-tabs'`.
diff --git a/README.md b/README.md
index e5f5076a5c..c49aa80fd5 100644
--- a/README.md
+++ b/README.md
@@ -25,23 +25,57 @@ or
npm install --save react-tabs
```
+## Compatibility (December 2026)
+
+`react-tabs` supports React 18 and 19.
+
+### Browsers
+
+`react-tabs` targets the current and previous stable releases of Chrome, Edge, Firefox, and Safari on desktop, Chrome on Android, and Safari on iOS. Firefox ESR is also supported.
+
+The npm package ships ES2020 JavaScript as native ESM. The table shows the earliest versions that can parse the optional chaining (`?.`) and nullish coalescing (`??`) used in the published code. [Optional chaining support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#browser_compatibility) · [Nullish coalescing support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#browser_compatibility)
+
+| Browser | Earliest version that parses the published syntax | Released |
+| ----------------------------- | ------------------------------------------------: | -------------------------------------------------------------------------------------------------------------- |
+| Chrome on desktop and Android | 80 | [February 2020](https://chromereleases.googleblog.com/2020/02/stable-channel-update-for-desktop.html) |
+| Edge | 80 | [February 2020](https://learn.microsoft.com/en-us/deployedge/microsoft-edge-relnotes-security#february-7-2020) |
+| Firefox | 74 | [March 2020](https://www.firefox.com/en-US/firefox/74.0/releasenotes/) |
+| Safari on macOS | 13.1 | [March 2020](https://developer.apple.com/documentation/safari-release-notes) |
+| Safari on iOS | 13.4 | [March 2020](https://support.apple.com/en-ca/103828) |
+
+These browser versions are roughly seven years old as of December 2026.
+
+The library also uses `Element.closest()` for tab navigation and `MutationObserver` to track changes to tabs and panels. Both are available in the versions above, and the default CSS adds no newer browser requirement. [Element.closest() support](https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#browser_compatibility) · [MutationObserver support](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#browser_compatibility)
+
+These versions describe the package's feature requirements. Your React renderer and application build can set a higher minimum. A browser build tool also needs to resolve the package's React and `clsx` imports and handle `process.env.NODE_ENV`.
+
+### Server-side rendering
+
+Use the latest patch of Node.js **22.x**, **24.x**, or **26.x**. These are the supported [Node.js LTS release lines](https://nodejs.org/en/about/previous-releases) as of December 2026.
+
+Load the package with `import`. CommonJS servers can use `require('react-tabs')` on Node.js 22.12 and later, which can load [synchronous ES modules](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require).
+
+In frameworks with React Server Components, such as the Next.js App Router, you can import `react-tabs` from a Server Component. Define event handlers such as `onSelect` in a Client Component that renders the tabs.
+
## Basic Example
+`react-tabs` ships as native ESM. Import components from `react-tabs` and stylesheets from `react-tabs/style/`. See the [migration guide](https://github.com/reactjs/react-tabs/blob/main/MIGRATION.md#switch-to-the-esm-package-entry-point) for CommonJS consumers.
+
```js
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
export default () => (
-
+
- Title 1
- Title 2
+ Overview
+ Details
-
+
Any content 1
-
+
Any content 2
@@ -52,12 +86,14 @@ export default () => (
## Components
-react-tabs consists of 4 components which all need to be used together.
+Each group uses one `TabList`. Every `Tab` in the list needs a `TabPanel` with the same `itemKey`. Panels can be rendered in a different order from the tabs.
### <Tabs />
If you specify additional props on the `` component they will be forwarded to the rendered ``.
+`onClick` and `onKeyDown` run before the built-in tab handlers. Call `event.preventDefault()` to skip the built-in handling for that event.
+
#### className: `string | Array | { [string]: boolean }`
> default: `"react-tabs"`
@@ -72,19 +108,13 @@ Provide a custom class name for the outer `` of the tabs.
If set to `true` the tabs will be focused on initial render. This allows immediate use of keyboard keys to switch tabs after the first render.
-#### defaultIndex: `number`
-
-> default: `0`
+#### defaultKey: `TabKey`
-This allows changing the tab that should be open on initial render. This is a zero-based index, so first tab is `0`, second tab is `1`, ...
+Required in uncontrolled mode. Use the key of the tab to select on the initial render. Set either `defaultKey` or `selectedKey` for each group.
-> This can only be used in uncontrolled mode when react-tabs handles the current selected tab internally and for this reason cannot be used together with `selectedIndex`. See [here](#controlled-vs-uncontrolled-mode) for more info on modes.
+#### direction: `"ltr" | "rtl"`
-#### direction: `string`
-
-> default: `"ltr"`
-
-Provide the direction of the component, can be either rtl or ltr.
+Sets the tab group's HTML direction when the `dir` prop is omitted. When both props are omitted, the group inherits its direction from the page. Left and right arrow navigation follows the group's rendered direction, including changes made through CSS.
#### disabledTabClassName: `string`
@@ -100,7 +130,7 @@ Provide a custom class name for disabled tabs.
Disable up & down arrow keys to change tabs.
-#### domRef: `(node: ?HTMLElement) => void`
+#### domRef: `(node: HTMLDivElement | null) => void`
> default: `null`
@@ -122,29 +152,19 @@ By default the tab that is clicked will also be focused in the DOM. If set to `f
> default: `false`
-By default only the current active tab will be rendered to DOM. If set to `true` all tabs will be rendered to the DOM always.
+Panel elements stay mounted. By default only the selected panel renders its contents. Set this to `true` to keep all panel contents mounted, including in hidden panels.
> This can also be enabled for each individual `` component with its prop `forceRender`.
-#### onSelect: `(index: number, lastIndex: number, event: Event) => ?boolean`
-
-> default: `undefined`
+#### onSelect: `(nextKey: TabKey, previousKey: TabKey, event: TabEvent | undefined) => boolean | void`
-This event handler is called every time a tab is about to change. It will be called with the `index` that it will be changed to, the `lastIndex` which was selected before and the underlying `event` which is usually either a `keydown` or `click` event. When `index` and `lastIndex` are equal it means the user clicked on the currently active tab.
+Called before a mouse or keyboard selection, including when a user activates the current tab. Return `false` to cancel the selection. The event is a React mouse or keyboard event.
-The callback can optionally return `false` to cancel the change to the new tab.
+If an uncontrolled group loses the selected tab, the first enabled tab with a matching panel becomes selected. The callback fires once after that selection is applied, with `event` set to `undefined`. The return value has no effect on automatic recovery. If no replacement is available, the group waits until one appears.
-> Returning `false` when the change to the new tab should be canceled is also important in controlled mode, as react-tabs still internally handles the focus of the tabs.
+#### selectedKey: `TabKey`
-> In controlled mode the `onSelect` handler is a required prop.
-
-#### selectedIndex: `number`
-
-> default: `null`
-
-Set the currently selected tab. This is a zero-based index, so first tab is `0`, second tab is `1`, ...
-
-This enables controlled mode, which also requires `onSelect` to be set. See [here](#controlled-vs-uncontrolled-mode) for more info on modes.
+Required in controlled mode along with `onSelect`. Set this to the key of the selected tab. If the key has no matching pair, the group shows no selection and gives the first enabled pair the default keyboard entry point.
#### selectedTabClassName: `string`
@@ -178,6 +198,10 @@ Provide a custom class name for the `
`.
If you specify additional props on the `` component they will be forwarded to the rendered ``.
+#### itemKey: `TabKey`
+
+Required. Use a nonempty string or finite number that identifies this tab within its group. Give the corresponding panel the same key. For dynamic lists, keep the key attached to the item when it moves. The number `1` and the string `'1'` are distinct keys.
+
#### className: `string | Array | { [string]: boolean }`
> default: `"react-tabs__tab"`
@@ -208,16 +232,22 @@ Provide a custom class name for the active tab.
> This option can also be set for all `` components with the prop `selectedTabClassName` on ``.
-#### tabIndex: `string`
+#### tabIndex: `number`
-> default: if selected `"0"` otherwise `null`
+> default: `0` for the selected tab. If there is no selection, the first enabled tab with a matching panel gets `0`. Other tabs omit the attribute.
-Overrides the tabIndex to enabled tabbing between tabs.
+Overrides the tab's position in the keyboard tab order. Use `tabIndex={0}` to include a tab in the tab order or `tabIndex={-1}` to leave it out while keeping it programmatically focusable.
### <TabPanel />
If you specify additional props on the `` component they will be forwarded to the rendered ``.
+Inactive panels get the HTML `hidden` attribute and are hidden without the default stylesheet. See the [migration guide](https://github.com/reactjs/react-tabs/blob/main/MIGRATION.md#account-for-hidden-panels-in-exit-animations) if you animate panels when they become inactive.
+
+#### itemKey: `TabKey`
+
+Required. Use the same key as exactly one `Tab` in the same group. Keep the panel element mounted when its contents load on demand so the tab's ARIA link has a target.
+
#### className: `string | Array | { [string]: boolean }`
> default: `"react-tabs__tab-panel"`
@@ -230,7 +260,7 @@ Provide a custom class name for the `` containing the tab content.
> default: `false`
-By default the tab content will only be rendered when the tab is active. If set to `true` the tab will also be rendered if inactive.
+By default the panel renders its contents when selected. Set this to `true` to keep the contents mounted while inactive.
> This can also be enabled for all `` components with the prop `forceRenderTabPanel` on ``.
@@ -244,48 +274,32 @@ Provide a custom class name for the active tab panel.
## Controlled vs Uncontrolled mode
-React tabs has two different modes it can operate in, which change the way how much you need to take care about the state yourself.
-
### Uncontrolled mode
-This is the default mode of react-tabs and makes the react-tabs components handle its state internally. You can change the starting tab with `defaultIndex` and you can listen for changes with `onSelect`.
-
-In this mode you cannot force a tab change during runtime.
-
-```js
- console.log(index)}>
-
- Title 1
- Title 2
-
-
-
-
-```
+Set `defaultKey` to the initial selection. The group manages selection after that. You can listen for changes with `onSelect`. See the [basic example](#basic-example).
### Controlled mode
-This mode has to be enabled by supplying `selectedIndex` to the `` component.
+Set `selectedKey` and update it from `onSelect`. Use controlled mode when other parts of your app need to set the selection, such as routing or removing the currently selected item.
-In this mode react-tabs does not handle any tab selection state internally and leaves all the state management up to the outer application.
+```jsx
+import { useState } from 'react';
+import { Tab, TabList, TabPanel, Tabs } from 'react-tabs';
-This mode also enforces you to set a handler for `onSelect`. `defaultIndex` does not have any effect and will therefore throw an error.
-
-```js
-const App = () => {
- const [tabIndex, setTabIndex] = useState(0);
+function AccountTabs() {
+ const [selectedKey, setSelectedKey] = useState('overview');
return (
- setTabIndex(index)}>
+
- Title 1
- Title 2
+ Overview
+ Billing
-
-
+ Account overview
+ Billing settings
);
-};
+}
```
## Styling
@@ -326,47 +340,19 @@ You can also always just simply copy the default style to your own css/scss/less
### Custom Components
-#### Set `tabsRole`
-
-In case you want to create your own component wrapping the ones that the library provides, you have to set its `tabsRole`. This value is used inside react-tabs to check the role of a component inside ``.
-
-Possible values for tabsRole are:
-
-- Tab
-- TabPanel
-- TabList
-- Tabs
-
-#### Pass through properties
-
-Note: Because of how react-tabs works internally (it uses cloning to opaquely control various parts of the tab state), you need to pass any incoming props to the component you're wrapping. The easiest way to do this is to use the rest and spread operators, e.g. see `{...otherProps}` below.
+Render the built-in component in your wrapper, forwarding `itemKey` and the other public props your wrapper accepts.
```tsx
-import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
-import type { ReactTabsFunctionComponent, TabProps } from 'react-tabs';
-
-// All custom elements should pass through other props
-const CustomTab: ReactTabsFunctionComponent = ({
- children,
- ...otherProps
-}) => (
-
-
Husband of Marge; father of Bart, Lisa, and Maggie.
-
+
Wife of Homer; mother of Bart, Lisa, and Maggie.
-
+
Oldest child and only son of Homer and Marge; brother of Lisa and Maggie.
-
+
Middle child and eldest daughter of Homer and Marge; sister of Bart and Maggie.
-
+
Youngest child and daughter of Homer and Marge; sister of Bart and Lisa.
-
-
+
+
- Philip J. Fry
- Turanga Leela
- Bender Bending Rodriguez
- Amy Wong
- Professor Hubert J. Farnsworth
- Doctor John Zoidberg
+ Philip J. Fry
+ Turanga Leela
+ Bender Bending Rodriguez
+ Amy Wong
+ Professor Hubert J. Farnsworth
+ Doctor John Zoidberg
-
+
Protagonist, from the 20th Century. Delivery boy. Many times great-uncle to Professor Hubert Farnsworth. Suitor of Leela.
-
+
Mutant cyclops. Captain of the Planet Express Ship. Love interest of Fry.
-
+
A kleptomaniacal, lazy, cigar-smoking, heavy-drinking robot who is Fry's best friend. Built in Tijuana, Mexico, he is the Planet Express Ship's cook.
-
+
Chinese-Martian intern at Planet Express. Fonfon Ru of Kif Kroker.
-
+
Many times great-nephew of Fry. CEO and owner of Planet Express delivery company. Tenured professor of Mars University.
-
+
Alien from Decapod 10. Planet Express' staff doctor and steward. Has a medical degree and Ph.D in art history.
Mario (Japanese: マリオ Hepburn: Mario, [ma.ɾʲi.o]) (English:
/ˈmɑːrioʊ/; Italian: [ˈmaːrjo]) is a fictional character in the Mario video
@@ -28,7 +28,7 @@ const code = `const Component = (
-
+
Luigi (Japanese: ルイージ Hepburn: Ruīji, [ɾɯ.iː.dʑi̥]) (English: /luˈiːdʒi/;
Italian: [luˈiːdʒi]) is a fictional character featured in video games and related media
@@ -43,7 +43,7 @@ const code = `const Component = (
-
+
Princess Peach (Japanese: ピーチ姫 Hepburn: Pīchi-hime, [piː.tɕi̥ çi̥.me])
is a character in Nintendo's Mario franchise. Originally created by Shigeru Miyamoto,
@@ -59,7 +59,7 @@ const code = `const Component = (
-
+
Yoshi (ヨッシー Yosshī, [joɕ.ɕiː]) (English: /ˈjoʊʃi/ or /ˈjɒʃi/), once
romanized as Yossy, is a fictional anthropomorphic dinosaur who appears in
@@ -78,7 +78,7 @@ const code = `const Component = (
,
);
@@ -417,11 +644,13 @@ describe('', () => {
});
test('should pass through custom properties', () => {
- expectToMatchSnapshot();
+ expectToMatchSnapshot(
+ ,
+ );
});
test('should not add known props to dom', () => {
- expectToMatchSnapshot();
+ expectToMatchSnapshot();
});
test('should cancel if event handler returns false', async () => {
@@ -496,47 +725,47 @@ describe('', () => {
test('should allow for higher order components', () => {
expectToMatchSnapshot(
-
+
- Foo
- Bar
+ Foo
+ Bar
- Foo
- Bar
+ Foo
+ Bar,
);
});
test('should allow string children', () => {
expectToMatchSnapshot(
-
+
Foo
Foo
- Foo
+ Foo
Foo
- Bar
+ Bar
Foo
- Bar
- Foo
+ Bar
+ Foo
Foo
,
);
});
test('should focus the initially selected uncontrolled tab', () => {
- render(createTabs({ defaultFocus: true, defaultIndex: 1 }));
+ render(createTabs({ defaultFocus: true, defaultKey: 1 }));
expect(screen.getByTestId('tab2')).toHaveFocus();
});
test('should focus the initially selected controlled tab only on mount', () => {
const onSelect = vi.fn();
- const renderTabs = (selectedIndex, defaultFocus) => (
+ const renderTabs = (selectedKey, defaultFocus) => (
<>
- {createTabs({ defaultFocus, selectedIndex, onSelect })}
+ {createTabs({ defaultFocus, selectedKey, onSelect })}
>
);
const { rerender } = render(renderTabs(1, true));
@@ -621,16 +850,16 @@ describe('', () => {
});
test('should retain tab focus when controlled tabs are inside a fragment', () => {
- const renderTabs = (selectedIndex) => (
- {}}>
+ const renderTabs = (selectedKey) => (
+ {}}>
<>
- First
- Second
+ First
+ Second
>
- First panel
- Second panel
+ First panel
+ Second panel
);
const { rerender } = render(renderTabs(0));
@@ -642,18 +871,18 @@ describe('', () => {
});
test('should preserve outside focus when controlled tabs are inside a fragment', () => {
- const renderTabs = (selectedIndex) => (
+ const renderTabs = (selectedKey) => (
<>
- {}}>
+ {}}>
<>
- First
- Second
+ First
+ Second
>
- First panel
- Second panel
+ First panel
+ Second panel
>
);
@@ -709,16 +938,24 @@ describe('', () => {
});
test('should render first tab once tabs are available', () => {
- const { rerender } = render();
+ const { rerender } = render();
rerender(
-
+
- Tab1
- Tab2
+
+ Tab1
+
+
+ Tab2
+
- Hello Tab1
- Hello Tab2
+
+ Hello Tab1
+
+
+ Hello Tab2
+ ,
);
diff --git a/src/components/__tests__/__snapshots__/Tab-test.jsx.snap b/src/components/__tests__/__snapshots__/Tab-test.jsx.snap
index 56ffb188b2..0e5e088801 100644
--- a/src/components/__tests__/__snapshots__/Tab-test.jsx.snap
+++ b/src/components/__tests__/__snapshots__/Tab-test.jsx.snap
@@ -2,12 +2,10 @@
exports[` > override the tabIndex if it was provided 1`] = `
@@ -17,97 +15,83 @@ exports[` > override the tabIndex if it was provided 1`] = `
exports[` > should accept className 1`] = `
`;
exports[` > should allow to be wrapped in higher-order-component 1`] = `
`;
exports[` > should have sane defaults 1`] = `
`;
exports[` > should not allow overriding all default properties 1`] = `
`;
exports[` > should pass through custom properties 1`] = `
`;
exports[` > should support being disabled 1`] = `
`;
exports[` > should support being disabled with custom class name 1`] = `
`;
exports[` > should support being selected 1`] = `
@@ -117,12 +101,12 @@ exports[` > should support being selected 1`] = `
exports[` > should support being selected with custom class 1`] = `
@@ -490,44 +429,48 @@ exports[` > should allow for higher order components 1`] = `
>
Foo
Bar
Foo
@@ -541,16 +484,17 @@ exports[` > should allow string children 1`] = `
Foo
Foo
@@ -558,12 +502,12 @@ exports[` > should allow string children 1`] = `
Foo
Bar
@@ -571,17 +515,20 @@ exports[` > should allow string children 1`] = `
Foo
Bar
Foo
@@ -621,25 +568,26 @@ exports[` > validation > should allow other DOM nodes 1`] = `
>
@@ -654,15 +602,18 @@ exports[` > validation > should allow other DOM nodes 1`] = `
class="tab-panels"
>
@@ -675,45 +626,49 @@ exports[` > validation > should allow random order for elements 1`] = `
data-rttabs="true"
>
Hello Foo
Foo
Bar
Hello Bar
@@ -728,6 +683,7 @@ exports[` > validation > should be okay with rendering just TabList 1`]
>