@human-kit/ui 1.5.0 → 1.5.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.
@@ -198,8 +198,15 @@
198
198
  buttonRef?.getAttribute('aria-expanded') === 'true';
199
199
  }
200
200
 
201
+ // Only a button that opts in with `data-pressed-when-expanded` needs to watch `aria-expanded`.
202
+ // The attribute comes in through the props, so the check is reactive. Before, every button
203
+ // made a `MutationObserver` for nothing, and a page with 1000 buttons made 1000 of them.
204
+ const watchesExpanded = $derived(
205
+ (restProps as Record<string, unknown>)['data-pressed-when-expanded'] === 'true'
206
+ );
207
+
201
208
  $effect(() => {
202
- if (!buttonRef) {
209
+ if (!buttonRef || !watchesExpanded) {
203
210
  expandedPressed = false;
204
211
  return;
205
212
  }
@@ -2,9 +2,45 @@
2
2
  type PointerPressOwner = {
3
3
  key: symbol;
4
4
  pointerId: number | null;
5
+ /** Clears the pressed state of the owning item. */
6
+ release: () => void;
5
7
  };
6
8
 
7
9
  let pointerPressOwner: PointerPressOwner | null = null;
10
+
11
+ // A press can end anywhere on the page, so the end of the press is seen on `window`. One
12
+ // handler, shared by all the items and counted by the mounted items, serves them all. Before,
13
+ // each item added three listeners, and a list of 1000 items ran 3000 handlers on every
14
+ // pointer end.
15
+ let pointerEndListenerRefCount = 0;
16
+
17
+ function handleGlobalPointerEnd(event: PointerEvent | MouseEvent) {
18
+ const owner = pointerPressOwner;
19
+ if (!owner) return;
20
+ const pointerId = 'pointerId' in event ? event.pointerId : null;
21
+ if (owner.pointerId !== null && pointerId !== null && owner.pointerId !== pointerId) return;
22
+ owner.release();
23
+ }
24
+
25
+ function acquireGlobalPointerEndListeners() {
26
+ if (typeof window === 'undefined') return;
27
+ if (pointerEndListenerRefCount === 0) {
28
+ window.addEventListener('pointerup', handleGlobalPointerEnd);
29
+ window.addEventListener('pointercancel', handleGlobalPointerEnd);
30
+ window.addEventListener('mouseup', handleGlobalPointerEnd);
31
+ }
32
+ pointerEndListenerRefCount += 1;
33
+ }
34
+
35
+ function releaseGlobalPointerEndListeners() {
36
+ if (typeof window === 'undefined') return;
37
+ pointerEndListenerRefCount = Math.max(0, pointerEndListenerRefCount - 1);
38
+ if (pointerEndListenerRefCount === 0) {
39
+ window.removeEventListener('pointerup', handleGlobalPointerEnd);
40
+ window.removeEventListener('pointercancel', handleGlobalPointerEnd);
41
+ window.removeEventListener('mouseup', handleGlobalPointerEnd);
42
+ }
43
+ }
8
44
  </script>
9
45
 
10
46
  <script lang="ts">
@@ -159,17 +195,11 @@
159
195
  listboxCtx.keyboardNav.updateItems();
160
196
  }
161
197
 
162
- window.addEventListener('pointerup', handleGlobalPointerEnd);
163
- window.addEventListener('pointercancel', handleGlobalPointerEnd);
164
- window.addEventListener('mouseup', handleGlobalPointerEnd);
198
+ acquireGlobalPointerEndListeners();
165
199
  });
166
200
 
167
201
  onDestroy(() => {
168
- if (typeof window !== 'undefined') {
169
- window.removeEventListener('pointerup', handleGlobalPointerEnd);
170
- window.removeEventListener('pointercancel', handleGlobalPointerEnd);
171
- window.removeEventListener('mouseup', handleGlobalPointerEnd);
172
- }
202
+ releaseGlobalPointerEndListeners();
173
203
  clearOwnedPointerPress();
174
204
  listboxCtx.unregisterItem(id);
175
205
  unsubscribeSelection?.();
@@ -238,7 +268,7 @@
238
268
  }
239
269
 
240
270
  function startPointerPress(pointerId: number | null) {
241
- pointerPressOwner = { key: pointerPressOwnerKey, pointerId };
271
+ pointerPressOwner = { key: pointerPressOwnerKey, pointerId, release: clearPressedState };
242
272
  isPressed = true;
243
273
  pressedKey = null;
244
274
  }
@@ -249,14 +279,6 @@
249
279
  clearOwnedPointerPress();
250
280
  }
251
281
 
252
- function handleGlobalPointerEnd(event: PointerEvent | MouseEvent) {
253
- if (!ownsPointerPress(event)) {
254
- return;
255
- }
256
-
257
- clearPressedState();
258
- }
259
-
260
282
  function listboxHasDomFocus() {
261
283
  const root = elementRef?.closest('[role="listbox"]');
262
284
  return !!root && !!document.activeElement && root.contains(document.activeElement);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",