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

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 (134) hide show
  1. package/README.md +27 -60
  2. package/dist/FOCUS_STATE_CONTRACT.md +1 -0
  3. package/dist/color-picker/README.md +78 -0
  4. package/dist/color-picker/area/color-picker-area.svelte +195 -0
  5. package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
  6. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
  7. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
  8. package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
  9. package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
  10. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
  11. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
  12. package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
  13. package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
  14. package/dist/color-picker/index.d.ts +16 -0
  15. package/dist/color-picker/index.js +15 -0
  16. package/dist/color-picker/index.parts.d.ts +12 -0
  17. package/dist/color-picker/index.parts.js +12 -0
  18. package/dist/color-picker/label/color-picker-label.svelte +34 -0
  19. package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
  20. package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
  21. package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
  22. package/dist/color-picker/root/color-picker-root.svelte +365 -0
  23. package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
  24. package/dist/color-picker/root/color.d.ts +68 -0
  25. package/dist/color-picker/root/color.js +266 -0
  26. package/dist/color-picker/root/context.d.ts +127 -0
  27. package/dist/color-picker/root/context.js +44 -0
  28. package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
  29. package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
  30. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
  31. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
  32. package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
  33. package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
  34. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
  35. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
  36. package/dist/color-picker/types.d.ts +176 -0
  37. package/dist/color-picker/types.js +1 -0
  38. package/dist/index.d.ts +10 -0
  39. package/dist/index.js +10 -0
  40. package/dist/internal/localized-strings.d.ts +176 -0
  41. package/dist/internal/localized-strings.js +176 -0
  42. package/dist/pin-input/README.md +55 -0
  43. package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
  44. package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
  45. package/dist/pin-input/index.d.ts +6 -0
  46. package/dist/pin-input/index.js +5 -0
  47. package/dist/pin-input/index.parts.d.ts +3 -0
  48. package/dist/pin-input/index.parts.js +3 -0
  49. package/dist/pin-input/label/pin-input-label.svelte +43 -0
  50. package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
  51. package/dist/pin-input/root/context.d.ts +70 -0
  52. package/dist/pin-input/root/context.js +15 -0
  53. package/dist/pin-input/root/pin-input-root.svelte +337 -0
  54. package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
  55. package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
  56. package/dist/pin-input/root/pin-input-utils.js +44 -0
  57. package/dist/pin-input/types.d.ts +84 -0
  58. package/dist/pin-input/types.js +1 -0
  59. package/dist/primitives/aria-hide-outside.d.ts +13 -0
  60. package/dist/primitives/aria-hide-outside.js +7 -1
  61. package/dist/primitives/focus-trap.d.ts +2 -0
  62. package/dist/primitives/focus-trap.js +7 -1
  63. package/dist/primitives/presence.svelte.js +6 -1
  64. package/dist/rating/README.md +58 -0
  65. package/dist/rating/index.d.ts +7 -0
  66. package/dist/rating/index.js +6 -0
  67. package/dist/rating/index.parts.d.ts +4 -0
  68. package/dist/rating/index.parts.js +4 -0
  69. package/dist/rating/item/rating-item.svelte +177 -0
  70. package/dist/rating/item/rating-item.svelte.d.ts +4 -0
  71. package/dist/rating/label/rating-label.svelte +42 -0
  72. package/dist/rating/label/rating-label.svelte.d.ts +4 -0
  73. package/dist/rating/output/rating-output.svelte +38 -0
  74. package/dist/rating/output/rating-output.svelte.d.ts +4 -0
  75. package/dist/rating/root/context.d.ts +87 -0
  76. package/dist/rating/root/context.js +15 -0
  77. package/dist/rating/root/rating-root.svelte +521 -0
  78. package/dist/rating/root/rating-root.svelte.d.ts +4 -0
  79. package/dist/rating/root/rating-utils.d.ts +28 -0
  80. package/dist/rating/root/rating-utils.js +53 -0
  81. package/dist/rating/types.d.ts +124 -0
  82. package/dist/rating/types.js +1 -0
  83. package/dist/search-field/README.md +29 -0
  84. package/dist/search-field/clear/README.md +5 -0
  85. package/dist/search-field/clear/search-field-clear.svelte +99 -0
  86. package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
  87. package/dist/search-field/index.d.ts +15 -0
  88. package/dist/search-field/index.js +6 -0
  89. package/dist/search-field/index.parts.d.ts +4 -0
  90. package/dist/search-field/index.parts.js +4 -0
  91. package/dist/search-field/input/README.md +5 -0
  92. package/dist/search-field/input/search-field-input.svelte +158 -0
  93. package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
  94. package/dist/search-field/label/README.md +3 -0
  95. package/dist/search-field/label/search-field-label.svelte +27 -0
  96. package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
  97. package/dist/search-field/root/README.md +5 -0
  98. package/dist/search-field/root/context.d.ts +28 -0
  99. package/dist/search-field/root/context.js +15 -0
  100. package/dist/search-field/root/search-field-root.svelte +230 -0
  101. package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
  102. package/dist/toast/README.md +75 -0
  103. package/dist/toast/action/toast-action.svelte +42 -0
  104. package/dist/toast/action/toast-action.svelte.d.ts +4 -0
  105. package/dist/toast/close/toast-close.svelte +41 -0
  106. package/dist/toast/close/toast-close.svelte.d.ts +4 -0
  107. package/dist/toast/content/toast-content.svelte +24 -0
  108. package/dist/toast/content/toast-content.svelte.d.ts +4 -0
  109. package/dist/toast/description/toast-description.svelte +41 -0
  110. package/dist/toast/description/toast-description.svelte.d.ts +4 -0
  111. package/dist/toast/index.d.ts +34 -0
  112. package/dist/toast/index.js +15 -0
  113. package/dist/toast/index.parts.d.ts +9 -0
  114. package/dist/toast/index.parts.js +9 -0
  115. package/dist/toast/positioner/toast-positioner.svelte +75 -0
  116. package/dist/toast/positioner/toast-positioner.svelte.d.ts +4 -0
  117. package/dist/toast/provider/context.d.ts +36 -0
  118. package/dist/toast/provider/context.js +22 -0
  119. package/dist/toast/provider/toast-manager.svelte.d.ts +113 -0
  120. package/dist/toast/provider/toast-manager.svelte.js +210 -0
  121. package/dist/toast/provider/toast-provider.svelte +98 -0
  122. package/dist/toast/provider/toast-provider.svelte.d.ts +4 -0
  123. package/dist/toast/root/context.d.ts +18 -0
  124. package/dist/toast/root/context.js +15 -0
  125. package/dist/toast/root/toast-root.svelte +296 -0
  126. package/dist/toast/root/toast-root.svelte.d.ts +4 -0
  127. package/dist/toast/title/toast-title.svelte +38 -0
  128. package/dist/toast/title/toast-title.svelte.d.ts +4 -0
  129. package/dist/toast/types.d.ts +100 -0
  130. package/dist/toast/types.js +1 -0
  131. package/dist/toast/viewport/toast-viewport.svelte +389 -0
  132. package/dist/toast/viewport/toast-viewport.svelte.d.ts +4 -0
  133. package/dist/tooltip/root/tooltip-root.svelte +16 -11
  134. package/package.json +26 -1
@@ -2,6 +2,7 @@
2
2
  * Focus trap primitive.
3
3
  * Traps keyboard focus within a container element.
4
4
  */
5
+ import { HIDE_OUTSIDE_EXEMPT_ATTRIBUTE } from './aria-hide-outside';
5
6
  import { focusWithModality, getInteractionModality } from './input-modality';
6
7
  const FOCUSABLE_SELECTOR = [
7
8
  'a[href]',
@@ -49,7 +50,8 @@ function resolveInitialFocus(container, initialFocus) {
49
50
  }
50
51
  return initialFocus;
51
52
  }
52
- function getFocusableElements(container) {
53
+ /** The elements in `container` that a `Tab` can reach, in DOM order. */
54
+ export function getFocusableElements(container) {
53
55
  return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter((element) =>
54
56
  // getClientRects covers display:none and detached nodes while still
55
57
  // accepting position:fixed descendants (whose offsetParent is null).
@@ -81,6 +83,10 @@ export function focusTrap(node, options = true) {
81
83
  // nested overlay must not intercept it.
82
84
  if (!isTopmostTrap(node))
83
85
  return;
86
+ // A surface that stays reachable behind the modal, such as a toast viewport, manages its
87
+ // own Tab while the focus is in it, and sends the focus back on its own.
88
+ if (document.activeElement?.closest(`[${HIDE_OUTSIDE_EXEMPT_ATTRIBUTE}]`))
89
+ return;
84
90
  const focusableElements = getFocusableElements(node);
85
91
  if (focusableElements.length === 0) {
86
92
  event.preventDefault();
@@ -9,6 +9,7 @@
9
9
  * Must be called during component initialization — it wires its own `$effect`s, so the machine
10
10
  * starts on mount and tears down (canceling any in-flight motion tracker) on destroy.
11
11
  */
12
+ import { untrack } from 'svelte';
12
13
  import { trackMotionEnd } from './motion';
13
14
  /**
14
15
  * Creates a presence machine driven by `getOpen`, timing enter/exit phases from `getElement`'s
@@ -20,7 +21,11 @@ import { trackMotionEnd } from './motion';
20
21
  */
21
22
  export function createPresence(getOpen, getElement, options = {}) {
22
23
  let isMounted = $state(options.initiallyMounted ?? false);
23
- let isEntering = $state(false);
24
+ // A node that is in the DOM from the first render, such as a toast, must paint its first frame
25
+ // in the enter state. The effect below runs after the mount, and a layout read in between
26
+ // (a measure, a floating position) would fix the final styles first: the enter would then
27
+ // play backwards before it plays forwards. So the enter state starts here, at creation.
28
+ let isEntering = $state(!(options.initiallyMounted ?? false) && untrack(getOpen));
24
29
  let isExiting = $state(false);
25
30
  let tracker;
26
31
  function clearTracker() {
@@ -0,0 +1,58 @@
1
+ # Rating
2
+
3
+ ## Description
4
+
5
+ `Rating` is a form field with a value on a small scale, for example five stars. The user answers with a press or with the arrows. With whole items the rating is a radio group, because each value is one item. With half items it is a slider, because a radio group cannot say 3.5.
6
+
7
+ ## Anatomy
8
+
9
+ - `Rating.Root`
10
+ - `Rating.Label`
11
+ - `Rating.Output`
12
+ - `Rating.Item`
13
+
14
+ ```svelte
15
+ <Rating.Root name="quality" count={5}>
16
+ <Rating.Label>Quality</Rating.Label>
17
+ <Rating.Output />
18
+ {#each { length: 5 } as _, index (index)}
19
+ <Rating.Item />
20
+ {/each}
21
+ </Rating.Root>
22
+ ```
23
+
24
+ ## Usage guidelines
25
+
26
+ - Use `bind:value` for the state, or `value` with `onChange` and `controlledValue` to hold it yourself.
27
+ - Give one `Rating.Item` for each item of `count`.
28
+ - Use `precision={0.5}` for half items. The root is a slider then, and the items are decoration.
29
+ - Paint each item with `--rating-item-fill`, which goes from 0 to 1. A half item has 0.5.
30
+ - `allowClear` is on: a press on the item of the value puts the value back to 0. `Delete` and `Backspace` do the same.
31
+ - Use `getValueText` and `getItemLabel` for the text of the value and of each item.
32
+ - The value follows the pointer before a press: `Rating.Output` and `--rating-display-value` show the value of the press before it.
33
+
34
+ ## API reference
35
+
36
+ - `Rating.Root`
37
+ - `value?: number`
38
+ - `defaultValue?: number`
39
+ - `controlledValue?: boolean`
40
+ - `onChange?: (value, details) => void`
41
+ - `onChangeEnd?: (value, details) => void`
42
+ - `count?: number`, `precision?: number`
43
+ - `allowClear?: boolean`
44
+ - `disabled?: boolean`, `readonly?: boolean`, `required?: boolean`, `invalid?: boolean`
45
+ - `name?: string`, `form?: string`
46
+ - `getItemLabel?: (value, count) => string`
47
+ - `getValueText?: (value, count) => string`
48
+ - `Rating.Item`
49
+ - `index?: number`
50
+ - `aria-label?: string`
51
+
52
+ ## Accessibility
53
+
54
+ - With a precision of 1 the root is a `role="radiogroup"`, and each item is a `role="radio"` with its own name, for example "3 of 5". The focus follows the value, which is the rule of a radio group.
55
+ - With a smaller precision the root is a `role="slider"` with `aria-valuenow` and `aria-valuetext`, and it is the one tab stop.
56
+ - The keyboard: the arrows step by the precision. `Home` goes to the first item, `End` goes to the last one, and `Delete` or `Backspace` clears the value. The horizontal arrows follow the text direction.
57
+ - `Rating.Output` is an `<output>` for the root, with `aria-live="off"`.
58
+ - The value in a form comes from a hidden input. A `<form>` reset takes the first value back.
@@ -0,0 +1,7 @@
1
+ export * as Rating from './index.parts.js';
2
+ export { default as RatingRoot } from './root/rating-root.svelte';
3
+ export { default as RatingLabel } from './label/rating-label.svelte';
4
+ export { default as RatingOutput } from './output/rating-output.svelte';
5
+ export { default as RatingItem } from './item/rating-item.svelte';
6
+ export type { RatingRootProps, RatingLabelProps, RatingOutputProps, RatingOutputRenderState, RatingItemProps, RatingItemRenderState } from './types.js';
7
+ export { getRatingContext, setRatingContext, useRatingContext, type RatingChangeDetails, type RatingChangeReason, type RatingContext } from './root/context.js';
@@ -0,0 +1,6 @@
1
+ export * as Rating from './index.parts.js';
2
+ export { default as RatingRoot } from './root/rating-root.svelte';
3
+ export { default as RatingLabel } from './label/rating-label.svelte';
4
+ export { default as RatingOutput } from './output/rating-output.svelte';
5
+ export { default as RatingItem } from './item/rating-item.svelte';
6
+ export { getRatingContext, setRatingContext, useRatingContext } from './root/context.js';
@@ -0,0 +1,4 @@
1
+ export { default as Root } from './root/rating-root.svelte';
2
+ export { default as Label } from './label/rating-label.svelte';
3
+ export { default as Output } from './output/rating-output.svelte';
4
+ export { default as Item } from './item/rating-item.svelte';
@@ -0,0 +1,4 @@
1
+ export { default as Root } from './root/rating-root.svelte';
2
+ export { default as Label } from './label/rating-label.svelte';
3
+ export { default as Output } from './output/rating-output.svelte';
4
+ export { default as Item } from './item/rating-item.svelte';
@@ -0,0 +1,177 @@
1
+ <script lang="ts">
2
+ import { untrack, type Snippet } from 'svelte';
3
+ import { getRatingValueFromPointer, snapRatingValue } from '../root/rating-utils';
4
+ import { useRatingContext } from '../root/context';
5
+ import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
6
+ import {
7
+ shouldShowFocusVisible,
8
+ trackInteractionModality
9
+ } from '../../primitives/input-modality';
10
+ import type { RatingItemProps, RatingItemRenderState } from '../types';
11
+
12
+ /**
13
+ * Rating.Item — one item of the scale, for example one star.
14
+ *
15
+ * In a radio group the item is a radio, and it is the tab stop of its value. In a slider, at
16
+ * a precision below 1, the item is decoration: the root holds the role and the focus. The
17
+ * item shows how full it is in `--rating-item-fill`, from 0 to 1, thus a half star needs no
18
+ * second element.
19
+ */
20
+ let {
21
+ index: indexProp,
22
+ 'aria-label': ariaLabel,
23
+ children,
24
+ class: className = '',
25
+ element = $bindable<HTMLSpanElement | null>(null),
26
+ style,
27
+ onpointerdown,
28
+ onpointerenter,
29
+ onpointermove,
30
+ onkeydown,
31
+ onfocus,
32
+ onblur,
33
+ ...restProps
34
+ }: RatingItemProps = $props();
35
+
36
+ const ctx = useRatingContext('Rating.Item');
37
+
38
+ let itemRef: HTMLSpanElement | null = $state(null);
39
+
40
+ const registration = ctx.registerItem({
41
+ index: untrack(() => indexProp),
42
+ elementRef: () => itemRef
43
+ });
44
+ const index = registration.index;
45
+ const value = $derived(index + 1);
46
+
47
+ $effect(() => {
48
+ element = itemRef;
49
+ return () => {
50
+ element = null;
51
+ };
52
+ });
53
+
54
+ $effect(() => registration.unregister);
55
+
56
+ const fill = $derived(ctx.getItemFill(value));
57
+ const selected = $derived(ctx.value === value);
58
+ const highlighted = $derived(fill > 0);
59
+ const focused = $derived(ctx.focusedIndex === index);
60
+ const focusVisible = $derived(focused && ctx.isFocusVisible);
61
+ const tabIndex = $derived(ctx.getItemTabIndex(value));
62
+
63
+ const renderState: RatingItemRenderState = $derived({
64
+ value,
65
+ index,
66
+ fill,
67
+ selected,
68
+ highlighted,
69
+ focused
70
+ });
71
+
72
+ /** The value the pointer is on: the whole item, or the part of it under the pointer. */
73
+ function previewValue(event: PointerEvent): number {
74
+ if (ctx.precision === 1 || !itemRef) return value;
75
+ const inside = getRatingValueFromPointer(event.clientX, itemRef.getBoundingClientRect(), {
76
+ count: 1,
77
+ precision: ctx.precision,
78
+ rtl: ctx.isRtl
79
+ });
80
+ return snapRatingValue(value - 1 + inside, ctx.count, ctx.precision);
81
+ }
82
+
83
+ function handlePointerDown(event: PointerEvent & { currentTarget: EventTarget & HTMLElement }) {
84
+ onpointerdown?.(event);
85
+ if (event.defaultPrevented) return;
86
+ ctx.pressItem(value, event);
87
+ }
88
+
89
+ function handlePointerEnter(event: PointerEvent & { currentTarget: EventTarget & HTMLElement }) {
90
+ onpointerenter?.(event);
91
+ // A finger has no preview: it is on the item only while it presses it, and the leave that
92
+ // ends a preview does not come from each browser.
93
+ if (event.pointerType === 'touch') return;
94
+ ctx.setHoverValue(previewValue(event));
95
+ }
96
+
97
+ function handlePointerMove(event: PointerEvent & { currentTarget: EventTarget & HTMLElement }) {
98
+ onpointermove?.(event);
99
+ // A move inside one item changes the preview only at a precision below 1, where the two
100
+ // halves of the item are two values.
101
+ if (event.pointerType === 'touch' || ctx.precision === 1) return;
102
+ ctx.setHoverValue(previewValue(event));
103
+ }
104
+
105
+ function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLElement }) {
106
+ onkeydown?.(event);
107
+ if (event.defaultPrevented || event.key !== ' ') return;
108
+ // The arrows move the value and carry the focus with them, thus Space has work to do only
109
+ // when the focus sits on an item that is not the value: an empty rating.
110
+ event.preventDefault();
111
+ event.stopPropagation();
112
+ if (ctx.setValue(value, { reason: 'keyboard', event })) {
113
+ ctx.commitValue({ reason: 'keyboard', event });
114
+ }
115
+ }
116
+
117
+ function handleFocus(event: FocusEvent & { currentTarget: EventTarget & HTMLElement }) {
118
+ onfocus?.(event);
119
+ trackInteractionModality(event, itemRef);
120
+ ctx.setItemFocus(index, true);
121
+ ctx.setFocusVisible(shouldShowFocusVisible(itemRef));
122
+ }
123
+
124
+ // The focus ring must appear when a pointer press is followed by a key press, and the
125
+ // modality alone changes there: no focus event fires to re-read it.
126
+ watchFocusVisible({
127
+ isFocused: () => focused,
128
+ element: () => itemRef,
129
+ set: (visible) => ctx.setFocusVisible(visible)
130
+ });
131
+
132
+ function handleBlur(event: FocusEvent & { currentTarget: EventTarget & HTMLElement }) {
133
+ onblur?.(event);
134
+ ctx.setItemFocus(index, false);
135
+ }
136
+
137
+ const inlineStyle = $derived(
138
+ `--rating-item-fill: ${fill}; --rating-item-value: ${value};` +
139
+ ` touch-action: none;${style ? ` ${style}` : ''}`
140
+ );
141
+ </script>
142
+
143
+ <!-- The item is a radio in a radio group, and the checker cannot read a role that the context
144
+ decides. In a slider the context gives no tabindex at all. -->
145
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
146
+ <span
147
+ {...restProps}
148
+ bind:this={itemRef}
149
+ id={ctx.getItemId(index)}
150
+ role={ctx.isRadioGroup ? 'radio' : 'presentation'}
151
+ tabindex={tabIndex ?? undefined}
152
+ aria-checked={ctx.isRadioGroup ? selected : undefined}
153
+ aria-label={ctx.isRadioGroup ? (ariaLabel ?? ctx.getItemLabel(value)) : undefined}
154
+ aria-disabled={ctx.isRadioGroup && ctx.isDisabled ? true : undefined}
155
+ class={className}
156
+ style={inlineStyle}
157
+ data-rating-item="true"
158
+ data-index={index}
159
+ data-value={value}
160
+ data-selected={selected || undefined}
161
+ data-highlighted={highlighted || undefined}
162
+ data-partial={(fill > 0 && fill < 1) || undefined}
163
+ data-disabled={ctx.isDisabled || undefined}
164
+ data-readonly={ctx.isReadOnly || undefined}
165
+ data-focused={focused || undefined}
166
+ data-focus-visible={focusVisible || undefined}
167
+ onpointerdown={handlePointerDown}
168
+ onpointerenter={handlePointerEnter}
169
+ onpointermove={handlePointerMove}
170
+ onkeydown={handleKeyDown}
171
+ onfocus={handleFocus}
172
+ onblur={handleBlur}
173
+ >
174
+ {#if children}
175
+ {@render (children as Snippet<[RatingItemRenderState]>)(renderState)}
176
+ {/if}
177
+ </span>
@@ -0,0 +1,4 @@
1
+ import type { RatingItemProps } from '../types';
2
+ declare const RatingItem: import("svelte").Component<RatingItemProps, {}, "element">;
3
+ type RatingItem = ReturnType<typeof RatingItem>;
4
+ export default RatingItem;
@@ -0,0 +1,42 @@
1
+ <script lang="ts">
2
+ import { useRatingContext } from '../root/context';
3
+ import type { RatingLabelProps } from '../types';
4
+
5
+ /**
6
+ * Rating.Label — the accessible name of the rating.
7
+ *
8
+ * It registers its id with the root, which gives it to the root element as `aria-labelledby`.
9
+ * It renders a `<span>` and not a `<label>`, because a `<label>` names one control and a
10
+ * radio group is a set of them. A press on it puts the focus on the rating.
11
+ */
12
+ let {
13
+ children,
14
+ class: className = '',
15
+ id: idProp,
16
+ onclick,
17
+ ...restProps
18
+ }: RatingLabelProps = $props();
19
+
20
+ const ctx = useRatingContext('Rating.Label');
21
+ const id = $derived(idProp ?? `${ctx.rootId}-label`);
22
+
23
+ // The returned unregister runs on destroy, and again whenever the id changes.
24
+ $effect(() => ctx.registerLabel(id));
25
+
26
+ function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLSpanElement }) {
27
+ onclick?.(event);
28
+ if (event.defaultPrevented || ctx.isDisabled) return;
29
+ ctx.focusValue(ctx.value);
30
+ }
31
+ </script>
32
+
33
+ <span
34
+ {...restProps}
35
+ {id}
36
+ class={className}
37
+ data-rating-label="true"
38
+ data-disabled={ctx.isDisabled || undefined}
39
+ onclick={handleClick}
40
+ >
41
+ {@render children?.()}
42
+ </span>
@@ -0,0 +1,4 @@
1
+ import type { RatingLabelProps } from '../types';
2
+ declare const RatingLabel: import("svelte").Component<RatingLabelProps, {}, "">;
3
+ type RatingLabel = ReturnType<typeof RatingLabel>;
4
+ export default RatingLabel;
@@ -0,0 +1,38 @@
1
+ <script lang="ts">
2
+ import { useRatingContext } from '../root/context';
3
+ import type { RatingOutputProps, RatingOutputRenderState } from '../types';
4
+
5
+ /**
6
+ * Rating.Output — the text of the value.
7
+ *
8
+ * It renders an `<output>` that points at the root, with `aria-live="off"`: the root reads
9
+ * its own value, and a live region would read it a second time. The text follows the pointer
10
+ * while the pointer is on the items, thus the reader sees the value of a press before it.
11
+ */
12
+ let { children, class: className = '', ...restProps }: RatingOutputProps = $props();
13
+
14
+ const ctx = useRatingContext('Rating.Output');
15
+
16
+ const text = $derived(ctx.getValueText(ctx.displayValue));
17
+ const renderState = $derived<RatingOutputRenderState>({
18
+ value: ctx.value,
19
+ displayValue: ctx.displayValue,
20
+ count: ctx.count,
21
+ text
22
+ });
23
+ </script>
24
+
25
+ <output
26
+ {...restProps}
27
+ for={ctx.rootId}
28
+ aria-live="off"
29
+ class={className}
30
+ data-rating-output="true"
31
+ data-disabled={ctx.isDisabled || undefined}
32
+ >
33
+ {#if children}
34
+ {@render children(renderState)}
35
+ {:else}
36
+ {text}
37
+ {/if}
38
+ </output>
@@ -0,0 +1,4 @@
1
+ import type { RatingOutputProps } from '../types';
2
+ declare const RatingOutput: import("svelte").Component<RatingOutputProps, {}, "">;
3
+ type RatingOutput = ReturnType<typeof RatingOutput>;
4
+ export default RatingOutput;
@@ -0,0 +1,87 @@
1
+ /** How a value changed. */
2
+ export type RatingChangeReason = 'keyboard' | 'pointer' | 'clear' | 'form-reset';
3
+ export type RatingChangeDetails = {
4
+ reason: RatingChangeReason;
5
+ event?: Event;
6
+ };
7
+ /**
8
+ * Context shared between Rating.Root and its parts.
9
+ */
10
+ export type RatingContext = {
11
+ /** The instance id. Every ARIA id of the parts is made from it. */
12
+ instanceId: string;
13
+ /** The id of the root element. */
14
+ rootId: string;
15
+ /** The id of the `Rating.Label` element, when one is in the DOM. */
16
+ labelId: string | null;
17
+ /** The value, from 0 to `count`. */
18
+ value: number;
19
+ /** The value the user sees: the value under the pointer, or the value. */
20
+ displayValue: number;
21
+ /** The count of items. */
22
+ count: number;
23
+ /** The distance between two values. 1 gives whole stars, and 0.5 gives half stars. */
24
+ precision: number;
25
+ /** Whether a press on the selected item puts the value back to 0. */
26
+ allowClear: boolean;
27
+ isDisabled: boolean;
28
+ isReadOnly: boolean;
29
+ isRequired: boolean;
30
+ isInvalid: boolean;
31
+ /** Whether the pointer is on the items. */
32
+ isHovering: boolean;
33
+ /** Whether the focus in the rating must show as keyboard focus. */
34
+ isFocusVisible: boolean;
35
+ /** Whether the root is in a right-to-left context. */
36
+ isRtl: boolean;
37
+ /**
38
+ * Whether each item is a radio. A precision of 1 makes a radio group, because each value is
39
+ * one item. A smaller precision makes a slider: a radio group cannot say 3.5.
40
+ */
41
+ isRadioGroup: boolean;
42
+ /** The `aria-label` given to the root. */
43
+ ariaLabel: string | undefined;
44
+ /** The `aria-labelledby` given to the root. */
45
+ ariaLabelledBy: string | undefined;
46
+ /** The `aria-describedby` given to the root. */
47
+ ariaDescribedBy: string | undefined;
48
+ /** The index of the item that has the focus, or null. */
49
+ focusedIndex: number | null;
50
+ /** Registers the id of `Rating.Label`. Returns the unregister function. */
51
+ registerLabel: (id: string) => () => void;
52
+ /** Registers an item. Without an index, the item takes the next free one, in mount order. */
53
+ registerItem: (options: {
54
+ index?: number;
55
+ elementRef: () => HTMLElement | null;
56
+ }) => {
57
+ index: number;
58
+ unregister: () => void;
59
+ };
60
+ /** The id of one item element. */
61
+ getItemId: (index: number) => string;
62
+ /** How much of one item the value fills, from 0 to 1. */
63
+ getItemFill: (itemValue: number) => number;
64
+ /** The text of one item, for example "3 Stars". */
65
+ getItemLabel: (itemValue: number) => string;
66
+ /** The text of the value, for `Rating.Output` and for `aria-valuetext`. */
67
+ getValueText: (value: number) => string;
68
+ /** The `tabindex` of one item. Null keeps the item out of the tab order. */
69
+ getItemTabIndex: (itemValue: number) => number | null;
70
+ /** Writes a value. The value is snapped and clamped. Returns whether the value changed. */
71
+ setValue: (value: number, details: RatingChangeDetails) => boolean;
72
+ /** Moves the value by one step, or to a value the keyboard names. */
73
+ stepValue: (direction: -1 | 1, event?: Event) => boolean;
74
+ /** Reports the end of a sequence of changes: a key press, a press on an item. */
75
+ commitValue: (details: RatingChangeDetails) => void;
76
+ /** Answers a press on an item, with the position of the pointer inside it. */
77
+ pressItem: (itemValue: number, event: PointerEvent) => void;
78
+ /** Holds the value the pointer is on, for the preview. Null ends the preview. */
79
+ setHoverValue: (value: number | null) => void;
80
+ /** Puts the focus on the item of a value. */
81
+ focusValue: (value: number) => void;
82
+ setItemFocus: (index: number, focused: boolean) => void;
83
+ setFocusVisible: (visible: boolean) => void;
84
+ };
85
+ export declare function setRatingContext(ctx: RatingContext): void;
86
+ export declare function getRatingContext(): RatingContext | undefined;
87
+ export declare function useRatingContext(part: string): RatingContext;
@@ -0,0 +1,15 @@
1
+ import { getContext, setContext } from 'svelte';
2
+ const RATING_KEY = Symbol('rating');
3
+ export function setRatingContext(ctx) {
4
+ setContext(RATING_KEY, ctx);
5
+ }
6
+ export function getRatingContext() {
7
+ return getContext(RATING_KEY);
8
+ }
9
+ export function useRatingContext(part) {
10
+ const ctx = getRatingContext();
11
+ if (!ctx) {
12
+ throw new Error(`${part} must be used inside a Rating.Root`);
13
+ }
14
+ return ctx;
15
+ }