@zag-js/dom-utils
Advanced tools
Comparing version 0.0.0-dev-20220604091934 to 0.0.0-dev-20220604123458
export * from "./attrs"; | ||
export * from "./auto-resize"; | ||
export * from "./body-pointer-event"; | ||
export * from "./body-scroll-lock"; | ||
export * from "./computed-style"; | ||
@@ -6,0 +5,0 @@ export * from "./constants"; |
@@ -1,2 +0,1 @@ | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
@@ -8,3 +7,2 @@ var __defProps = Object.defineProperties; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
@@ -38,3 +36,2 @@ var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
@@ -109,3 +106,2 @@ | ||
preventBodyPointerEvents: () => preventBodyPointerEvents, | ||
preventBodyScroll: () => preventBodyScroll, | ||
query: () => query, | ||
@@ -239,19 +235,16 @@ queryAll: () => queryAll, | ||
// ../core/dist/index.mjs | ||
var runIfFn = (v, ...a) => { | ||
const res = typeof v === "function" ? v(...a) : v; | ||
return res != null ? res : void 0; | ||
}; | ||
var cast = (v) => v; | ||
var noop = () => { | ||
}; | ||
var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v); | ||
var platform = (v) => isDom() && v.test(navigator.platform); | ||
var isDom = () => typeof window !== "undefined"; | ||
var isMac = () => platform(/^Mac/); | ||
var isIPhone = () => platform(/^iPhone/); | ||
var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1; | ||
var isIos = () => isIPhone() || isIPad(); | ||
var isArray = (v) => Array.isArray(v); | ||
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v)); | ||
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); | ||
function getPlatform() { | ||
var _a; | ||
const agent = navigator.userAgentData; | ||
return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform; | ||
} | ||
var pt = (v) => isDom() && v.test(getPlatform()); | ||
var isTouchDevice = isDom() && !!navigator.maxTouchPoints; | ||
var isMac = () => pt(/^Mac/) && !isTouchDevice; | ||
var isApple = () => pt(/mac|iphone|ipad|ipod/i); | ||
var isIos = () => isApple() && !isMac(); | ||
var supportsPointerEvent = () => isDom() && window.onpointerdown === null; | ||
@@ -263,2 +256,10 @@ var supportsTouchEvent = () => isDom() && window.ontouchstart === null; | ||
var isLeftClick = (v) => v.button === 0; | ||
var runIfFn = (v, ...a) => { | ||
const res = typeof v === "function" ? v(...a) : v; | ||
return res != null ? res : void 0; | ||
}; | ||
var cast = (v) => v; | ||
var noop = () => { | ||
}; | ||
var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v); | ||
@@ -455,4 +456,4 @@ // src/global-listener.ts | ||
// src/body-scroll-lock.ts | ||
var import_scroll_into_view_if_needed = __toESM(require("scroll-into-view-if-needed")); | ||
// src/constants.ts | ||
var MAX_Z_INDEX = 2147483647; | ||
@@ -516,137 +517,2 @@ // src/query.ts | ||
// src/scrollable.ts | ||
function isScrollParent(el) { | ||
const { overflow, overflowX, overflowY } = getComputedStyle2(el); | ||
return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX); | ||
} | ||
function getScrollParent(el) { | ||
if (["html", "body", "#document"].includes(getNodeName(el))) { | ||
return getOwnerDocument(el).body; | ||
} | ||
if (isHTMLElement(el) && isScrollParent(el)) { | ||
return el; | ||
} | ||
return getScrollParent(getParent(el)); | ||
} | ||
function getScrollParents(el, list = []) { | ||
const scrollParent = getScrollParent(el); | ||
const isBody = scrollParent === getOwnerDocument(el).body; | ||
const win = getOwnerWindow(scrollParent); | ||
const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent; | ||
const parents = list.concat(target); | ||
if (isBody) | ||
return parents; | ||
return parents.concat(getScrollParents(getParent(target))); | ||
} | ||
function getScrollOffset(el) { | ||
if (isWindow(el)) { | ||
return { scrollLeft: el.scrollX, scrollTop: el.scrollY }; | ||
} | ||
return { scrollLeft: el.scrollLeft, scrollTop: el.scrollTop }; | ||
} | ||
// src/body-scroll-lock.ts | ||
var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]); | ||
function preventBodyScroll(opts) { | ||
var _a; | ||
const { document: docProp, disabled = false, allowPinchZoom } = opts != null ? opts : {}; | ||
const doc = docProp != null ? docProp : document; | ||
const win = (_a = doc == null ? void 0 : doc.defaultView) != null ? _a : window; | ||
const viewport = isDom() ? win.visualViewport : null; | ||
const docEl = doc.documentElement; | ||
function preventScrollStandard() { | ||
if (docEl.hasAttribute("scroll-lock")) | ||
return; | ||
const fn = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), () => docEl.setAttribute("scroll-lock", "true")); | ||
return pipe(fn, () => docEl.removeAttribute("scroll-lock")); | ||
} | ||
function preventScrollMobileSafari() { | ||
if (docEl.hasAttribute("scroll-lock")) | ||
return; | ||
let scrollable; | ||
let lastY = 0; | ||
let onTouchStart = (e) => { | ||
scrollable = getScrollParent(e.target); | ||
if (scrollable === docEl && scrollable === doc.body) { | ||
return; | ||
} | ||
lastY = e.changedTouches[0].pageY; | ||
}; | ||
let onTouchMove = (e) => { | ||
if (e.touches.length === 2 && allowPinchZoom) | ||
return; | ||
if (!scrollable || scrollable === docEl || scrollable === doc.body) { | ||
e.preventDefault(); | ||
return; | ||
} | ||
let y = e.changedTouches[0].pageY; | ||
let scrollTop = scrollable.scrollTop; | ||
let bottom = scrollable.scrollHeight - scrollable.clientHeight; | ||
if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) { | ||
e.preventDefault(); | ||
} | ||
lastY = y; | ||
}; | ||
let onTouchEnd = (e) => { | ||
let target = e.target; | ||
if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) { | ||
e.preventDefault(); | ||
target.style.transform = "translateY(-2000px)"; | ||
target.focus(); | ||
win.requestAnimationFrame(() => { | ||
target.style.transform = ""; | ||
}); | ||
} | ||
}; | ||
let onFocus = (e) => { | ||
let target = e.target; | ||
if (target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target.type)) { | ||
target.style.transform = "translateY(-2000px)"; | ||
win.requestAnimationFrame(() => { | ||
target.style.transform = ""; | ||
if (!viewport) | ||
return; | ||
if (viewport.height < win.innerHeight) { | ||
win.requestAnimationFrame(function() { | ||
(0, import_scroll_into_view_if_needed.default)(target, { scrollMode: "if-needed" }); | ||
}); | ||
} else { | ||
const onResize = () => { | ||
(0, import_scroll_into_view_if_needed.default)(target, { scrollMode: "if-needed" }); | ||
}; | ||
addDomEvent(viewport, "resize", onResize, { once: true }); | ||
} | ||
}); | ||
} | ||
}; | ||
let onWindowScroll = () => { | ||
win.scrollTo(0, 0); | ||
}; | ||
let scrollX = win.scrollX; | ||
let scrollY = win.scrollY; | ||
let restoreStyles = pipe(setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`), setStyle(docEl, "overflow", "hidden"), setStyle(doc.body, "marginTop", `-${scrollY}px`)); | ||
win.scrollTo(0, 0); | ||
let removeEvents = pipe(addDomEvent(doc, "touchstart", onTouchStart, { passive: false, capture: true }), addDomEvent(doc, "touchmove", onTouchMove, { passive: false, capture: true }), addDomEvent(doc, "touchend", onTouchEnd, { passive: false, capture: true }), addDomEvent(doc, "focus", onFocus, true), addDomEvent(win, "scroll", onWindowScroll), () => docEl.setAttribute("scroll-lock", "true")); | ||
return () => { | ||
restoreStyles(); | ||
removeEvents(); | ||
win.scrollTo(scrollX, scrollY); | ||
docEl.removeAttribute("scroll-lock"); | ||
}; | ||
} | ||
if (disabled) | ||
return noop; | ||
return isIos() ? preventScrollMobileSafari() : preventScrollStandard(); | ||
} | ||
function setStyle(el, key, value) { | ||
let cur = el.style[key]; | ||
el.style[key] = value; | ||
return () => { | ||
el.style[key] = cur; | ||
}; | ||
} | ||
// src/constants.ts | ||
var MAX_Z_INDEX = 2147483647; | ||
// src/event.ts | ||
@@ -1194,2 +1060,33 @@ function isKeyboardClick(e) { | ||
// src/scrollable.ts | ||
function isScrollParent(el) { | ||
const { overflow, overflowX, overflowY } = getComputedStyle2(el); | ||
return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX); | ||
} | ||
function getScrollParent(el) { | ||
if (["html", "body", "#document"].includes(getNodeName(el))) { | ||
return getOwnerDocument(el).body; | ||
} | ||
if (isHTMLElement(el) && isScrollParent(el)) { | ||
return el; | ||
} | ||
return getScrollParent(getParent(el)); | ||
} | ||
function getScrollParents(el, list = []) { | ||
const scrollParent = getScrollParent(el); | ||
const isBody = scrollParent === getOwnerDocument(el).body; | ||
const win = getOwnerWindow(scrollParent); | ||
const target = isBody ? [win].concat(win.visualViewport || [], isScrollParent(scrollParent) ? scrollParent : []) : scrollParent; | ||
const parents = list.concat(target); | ||
if (isBody) | ||
return parents; | ||
return parents.concat(getScrollParents(getParent(target))); | ||
} | ||
function getScrollOffset(el) { | ||
if (isWindow(el)) { | ||
return { scrollLeft: el.scrollX, scrollTop: el.scrollY }; | ||
} | ||
return { scrollLeft: el.scrollLeft, scrollTop: el.scrollTop }; | ||
} | ||
// src/typeahead.ts | ||
@@ -1196,0 +1093,0 @@ function findByTypeahead(_items, options) { |
{ | ||
"name": "@zag-js/dom-utils", | ||
"version": "0.0.0-dev-20220604091934", | ||
"version": "0.0.0-dev-20220604123458", | ||
"description": "", | ||
@@ -29,4 +29,3 @@ "keywords": [ | ||
"@types/react": "^18.0.0", | ||
"@zag-js/utils": "0.0.0-dev-20220604091934", | ||
"scroll-into-view-if-needed": "^2.2.29" | ||
"@zag-js/utils": "0.0.0-dev-20220604123458" | ||
}, | ||
@@ -33,0 +32,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2
246787
67
2563
+ Added@zag-js/utils@0.0.0-dev-20220604123458(transitive)
- Removedscroll-into-view-if-needed@^2.2.29
- Removed@zag-js/utils@0.0.0-dev-20220604091934(transitive)
- Removedcompute-scroll-into-view@1.0.20(transitive)
- Removedscroll-into-view-if-needed@2.2.31(transitive)