@wordpress/components
Version:
UI components for WordPress.
207 lines (206 loc) • 7.54 kB
JavaScript
import { focus } from "@wordpress/dom";
import { forwardRef, useCallback } from "@wordpress/element";
import { DOWN, END, HOME, LEFT, RIGHT, UP } from "@wordpress/keycodes";
import RovingTabIndexContainer from "./roving-tab-index.mjs";
import { jsx } from "react/jsx-runtime";
import TreeGridRow from "./row.mjs";
import TreeGridCell from "./cell.mjs";
import TreeGridItem from "./item.mjs";
// packages/components/src/tree-grid/index.tsx
/**
* Return focusables in a row element, excluding those from other branches
* nested within the row.
*
* @param rowElement The DOM element representing the row.
*
* @return The array of focusables in the row.
*/
function getRowFocusables(rowElement) {
return focus.focusable.find(rowElement, { sequential: true }).filter((focusable) => {
return focusable.closest("[role=\"row\"]") === rowElement;
});
}
/**
* Renders both a table and tbody element, used to create a tree hierarchy.
*
*/
function UnforwardedTreeGrid({ children, onExpandRow = () => {}, onCollapseRow = () => {}, onFocusRow = () => {}, applicationAriaLabel, ...props }, ref) {
const onKeyDown = useCallback((event) => {
const { keyCode, metaKey, ctrlKey, altKey } = event;
if (metaKey || ctrlKey || altKey || ![
UP,
DOWN,
LEFT,
RIGHT,
HOME,
END
].includes(keyCode)) return;
event.stopPropagation();
const { activeElement } = document;
const { currentTarget: treeGridElement } = event;
if (!activeElement || !treeGridElement.contains(activeElement)) return;
const activeRow = activeElement.closest("[role=\"row\"]");
if (!activeRow) return;
const focusablesInRow = getRowFocusables(activeRow);
const currentColumnIndex = focusablesInRow.indexOf(activeElement);
const canExpandCollapse = 0 === currentColumnIndex;
const cannotFocusNextColumn = canExpandCollapse && (activeRow.getAttribute("data-expanded") === "false" || activeRow.getAttribute("aria-expanded") === "false") && keyCode === RIGHT;
if ([LEFT, RIGHT].includes(keyCode)) {
let nextIndex;
if (keyCode === LEFT) nextIndex = Math.max(0, currentColumnIndex - 1);
else nextIndex = Math.min(currentColumnIndex + 1, focusablesInRow.length - 1);
if (canExpandCollapse) {
if (keyCode === LEFT) {
if (activeRow.getAttribute("data-expanded") === "true" || activeRow.getAttribute("aria-expanded") === "true") {
onCollapseRow(activeRow);
event.preventDefault();
return;
}
const level = Math.max(parseInt(activeRow?.getAttribute("aria-level") ?? "1", 10) - 1, 1);
const rows = Array.from(treeGridElement.querySelectorAll("[role=\"row\"]"));
let parentRow = activeRow;
const currentRowIndex = rows.indexOf(activeRow);
for (let i = currentRowIndex; i >= 0; i--) {
const ariaLevel = rows[i].getAttribute("aria-level");
if (ariaLevel !== null && parseInt(ariaLevel, 10) === level) {
parentRow = rows[i];
break;
}
}
getRowFocusables(parentRow)?.[0]?.focus();
}
if (keyCode === RIGHT) {
if (activeRow.getAttribute("data-expanded") === "false" || activeRow.getAttribute("aria-expanded") === "false") {
onExpandRow(activeRow);
event.preventDefault();
return;
}
const focusableItems = getRowFocusables(activeRow);
if (focusableItems.length > 0) focusableItems[nextIndex]?.focus();
}
event.preventDefault();
return;
}
if (cannotFocusNextColumn) return;
focusablesInRow[nextIndex].focus();
event.preventDefault();
} else if ([UP, DOWN].includes(keyCode)) {
const rows = Array.from(treeGridElement.querySelectorAll("[role=\"row\"]"));
const currentRowIndex = rows.indexOf(activeRow);
let nextRowIndex;
if (keyCode === UP) nextRowIndex = Math.max(0, currentRowIndex - 1);
else nextRowIndex = Math.min(currentRowIndex + 1, rows.length - 1);
if (nextRowIndex === currentRowIndex) {
event.preventDefault();
return;
}
const focusablesInNextRow = getRowFocusables(rows[nextRowIndex]);
if (!focusablesInNextRow || !focusablesInNextRow.length) {
event.preventDefault();
return;
}
focusablesInNextRow[Math.min(currentColumnIndex, focusablesInNextRow.length - 1)].focus();
onFocusRow(event, activeRow, rows[nextRowIndex]);
event.preventDefault();
} else if ([HOME, END].includes(keyCode)) {
const rows = Array.from(treeGridElement.querySelectorAll("[role=\"row\"]"));
const currentRowIndex = rows.indexOf(activeRow);
let nextRowIndex;
if (keyCode === HOME) nextRowIndex = 0;
else nextRowIndex = rows.length - 1;
if (nextRowIndex === currentRowIndex) {
event.preventDefault();
return;
}
const focusablesInNextRow = getRowFocusables(rows[nextRowIndex]);
if (!focusablesInNextRow || !focusablesInNextRow.length) {
event.preventDefault();
return;
}
focusablesInNextRow[Math.min(currentColumnIndex, focusablesInNextRow.length - 1)].focus();
onFocusRow(event, activeRow, rows[nextRowIndex]);
event.preventDefault();
}
}, [
onExpandRow,
onCollapseRow,
onFocusRow
]);
return /* @__PURE__ */ jsx(RovingTabIndexContainer, { children: /* @__PURE__ */ jsx("div", {
role: "application",
"aria-label": applicationAriaLabel,
children: /* @__PURE__ */ jsx("table", {
...props,
role: "treegrid",
onKeyDown,
ref,
children: /* @__PURE__ */ jsx("tbody", { children })
})
}) });
}
/**
* `TreeGrid` is used to create a tree hierarchy.
* It is not a visually styled component, but instead helps with adding
* keyboard navigation and roving tab index behaviors to tree grid structures.
*
* A tree grid is a hierarchical 2 dimensional UI component, for example it could be
* used to implement a file system browser.
*
* A tree grid allows the user to navigate using arrow keys.
* Up/down to navigate vertically across rows, and left/right to navigate horizontally
* between focusables in a row.
*
* The `TreeGrid` renders both a `table` and `tbody` element, and is intended to be used
* with `TreeGridRow` (`tr`) and `TreeGridCell` (`td`) to build out a grid.
*
* ```jsx
* function TreeMenu() {
* return (
* <TreeGrid>
* <TreeGridRow level={ 1 } positionInSet={ 1 } setSize={ 2 }>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onSelect } { ...props }>Select</Button>
* ) }
* </TreeGridCell>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onMove } { ...props }>Move</Button>
* ) }
* </TreeGridCell>
* </TreeGridRow>
* <TreeGridRow level={ 1 } positionInSet={ 2 } setSize={ 2 }>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onSelect } { ...props }>Select</Button>
* ) }
* </TreeGridCell>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onMove } { ...props }>Move</Button>
* ) }
* </TreeGridCell>
* </TreeGridRow>
* <TreeGridRow level={ 2 } positionInSet={ 1 } setSize={ 1 }>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onSelect } { ...props }>Select</Button>
* ) }
* </TreeGridCell>
* <TreeGridCell>
* { ( props ) => (
* <Button onClick={ onMove } { ...props }>Move</Button>
* ) }
* </TreeGridCell>
* </TreeGridRow>
* </TreeGrid>
* );
* }
* ```
*
* @see {@link https://www.w3.org/TR/wai-aria-practices/examples/treegrid/treegrid-1.html}
*/
const TreeGrid = forwardRef(UnforwardedTreeGrid);
TreeGrid.displayName = "TreeGrid";
export { TreeGrid, TreeGrid as default, TreeGridCell, TreeGridItem, TreeGridRow };
//# sourceMappingURL=index.mjs.map