@human-kit/ui 1.0.0-beta.7 → 1.0.0-beta.8

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.
Files changed (167) hide show
  1. package/dist/FOCUS_STATE_CONTRACT.md +15 -4
  2. package/dist/FOCUS_STATE_REVIEW_TEMPLATE.md +8 -8
  3. package/dist/avatar/README.md +52 -0
  4. package/dist/avatar/count/avatar-count.svelte +56 -0
  5. package/dist/avatar/count/avatar-count.svelte.d.ts +4 -0
  6. package/dist/avatar/fallback/avatar-fallback.svelte +67 -0
  7. package/dist/avatar/fallback/avatar-fallback.svelte.d.ts +4 -0
  8. package/dist/avatar/group/avatar-group.svelte +65 -0
  9. package/dist/avatar/group/avatar-group.svelte.d.ts +4 -0
  10. package/dist/avatar/group/context.d.ts +15 -0
  11. package/dist/avatar/group/context.js +15 -0
  12. package/dist/avatar/image/avatar-image.svelte +104 -0
  13. package/dist/avatar/image/avatar-image.svelte.d.ts +4 -0
  14. package/dist/avatar/index.d.ts +22 -0
  15. package/dist/avatar/index.js +10 -0
  16. package/dist/avatar/index.parts.d.ts +5 -0
  17. package/dist/avatar/index.parts.js +5 -0
  18. package/dist/avatar/root/avatar-root.svelte +79 -0
  19. package/dist/avatar/root/avatar-root.svelte.d.ts +4 -0
  20. package/dist/avatar/root/context.d.ts +14 -0
  21. package/dist/avatar/root/context.js +15 -0
  22. package/dist/avatar/types.d.ts +84 -0
  23. package/dist/avatar/types.js +1 -0
  24. package/dist/breadcrumbs/README.md +65 -0
  25. package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte +62 -0
  26. package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte.d.ts +4 -0
  27. package/dist/breadcrumbs/index.d.ts +23 -0
  28. package/dist/breadcrumbs/index.js +10 -0
  29. package/dist/breadcrumbs/index.parts.d.ts +6 -0
  30. package/dist/breadcrumbs/index.parts.js +6 -0
  31. package/dist/breadcrumbs/item/breadcrumbs-item.svelte +42 -0
  32. package/dist/breadcrumbs/item/breadcrumbs-item.svelte.d.ts +4 -0
  33. package/dist/breadcrumbs/link/breadcrumbs-link.svelte +47 -0
  34. package/dist/breadcrumbs/link/breadcrumbs-link.svelte.d.ts +4 -0
  35. package/dist/breadcrumbs/list/breadcrumbs-list.svelte +31 -0
  36. package/dist/breadcrumbs/list/breadcrumbs-list.svelte.d.ts +4 -0
  37. package/dist/breadcrumbs/root/breadcrumbs-root.svelte +112 -0
  38. package/dist/breadcrumbs/root/breadcrumbs-root.svelte.d.ts +4 -0
  39. package/dist/breadcrumbs/root/context.d.ts +19 -0
  40. package/dist/breadcrumbs/root/context.js +15 -0
  41. package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte +35 -0
  42. package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte.d.ts +4 -0
  43. package/dist/breadcrumbs/types.d.ts +80 -0
  44. package/dist/breadcrumbs/types.js +1 -0
  45. package/dist/combobox/popover/combobox-popover.svelte +4 -56
  46. package/dist/dialog/root/dialog-root.svelte +15 -2
  47. package/dist/drawer/root/drawer-root.svelte +15 -2
  48. package/dist/index.d.ts +14 -0
  49. package/dist/index.js +13 -0
  50. package/dist/internal/localized-strings.d.ts +48 -0
  51. package/dist/internal/localized-strings.js +48 -0
  52. package/dist/internal/number-precision.d.ts +9 -0
  53. package/dist/internal/number-precision.js +27 -0
  54. package/dist/menu/root/focus-state.d.ts +2 -2
  55. package/dist/menu/root/focus-state.js +8 -32
  56. package/dist/menu/root/menu-state.svelte.js +5 -1
  57. package/dist/numberfield/root/number-utils.js +1 -20
  58. package/dist/popover/root/focus-state.d.ts +2 -2
  59. package/dist/popover/root/focus-state.js +7 -34
  60. package/dist/primitives/contain-wheel.d.ts +9 -0
  61. package/dist/primitives/contain-wheel.js +41 -0
  62. package/dist/primitives/floating.d.ts +6 -0
  63. package/dist/primitives/floating.js +26 -5
  64. package/dist/primitives/keyboard-navigation.d.ts +6 -0
  65. package/dist/primitives/keyboard-navigation.js +2 -1
  66. package/dist/primitives/trigger-focus-state.d.ts +29 -0
  67. package/dist/primitives/trigger-focus-state.js +57 -0
  68. package/dist/progress/README.md +54 -0
  69. package/dist/progress/index.d.ts +21 -0
  70. package/dist/progress/index.js +9 -0
  71. package/dist/progress/index.parts.d.ts +5 -0
  72. package/dist/progress/index.parts.js +5 -0
  73. package/dist/progress/indicator/progress-indicator.svelte +37 -0
  74. package/dist/progress/indicator/progress-indicator.svelte.d.ts +4 -0
  75. package/dist/progress/label/progress-label.svelte +34 -0
  76. package/dist/progress/label/progress-label.svelte.d.ts +4 -0
  77. package/dist/progress/root/context.d.ts +26 -0
  78. package/dist/progress/root/context.js +15 -0
  79. package/dist/progress/root/progress-root.svelte +136 -0
  80. package/dist/progress/root/progress-root.svelte.d.ts +4 -0
  81. package/dist/progress/root/progress-utils.d.ts +12 -0
  82. package/dist/progress/root/progress-utils.js +41 -0
  83. package/dist/progress/track/progress-track.svelte +27 -0
  84. package/dist/progress/track/progress-track.svelte.d.ts +4 -0
  85. package/dist/progress/types.d.ts +81 -0
  86. package/dist/progress/types.js +1 -0
  87. package/dist/progress/value/progress-value.svelte +36 -0
  88. package/dist/progress/value/progress-value.svelte.d.ts +4 -0
  89. package/dist/select/README.md +129 -0
  90. package/dist/select/index.d.ts +30 -0
  91. package/dist/select/index.js +16 -0
  92. package/dist/select/index.parts.d.ts +8 -0
  93. package/dist/select/index.parts.js +9 -0
  94. package/dist/select/item/select-item.svelte +68 -0
  95. package/dist/select/item/select-item.svelte.d.ts +10 -0
  96. package/dist/select/item-indicator/select-item-indicator.svelte +55 -0
  97. package/dist/select/item-indicator/select-item-indicator.svelte.d.ts +4 -0
  98. package/dist/select/label/select-label.svelte +46 -0
  99. package/dist/select/label/select-label.svelte.d.ts +4 -0
  100. package/dist/select/list/select-list.svelte +241 -0
  101. package/dist/select/list/select-list.svelte.d.ts +62 -0
  102. package/dist/select/popover/select-popover.svelte +80 -0
  103. package/dist/select/popover/select-popover.svelte.d.ts +9 -0
  104. package/dist/select/root/context.d.ts +106 -0
  105. package/dist/select/root/context.js +22 -0
  106. package/dist/select/root/select-root.svelte +544 -0
  107. package/dist/select/root/select-root.svelte.d.ts +25 -0
  108. package/dist/select/trigger/select-trigger.svelte +192 -0
  109. package/dist/select/trigger/select-trigger.svelte.d.ts +4 -0
  110. package/dist/select/types.d.ts +133 -0
  111. package/dist/select/types.js +1 -0
  112. package/dist/select/value/select-value.svelte +61 -0
  113. package/dist/select/value/select-value.svelte.d.ts +4 -0
  114. package/dist/separator/README.md +35 -0
  115. package/dist/separator/index.d.ts +7 -0
  116. package/dist/separator/index.js +3 -0
  117. package/dist/separator/separator.svelte +254 -0
  118. package/dist/separator/separator.svelte.d.ts +45 -0
  119. package/dist/separator/types.d.ts +1 -0
  120. package/dist/separator/types.js +1 -0
  121. package/dist/slider/README.md +61 -0
  122. package/dist/slider/fill/slider-fill.svelte +40 -0
  123. package/dist/slider/fill/slider-fill.svelte.d.ts +4 -0
  124. package/dist/slider/index.d.ts +9 -0
  125. package/dist/slider/index.js +8 -0
  126. package/dist/slider/index.parts.d.ts +6 -0
  127. package/dist/slider/index.parts.js +6 -0
  128. package/dist/slider/label/slider-label.svelte +44 -0
  129. package/dist/slider/label/slider-label.svelte.d.ts +4 -0
  130. package/dist/slider/output/slider-output.svelte +46 -0
  131. package/dist/slider/output/slider-output.svelte.d.ts +4 -0
  132. package/dist/slider/root/context.d.ts +106 -0
  133. package/dist/slider/root/context.js +15 -0
  134. package/dist/slider/root/slider-root.svelte +545 -0
  135. package/dist/slider/root/slider-root.svelte.d.ts +4 -0
  136. package/dist/slider/root/slider-utils.d.ts +57 -0
  137. package/dist/slider/root/slider-utils.js +88 -0
  138. package/dist/slider/thumb/slider-thumb.svelte +276 -0
  139. package/dist/slider/thumb/slider-thumb.svelte.d.ts +4 -0
  140. package/dist/slider/track/slider-track.svelte +60 -0
  141. package/dist/slider/track/slider-track.svelte.d.ts +4 -0
  142. package/dist/slider/types.d.ts +144 -0
  143. package/dist/slider/types.js +1 -0
  144. package/dist/tooltip/README.md +53 -0
  145. package/dist/tooltip/arrow/tooltip-arrow.svelte +36 -0
  146. package/dist/tooltip/arrow/tooltip-arrow.svelte.d.ts +4 -0
  147. package/dist/tooltip/content/tooltip-content.svelte +148 -0
  148. package/dist/tooltip/content/tooltip-content.svelte.d.ts +4 -0
  149. package/dist/tooltip/index.d.ts +8 -0
  150. package/dist/tooltip/index.js +7 -0
  151. package/dist/tooltip/index.parts.d.ts +5 -0
  152. package/dist/tooltip/index.parts.js +5 -0
  153. package/dist/tooltip/provider/tooltip-provider.svelte +36 -0
  154. package/dist/tooltip/provider/tooltip-provider.svelte.d.ts +4 -0
  155. package/dist/tooltip/root/context.d.ts +46 -0
  156. package/dist/tooltip/root/context.js +22 -0
  157. package/dist/tooltip/root/safe-polygon.d.ts +26 -0
  158. package/dist/tooltip/root/safe-polygon.js +66 -0
  159. package/dist/tooltip/root/tooltip-group.d.ts +26 -0
  160. package/dist/tooltip/root/tooltip-group.js +41 -0
  161. package/dist/tooltip/root/tooltip-root.svelte +454 -0
  162. package/dist/tooltip/root/tooltip-root.svelte.d.ts +4 -0
  163. package/dist/tooltip/trigger/tooltip-trigger.svelte +52 -0
  164. package/dist/tooltip/trigger/tooltip-trigger.svelte.d.ts +4 -0
  165. package/dist/tooltip/types.d.ts +105 -0
  166. package/dist/tooltip/types.js +1 -0
  167. package/package.json +36 -1
@@ -1,12 +1,10 @@
1
- import { focusWithModality, resolveCloseInteractionModality } from '../../primitives/input-modality';
2
- export function clearTriggerFocusState(trigger) {
3
- delete trigger.dataset.focused;
4
- delete trigger.dataset.focusVisible;
5
- }
1
+ import { applyTriggerCloseFocusState as applyTriggerCloseFocusStateShared, addTriggerBlurCleanup, clearTriggerFocusState, focusFellToBody } from '../../primitives/trigger-focus-state';
2
+ export { addTriggerBlurCleanup, clearTriggerFocusState, focusFellToBody };
6
3
  // Per APG/React Aria/Radix, focus returns to the trigger only for keyboard or
7
4
  // imperative closes. User-directed dismissals ('outside-press', 'focus-out',
8
5
  // 'scroll') must leave focus where the user just put it (e.g. the caret in an
9
- // outside input, or the element tabbed to).
6
+ // outside input, or the element tabbed to). The one exception, an outside press
7
+ // that left the focus on nothing, lives in the shared primitive.
10
8
  const TRIGGER_REFOCUS_CLOSE_REASONS = new Set([
11
9
  'escape-key',
12
10
  'close-press',
@@ -16,32 +14,7 @@ export function shouldRefocusTriggerOnClose(reason) {
16
14
  return TRIGGER_REFOCUS_CLOSE_REASONS.has(reason);
17
15
  }
18
16
  export function applyTriggerCloseFocusState(trigger, reason, event) {
19
- const closeModality = resolveCloseInteractionModality(reason, event);
20
- if (shouldRefocusTriggerOnClose(reason)) {
21
- focusWithModality(trigger, closeModality);
22
- }
23
- if (reason === 'outside-press' || reason === 'escape-key') {
24
- trigger.dataset.focused = 'true';
25
- }
26
- else {
27
- delete trigger.dataset.focused;
28
- }
29
- if (closeModality === 'keyboard') {
30
- trigger.dataset.focusVisible = 'true';
31
- }
32
- else {
33
- delete trigger.dataset.focusVisible;
34
- }
35
- }
36
- export function addTriggerBlurCleanup(trigger, once = false) {
37
- const handleBlur = () => {
38
- clearTriggerFocusState(trigger);
39
- if (once) {
40
- trigger.removeEventListener('blur', handleBlur);
41
- }
42
- };
43
- trigger.addEventListener('blur', handleBlur);
44
- return () => {
45
- trigger.removeEventListener('blur', handleBlur);
46
- };
17
+ applyTriggerCloseFocusStateShared(trigger, reason, event, {
18
+ refocus: shouldRefocusTriggerOnClose(reason)
19
+ });
47
20
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Keeps a wheel event inside a floating panel.
3
+ *
4
+ * A non-modal popover closes when the page scrolls under it, and a wheel over a list that
5
+ * has reached its end would scroll the page. So the event scrolls the panel, or a scrollable
6
+ * element inside it, while one of them can still move in that direction, and it is consumed
7
+ * otherwise.
8
+ */
9
+ export declare function containWheel(event: WheelEvent, panel: HTMLElement): void;
@@ -0,0 +1,41 @@
1
+ function canElementScrollInDirection(element, deltaY) {
2
+ const isScrollingDown = deltaY > 0;
3
+ const isScrollingUp = deltaY < 0;
4
+ const canScrollDown = element.scrollTop < element.scrollHeight - element.clientHeight;
5
+ const canScrollUp = element.scrollTop > 0;
6
+ return (isScrollingDown && canScrollDown) || (isScrollingUp && canScrollUp);
7
+ }
8
+ function isScrollableElement(element) {
9
+ const { overflowY } = getComputedStyle(element);
10
+ return (['auto', 'scroll', 'overlay'].includes(overflowY) && element.scrollHeight > element.clientHeight);
11
+ }
12
+ function hasScrollableDescendantForWheel(boundary, target, deltaY) {
13
+ let current = target instanceof HTMLElement ? target : target instanceof Node ? target.parentElement : null;
14
+ while (current) {
15
+ if (isScrollableElement(current) && canElementScrollInDirection(current, deltaY)) {
16
+ return true;
17
+ }
18
+ if (current === boundary) {
19
+ break;
20
+ }
21
+ current = current.parentElement;
22
+ }
23
+ return false;
24
+ }
25
+ /**
26
+ * Keeps a wheel event inside a floating panel.
27
+ *
28
+ * A non-modal popover closes when the page scrolls under it, and a wheel over a list that
29
+ * has reached its end would scroll the page. So the event scrolls the panel, or a scrollable
30
+ * element inside it, while one of them can still move in that direction, and it is consumed
31
+ * otherwise.
32
+ */
33
+ export function containWheel(event, panel) {
34
+ if (hasScrollableDescendantForWheel(panel, event.target, event.deltaY) ||
35
+ (isScrollableElement(panel) && canElementScrollInDirection(panel, event.deltaY))) {
36
+ event.stopPropagation();
37
+ return;
38
+ }
39
+ event.preventDefault();
40
+ event.stopPropagation();
41
+ }
@@ -55,6 +55,12 @@ export type FloatingOptions = {
55
55
  * so a panel that shrinks to fit still leaves the gutter visible.
56
56
  */
57
57
  collisionPadding?: number;
58
+ /**
59
+ * An arrow element inside the floating element. The action moves it along the edge that faces
60
+ * the anchor, with `left` or `top`, and it writes the side in `data-placement`. The offset from
61
+ * that edge is for the CSS of the arrow.
62
+ */
63
+ arrow?: HTMLElement | null;
58
64
  /** Callback when position is updated. */
59
65
  onPositionUpdate?: (x: number, y: number, placement: FloatingPlacement) => void;
60
66
  };
@@ -1,4 +1,4 @@
1
- import { computePosition, flip, shift, limitShift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
1
+ import { arrow as arrowMiddleware, computePosition, flip, shift, limitShift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
2
2
  /**
3
3
  * A zero-size anchor at a viewport point, for panels that follow the pointer
4
4
  * instead of an element — a context menu at the cursor, a toolbar at a text
@@ -24,6 +24,9 @@ export function createPointAnchor(x, y, contextElement) {
24
24
  ...(contextElement ? { contextElement } : {})
25
25
  };
26
26
  }
27
+ function isVirtualAnchor(anchor) {
28
+ return anchor !== null && !(anchor instanceof Element);
29
+ }
27
30
  /** Viewport gutter applied when no `collisionPadding` is given. */
28
31
  const DEFAULT_COLLISION_PADDING = 8;
29
32
  /**
@@ -166,14 +169,21 @@ export function floating(floatingElement, options) {
166
169
  floatingEl.style.maxWidth = `${clampedAvailableWidth}px`;
167
170
  floatingEl.style.maxHeight = `${clampedAvailableHeight}px`;
168
171
  }
169
- })
172
+ }),
173
+ ...(currentOptions.arrow ? [arrowMiddleware({ element: currentOptions.arrow })] : [])
170
174
  ];
171
175
  }
176
+ function applyArrowPosition(arrowElement, data, finalPlacement) {
177
+ const side = finalPlacement.split('-')[0];
178
+ arrowElement.dataset.placement = side;
179
+ arrowElement.style.left = data?.x !== undefined ? `${data.x}px` : '';
180
+ arrowElement.style.top = data?.y !== undefined ? `${data.y}px` : '';
181
+ }
172
182
  async function updatePosition() {
173
183
  const anchor = currentOptions.anchor;
174
184
  if (!anchor || !floatingElement)
175
185
  return;
176
- const { x, y, placement: finalPlacement } = await computePosition(anchor, floatingElement, {
186
+ const { x, y, placement: finalPlacement, middlewareData } = await computePosition(anchor, floatingElement, {
177
187
  placement: normalizeExtendedPlacement(currentOptions.placement || 'bottom'),
178
188
  middleware: buildMiddleware(),
179
189
  strategy: 'fixed' // Use fixed strategy for portal-rendered elements
@@ -182,6 +192,9 @@ export function floating(floatingElement, options) {
182
192
  left: `${x}px`,
183
193
  top: `${y}px`
184
194
  });
195
+ if (currentOptions.arrow) {
196
+ applyArrowPosition(currentOptions.arrow, middlewareData.arrow, finalPlacement);
197
+ }
185
198
  currentOptions.onPositionUpdate?.(x, y, finalPlacement);
186
199
  }
187
200
  function stop() {
@@ -199,13 +212,21 @@ export function floating(floatingElement, options) {
199
212
  return {
200
213
  update(newOptions) {
201
214
  const anchorChanged = newOptions.anchor !== currentOptions.anchor;
215
+ // Two virtual anchors on the same context element are two points on one surface: a
216
+ // panel that follows the pointer makes one per move. The listeners of `autoUpdate`
217
+ // are on the context element, thus they stay, and only the position runs again.
218
+ const sameSurface = anchorChanged &&
219
+ isVirtualAnchor(newOptions.anchor) &&
220
+ isVirtualAnchor(currentOptions.anchor) &&
221
+ newOptions.anchor.contextElement !== undefined &&
222
+ newOptions.anchor.contextElement === currentOptions.anchor.contextElement;
202
223
  currentOptions = newOptions;
203
- if (anchorChanged) {
224
+ if (anchorChanged && !(sameSurface && cleanup)) {
204
225
  // Re-subscribe autoUpdate to the new anchor (or stop when it's gone).
205
226
  start();
206
227
  }
207
228
  else if (cleanup) {
208
- // Same anchor, new placement/offset/boundary: reposition immediately.
229
+ // Same anchor, new placement/offset/boundary/arrow: reposition immediately.
209
230
  void updatePosition();
210
231
  }
211
232
  },
@@ -79,6 +79,12 @@ export type KeyboardNavigationReturn = {
79
79
  setCurrentId: (id: string | number | null) => void;
80
80
  /** Update items (call after DOM changes) */
81
81
  updateItems: () => void;
82
+ /**
83
+ * Searches with a printable character, the same as a key press on the container. For a
84
+ * widget that opens on a character typed outside the container, such as a select: the
85
+ * character is applied once the items exist.
86
+ */
87
+ typeahead: (char: string) => void;
82
88
  };
83
89
  /**
84
90
  * Creates a keyboard navigation controller for list-like components.
@@ -358,7 +358,8 @@ export function createKeyboardNavigation(options = {}) {
358
358
  focusLast,
359
359
  focusById,
360
360
  setCurrentId,
361
- updateItems
361
+ updateItems,
362
+ typeahead: handleTypeahead
362
363
  };
363
364
  }
364
365
  /**
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The focus state of a trigger after its floating panel closes.
3
+ *
4
+ * Shared by Popover and Menu, whose close reasons differ only in which of them return the
5
+ * focus to the trigger. The rule for the attributes is the same for both: the trigger shows
6
+ * `data-focused` and `data-focus-visible` only while it holds the focus. Written by hand for
7
+ * a trigger that does not hold it, they would clear only on a blur of the trigger — and a
8
+ * trigger that never took the focus never blurs.
9
+ */
10
+ export declare function clearTriggerFocusState(trigger: HTMLElement): void;
11
+ /**
12
+ * Whether the last interaction left the focus on nothing: the body, or no element at all.
13
+ *
14
+ * An outside press on a heading, on a paragraph or on the background moves the focus to the
15
+ * body, and a keyboard user then has nowhere to continue from — Space and Enter open nothing.
16
+ * The focus returns to the trigger in that case, and only in that case: a press on a button or
17
+ * an input keeps the focus the user just gave it.
18
+ */
19
+ export declare function focusFellToBody(): boolean;
20
+ export type ApplyTriggerCloseFocusStateOptions = {
21
+ /**
22
+ * Whether the close reason returns the focus to the trigger on its own. An outside press
23
+ * never does, and it is handled here: the focus returns only when the press left it on
24
+ * nothing.
25
+ */
26
+ refocus: boolean;
27
+ };
28
+ export declare function applyTriggerCloseFocusState(trigger: HTMLElement, reason: string, event: Event | undefined, options: ApplyTriggerCloseFocusStateOptions): void;
29
+ export declare function addTriggerBlurCleanup(trigger: HTMLElement, once?: boolean): () => void;
@@ -0,0 +1,57 @@
1
+ import { focusWithModality, resolveCloseInteractionModality } from './input-modality.js';
2
+ /**
3
+ * The focus state of a trigger after its floating panel closes.
4
+ *
5
+ * Shared by Popover and Menu, whose close reasons differ only in which of them return the
6
+ * focus to the trigger. The rule for the attributes is the same for both: the trigger shows
7
+ * `data-focused` and `data-focus-visible` only while it holds the focus. Written by hand for
8
+ * a trigger that does not hold it, they would clear only on a blur of the trigger — and a
9
+ * trigger that never took the focus never blurs.
10
+ */
11
+ export function clearTriggerFocusState(trigger) {
12
+ delete trigger.dataset.focused;
13
+ delete trigger.dataset.focusVisible;
14
+ }
15
+ /**
16
+ * Whether the last interaction left the focus on nothing: the body, or no element at all.
17
+ *
18
+ * An outside press on a heading, on a paragraph or on the background moves the focus to the
19
+ * body, and a keyboard user then has nowhere to continue from — Space and Enter open nothing.
20
+ * The focus returns to the trigger in that case, and only in that case: a press on a button or
21
+ * an input keeps the focus the user just gave it.
22
+ */
23
+ export function focusFellToBody() {
24
+ const active = document.activeElement;
25
+ return !active || active === document.body || active === document.documentElement;
26
+ }
27
+ export function applyTriggerCloseFocusState(trigger, reason, event, options) {
28
+ const closeModality = resolveCloseInteractionModality(reason, event);
29
+ if (options.refocus || (reason === 'outside-press' && focusFellToBody())) {
30
+ focusWithModality(trigger, closeModality);
31
+ }
32
+ const holdsFocus = document.activeElement === trigger;
33
+ if (holdsFocus) {
34
+ trigger.dataset.focused = 'true';
35
+ }
36
+ else {
37
+ delete trigger.dataset.focused;
38
+ }
39
+ if (holdsFocus && closeModality === 'keyboard') {
40
+ trigger.dataset.focusVisible = 'true';
41
+ }
42
+ else {
43
+ delete trigger.dataset.focusVisible;
44
+ }
45
+ }
46
+ export function addTriggerBlurCleanup(trigger, once = false) {
47
+ const handleBlur = () => {
48
+ clearTriggerFocusState(trigger);
49
+ if (once) {
50
+ trigger.removeEventListener('blur', handleBlur);
51
+ }
52
+ };
53
+ trigger.addEventListener('blur', handleBlur);
54
+ return () => {
55
+ trigger.removeEventListener('blur', handleBlur);
56
+ };
57
+ }
@@ -0,0 +1,54 @@
1
+ # Progress
2
+
3
+ ## Description
4
+
5
+ `Progress` shows the status of a task that takes time. It holds a value between `min` and `max`, or no value while the end of the task is not known. A screen reader reads its name and its value.
6
+
7
+ ## Anatomy
8
+
9
+ - `Progress.Root`
10
+ - `Progress.Label`
11
+ - `Progress.Track`
12
+ - `Progress.Indicator`
13
+ - `Progress.Value`
14
+
15
+ ```svelte
16
+ <Progress.Root value={31}>
17
+ <Progress.Label>Export data</Progress.Label>
18
+ <Progress.Value />
19
+ <Progress.Track>
20
+ <Progress.Indicator />
21
+ </Progress.Track>
22
+ </Progress.Root>
23
+ ```
24
+
25
+ ## Usage guidelines
26
+
27
+ - Give the bar a name with `Progress.Label`, or with `aria-label` on the root.
28
+ - Give `value={null}` while the end of the task is not known. Animate the indicator with CSS on `data-indeterminate`.
29
+ - Give `format` to show the value in a unit. Without it, the text is the position in the range as a percentage.
30
+ - Read `percent` and `status` from `bind:context` for a shape of your own, such as a ring.
31
+ - Put `aria-busy="true"` on the region that the task fills. The bar says how far the task is; it does not say what the task changes.
32
+
33
+ ## API reference
34
+
35
+ - `Progress.Root`
36
+ - `value: number | null`
37
+ - `min?: number` (0), `max?: number` (100)
38
+ - `format?: Intl.NumberFormatOptions`
39
+ - `getValueText?: (formattedValue, value) => string`
40
+ - `aria-valuetext?: string`
41
+ - `orientation?: 'horizontal' | 'vertical'` (horizontal)
42
+ - `element?`, `context?`
43
+ - `Progress.Value`
44
+ - `children?: Snippet<[{ formattedValue, value, status }]>`
45
+
46
+ ## Accessibility
47
+
48
+ - `Progress.Root` has `role="progressbar"` with `aria-valuemin`, `aria-valuemax`, `aria-valuenow` and `aria-valuetext`.
49
+ - An indeterminate progress has no `aria-valuenow` and no `aria-valuetext`. A number for a task with no known end would be false.
50
+ - `Progress.Label` writes `aria-labelledby` on the bar. An `aria-labelledby` that you give stands.
51
+ - `Progress.Value` is `aria-hidden`: the bar already gives the value to the screen reader.
52
+ - The value follows the locale of `LocaleProvider`, and the runtime locale without one.
53
+ - The bar takes no focus, and it has no focus state. It is not in the focus contract.
54
+ - The indicator fills from the start edge of the text direction: a right-to-left page fills from the right.
@@ -0,0 +1,21 @@
1
+ import type { ComponentProps } from 'svelte';
2
+ import type ProgressIndicatorComponent from './indicator/progress-indicator.svelte';
3
+ import type ProgressLabelComponent from './label/progress-label.svelte';
4
+ import type ProgressRootComponent from './root/progress-root.svelte';
5
+ import type ProgressTrackComponent from './track/progress-track.svelte';
6
+ import type ProgressValueComponent from './value/progress-value.svelte';
7
+ export * as Progress from './index.parts.js';
8
+ export { default as ProgressRoot } from './root/progress-root.svelte';
9
+ export { default as ProgressLabel } from './label/progress-label.svelte';
10
+ export { default as ProgressTrack } from './track/progress-track.svelte';
11
+ export { default as ProgressIndicator } from './indicator/progress-indicator.svelte';
12
+ export { default as ProgressValue } from './value/progress-value.svelte';
13
+ export type ProgressRootProps = ComponentProps<typeof ProgressRootComponent>;
14
+ export type ProgressLabelProps = ComponentProps<typeof ProgressLabelComponent>;
15
+ export type ProgressTrackProps = ComponentProps<typeof ProgressTrackComponent>;
16
+ export type ProgressIndicatorProps = ComponentProps<typeof ProgressIndicatorComponent>;
17
+ export type ProgressValueProps = ComponentProps<typeof ProgressValueComponent>;
18
+ export type { ProgressValueState } from './types.js';
19
+ export { getProgressContext, setProgressContext, useProgressContext, type ProgressContext, type ProgressOrientation, type ProgressStatus } from './root/context.js';
20
+ import * as ProgressParts from './index.parts.js';
21
+ export default ProgressParts;
@@ -0,0 +1,9 @@
1
+ export * as Progress from './index.parts.js';
2
+ export { default as ProgressRoot } from './root/progress-root.svelte';
3
+ export { default as ProgressLabel } from './label/progress-label.svelte';
4
+ export { default as ProgressTrack } from './track/progress-track.svelte';
5
+ export { default as ProgressIndicator } from './indicator/progress-indicator.svelte';
6
+ export { default as ProgressValue } from './value/progress-value.svelte';
7
+ export { getProgressContext, setProgressContext, useProgressContext } from './root/context.js';
8
+ import * as ProgressParts from './index.parts.js';
9
+ export default ProgressParts;
@@ -0,0 +1,5 @@
1
+ export { default as Root } from './root/progress-root.svelte';
2
+ export { default as Label } from './label/progress-label.svelte';
3
+ export { default as Track } from './track/progress-track.svelte';
4
+ export { default as Indicator } from './indicator/progress-indicator.svelte';
5
+ export { default as Value } from './value/progress-value.svelte';
@@ -0,0 +1,5 @@
1
+ export { default as Root } from './root/progress-root.svelte';
2
+ export { default as Label } from './label/progress-label.svelte';
3
+ export { default as Track } from './track/progress-track.svelte';
4
+ export { default as Indicator } from './indicator/progress-indicator.svelte';
5
+ export { default as Value } from './value/progress-value.svelte';
@@ -0,0 +1,37 @@
1
+ <script lang="ts">
2
+ import type { ProgressIndicatorProps } from '../types.js';
3
+ import { useProgressContext } from '../root/context';
4
+
5
+ /**
6
+ * Progress.Indicator — the part of the track that the task has done.
7
+ *
8
+ * It sizes itself to the percentage: the width from the start edge of a horizontal track, and
9
+ * the height from the bottom of a vertical one. The start edge follows the direction of the
10
+ * text, thus a right-to-left page fills from the right. An indeterminate progress gets no
11
+ * size: your CSS animates it.
12
+ */
13
+ let { children, class: className = '', style, ...restProps }: ProgressIndicatorProps = $props();
14
+
15
+ const progress = useProgressContext('Progress.Indicator');
16
+
17
+ const sizeStyle = $derived.by(() => {
18
+ if (progress.percent === null) return '';
19
+ if (progress.orientation === 'vertical') {
20
+ return `position: absolute; inset-inline: 0; bottom: 0; height: ${progress.percent}%;`;
21
+ }
22
+ return `position: absolute; inset-block: 0; inset-inline-start: 0; width: ${progress.percent}%;`;
23
+ });
24
+ </script>
25
+
26
+ <div
27
+ {...restProps}
28
+ class={className}
29
+ style="{sizeStyle}{style ? ` ${style}` : ''}"
30
+ data-progress-indicator="true"
31
+ data-orientation={progress.orientation}
32
+ data-progressing={progress.status === 'progressing' || undefined}
33
+ data-complete={progress.status === 'complete' || undefined}
34
+ data-indeterminate={progress.status === 'indeterminate' || undefined}
35
+ >
36
+ {@render children?.()}
37
+ </div>
@@ -0,0 +1,4 @@
1
+ import type { ProgressIndicatorProps } from '../types.js';
2
+ declare const ProgressIndicator: import("svelte").Component<ProgressIndicatorProps, {}, "">;
3
+ type ProgressIndicator = ReturnType<typeof ProgressIndicator>;
4
+ export default ProgressIndicator;
@@ -0,0 +1,34 @@
1
+ <script lang="ts">
2
+ import type { ProgressLabelProps } from '../types.js';
3
+ import { useProgressContext } from '../root/context';
4
+
5
+ /**
6
+ * Progress.Label — the name of the task.
7
+ *
8
+ * It registers its id with the root, which gives it to the progress bar as `aria-labelledby`.
9
+ * A bare text beside the bar leaves the bar unnamed: a `role="progressbar"` takes its name
10
+ * from `aria-labelledby` or `aria-label`, never from the text near it. It renders a `<span>`
11
+ * and not a `<label>`, because a `<label>` names a form control, and a progress bar is none.
12
+ */
13
+ let { children, class: className = '', id: idProp, ...restProps }: ProgressLabelProps = $props();
14
+
15
+ const progress = useProgressContext('Progress.Label');
16
+ const generatedId = $props.id();
17
+ const id = $derived(idProp ?? generatedId);
18
+
19
+ // The returned unregister runs on destroy, and again whenever the id changes.
20
+ $effect(() => progress.registerLabel(id));
21
+ </script>
22
+
23
+ <span
24
+ {...restProps}
25
+ {id}
26
+ class={className}
27
+ data-progress-label="true"
28
+ data-orientation={progress.orientation}
29
+ data-progressing={progress.status === 'progressing' || undefined}
30
+ data-complete={progress.status === 'complete' || undefined}
31
+ data-indeterminate={progress.status === 'indeterminate' || undefined}
32
+ >
33
+ {@render children?.()}
34
+ </span>
@@ -0,0 +1,4 @@
1
+ import type { ProgressLabelProps } from '../types.js';
2
+ declare const ProgressLabel: import("svelte").Component<ProgressLabelProps, {}, "">;
3
+ type ProgressLabel = ReturnType<typeof ProgressLabel>;
4
+ export default ProgressLabel;
@@ -0,0 +1,26 @@
1
+ import type { ProgressStatus } from './progress-utils';
2
+ export type ProgressOrientation = 'horizontal' | 'vertical';
3
+ export type ProgressContext = {
4
+ /** The id of the root element. */
5
+ id: string;
6
+ /** The value, held in the range. `null` while the progress is indeterminate. */
7
+ value: number | null;
8
+ min: number;
9
+ max: number;
10
+ /** The position in the range, from 0 to 100. `null` while the progress is indeterminate. */
11
+ percent: number | null;
12
+ status: ProgressStatus;
13
+ orientation: ProgressOrientation;
14
+ /** The value as text, for the user. Empty while the progress is indeterminate. */
15
+ formattedValue: string;
16
+ /** The text a screen reader gets for the value. */
17
+ valueText: string | undefined;
18
+ /** The ids of the registered labels, for `aria-labelledby`. */
19
+ labelledBy: string | undefined;
20
+ /** Registers a label id; the returned function unregisters it. */
21
+ registerLabel: (id: string) => () => void;
22
+ };
23
+ export declare function setProgressContext(context: ProgressContext): void;
24
+ export declare function getProgressContext(): ProgressContext | undefined;
25
+ export declare function useProgressContext(part?: string): ProgressContext;
26
+ export type { ProgressStatus };
@@ -0,0 +1,15 @@
1
+ import { getContext, setContext } from 'svelte';
2
+ const KEY = Symbol('progress');
3
+ export function setProgressContext(context) {
4
+ setContext(KEY, context);
5
+ }
6
+ export function getProgressContext() {
7
+ return getContext(KEY);
8
+ }
9
+ export function useProgressContext(part = 'Progress') {
10
+ const context = getProgressContext();
11
+ if (!context) {
12
+ throw new Error(`${part} must be used within Progress.Root.`);
13
+ }
14
+ return context;
15
+ }