UNPKG

@zag-js/splitter

Version:

Core logic for the splitter widget implemented as a state machine

100 lines (98 loc) 4.12 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); // src/utils/stacking-order.ts var stacking_order_exports = {}; __export(stacking_order_exports, { compareStackingOrder: () => compareStackingOrder }); module.exports = __toCommonJS(stacking_order_exports); var import_dom_query = require("@zag-js/dom-query"); var import_utils = require("@zag-js/utils"); function compareStackingOrder(a, b) { if (a === b) throw new Error("Cannot compare node with itself"); const ancestors = { a: (0, import_dom_query.getAncestorElements)(a), b: (0, import_dom_query.getAncestorElements)(b) }; let commonAncestor = null; while (ancestors.a.at(-1) === ancestors.b.at(-1)) { const currentA = ancestors.a.pop(); ancestors.b.pop(); commonAncestor = currentA; } (0, import_utils.ensure)( commonAncestor, () => "[stacking-order] Stacking order can only be calculated for elements with a common ancestor" ); const zIndexes = { a: getZIndex(findStackingContext(ancestors.a)), b: getZIndex(findStackingContext(ancestors.b)) }; if (zIndexes.a === zIndexes.b) { const children = commonAncestor.childNodes; const furthestAncestors = { a: ancestors.a.at(-1), b: ancestors.b.at(-1) }; let i = children.length; while (i--) { const child = children[i]; if (child === furthestAncestors.a) return 1; if (child === furthestAncestors.b) return -1; } } return Math.sign(zIndexes.a - zIndexes.b); } var STACKING_PROPS_REGEX = /\b(?:position|zIndex|opacity|transform|webkitTransform|mixBlendMode|filter|webkitFilter|isolation)\b/; function isFlexItem(node) { const parent = (0, import_dom_query.getParentElement)(node); const display = (0, import_dom_query.getComputedStyle)(parent ?? node).display; return display === "flex" || display === "inline-flex"; } function createsStackingContext(node) { const style = (0, import_dom_query.getComputedStyle)(node); if (style.position === "fixed") return true; if (style.zIndex !== "auto" && (style.position !== "static" || isFlexItem(node))) return true; if (+style.opacity < 1) return true; if ((0, import_utils.hasProp)(style, "transform") && style.transform !== "none") return true; if ((0, import_utils.hasProp)(style, "webkitTransform") && style.webkitTransform !== "none") return true; if ((0, import_utils.hasProp)(style, "mixBlendMode") && style.mixBlendMode !== "normal") return true; if ((0, import_utils.hasProp)(style, "filter") && style.filter !== "none") return true; if ((0, import_utils.hasProp)(style, "webkitFilter") && style.webkitFilter !== "none") return true; if ((0, import_utils.hasProp)(style, "isolation") && style.isolation === "isolate") return true; if (STACKING_PROPS_REGEX.test(style.willChange)) return true; if (style.webkitOverflowScrolling === "touch") return true; return false; } function findStackingContext(nodes) { let i = nodes.length; while (i--) { const node = nodes[i]; (0, import_utils.ensure)(node, () => "[stacking-order] missing node in findStackingContext"); if (createsStackingContext(node)) return node; } return null; } var getZIndex = (node) => { return node && Number((0, import_dom_query.getComputedStyle)(node).zIndex) || 0; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { compareStackingOrder });