@wordpress/components
Version:
UI components for WordPress.
256 lines (255 loc) • 9.66 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_dom = require("@wordpress/dom");
let _wordpress_element = require("@wordpress/element");
let _wordpress_keycodes = require("@wordpress/keycodes");
let __roving_tab_index_tsx = require("./roving-tab-index.cjs");
__roving_tab_index_tsx = __toESM(__roving_tab_index_tsx);
let react_jsx_runtime = require("react/jsx-runtime");
let __row_tsx = require("./row.cjs");
__row_tsx = __toESM(__row_tsx);
let __cell_tsx = require("./cell.cjs");
__cell_tsx = __toESM(__cell_tsx);
let __item_tsx = require("./item.cjs");
__item_tsx = __toESM(__item_tsx);
// 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 _wordpress_dom.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 = (0, _wordpress_element.useCallback)((event) => {
const { keyCode, metaKey, ctrlKey, altKey } = event;
if (metaKey || ctrlKey || altKey || ![
_wordpress_keycodes.UP,
_wordpress_keycodes.DOWN,
_wordpress_keycodes.LEFT,
_wordpress_keycodes.RIGHT,
_wordpress_keycodes.HOME,
_wordpress_keycodes.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 === _wordpress_keycodes.RIGHT;
if ([_wordpress_keycodes.LEFT, _wordpress_keycodes.RIGHT].includes(keyCode)) {
let nextIndex;
if (keyCode === _wordpress_keycodes.LEFT) nextIndex = Math.max(0, currentColumnIndex - 1);
else nextIndex = Math.min(currentColumnIndex + 1, focusablesInRow.length - 1);
if (canExpandCollapse) {
if (keyCode === _wordpress_keycodes.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 === _wordpress_keycodes.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 ([_wordpress_keycodes.UP, _wordpress_keycodes.DOWN].includes(keyCode)) {
const rows = Array.from(treeGridElement.querySelectorAll("[role=\"row\"]"));
const currentRowIndex = rows.indexOf(activeRow);
let nextRowIndex;
if (keyCode === _wordpress_keycodes.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 ([_wordpress_keycodes.HOME, _wordpress_keycodes.END].includes(keyCode)) {
const rows = Array.from(treeGridElement.querySelectorAll("[role=\"row\"]"));
const currentRowIndex = rows.indexOf(activeRow);
let nextRowIndex;
if (keyCode === _wordpress_keycodes.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__ */ (0, react_jsx_runtime.jsx)(__roving_tab_index_tsx.default, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
role: "application",
"aria-label": applicationAriaLabel,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("table", {
...props,
role: "treegrid",
onKeyDown,
ref,
children: /* @__PURE__ */ (0, react_jsx_runtime.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 = (0, _wordpress_element.forwardRef)(UnforwardedTreeGrid);
TreeGrid.displayName = "TreeGrid";
exports.TreeGrid = TreeGrid;
exports.default = TreeGrid;
Object.defineProperty(exports, "TreeGridCell", {
enumerable: true,
get: function() {
return __cell_tsx.default;
}
});
Object.defineProperty(exports, "TreeGridItem", {
enumerable: true,
get: function() {
return __item_tsx.default;
}
});
Object.defineProperty(exports, "TreeGridRow", {
enumerable: true,
get: function() {
return __row_tsx.default;
}
});
//# sourceMappingURL=index.cjs.map