@zag-js/dom-query
Advanced tools
Comparing version 0.0.0-dev-20230202113057 to 0.0.0-dev-20230222120620
@@ -1,4 +0,4 @@ | ||
type Target = HTMLElement | EventTarget; | ||
declare function contains(parent: Target | null | undefined, child: Target | null): boolean; | ||
type Target = HTMLElement | EventTarget | null | undefined; | ||
declare function contains(parent: Target, child: Target): boolean; | ||
export { contains }; |
@@ -27,3 +27,3 @@ "use strict"; | ||
// src/is-element.ts | ||
// src/is-html-element.ts | ||
function isHTMLElement(value) { | ||
@@ -30,0 +30,0 @@ return typeof value === "object" && value?.nodeType === Node.ELEMENT_NODE && typeof value?.nodeName === "string"; |
@@ -0,3 +1,20 @@ | ||
export { ariaAttr, dataAttr } from './attrs.js'; | ||
export { contains } from './contains.js'; | ||
export { isDocument, isHTMLElement, isWindow } from './is-element.js'; | ||
export { CustomEventEmitter, CustomEventListener, defineHelpers } from './helpers.js'; | ||
export { createScope } from './create-scope.js'; | ||
export { getDocument, getWindow } from './env.js'; | ||
export { getActiveElement } from './get-active-element.js'; | ||
export { indexOfId, itemById, nextById, prevById } from './get-by-id.js'; | ||
export { getByText } from './get-by-text.js'; | ||
export { TypeaheadOptions, TypeaheadState, getByTypeahead } from './get-by-typeahead.js'; | ||
export { getComputedStyle } from './get-computed-style.js'; | ||
export { getEventTarget } from './get-event-target.js'; | ||
export { getParent, getScrollParent, getScrollParents } from './get-scroll-parent.js'; | ||
export { isEditableElement } from './is-editable-element.js'; | ||
export { isHTMLElement } from './is-html-element.js'; | ||
export { getPlatform, isApple, isDom, isFirefox, isIPhone, isIos, isMac, isSafari, isTouchDevice } from './platform.js'; | ||
export { query, queryAll } from './query.js'; | ||
export { nextTick, raf } from './raf.js'; | ||
declare const MAX_Z_INDEX = 2147483647; | ||
export { MAX_Z_INDEX }; |
@@ -23,17 +23,48 @@ "use strict"; | ||
__export(src_exports, { | ||
MAX_Z_INDEX: () => MAX_Z_INDEX, | ||
ariaAttr: () => ariaAttr, | ||
contains: () => contains, | ||
defineHelpers: () => defineHelpers, | ||
isDocument: () => isDocument, | ||
createScope: () => createScope, | ||
dataAttr: () => dataAttr, | ||
getActiveElement: () => getActiveElement, | ||
getByText: () => getByText, | ||
getByTypeahead: () => getByTypeahead, | ||
getComputedStyle: () => getComputedStyle, | ||
getDocument: () => getDocument2, | ||
getEventTarget: () => getEventTarget, | ||
getParent: () => getParent, | ||
getPlatform: () => getPlatform, | ||
getScrollParent: () => getScrollParent, | ||
getScrollParents: () => getScrollParents, | ||
getWindow: () => getWindow, | ||
indexOfId: () => indexOfId, | ||
isApple: () => isApple, | ||
isDom: () => isDom, | ||
isEditableElement: () => isEditableElement, | ||
isFirefox: () => isFirefox, | ||
isHTMLElement: () => isHTMLElement, | ||
isWindow: () => isWindow | ||
isIPhone: () => isIPhone, | ||
isIos: () => isIos, | ||
isMac: () => isMac, | ||
isSafari: () => isSafari, | ||
isTouchDevice: () => isTouchDevice, | ||
itemById: () => itemById, | ||
nextById: () => nextById, | ||
nextTick: () => nextTick, | ||
prevById: () => prevById, | ||
query: () => query, | ||
queryAll: () => queryAll, | ||
raf: () => raf | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/is-element.ts | ||
function isDocument(value) { | ||
return value.nodeType === Node.DOCUMENT_NODE; | ||
} | ||
function isWindow(value) { | ||
return value?.toString() === "[object Window]"; | ||
} | ||
// src/attrs.ts | ||
var dataAttr = (guard) => { | ||
return guard ? "" : void 0; | ||
}; | ||
var ariaAttr = (guard) => { | ||
return guard ? "true" : void 0; | ||
}; | ||
// src/is-html-element.ts | ||
function isHTMLElement(value) { | ||
@@ -52,4 +83,28 @@ return typeof value === "object" && value?.nodeType === Node.ELEMENT_NODE && typeof value?.nodeName === "string"; | ||
// src/get-element.ts | ||
function getDocument(el) { | ||
// src/create-scope.ts | ||
var getDocument = (node) => { | ||
if (node.nodeType === Node.DOCUMENT_NODE) | ||
return node; | ||
return node.ownerDocument ?? document; | ||
}; | ||
function createScope(methods) { | ||
const screen = { | ||
getRootNode: (ctx) => ctx.getRootNode?.() ?? document, | ||
getDoc: (ctx) => getDocument(screen.getRootNode(ctx)), | ||
getWin: (ctx) => screen.getDoc(ctx).defaultView ?? window, | ||
getActiveElement: (ctx) => screen.getDoc(ctx).activeElement, | ||
getById: (ctx, id) => screen.getRootNode(ctx).getElementById(id), | ||
queryById: (ctx, id) => { | ||
const el = screen.getById(ctx, id); | ||
if (!el) | ||
throw new Error(`Element with id "${id}" not found.`); | ||
return el; | ||
} | ||
}; | ||
return { ...screen, ...methods }; | ||
} | ||
// src/env.ts | ||
var isDocument = (el) => el.nodeType === Node.DOCUMENT_NODE; | ||
function getDocument2(el) { | ||
if (isDocument(el)) | ||
@@ -60,59 +115,227 @@ return el; | ||
function getWindow(el) { | ||
return getDocument(el).defaultView ?? window; | ||
return el?.ownerDocument.defaultView ?? window; | ||
} | ||
// src/helpers.ts | ||
function defineHelpers(rest) { | ||
const dom = { | ||
getRootNode(ctx) { | ||
return ctx.getRootNode?.() ?? document; | ||
}, | ||
getDoc(ctx) { | ||
return getDocument(dom.getRootNode(ctx)); | ||
}, | ||
getWin(ctx) { | ||
return getWindow(dom.getDoc(ctx)); | ||
}, | ||
getActiveElement(ctx) { | ||
return dom.getDoc(ctx).activeElement; | ||
}, | ||
getById(ctx, id) { | ||
return dom.getRootNode(ctx).getElementById(id); | ||
}, | ||
createEmitter(ctx, target) { | ||
return function emit(evt, detail, options) { | ||
const { bubbles = true, cancelable, composed = true } = options ?? {}; | ||
const eventName = `zag:${String(evt)}`; | ||
const init = { | ||
bubbles, | ||
cancelable, | ||
composed, | ||
detail | ||
}; | ||
const win = dom.getWin(ctx); | ||
const event = new win.CustomEvent(eventName, init); | ||
const node = typeof target === "function" ? target() : target; | ||
node.dispatchEvent(event); | ||
}; | ||
}, | ||
createListener(target) { | ||
return function on(evt, listener) { | ||
const eventName = `zag:${String(evt)}`; | ||
const node = typeof target === "function" ? target() : target; | ||
node.addEventListener(eventName, listener); | ||
return function off() { | ||
return node.removeEventListener(eventName, listener); | ||
}; | ||
}; | ||
// src/get-active-element.ts | ||
function getActiveElement(el) { | ||
let activeElement = el.ownerDocument.activeElement; | ||
while (activeElement?.shadowRoot) { | ||
const el2 = activeElement.shadowRoot.activeElement; | ||
if (el2 === activeElement) | ||
break; | ||
else | ||
activeElement = el2; | ||
} | ||
return activeElement; | ||
} | ||
// src/get-by-id.ts | ||
function itemById(v, id) { | ||
return v.find((node) => node.id === id); | ||
} | ||
function indexOfId(v, id) { | ||
const item = itemById(v, id); | ||
return item ? v.indexOf(item) : -1; | ||
} | ||
function nextById(v, id, loop = true) { | ||
let idx = indexOfId(v, id); | ||
idx = loop ? (idx + 1) % v.length : Math.min(idx + 1, v.length - 1); | ||
return v[idx]; | ||
} | ||
function prevById(v, id, loop = true) { | ||
let idx = indexOfId(v, id); | ||
if (idx === -1) | ||
return loop ? v[v.length - 1] : null; | ||
idx = loop ? (idx - 1 + v.length) % v.length : Math.max(0, idx - 1); | ||
return v[idx]; | ||
} | ||
// src/get-by-text.ts | ||
var getValueText = (item) => item.dataset.valuetext ?? item.textContent ?? ""; | ||
var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase()); | ||
var wrap = (v, idx) => { | ||
return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]); | ||
}; | ||
function getByText(v, text, currentId) { | ||
const index = currentId ? indexOfId(v, currentId) : -1; | ||
let items = currentId ? wrap(v, index) : v; | ||
const isSingleKey = text.length === 1; | ||
if (isSingleKey) { | ||
items = items.filter((item) => item.id !== currentId); | ||
} | ||
return items.find((item) => match(getValueText(item), text)); | ||
} | ||
// src/get-by-typeahead.ts | ||
function getByTypeaheadImpl(_items, options) { | ||
const { state, activeId, key, timeout = 350 } = options; | ||
const search = state.keysSoFar + key; | ||
const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]); | ||
const query2 = isRepeated ? search[0] : search; | ||
let items = _items.slice(); | ||
const next = getByText(items, query2, activeId); | ||
function cleanup() { | ||
clearTimeout(state.timer); | ||
state.timer = -1; | ||
} | ||
function update(value) { | ||
state.keysSoFar = value; | ||
cleanup(); | ||
if (value !== "") { | ||
state.timer = +setTimeout(() => { | ||
update(""); | ||
cleanup(); | ||
}, timeout); | ||
} | ||
} | ||
update(search); | ||
return next; | ||
} | ||
var getByTypeahead = /* @__PURE__ */ Object.assign(getByTypeaheadImpl, { | ||
defaultOptions: { keysSoFar: "", timer: -1 }, | ||
isValidEvent: isValidTypeaheadEvent | ||
}); | ||
function isValidTypeaheadEvent(event) { | ||
return event.key.length === 1 && !event.ctrlKey && !event.metaKey; | ||
} | ||
// src/get-computed-style.ts | ||
var styleCache = /* @__PURE__ */ new WeakMap(); | ||
function getComputedStyle(el) { | ||
if (!styleCache.has(el)) { | ||
const win = el.ownerDocument.defaultView || window; | ||
styleCache.set(el, win.getComputedStyle(el)); | ||
} | ||
return styleCache.get(el); | ||
} | ||
// src/get-event-target.ts | ||
function getEventTarget(event) { | ||
return event.composedPath?.()[0] ?? event.target; | ||
} | ||
// src/get-scroll-parent.ts | ||
function isScrollParent(el) { | ||
const win = el.ownerDocument.defaultView || window; | ||
const { overflow, overflowX, overflowY } = win.getComputedStyle(el); | ||
return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX); | ||
} | ||
function getParent(el) { | ||
if (el.localName === "html") | ||
return el; | ||
return el.assignedSlot || el.parentElement || el.ownerDocument.documentElement; | ||
} | ||
function getScrollParent(el) { | ||
if (["html", "body", "#document"].includes(el.localName)) { | ||
return el.ownerDocument.body; | ||
} | ||
if (isHTMLElement(el) && isScrollParent(el)) { | ||
return el; | ||
} | ||
return getScrollParent(getParent(el)); | ||
} | ||
function getScrollParents(el, list = []) { | ||
const parent = getScrollParent(el); | ||
const isBody = parent === el.ownerDocument.body; | ||
const win = parent.ownerDocument.defaultView || window; | ||
const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(parent) ? parent : []) : parent; | ||
const parents = list.concat(target); | ||
return isBody ? parents : parents.concat(getScrollParents(getParent(target))); | ||
} | ||
// src/is-editable-element.ts | ||
function isEditableElement(el) { | ||
if (el == null || !isHTMLElement(el)) { | ||
return false; | ||
} | ||
try { | ||
const win = el.ownerDocument.defaultView || window; | ||
return el instanceof win.HTMLInputElement && el.selectionStart != null || /(textarea|select)/.test(el.localName) || el.isContentEditable; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
// src/platform.ts | ||
var isDom = () => typeof window !== "undefined"; | ||
function getPlatform() { | ||
const agent = navigator.userAgentData; | ||
return agent?.platform ?? navigator.platform; | ||
} | ||
var pt = (v) => isDom() && v.test(getPlatform()); | ||
var ua = (v) => isDom() && v.test(navigator.userAgent); | ||
var vn = (v) => isDom() && v.test(navigator.vendor); | ||
var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints; | ||
var isMac = () => pt(/^Mac/) && !isTouchDevice; | ||
var isIPhone = () => pt(/^iPhone/); | ||
var isSafari = () => isApple() && vn(/apple/i); | ||
var isFirefox = () => ua(/firefox\//i); | ||
var isApple = () => pt(/mac|iphone|ipad|ipod/i); | ||
var isIos = () => isApple() && !isMac(); | ||
// src/query.ts | ||
function queryAll(root, selector) { | ||
return Array.from(root?.querySelectorAll(selector) ?? []); | ||
} | ||
function query(root, selector) { | ||
return root?.querySelector(selector); | ||
} | ||
// src/raf.ts | ||
function nextTick(fn) { | ||
const set = /* @__PURE__ */ new Set(); | ||
function raf2(fn2) { | ||
const id = globalThis.requestAnimationFrame(fn2); | ||
set.add(() => globalThis.cancelAnimationFrame(id)); | ||
} | ||
raf2(() => raf2(fn)); | ||
return function cleanup() { | ||
set.forEach((fn2) => fn2()); | ||
}; | ||
return { ...dom, ...rest }; | ||
} | ||
function raf(fn) { | ||
const id = globalThis.requestAnimationFrame(fn); | ||
return () => { | ||
globalThis.cancelAnimationFrame(id); | ||
}; | ||
} | ||
// src/index.ts | ||
var MAX_Z_INDEX = 2147483647; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
MAX_Z_INDEX, | ||
ariaAttr, | ||
contains, | ||
defineHelpers, | ||
isDocument, | ||
createScope, | ||
dataAttr, | ||
getActiveElement, | ||
getByText, | ||
getByTypeahead, | ||
getComputedStyle, | ||
getDocument, | ||
getEventTarget, | ||
getParent, | ||
getPlatform, | ||
getScrollParent, | ||
getScrollParents, | ||
getWindow, | ||
indexOfId, | ||
isApple, | ||
isDom, | ||
isEditableElement, | ||
isFirefox, | ||
isHTMLElement, | ||
isWindow | ||
isIPhone, | ||
isIos, | ||
isMac, | ||
isSafari, | ||
isTouchDevice, | ||
itemById, | ||
nextById, | ||
nextTick, | ||
prevById, | ||
query, | ||
queryAll, | ||
raf | ||
}); |
{ | ||
"name": "@zag-js/dom-query", | ||
"version": "0.0.0-dev-20230202113057", | ||
"version": "0.0.0-dev-20230222120620", | ||
"description": "The dom helper library for zag.js machines", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55824
70
1700
1