UNPKG

motion-plus-vue

Version:

Motion Plus Vue

81 lines (80 loc) 2.17 kB
import { ref as g, watch as S } from "vue"; import { onlyMouse as L, onlyLeftClicks as z } from "../utils/only-mouse-events.mjs"; import { findTarget as E } from "./find-target.mjs"; let f = null, n = { type: "default", isPressed: !1, fontSize: null, targetBoundingBox: null, target: null, zone: null }; function h(e) { let o = null; if ("closest" in e) { const u = e.closest("[data-cursor-zone]"); u instanceof HTMLElement && (o = u.dataset.cursorZone || null); } return o; } function P() { if (typeof window > "u") return { onChange: () => () => { } }; const e = /* @__PURE__ */ new Set(); function o(t) { n = { ...n, ...t }, e.forEach((r) => r(n)); } const u = z(() => { n.isPressed || o({ isPressed: !0 }); }), l = z(() => { n.isPressed && o({ isPressed: !1 }); }), a = L(({ target: t }) => { if (!t) return; const [r, d] = E(t); let s = !1; const i = { target: d, zone: h(t) }; r !== n.type && (i.type = r, s = !0), i.zone !== n.zone && (n.zone = i.zone, s = !0); const c = r === "pointer" && d ? d.getBoundingClientRect() : null; if (c !== n.targetBoundingBox && (i.targetBoundingBox = c, s = !0), r === "text") { const { fontSize: p } = window.getComputedStyle( t ), w = p ? parseInt(p) : null; w !== n.fontSize && (i.fontSize = w, s = !0); } else n.fontSize && (i.fontSize = null, s = !0); s && o(i); }); function v() { typeof window > "u" || (window.addEventListener("pointerover", a), window.addEventListener("pointerdown", u), window.addEventListener("pointerup", l)); } function m() { window.removeEventListener("pointerover", a), window.removeEventListener("pointerdown", u), window.removeEventListener("pointerup", l); } return { onChange: (t) => (e.size || v(), e.add(t), () => { e.delete(t), e.size || m(); }) }; } function y() { return f || (f = P()), f; } function O() { const e = g({ ...n }); return S( e, () => y().onChange((o) => { e.value = o; }), { immediate: !0, flush: "post" } ), e; } export { O as useCursorState };