| 1 | /* pointer.js: Docs' half of the light. rastrillo's light.js (served |
| 2 | beside this as static/light.js) lights rastrillo's own buttons and |
| 3 | cards; this does the same for Docs' own controls, which it does not |
| 4 | know: the toolbar's buttons, the shelf's pages, the list's rows and |
| 5 | the menus' items. |
| 6 | |
| 7 | What it writes, on the one element under the pointer, and nothing |
| 8 | else: --rst-mx and --rst-my, the pointer in the element's own pixels |
| 9 | (light.js's names and units, so one stylesheet vocabulary lights |
| 10 | both), and --docs-tx and --docs-ty, the same position from -1 to 1 |
| 11 | across the element, for the shelf's tilt. It clears them when the |
| 12 | pointer leaves, and every property has a default in the stylesheet, |
| 13 | so deleting this file and its tag leaves the still design. |
| 14 | |
| 15 | A press writes where it landed as --rst-px and --rst-py on the |
| 16 | control, light.js's names again, and the stylesheet blooms the light |
| 17 | there while it is held and fades it after release (brand/light.css, |
| 18 | --docs-press and --docs-flare). |
| 19 | |
| 20 | It never runs for touch, under reduced motion or forced colours, or |
| 21 | without a hovering pointer; one animation frame per move at most. */ |
| 22 | (function () { |
| 23 | "use strict"; |
| 24 | if (!window.matchMedia) return; |
| 25 | var LIT = "[docs-tool],[docs-tile],[docs-list] [rst-row],[estilo-menu-item]"; |
| 26 | var PROPS = ["--rst-mx", "--rst-my", "--docs-tx", "--docs-ty"]; |
| 27 | var quiet = [matchMedia("(prefers-reduced-motion: reduce)"), matchMedia("(forced-colors: active)")]; |
| 28 | var hover = matchMedia("(hover: hover)"); |
| 29 | var lit = null, pending = null, raf = 0, listening = false; |
| 30 | |
| 31 | function clear() { |
| 32 | if (!lit) return; |
| 33 | for (var i = 0; i < PROPS.length; i++) lit.style.removeProperty(PROPS[i]); |
| 34 | lit = null; |
| 35 | } |
| 36 | function step() { |
| 37 | raf = 0; |
| 38 | var e = pending; |
| 39 | pending = null; |
| 40 | if (!e) return; |
| 41 | var el = e.target && e.target.closest ? e.target.closest(LIT) : null; |
| 42 | if (el !== lit) clear(); |
| 43 | if (!el) return; |
| 44 | lit = el; |
| 45 | var r = el.getBoundingClientRect(); |
| 46 | if (!r.width || !r.height) return; |
| 47 | var x = e.clientX - r.left, y = e.clientY - r.top; |
| 48 | el.style.setProperty("--rst-mx", x.toFixed(0) + "px"); |
| 49 | el.style.setProperty("--rst-my", y.toFixed(0) + "px"); |
| 50 | el.style.setProperty("--docs-tx", Math.max(-1, Math.min(1, (x / r.width) * 2 - 1)).toFixed(3)); |
| 51 | el.style.setProperty("--docs-ty", Math.max(-1, Math.min(1, (y / r.height) * 2 - 1)).toFixed(3)); |
| 52 | } |
| 53 | function move(e) { |
| 54 | if (e.pointerType && e.pointerType !== "mouse" && e.pointerType !== "pen") return; |
| 55 | pending = e; |
| 56 | if (!raf) raf = requestAnimationFrame(step); |
| 57 | } |
| 58 | function out(e) { |
| 59 | if (!e.relatedTarget) clear(); |
| 60 | } |
| 61 | function press(e) { |
| 62 | if (e.button !== 0 || (e.pointerType && e.pointerType !== "mouse" && e.pointerType !== "pen")) return; |
| 63 | var el = e.target && e.target.closest ? e.target.closest(LIT) : null; |
| 64 | if (!el || el.disabled) return; |
| 65 | var r = el.getBoundingClientRect(); |
| 66 | el.style.setProperty("--rst-px", (e.clientX - r.left).toFixed(0) + "px"); |
| 67 | el.style.setProperty("--rst-py", (e.clientY - r.top).toFixed(0) + "px"); |
| 68 | } |
| 69 | function sync() { |
| 70 | var want = hover.matches && !quiet[0].matches && !quiet[1].matches; |
| 71 | if (want === listening) return; |
| 72 | listening = want; |
| 73 | if (want) { |
| 74 | document.addEventListener("pointermove", move, { passive: true }); |
| 75 | document.addEventListener("pointerout", out, { passive: true }); |
| 76 | document.addEventListener("pointerdown", press, { passive: true }); |
| 77 | } else { |
| 78 | document.removeEventListener("pointermove", move); |
| 79 | document.removeEventListener("pointerout", out); |
| 80 | document.removeEventListener("pointerdown", press); |
| 81 | clear(); |
| 82 | } |
| 83 | } |
| 84 | [hover, quiet[0], quiet[1]].forEach(function (m) { m.addEventListener("change", sync); }); |
| 85 | sync(); |
| 86 | })(); |
| 87 | |