Files
wdi-dashboard/node_modules/@chakra-ui/utils/dist/dom-query.js
2024-08-16 15:06:52 +05:30

195 lines
6.5 KiB
JavaScript

"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);
// src/dom-query.ts
var dom_query_exports = {};
__export(dom_query_exports, {
closest: () => closest,
focusNextTabbable: () => focusNextTabbable,
focusPreviousTabbable: () => focusPreviousTabbable,
getAllFocusable: () => getAllFocusable,
getAllTabbable: () => getAllTabbable,
getFirstFocusable: () => getFirstFocusable,
getFirstTabbableIn: () => getFirstTabbableIn,
getLastTabbableIn: () => getLastTabbableIn,
getNextTabbable: () => getNextTabbable,
getPreviousTabbable: () => getPreviousTabbable
});
module.exports = __toCommonJS(dom_query_exports);
// src/dom.ts
function isElement(el) {
return el != null && typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE;
}
function isHTMLElement(el) {
var _a;
if (!isElement(el)) {
return false;
}
const win = (_a = el.ownerDocument.defaultView) != null ? _a : window;
return el instanceof win.HTMLElement;
}
// src/tabbable.ts
var hasTabIndex = (element) => element.hasAttribute("tabindex");
var hasNegativeTabIndex = (element) => hasTabIndex(element) && element.tabIndex === -1;
function isDisabled(element) {
return Boolean(element.getAttribute("disabled")) === true || Boolean(element.getAttribute("aria-disabled")) === true;
}
function isHidden(element) {
if (element.parentElement && isHidden(element.parentElement))
return true;
return element.hidden;
}
function isContentEditable(element) {
const value = element.getAttribute("contenteditable");
return value !== "false" && value != null;
}
function isFocusable(element) {
if (!isHTMLElement(element) || isHidden(element) || isDisabled(element)) {
return false;
}
const { localName } = element;
const focusableTags = ["input", "select", "textarea", "button"];
if (focusableTags.indexOf(localName) >= 0)
return true;
const others = {
a: () => element.hasAttribute("href"),
audio: () => element.hasAttribute("controls"),
video: () => element.hasAttribute("controls")
};
if (localName in others) {
return others[localName]();
}
if (isContentEditable(element))
return true;
return hasTabIndex(element);
}
function isTabbable(element) {
if (!element)
return false;
return isHTMLElement(element) && isFocusable(element) && !hasNegativeTabIndex(element);
}
// src/dom-query.ts
var focusableElList = [
"input:not(:disabled):not([disabled])",
"select:not(:disabled):not([disabled])",
"textarea:not(:disabled):not([disabled])",
"embed",
"iframe",
"object",
"a[href]",
"area[href]",
"button:not(:disabled):not([disabled])",
"[tabindex]",
"audio[controls]",
"video[controls]",
"*[tabindex]:not([aria-disabled])",
"*[contenteditable]"
];
var focusableElSelector = focusableElList.join();
var isVisible = (el) => el.offsetWidth > 0 && el.offsetHeight > 0;
function getAllFocusable(container) {
const focusableEls = Array.from(
container.querySelectorAll(focusableElSelector)
);
focusableEls.unshift(container);
return focusableEls.filter((el) => isFocusable(el) && isVisible(el));
}
function getFirstFocusable(container) {
const allFocusable = getAllFocusable(container);
return allFocusable.length ? allFocusable[0] : null;
}
function getAllTabbable(container, fallbackToFocusable) {
const allFocusable = Array.from(
container.querySelectorAll(focusableElSelector)
);
const allTabbable = allFocusable.filter(isTabbable);
if (isTabbable(container)) {
allTabbable.unshift(container);
}
if (!allTabbable.length && fallbackToFocusable) {
return allFocusable;
}
return allTabbable;
}
function getFirstTabbableIn(container, fallbackToFocusable) {
const [first] = getAllTabbable(container, fallbackToFocusable);
return first || null;
}
function getLastTabbableIn(container, fallbackToFocusable) {
const allTabbable = getAllTabbable(container, fallbackToFocusable);
return allTabbable[allTabbable.length - 1] || null;
}
function getNextTabbable(container, fallbackToFocusable) {
const allFocusable = getAllFocusable(container);
const index = allFocusable.indexOf(document.activeElement);
const slice = allFocusable.slice(index + 1);
return slice.find(isTabbable) || allFocusable.find(isTabbable) || (fallbackToFocusable ? slice[0] : null);
}
function getPreviousTabbable(container, fallbackToFocusable) {
const allFocusable = getAllFocusable(container).reverse();
const index = allFocusable.indexOf(document.activeElement);
const slice = allFocusable.slice(index + 1);
return slice.find(isTabbable) || allFocusable.find(isTabbable) || (fallbackToFocusable ? slice[0] : null);
}
function focusNextTabbable(container, fallbackToFocusable) {
const nextTabbable = getNextTabbable(container, fallbackToFocusable);
if (nextTabbable && isHTMLElement(nextTabbable)) {
nextTabbable.focus();
}
}
function focusPreviousTabbable(container, fallbackToFocusable) {
const previousTabbable = getPreviousTabbable(container, fallbackToFocusable);
if (previousTabbable && isHTMLElement(previousTabbable)) {
previousTabbable.focus();
}
}
function matches(element, selectors) {
if ("matches" in element)
return element.matches(selectors);
if ("msMatchesSelector" in element)
return element.msMatchesSelector(selectors);
return element.webkitMatchesSelector(selectors);
}
function closest(element, selectors) {
if ("closest" in element)
return element.closest(selectors);
do {
if (matches(element, selectors))
return element;
element = element.parentElement || element.parentNode;
} while (element !== null && element.nodeType === 1);
return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
closest,
focusNextTabbable,
focusPreviousTabbable,
getAllFocusable,
getAllTabbable,
getFirstFocusable,
getFirstTabbableIn,
getLastTabbableIn,
getNextTabbable,
getPreviousTabbable
});