package.dist.index.mjs Maven / Gradle / Ivy
The newest version!
import { isIos, nextTick, raf } from '@zag-js/dom-query';
// src/index.ts
var state = "default";
var userSelect = "";
var elementMap = /* @__PURE__ */ new WeakMap();
function disableTextSelectionImpl(options = {}) {
const { target, doc } = options;
const docNode = doc ?? document;
const rootEl = docNode.documentElement;
if (isIos()) {
if (state === "default") {
userSelect = rootEl.style.webkitUserSelect;
rootEl.style.webkitUserSelect = "none";
}
state = "disabled";
} else if (target) {
elementMap.set(target, target.style.userSelect);
target.style.userSelect = "none";
}
return () => restoreTextSelection({ target, doc: docNode });
}
function restoreTextSelection(options = {}) {
const { target, doc } = options;
const docNode = doc ?? document;
const rootEl = docNode.documentElement;
if (isIos()) {
if (state !== "disabled") return;
state = "restoring";
setTimeout(() => {
nextTick(() => {
if (state === "restoring") {
if (rootEl.style.webkitUserSelect === "none") {
rootEl.style.webkitUserSelect = userSelect || "";
}
userSelect = "";
state = "default";
}
});
}, 300);
} else {
if (target && elementMap.has(target)) {
const prevUserSelect = elementMap.get(target);
if (target.style.userSelect === "none") {
target.style.userSelect = prevUserSelect ?? "";
}
if (target.getAttribute("style") === "") {
target.removeAttribute("style");
}
elementMap.delete(target);
}
}
}
function disableTextSelection(options = {}) {
const { defer, target, ...restOptions } = options;
const func = defer ? raf : (v) => v();
const cleanups = [];
cleanups.push(
func(() => {
const node = typeof target === "function" ? target() : target;
cleanups.push(disableTextSelectionImpl({ ...restOptions, target: node }));
})
);
return () => {
cleanups.forEach((fn) => fn?.());
};
}
export { disableTextSelection, restoreTextSelection };
© 2015 - 2025 Weber Informatics LLC | Privacy Policy