From 18a093fa294c9a1d204c843c9b6416415de75b82 Mon Sep 17 00:00:00 2001 From: Stefan Cameron Date: Mon, 26 Jun 2023 15:13:30 -0500 Subject: [PATCH] Add new getTabIndex() export (#1039) This is needed for https://github.com/focus-trap/focus-trap/pull/974 which will add support for positive tabindexes in focus-trap. Also updates the docs/typings to make them more consistent and fix some broken links. --- .changeset/lovely-seals-burn.md | 5 ++ README.md | 44 ++++++++++------ index.d.ts | 8 ++- src/index.js | 93 ++++++++++++++++++++++++--------- test/fixtures/fixtures.js | 1 + test/fixtures/tabindex.html | 28 ++++++++++ test/unit/node.test.js | 3 +- test/unit/unit.test.js | 38 +++++++++++++- 8 files changed, 175 insertions(+), 45 deletions(-) create mode 100644 .changeset/lovely-seals-burn.md create mode 100644 test/fixtures/tabindex.html diff --git a/.changeset/lovely-seals-burn.md b/.changeset/lovely-seals-burn.md new file mode 100644 index 00000000..e0b34b31 --- /dev/null +++ b/.changeset/lovely-seals-burn.md @@ -0,0 +1,5 @@ +--- +'tabbable': minor +--- + +Add new `getTabIndex()` API which enables Focus-trap to determine tab indexes in the same way as Tabbable when necessary (see [focus-trap#974](https://github.com/focus-trap/focus-trap/pull/974)). diff --git a/README.md b/README.md index c2c1931d..4cfa8d8f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Any of the above will _not_ be considered tabbable, though, if any of the follow - has a negative `tabindex` attribute - has a `disabled` attribute -- either the node itself _or an ancestor of it_ is hidden via `display: none` (*see ["Display check"](#display-check) below to modify this behavior) +- either the node itself _or an ancestor of it_ is hidden via `display: none` (*see ["Display check"](#displaycheck-option) below to modify this behavior) - has `visibility: hidden` style - is nested under a closed `
` element (with the exception of the first `` element) - is an `` element and a different radio in its group is `checked` @@ -70,17 +70,17 @@ npm install tabbable ```js import { tabbable } from 'tabbable'; -tabbable(rootNode, [options]); +tabbable(container, [options]); ``` -- `rootNode: Node` (**Required**) +- `container: Node` (**Required**) - `options`: - All the [common options](#common-options). - `includeContainer: boolean` (default: false) - - If set to `true`, `rootNode` will be included in the returned tabbable node array, if `rootNode` is tabbable. - - Note that whether this option is true or false, if the `rootNode` is [inert](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert), none of its children (deep) will be considered tabbable. + - If set to `true`, `container` will be included in the returned tabbable node array, if `container` is tabbable. + - Note that whether this option is true or false, if the `container` is [inert](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert), none of its children (deep) will be considered tabbable. -Returns an array of ordered tabbable nodes (i.e. in tab order) within the `rootNode`. +Returns an array of ordered tabbable nodes (i.e. in tab order) within the `container`. Summary of ordering principles: @@ -108,17 +108,17 @@ Returns a boolean indicating whether the provided node is considered tabbable. ```js import { focusable } from 'tabbable'; -focusable(rootNode, [options]); +focusable(container, [options]); ``` -- `rootNode: Node`: **Required** +- `container: Node`: **Required** - `options`: - All the [common options](#common-options). - `includeContainer: boolean` (default: false) - - If set to `true`, `rootNode` will be included in the returned focusable node array, if `rootNode` is focusable. - - Note that whether this option is true or false, if the `rootNode` is [inert](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert), none of its children (deep) will be considered focusable. + - If set to `true`, `container` will be included in the returned focusable node array, if `container` is focusable. + - Note that whether this option is true or false, if the `container` is [inert](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert), none of its children (deep) will be considered focusable. -Returns an array of focusable nodes within the `rootNode`, in DOM order. This will not match the order in which `tabbable()` returns nodes. +Returns an array of focusable nodes within the `container`, in DOM order. This will not match the order in which `tabbable()` returns nodes. ### isFocusable @@ -136,6 +136,20 @@ Returns a boolean indicating whether the provided node is considered _focusable_ > 💬 All tabbable elements are focusable, but not all focusable elements are tabbable. For example, elements with `tabindex="-1"` are focusable but not tabbable. Also note that if the node has an[inert](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert) ancestor, it will not be focusable. +### getTabIndex + +```js +import { getTabIndex } from 'tabbable'; + +getTabIndex(node); +``` + +- `node: Element` (**Required**) + +Returns a negative, 0, or positive number that expresses the node's tab index in the DOM, with exceptions made where there are browser inconsistencies related to `