Tabbable
Maintain a consistent tab order for tabbable elements.
Tabbable
This is a void element.
This is a void element.
if (true) {// <- Place cursor at start of line and press tab}
query
option.This is a void element.
Installation
npm install @udecode/plate-tabbable
Usage
import { createTabbablePlugin } from '@udecode/plate-tabbable';
const plugins = [
// ...otherPlugins,
createTabbablePlugin(),
];
Conflicts with other plugins
The Tabbable plugin may cause issues with other plugins that handle the Tab
key, such as:
- Lists
- Code blocks
- Indent plugin
Use the query
option to disable the Tabbable plugin when the Tab
key should be handled by another plugin:
query: (editor) => {
const inList = findNode(editor, { match: { type: ELEMENT_LI } });
const inCodeBlock = findNode(editor, { match: { type: ELEMENT_CODE_BLOCK } });
return !inList && !inCodeBlock;
},
Alternatively, if you're using the Indent plugin, you can enable the Tabbable plugin only when a specific type of node is selected, such as voids:
query: (editor) => !!findNode(editor, {
match: (node) => isVoid(editor, node),
}),
Non-void Slate nodes
One TabbableEntry
will be created for each tabbable DOM element in the editor, as determined using the tabbable
NPM package. The list of tabbables is then filtered using isTabbable
.
By default, isTabbable
only returns true for entries inside void Slate nodes. You can override isTabbable
to add support for DOM elements contained in other types of Slate node.
// Enable tabbable DOM elements inside CUSTOM_ELEMENT
isTabbable: (editor, tabbableEntry) => (
tabbableEntry.slateNode.type === CUSTOM_ELEMENT ||
isVoid(editor, tabbableEntry.slateNode)
),
DOM elements outside the editor
In some circumstances, you may want to allow users to tab from the editor to a DOM element rendered outside the editor, such as an interactive popover.
To do this, override insertTabbableEntries
to return an array of TabbableEntry
objects, one for each DOM element outside the editor that you want to include in the tabbable list. The slateNode
and path
of the TabbableEntry
should refer to the Slate node the user's cursor will be inside when the DOM element should be tabbable to. For example, if the DOM element appears when a link is selected, the slateNode
and path
should be that of the link.
Set the globalEventListener
option to true
to make sure the Tabbable plugin is able to return the user's focus to the editor.
// Add buttons inside .my-popover to the list of tabbables
globalEventListener: true,
insertTabbableEntries: (editor) => {
const [selectedNode, selectedNodePath] = getNodeEntry(editor, editor.selection);
return [
...document.querySelectorAll('.my-popover > button'),
].map((domNode) => ({
domNode,
slateNode: selectedNode,
path: selectedNodePath,
}));
},
API
createTabbablePlugin
Options
- Default:
() => true
- Default:
false
- Default:
() => []
- Default:
(editor, tabbableEntry) => isVoid(editor, tabbableEntry.slateNode)
Dynamically enables or disables the plugin. Returns true
by default.
Determines if the plugin adds its event listener to the document instead of the editor, which allows it to capture events from outside the editor.
Adds additional tabbable entries to the list of tabbable elements. Useful for adding tabbables that are not contained within the editor. It ignores isTabbable
.
Determines whether an element should be included in the tabbable list. Returns true
if the tabbableEntry's slateNode is void.
TabbableEntry
Defines the properties of a tabbable entry.
Attributes
The HTML element that represents the tabbable entry.
The corresponding Slate node of the tabbable entry.
The path to the Slate node in the Slate document.