UNPKG

@wordpress/dom

Version:
99 lines (98 loc) 3.44 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var tabbable_exports = {}; __export(tabbable_exports, { find: () => find, findNext: () => findNext, findPrevious: () => findPrevious, isTabbableIndex: () => isTabbableIndex }); module.exports = __toCommonJS(tabbable_exports); var import_focusable = require("./focusable"); function getTabIndex(element) { const tabIndex = element.getAttribute("tabindex"); return tabIndex === null ? 0 : parseInt(tabIndex, 10); } function isTabbableIndex(element) { return getTabIndex(element) !== -1; } function createStatefulCollapseRadioGroup() { const CHOSEN_RADIO_BY_NAME = {}; return function collapseRadioGroup(result, element) { const { nodeName, type, checked, name } = element; if (nodeName !== "INPUT" || type !== "radio" || !name) { return result.concat(element); } const hasChosen = CHOSEN_RADIO_BY_NAME.hasOwnProperty(name); const isChosen = checked || !hasChosen; if (!isChosen) { return result; } if (hasChosen) { const hadChosenElement = CHOSEN_RADIO_BY_NAME[name]; result = result.filter((e) => e !== hadChosenElement); } CHOSEN_RADIO_BY_NAME[name] = element; return result.concat(element); }; } function mapElementToObjectTabbable(element, index) { return { element, index }; } function mapObjectTabbableToElement(object) { return object.element; } function compareObjectTabbables(a, b) { const aTabIndex = getTabIndex(a.element); const bTabIndex = getTabIndex(b.element); if (aTabIndex === bTabIndex) { return a.index - b.index; } return aTabIndex - bTabIndex; } function filterTabbable(focusables) { return focusables.filter(isTabbableIndex).map(mapElementToObjectTabbable).sort(compareObjectTabbables).map(mapObjectTabbableToElement).reduce(createStatefulCollapseRadioGroup(), []); } function find(context) { return filterTabbable((0, import_focusable.find)(context)); } function findPrevious(element) { return filterTabbable((0, import_focusable.find)(element.ownerDocument.body)).reverse().find( (focusable) => ( // eslint-disable-next-line no-bitwise element.compareDocumentPosition(focusable) & element.DOCUMENT_POSITION_PRECEDING ) ); } function findNext(element) { return filterTabbable((0, import_focusable.find)(element.ownerDocument.body)).find( (focusable) => ( // eslint-disable-next-line no-bitwise element.compareDocumentPosition(focusable) & element.DOCUMENT_POSITION_FOLLOWING ) ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { find, findNext, findPrevious, isTabbableIndex }); //# sourceMappingURL=tabbable.js.map