@dile/ui 3.19.2 → 3.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export { DileHighlightMixin } from "./src/DileHighlightMixin.js";
@@ -0,0 +1,134 @@
1
+ const DEFAULT_DURATION = 1200;
2
+ const DEFAULT_SIZE = '4px';
3
+ const DEFAULT_COLOR = 'currentColor';
4
+ const SCROLL_END_TIMEOUT = 800;
5
+ const DEFAULT_SCROLL_OPTIONS = { behavior: 'smooth', block: 'center' };
6
+
7
+ /**
8
+ * Parses a `--dile-highlight-duration` value into milliseconds.
9
+ * Accepts unitless numbers (`1200`), milliseconds (`1200ms`) and seconds (`1.2s`).
10
+ * Returns the default duration (1200) for invalid or non-positive values.
11
+ */
12
+ export function parseHighlightDuration(value) {
13
+ const match = /^([+-]?(?:\d+\.?\d*|\.\d+))(ms|s)?$/i.exec(String(value ?? '').trim());
14
+ if (!match) return DEFAULT_DURATION;
15
+ const amount = parseFloat(match[1]) * (match[2]?.toLowerCase() === 's' ? 1000 : 1);
16
+ return Number.isFinite(amount) && amount > 0 ? amount : DEFAULT_DURATION;
17
+ }
18
+
19
+ function prefersReducedMotion() {
20
+ return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
21
+ }
22
+
23
+ function isFullyVisible(element) {
24
+ if (typeof element.getBoundingClientRect !== 'function') return false;
25
+ const rect = element.getBoundingClientRect();
26
+ const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
27
+ const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
28
+ return rect.top >= 0 && rect.left >= 0 && rect.bottom <= viewportHeight && rect.right <= viewportWidth;
29
+ }
30
+
31
+ /** Resolves on the next `scrollend` (when supported) or after a timeout, whichever comes first. */
32
+ function waitForScrollEnd(timeout = SCROLL_END_TIMEOUT) {
33
+ return new Promise((resolve) => {
34
+ const supportsScrollEnd = 'onscrollend' in window;
35
+ let timer;
36
+ const done = () => {
37
+ clearTimeout(timer);
38
+ if (supportsScrollEnd) window.removeEventListener('scrollend', done, true);
39
+ resolve();
40
+ };
41
+ if (supportsScrollEnd) {
42
+ // Capture phase so non-bubbling scrollend events from scroll containers are caught too.
43
+ window.addEventListener('scrollend', done, true);
44
+ }
45
+ timer = setTimeout(done, timeout);
46
+ });
47
+ }
48
+
49
+ export const DileHighlightMixin = (superclass) => class extends superclass {
50
+ constructor(...args) {
51
+ super(...args);
52
+ this._highlightAnimation = null;
53
+ this._highlightRequest = 0;
54
+ }
55
+
56
+ /** Element to highlight; the host by default. Override it to target an inner element. */
57
+ get highlightTarget() {
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * Scrolls the target into view (optionally) and plays a glow animation on it.
63
+ * @param {{ scroll?: boolean | ScrollIntoViewOptions }} [options]
64
+ * @returns {Promise<Animation | null>}
65
+ */
66
+ async highlight({ scroll = true } = {}) {
67
+ const request = ++this._highlightRequest;
68
+ this._cancelHighlightAnimation();
69
+
70
+ if (this.updateComplete) {
71
+ await this.updateComplete;
72
+ if (request !== this._highlightRequest) return null;
73
+ }
74
+
75
+ const target = this.highlightTarget;
76
+ if (!target) return null;
77
+
78
+ const reduceMotion = prefersReducedMotion();
79
+
80
+ if (scroll && typeof target.scrollIntoView === 'function') {
81
+ const scrollOptions = {
82
+ ...DEFAULT_SCROLL_OPTIONS,
83
+ ...(typeof scroll === 'object' ? scroll : {}),
84
+ };
85
+ if (reduceMotion) scrollOptions.behavior = 'auto';
86
+
87
+ const mustWait = scrollOptions.behavior === 'smooth' && !isFullyVisible(target);
88
+ const scrollEnd = mustWait ? waitForScrollEnd() : null;
89
+ target.scrollIntoView(scrollOptions);
90
+ if (scrollEnd) {
91
+ await scrollEnd;
92
+ if (request !== this._highlightRequest) return null;
93
+ }
94
+ }
95
+
96
+ if (typeof target.animate !== 'function') return null;
97
+
98
+ const styles = getComputedStyle(target);
99
+ const color = styles.getPropertyValue('--dile-highlight-color').trim() || DEFAULT_COLOR;
100
+ const size = styles.getPropertyValue('--dile-highlight-size').trim() || DEFAULT_SIZE;
101
+ const duration = parseHighlightDuration(styles.getPropertyValue('--dile-highlight-duration'));
102
+ const glow = `0 0 0 ${size} ${color}`;
103
+
104
+ const keyframes = reduceMotion
105
+ ? [{ boxShadow: glow }, { boxShadow: glow }]
106
+ : [
107
+ { boxShadow: '0 0 0 0 transparent' },
108
+ { boxShadow: glow, offset: 0.25 },
109
+ { boxShadow: glow, offset: 0.6 },
110
+ { boxShadow: '0 0 0 0 transparent' },
111
+ ];
112
+
113
+ this._cancelHighlightAnimation();
114
+ this._highlightAnimation = target.animate(keyframes, {
115
+ duration,
116
+ easing: reduceMotion ? 'linear' : 'ease',
117
+ // Add the glow on top of any box-shadow the target already has instead of replacing it.
118
+ composite: 'add',
119
+ });
120
+ return this._highlightAnimation;
121
+ }
122
+
123
+ disconnectedCallback() {
124
+ super.disconnectedCallback?.();
125
+ // Invalidate pending highlight() calls and stop the running animation.
126
+ this._highlightRequest++;
127
+ this._cancelHighlightAnimation();
128
+ }
129
+
130
+ _cancelHighlightAnimation() {
131
+ this._highlightAnimation?.cancel();
132
+ this._highlightAnimation = null;
133
+ }
134
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "3.19.2",
3
+ "version": "3.20.1",
4
4
  "description": "UI Core components from dile-components.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -28,5 +28,5 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "545b8f87465a4567904c577699562f3c5f676e68"
31
+ "gitHead": "2312fc5bacc0b5499c99cd25be6ae3509fd026c1"
32
32
  }