UNPKG

@tachui/modifiers

Version:

Essential styling modifiers for tachUI framework

465 lines (464 loc) 13.4 kB
var F = Object.defineProperty; var K = (r, u, e) => u in r ? F(r, u, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[u] = e; var h = (r, u, e) => K(r, typeof u != "symbol" ? u + "" : u, e); import { B as X } from "./base-CkGf4b9G.js"; import { BaseModifier as S } from "./base.js"; class b extends X { constructor(e) { const t = {}; for (const [o, a] of Object.entries(e)) typeof a == "function" && "peek" in a ? t[o] = a.peek() : t[o] = a; super(t); h(this, "type", "scroll"); h(this, "priority", 20); } apply(e, t) { if (!t.element) return; const o = this.computeScrollStyles(this.properties); this.applyStyles(t.element, o); } computeScrollStyles(e) { const t = {}; return e.scroll && Object.assign(t, this.generateScrollCSS(e.scroll)), e.scrollBehavior && (t.scrollBehavior = e.scrollBehavior), e.overscrollBehavior && (t.overscrollBehavior = e.overscrollBehavior), e.overscrollBehaviorX && (t.overscrollBehaviorX = e.overscrollBehaviorX), e.overscrollBehaviorY && (t.overscrollBehaviorY = e.overscrollBehaviorY), t; } generateScrollCSS(e) { const t = {}; return e.behavior && (t.scrollBehavior = e.behavior), e.margin && Object.assign(t, this.generateScrollMarginCSS(e.margin)), e.padding && Object.assign(t, this.generateScrollPaddingCSS(e.padding)), e.snap && Object.assign(t, this.generateScrollSnapCSS(e.snap)), t; } generateScrollMarginCSS(e) { if (typeof e == "number" || typeof e == "string") return { scrollMargin: this.formatSpacing(e) }; const t = {}; return e.top !== void 0 && (t.scrollMarginTop = this.formatSpacing(e.top)), e.right !== void 0 && (t.scrollMarginRight = this.formatSpacing(e.right)), e.bottom !== void 0 && (t.scrollMarginBottom = this.formatSpacing(e.bottom)), e.left !== void 0 && (t.scrollMarginLeft = this.formatSpacing(e.left)), t; } generateScrollPaddingCSS(e) { if (typeof e == "number" || typeof e == "string") return { scrollPadding: this.formatSpacing(e) }; const t = {}; return e.top !== void 0 && (t.scrollPaddingTop = this.formatSpacing(e.top)), e.right !== void 0 && (t.scrollPaddingRight = this.formatSpacing(e.right)), e.bottom !== void 0 && (t.scrollPaddingBottom = this.formatSpacing(e.bottom)), e.left !== void 0 && (t.scrollPaddingLeft = this.formatSpacing(e.left)), t; } generateScrollSnapCSS(e) { const t = {}; return e?.type && (t.scrollSnapType = e.type), e?.align && (t.scrollSnapAlign = e.align), e?.stop && (t.scrollSnapStop = e.stop), t; } formatSpacing(e) { return typeof e == "number" ? `${e}px` : e; } } function U(r) { return new b({ scroll: r }); } function $(r) { return new b({ scrollBehavior: r }); } function J(r) { return new b({ overscrollBehavior: r }); } function Q(r) { return new b({ overscrollBehaviorX: r }); } function W(r) { return new b({ overscrollBehaviorY: r }); } function Z(r) { return new b({ scroll: { margin: r } }); } function ee(r) { return new b({ scroll: { padding: r } }); } function te(r, u, e) { return new b({ scroll: { snap: { type: r, align: u, stop: e } } }); } class Y extends S { constructor() { super(...arguments); h(this, "type", "onLongPressGesture"); h(this, "priority", 85); } apply(e, t) { t.element && this.setupLongPressGesture(t.element, this.properties); } setupLongPressGesture(e, t) { const o = t.minimumDuration ?? 500, a = t.maximumDistance ?? 10; let f = null, p = null, n = !1, i = !1; const d = (v, c) => { n || (f = { x: v, y: c }, n = !0, i = !1, t.onPressingChanged?.(!0), p = window.setTimeout(() => { n && !i && (i = !0, t.perform()); }, o)); }, s = (v, c) => f ? Math.sqrt( Math.pow(v - f.x, 2) + Math.pow(c - f.y, 2) ) <= a : !1, l = () => { p && (clearTimeout(p), p = null), n && (n = !1, t.onPressingChanged?.(!1)), f = null, i = !1; }, m = (v) => { const c = v; v.preventDefault(), d(c.clientX, c.clientY); }, E = (v) => { if (!n) return; const c = v; s(c.clientX, c.clientY) || l(); }, y = () => { l(); }, L = () => { l(); }, w = (v) => { const c = v; if (c.touches.length === 1) { const g = c.touches[0]; v.preventDefault(), d(g.clientX, g.clientY); } }, M = (v) => { if (!n) return; const c = v; if (c.touches.length === 1) { const g = c.touches[0]; s(g.clientX, g.clientY) || l(); } else l(); }, C = () => { l(); }, B = () => { l(); }, P = (v) => { const c = v; c.button === 0 && (v.preventDefault(), d(c.clientX, c.clientY)); }, A = (v) => { if (!n) return; const c = v; s(c.clientX, c.clientY) || l(); }, T = () => { l(); }, k = () => { l(); }; "onpointerdown" in e ? (e.addEventListener( "pointerdown", m ), e.addEventListener( "pointermove", E ), e.addEventListener("pointerup", y), e.addEventListener( "pointercancel", L )) : "ontouchstart" in e ? (e.addEventListener( "touchstart", w, { passive: !1 } ), e.addEventListener("touchmove", M), e.addEventListener("touchend", C), e.addEventListener( "touchcancel", B )) : (e.addEventListener("mousedown", P), e.addEventListener("mousemove", A), e.addEventListener("mouseup", T), e.addEventListener("mouseleave", k)); const D = () => { l(), "onpointerdown" in e ? (e.removeEventListener( "pointerdown", m ), e.removeEventListener( "pointermove", E ), e.removeEventListener( "pointerup", y ), e.removeEventListener( "pointercancel", L )) : "ontouchstart" in e ? (e.removeEventListener( "touchstart", w ), e.removeEventListener( "touchmove", M ), e.removeEventListener("touchend", C), e.removeEventListener( "touchcancel", B )) : (e.removeEventListener( "mousedown", P ), e.removeEventListener( "mousemove", A ), e.removeEventListener("mouseup", T), e.removeEventListener( "mouseleave", k )); }; e._longPressCleanup = D; } } function oe(r) { return new Y(r); } class O extends S { constructor(e) { const t = { ...e, modifiers: e.modifiers || [] }; super(t); h(this, "type", "keyboardShortcut"); h(this, "priority", 80); } apply(e, t) { t.element && this.setupKeyboardShortcut(t.element, this.properties); } setupKeyboardShortcut(e, t) { const o = t.modifiers || [], a = (n) => { const i = n; if (i.key.toLowerCase() !== t.key.toLowerCase()) return; const d = new Set(o), s = /* @__PURE__ */ new Set(); (i.ctrlKey || i.metaKey) && (navigator.platform.includes("Mac") ? (i.metaKey && (s.add("cmd"), s.add("meta")), i.ctrlKey && s.add("ctrl")) : (i.ctrlKey && (s.add("ctrl"), s.add("cmd")), i.metaKey && s.add("meta"))), i.shiftKey && s.add("shift"), i.altKey && s.add("alt"); const l = Array.from(d).every( (y) => s.has(y) ), m = /* @__PURE__ */ new Set([...d]); d.has("cmd") && (m.add("meta"), m.add("ctrl")), d.has("meta") && m.add("cmd"), d.has("ctrl") && m.add("cmd"); const E = Array.from(s).some( (y) => !m.has( y ) ); l && !E && (n.preventDefault(), n.stopPropagation(), t.action()); }, f = e instanceof HTMLElement && e.tabIndex >= 0 ? e : document; f.addEventListener("keydown", a); const p = () => { f.removeEventListener( "keydown", a ); }; e._keyboardShortcutCleanup = p; } } function j(r) { return new O(r); } function re(r, u, e) { return j({ key: r, modifiers: u, action: e }); } class q extends S { constructor() { super(...arguments); h(this, "type", "focused"); h(this, "priority", 75); } apply(e, t) { t.element && this.setupFocusManagement(t.element, this.properties.focused); } setupFocusManagement(e, t) { if (!e || typeof e.focus != "function") return; const o = e; o.tabIndex < 0 && (o.tabIndex = 0); let a; const f = (n) => { n ? requestAnimationFrame(() => { document.activeElement !== o && o.focus(); }) : document.activeElement === o && o.blur(); }; if (typeof t == "function") { const n = t; if (f(n()), n.subscribe) a = n.subscribe((i) => { f(i); }); else if (process.env.NODE_ENV !== "test") { let i = n(), d = !0; const s = () => { if (!d) return; const l = n(); l !== i && (i = l, f(l)), d && requestAnimationFrame(s); }; a = () => { d = !1; }, requestAnimationFrame(s); } else a = () => { }; } else f(t); const p = () => { a && a(); }; e._focusedCleanup = p; } } function ne(r) { return new q({ focused: r }); } class G extends S { constructor(e = {}) { const t = { ...e, interactions: e.interactions || [] }; super(t); h(this, "type", "focusable"); h(this, "priority", 75); } apply(e, t) { t.element && this.setupFocusable(t.element, this.properties); } setupFocusable(e, t) { if (!e || typeof e.focus != "function") return; const o = e, a = t.isFocusable ?? !0, f = t.interactions ?? []; if (a) { o.tabIndex < 0 && (o.tabIndex = 0); const p = o.getAttribute("role"); if (f.includes("edit") ? (p || o.setAttribute("role", "textbox"), o.setAttribute("contenteditable", "true"), o.setAttribute( "aria-label", o.getAttribute("aria-label") || "Editable content" )) : f.includes("activate") && (p || o.setAttribute("role", "button"), o.setAttribute( "aria-label", o.getAttribute("aria-label") || "Activatable element" )), f.includes("activate")) { const n = (d) => { (d.key === " " || d.key === "Enter") && (d.preventDefault(), o.click()); }; o.addEventListener("keydown", n); const i = o._focusableCleanup || (() => { }); o._focusableCleanup = () => { i(), o.removeEventListener("keydown", n); }; } !o.getAttribute("aria-label") && !o.getAttribute("aria-labelledby") && o.setAttribute("aria-label", "Focusable element"), o._focusableCleanup || (o._focusableCleanup = () => { }); } else { o.tabIndex = -1, o.removeAttribute("role"), o.removeAttribute("aria-label"), o.removeAttribute("contenteditable"); const p = o._focusableCleanup; p && (p(), delete o._focusableCleanup); } } } function x(r, u) { return new G({ isFocusable: r ?? !0, interactions: u ?? [] }); } function se() { return x(!0, ["activate"]); } function ie() { return x(!0, ["edit"]); } class I extends S { constructor() { super(...arguments); h(this, "type", "onContinuousHover"); h(this, "priority", 70); } apply(e, t) { t.element && this.setupContinuousHover(t.element, this.properties); } setupContinuousHover(e, t) { const o = t.coordinateSpace ?? "local"; let a = !1; const f = (s) => { if (o === "global") return { x: s.clientX, y: s.clientY }; { const l = e.getBoundingClientRect(); return { x: s.clientX - l.left, y: s.clientY - l.top }; } }, p = (s) => { a = !0; const m = f(s); t.perform(m); }, n = (s) => { if (!a) return; const m = f(s); t.perform(m); }, i = () => { a = !1, t.perform(null); }; e.addEventListener("mouseenter", p), e.addEventListener("mousemove", n), e.addEventListener("mouseleave", i); const d = () => { a && (a = !1, t.perform(null)), e.removeEventListener( "mouseenter", p ), e.removeEventListener("mousemove", n), e.removeEventListener( "mouseleave", i ); }; e._continuousHoverCleanup = d; } } function _(r) { return new I(r); } function ae(r) { return _({ coordinateSpace: "local", perform: r }); } function le(r) { return _({ coordinateSpace: "global", perform: r }); } class R extends S { constructor() { super(...arguments); h(this, "type", "allowsHitTesting"); h(this, "priority", 95); } apply(e, t) { t.element && this.setupHitTesting(t.element, this.properties.enabled); } setupHitTesting(e, t) { if (!e || typeof e.style != "object") return; const o = e; t ? o.style.pointerEvents === "none" && (o.style.pointerEvents = "") : o.style.pointerEvents = "none", e._hitTestingEnabled = t; } } function H(r) { return new R({ enabled: r }); } function ce() { return H(!0); } function ue() { return H(!1); } export { R as A, q as F, O as K, Y as O, b as S, $ as a, Q as b, W as c, Z as d, ee as e, te as f, oe as g, re as h, ne as i, G as j, j as k, x as l, se as m, ie as n, J as o, I as p, _ as q, ae as r, U as s, le as t, H as u, ce as v, ue as w }; //# sourceMappingURL=allows-hit-testing-DscPrcOw.js.map