@zag-js/dom-query
Version:
The dom helper library for zag.js machines
132 lines (130 loc) • 4.22 kB
JavaScript
import "./chunk-QZ7TP4HQ.mjs";
// src/node.ts
import { isObject } from "./shared.mjs";
var ELEMENT_NODE = 1;
var DOCUMENT_NODE = 9;
var DOCUMENT_FRAGMENT_NODE = 11;
var isElement = (el) => isObject(el) && el.nodeType === ELEMENT_NODE && typeof el.nodeName === "string";
var isHTMLElement = (el) => isObject(el) && el.nodeType === ELEMENT_NODE && typeof el.nodeName === "string";
var isDocument = (el) => isObject(el) && el.nodeType === DOCUMENT_NODE;
var isWindow = (el) => isObject(el) && el === el.window;
var isVisualViewport = (el) => isObject(el) && el.constructor.name === "VisualViewport";
var getNodeName = (node) => {
if (isHTMLElement(node)) return node.localName || "";
return "#document";
};
function isRootElement(node) {
return ["html", "body", "#document"].includes(getNodeName(node));
}
var isNode = (el) => isObject(el) && el.nodeType !== void 0;
var isShadowRoot = (el) => isNode(el) && el.nodeType === DOCUMENT_FRAGMENT_NODE && "host" in el;
var isInputElement = (el) => isHTMLElement(el) && el.localName === "input";
var isAnchorElement = (el) => !!el?.matches("a[href]");
var isElementVisible = (el) => {
if (!isHTMLElement(el)) return false;
return el.offsetWidth > 0 || el.offsetHeight > 0 || el.getClientRects().length > 0;
};
function isActiveElement(element) {
if (!element) return false;
const rootNode = element.getRootNode();
return getActiveElement(rootNode) === element;
}
var TEXTAREA_SELECT_REGEX = /(textarea|select)/;
function isEditableElement(el) {
if (el == null || !isHTMLElement(el)) return false;
try {
return isInputElement(el) && el.selectionStart != null || TEXTAREA_SELECT_REGEX.test(el.localName) || el.isContentEditable || el.getAttribute("contenteditable") === "true" || el.getAttribute("contenteditable") === "";
} catch {
return false;
}
}
function getParentElement(node) {
const parentNode = node.parentNode;
if (isShadowRoot(parentNode)) return parentNode.host;
return parentNode;
}
function getAncestorElements(node) {
const ancestors = [];
while (node) {
ancestors.push(node);
node = getParentElement(node);
}
return ancestors;
}
function contains(parent, child) {
if (!parent || !child) return false;
if (!isHTMLElement(parent) || !isNode(child)) return false;
if (isHTMLElement(child) && parent === child) return true;
if (parent.contains(child)) return true;
const rootNode = child.getRootNode?.();
if (rootNode && isShadowRoot(rootNode)) {
let next = child;
while (next) {
if (parent === next) return true;
next = next.parentNode || next.host;
}
}
return false;
}
function getDocument(el) {
if (isDocument(el)) return el;
if (isWindow(el)) return el.document;
return el?.ownerDocument ?? document;
}
function getDocumentElement(el) {
return getDocument(el).documentElement;
}
function getWindow(el) {
if (isShadowRoot(el)) return getWindow(el.host);
if (isDocument(el)) return el.defaultView ?? window;
if (isHTMLElement(el)) return el.ownerDocument?.defaultView ?? window;
return window;
}
function getActiveElement(rootNode) {
let activeElement = rootNode.activeElement;
while (activeElement?.shadowRoot) {
const el = activeElement.shadowRoot.activeElement;
if (!el || el === activeElement) break;
else activeElement = el;
}
return activeElement;
}
function getParentNode(node) {
if (getNodeName(node) === "html") return node;
const result = node.assignedSlot || node.parentNode || isShadowRoot(node) && node.host || getDocumentElement(node);
return isShadowRoot(result) ? result.host : result;
}
function getRootNode(node) {
let result;
try {
result = node.getRootNode({ composed: true });
if (isDocument(result) || isShadowRoot(result)) return result;
} catch {
}
return node.ownerDocument ?? document;
}
export {
contains,
getActiveElement,
getAncestorElements,
getDocument,
getDocumentElement,
getNodeName,
getParentElement,
getParentNode,
getRootNode,
getWindow,
isActiveElement,
isAnchorElement,
isDocument,
isEditableElement,
isElement,
isElementVisible,
isHTMLElement,
isInputElement,
isNode,
isRootElement,
isShadowRoot,
isVisualViewport,
isWindow
};