@human-kit/ui 1.0.0-beta.9 → 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 (98) 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 +8 -0
  39. package/dist/index.js +8 -0
  40. package/dist/internal/localized-strings.d.ts +152 -0
  41. package/dist/internal/localized-strings.js +152 -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/rating/README.md +58 -0
  60. package/dist/rating/index.d.ts +7 -0
  61. package/dist/rating/index.js +6 -0
  62. package/dist/rating/index.parts.d.ts +4 -0
  63. package/dist/rating/index.parts.js +4 -0
  64. package/dist/rating/item/rating-item.svelte +177 -0
  65. package/dist/rating/item/rating-item.svelte.d.ts +4 -0
  66. package/dist/rating/label/rating-label.svelte +42 -0
  67. package/dist/rating/label/rating-label.svelte.d.ts +4 -0
  68. package/dist/rating/output/rating-output.svelte +38 -0
  69. package/dist/rating/output/rating-output.svelte.d.ts +4 -0
  70. package/dist/rating/root/context.d.ts +87 -0
  71. package/dist/rating/root/context.js +15 -0
  72. package/dist/rating/root/rating-root.svelte +521 -0
  73. package/dist/rating/root/rating-root.svelte.d.ts +4 -0
  74. package/dist/rating/root/rating-utils.d.ts +28 -0
  75. package/dist/rating/root/rating-utils.js +53 -0
  76. package/dist/rating/types.d.ts +124 -0
  77. package/dist/rating/types.js +1 -0
  78. package/dist/search-field/README.md +29 -0
  79. package/dist/search-field/clear/README.md +5 -0
  80. package/dist/search-field/clear/search-field-clear.svelte +99 -0
  81. package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
  82. package/dist/search-field/index.d.ts +15 -0
  83. package/dist/search-field/index.js +6 -0
  84. package/dist/search-field/index.parts.d.ts +4 -0
  85. package/dist/search-field/index.parts.js +4 -0
  86. package/dist/search-field/input/README.md +5 -0
  87. package/dist/search-field/input/search-field-input.svelte +158 -0
  88. package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
  89. package/dist/search-field/label/README.md +3 -0
  90. package/dist/search-field/label/search-field-label.svelte +27 -0
  91. package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
  92. package/dist/search-field/root/README.md +5 -0
  93. package/dist/search-field/root/context.d.ts +28 -0
  94. package/dist/search-field/root/context.js +15 -0
  95. package/dist/search-field/root/search-field-root.svelte +230 -0
  96. package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
  97. package/dist/tooltip/root/tooltip-root.svelte +16 -11
  98. package/package.json +21 -1
@@ -0,0 +1,521 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import { SvelteMap } from 'svelte/reactivity';
4
+ import { readable } from 'svelte/store';
5
+ import { dev } from '../../internal/environment';
6
+ import { resolveLocalizedString } from '../../internal/localized-strings';
7
+ import { isRtl } from '../../internal/rtl';
8
+ import { getLocaleContext } from '../../locale-provider/context';
9
+ import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
10
+ import {
11
+ focusWithModality,
12
+ shouldShowFocusVisible,
13
+ trackInteractionModality
14
+ } from '../../primitives/input-modality';
15
+ import type { RatingRootProps } from '../types';
16
+ import { setRatingContext, type RatingChangeDetails, type RatingContext } from './context';
17
+ import {
18
+ getRatingItemFill,
19
+ getRatingValueFromPointer,
20
+ normalizeRatingCount,
21
+ normalizeRatingPrecision,
22
+ snapRatingValue
23
+ } from './rating-utils';
24
+
25
+ /**
26
+ * Rating.Root — the scale, the value and the keyboard.
27
+ *
28
+ * With a precision of 1 the root is a radio group, and each item is a radio: each value is
29
+ * one item, and that is what a screen reader reads best. With a smaller precision the root is
30
+ * a slider, because a radio group cannot say 3.5. The root is the one tab stop then, and the
31
+ * items are decoration.
32
+ */
33
+ const generatedId = $props.id();
34
+
35
+ let {
36
+ id: idProp,
37
+ value = $bindable(),
38
+ defaultValue,
39
+ controlledValue = false,
40
+ onChange,
41
+ onChangeEnd,
42
+ count = 5,
43
+ precision = 1,
44
+ allowClear = true,
45
+ disabled = false,
46
+ readonly = false,
47
+ required = false,
48
+ invalid = false,
49
+ name,
50
+ form,
51
+ getItemLabel,
52
+ getValueText,
53
+ 'aria-label': ariaLabel,
54
+ 'aria-labelledby': ariaLabelledBy,
55
+ 'aria-describedby': ariaDescribedBy,
56
+ children,
57
+ class: className = '',
58
+ element = $bindable<HTMLDivElement | null>(null),
59
+ context = $bindable(),
60
+ onkeydown,
61
+ onkeyup,
62
+ onpointerleave,
63
+ style,
64
+ ...restProps
65
+ }: RatingRootProps = $props();
66
+
67
+ const localeStore = getLocaleContext()?.locale ?? readable<string | undefined>(undefined);
68
+
69
+ const instanceId = untrack(() => idProp) ?? generatedId;
70
+ const rootId = `rating-${instanceId}`;
71
+
72
+ let rootRef: HTMLDivElement | null = $state(null);
73
+ let labelId: string | null = $state(null);
74
+ let hoverValue: number | null = $state(null);
75
+ let focusedIndex: number | null = $state(null);
76
+ let focusVisible = $state(false);
77
+ let rtl = $derived(isRtl(rootRef));
78
+
79
+ type ItemRegistration = { elementRef: () => HTMLElement | null };
80
+ const items = new SvelteMap<number, ItemRegistration>();
81
+
82
+ const resolvedCount = $derived(normalizeRatingCount(count));
83
+ const resolvedPrecision = $derived(normalizeRatingPrecision(precision));
84
+ const isRadioGroup = $derived(resolvedPrecision === 1);
85
+ const resolvedLocale = $derived($localeStore);
86
+
87
+ $effect(() => {
88
+ element = rootRef;
89
+ });
90
+
91
+ // Read one time: the component writes the value back, thus a check that follows the value
92
+ // would report the echo of its own write.
93
+ if (dev && untrack(() => value !== undefined && defaultValue !== undefined)) {
94
+ console.warn(
95
+ '[Rating]: Both "value" and "defaultValue" are provided. ' +
96
+ 'Use "value" for controlled mode or "defaultValue" for uncontrolled mode, not both.'
97
+ );
98
+ }
99
+
100
+ // --- Value --------------------------------------------------------------------------------
101
+
102
+ const initialValue = untrack(() => value) ?? untrack(() => defaultValue) ?? 0;
103
+ let valueInternal = $state(initialValue);
104
+
105
+ // Controlled-ness is opt-in, not inferred: `bind:value` and `value={...}` are the same thing
106
+ // at runtime. The prop wins whenever the consumer supplies it.
107
+ const currentValue = $derived(
108
+ controlledValue || value !== undefined ? (value ?? valueInternal) : valueInternal
109
+ );
110
+ const displayValue = $derived(hoverValue ?? currentValue);
111
+
112
+ // A `bind:value` that starts undefined gets the value at mount, thus the parent reads the
113
+ // default without a first change.
114
+ $effect(() => {
115
+ if (controlledValue) return;
116
+ untrack(() => {
117
+ if (value === undefined) value = valueInternal;
118
+ });
119
+ });
120
+
121
+ function setValue(nextRaw: number, details: RatingChangeDetails): boolean {
122
+ if (disabled || readonly) return false;
123
+ const next = snapRatingValue(nextRaw, resolvedCount, resolvedPrecision);
124
+ if (next === currentValue) return false;
125
+ if (!controlledValue) {
126
+ valueInternal = next;
127
+ value = next;
128
+ }
129
+ onChange?.(next, details);
130
+ return true;
131
+ }
132
+
133
+ function commitValue(details: RatingChangeDetails) {
134
+ onChangeEnd?.(currentValue, details);
135
+ }
136
+
137
+ function stepValue(direction: -1 | 1, event?: Event): boolean {
138
+ const lowest = allowClear ? 0 : resolvedPrecision;
139
+ const next = currentValue + direction * resolvedPrecision;
140
+ if (next < lowest) return false;
141
+ return setValue(next, { reason: 'keyboard', event });
142
+ }
143
+
144
+ // --- Text ---------------------------------------------------------------------------------
145
+
146
+ function defaultValueText(val: number): string {
147
+ if (val === 0) return resolveLocalizedString(resolvedLocale, 'rating.empty');
148
+ return resolveLocalizedString(resolvedLocale, 'rating.valueText', {
149
+ value: val,
150
+ count: resolvedCount
151
+ });
152
+ }
153
+
154
+ function valueTextOf(val: number): string {
155
+ return getValueText ? getValueText(val, resolvedCount) : defaultValueText(val);
156
+ }
157
+
158
+ function itemLabelOf(val: number): string {
159
+ if (getItemLabel) return getItemLabel(val, resolvedCount);
160
+ return resolveLocalizedString(resolvedLocale, 'rating.valueText', {
161
+ value: val,
162
+ count: resolvedCount
163
+ });
164
+ }
165
+
166
+ // --- Pointer ------------------------------------------------------------------------------
167
+
168
+ /** The value of a press inside one item: the whole item, or the part the pointer is on. */
169
+ function valueFromPointer(itemValue: number, event: PointerEvent, target: HTMLElement): number {
170
+ if (isRadioGroup) return itemValue;
171
+ const rect = target.getBoundingClientRect();
172
+ const inside = getRatingValueFromPointer(event.clientX, rect, {
173
+ count: 1,
174
+ precision: resolvedPrecision,
175
+ rtl
176
+ });
177
+ return snapRatingValue(itemValue - 1 + inside, resolvedCount, resolvedPrecision);
178
+ }
179
+
180
+ function pressItem(itemValue: number, event: PointerEvent) {
181
+ if (disabled || readonly || event.button !== 0) return;
182
+ const target = event.currentTarget;
183
+ if (!(target instanceof HTMLElement)) return;
184
+
185
+ // The press must not select the text beside the items, and the focus below must carry the
186
+ // pointer modality: a press shows no focus ring.
187
+ event.preventDefault();
188
+ trackInteractionModality(event, target);
189
+ rtl = isRtl(rootRef);
190
+
191
+ const next = valueFromPointer(itemValue, event, target);
192
+ const cleared = allowClear && next === currentValue;
193
+ const reason = cleared ? 'clear' : 'pointer';
194
+ const changed = setValue(cleared ? 0 : next, { reason, event });
195
+ // The preview follows a pointer that can rest on the items. A finger cannot, and the leave
196
+ // that would end the preview does not come from each browser.
197
+ hoverValue = event.pointerType === 'touch' ? null : cleared ? 0 : next;
198
+ focusValue(cleared ? 0 : next, 'pointer');
199
+ if (changed) commitValue({ reason, event });
200
+ }
201
+
202
+ function setHoverValue(next: number | null) {
203
+ if (disabled || readonly) return;
204
+ hoverValue = next;
205
+ }
206
+
207
+ function handlePointerLeave(
208
+ event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
209
+ ) {
210
+ onpointerleave?.(event);
211
+ hoverValue = null;
212
+ }
213
+
214
+ $effect(() => {
215
+ if (!disabled && !readonly) return;
216
+ hoverValue = null;
217
+ if (disabled) {
218
+ focusedIndex = null;
219
+ focusVisible = false;
220
+ }
221
+ });
222
+
223
+ // --- Focus --------------------------------------------------------------------------------
224
+
225
+ /** The item the tab order lands on: the value, or the first item when there is no value. */
226
+ const tabIndexValue = $derived(
227
+ currentValue >= 1 ? Math.min(Math.ceil(currentValue), resolvedCount) : 1
228
+ );
229
+
230
+ function getItemTabIndex(itemValue: number): number | null {
231
+ if (!isRadioGroup || disabled) return null;
232
+ return itemValue === tabIndexValue ? 0 : -1;
233
+ }
234
+
235
+ function focusValue(val: number, modality: 'keyboard' | 'pointer' = 'keyboard') {
236
+ if (disabled) return;
237
+ // A slider has one tab stop, and it is the root. Without this the arrows after a press
238
+ // went to the body, and the rating did not answer them until the reader pressed Tab.
239
+ if (!isRadioGroup) {
240
+ if (!rootRef || document.activeElement === rootRef) return;
241
+ focusWithModality(rootRef, modality);
242
+ return;
243
+ }
244
+ const index = Math.min(Math.max(Math.ceil(val), 1), resolvedCount) - 1;
245
+ const node = items.get(index)?.elementRef();
246
+ if (!node || document.activeElement === node) return;
247
+ focusWithModality(node, modality);
248
+ }
249
+
250
+ // The focus ring must appear when a pointer press is followed by a key press, and the
251
+ // modality alone changes there: no focus event fires to re-read it. In a radio group the
252
+ // focus is on an item, and the item watches its own.
253
+ watchFocusVisible({
254
+ isFocused: () => !isRadioGroup && focusedIndex !== null,
255
+ element: () => rootRef,
256
+ set: (visible) => {
257
+ focusVisible = visible;
258
+ }
259
+ });
260
+
261
+ // --- Keyboard -----------------------------------------------------------------------------
262
+
263
+ // The keyboard reports `onChange` on each key press, and `onChangeEnd` one time at the
264
+ // release, thus a held key does not report an end on each repeat.
265
+ let keyboardChanged = false;
266
+
267
+ function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
268
+ onkeydown?.(event);
269
+ if (event.defaultPrevented || disabled || readonly) return;
270
+ if (event.ctrlKey || event.metaKey || event.altKey) return;
271
+
272
+ const forward = isRtl(rootRef) ? 'ArrowLeft' : 'ArrowRight';
273
+ const backward = isRtl(rootRef) ? 'ArrowRight' : 'ArrowLeft';
274
+ let changed = false;
275
+
276
+ switch (event.key) {
277
+ case forward:
278
+ case 'ArrowUp':
279
+ changed = stepValue(1, event);
280
+ break;
281
+ case backward:
282
+ case 'ArrowDown':
283
+ changed = stepValue(-1, event);
284
+ break;
285
+ case 'Home':
286
+ changed = setValue(resolvedPrecision, { reason: 'keyboard', event });
287
+ break;
288
+ case 'End':
289
+ changed = setValue(resolvedCount, { reason: 'keyboard', event });
290
+ break;
291
+ case 'Delete':
292
+ case 'Backspace':
293
+ if (!allowClear) return;
294
+ changed = setValue(0, { reason: 'clear', event });
295
+ break;
296
+ default:
297
+ return;
298
+ }
299
+
300
+ event.preventDefault();
301
+ if (changed) keyboardChanged = true;
302
+ // In a radio group the focus follows the value, which is the rule of a radio group: the
303
+ // arrows do not only move, they answer the question.
304
+ focusValue(currentValue);
305
+ }
306
+
307
+ function handleKeyUp(event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
308
+ onkeyup?.(event);
309
+ if (!keyboardChanged) return;
310
+ keyboardChanged = false;
311
+ commitValue({ reason: 'keyboard', event });
312
+ }
313
+
314
+ // --- Form reset ---------------------------------------------------------------------------
315
+
316
+ let hiddenInputRef: HTMLInputElement | null = $state(null);
317
+
318
+ $effect(() => {
319
+ const formElement = hiddenInputRef?.form;
320
+ if (!formElement) return;
321
+
322
+ const handleFormReset = () => {
323
+ // The browser resets the native inputs after the `reset` event; re-sync afterwards.
324
+ queueMicrotask(() => {
325
+ if (currentValue === initialValue) return;
326
+ if (!controlledValue) {
327
+ valueInternal = initialValue;
328
+ value = initialValue;
329
+ }
330
+ onChange?.(initialValue, { reason: 'form-reset' });
331
+ });
332
+ };
333
+
334
+ formElement.addEventListener('reset', handleFormReset);
335
+ return () => formElement.removeEventListener('reset', handleFormReset);
336
+ });
337
+
338
+ // --- Context ------------------------------------------------------------------------------
339
+
340
+ const ratingContext: RatingContext = {
341
+ instanceId,
342
+ rootId,
343
+ get labelId() {
344
+ return labelId;
345
+ },
346
+ get value() {
347
+ return currentValue;
348
+ },
349
+ get displayValue() {
350
+ return displayValue;
351
+ },
352
+ get count() {
353
+ return resolvedCount;
354
+ },
355
+ get precision() {
356
+ return resolvedPrecision;
357
+ },
358
+ get allowClear() {
359
+ return allowClear;
360
+ },
361
+ get isDisabled() {
362
+ return disabled;
363
+ },
364
+ get isReadOnly() {
365
+ return readonly;
366
+ },
367
+ get isRequired() {
368
+ return required;
369
+ },
370
+ get isInvalid() {
371
+ return invalid;
372
+ },
373
+ get isHovering() {
374
+ return hoverValue !== null;
375
+ },
376
+ get isFocusVisible() {
377
+ return focusVisible;
378
+ },
379
+ get isRtl() {
380
+ return rtl;
381
+ },
382
+ get isRadioGroup() {
383
+ return isRadioGroup;
384
+ },
385
+ get ariaLabel() {
386
+ return ariaLabel;
387
+ },
388
+ get ariaLabelledBy() {
389
+ return ariaLabelledBy;
390
+ },
391
+ get ariaDescribedBy() {
392
+ return ariaDescribedBy;
393
+ },
394
+ get focusedIndex() {
395
+ return focusedIndex;
396
+ },
397
+ registerLabel(id) {
398
+ labelId = id;
399
+ return () => {
400
+ if (labelId === id) labelId = null;
401
+ };
402
+ },
403
+ registerItem(options) {
404
+ let index = options.index;
405
+ if (index === undefined) {
406
+ index = 0;
407
+ while (items.has(index)) index += 1;
408
+ }
409
+ items.set(index, { elementRef: options.elementRef });
410
+ const registeredIndex = index;
411
+ return {
412
+ index: registeredIndex,
413
+ unregister: () => {
414
+ if (items.get(registeredIndex)?.elementRef === options.elementRef) {
415
+ items.delete(registeredIndex);
416
+ }
417
+ }
418
+ };
419
+ },
420
+ getItemId(index) {
421
+ return `${rootId}-item-${index}`;
422
+ },
423
+ getItemFill(itemValue) {
424
+ return getRatingItemFill(itemValue, displayValue);
425
+ },
426
+ getItemLabel: itemLabelOf,
427
+ getValueText: valueTextOf,
428
+ getItemTabIndex,
429
+ setValue,
430
+ stepValue,
431
+ commitValue,
432
+ pressItem,
433
+ setHoverValue,
434
+ focusValue: (val) => focusValue(val),
435
+ setItemFocus(index, focused) {
436
+ if (focused) {
437
+ focusedIndex = index;
438
+ } else if (focusedIndex === index) {
439
+ focusedIndex = null;
440
+ focusVisible = false;
441
+ }
442
+ },
443
+ setFocusVisible(visible) {
444
+ focusVisible = visible;
445
+ }
446
+ };
447
+
448
+ setRatingContext(ratingContext);
449
+ context = ratingContext;
450
+
451
+ const defaultLabel = $derived(resolveLocalizedString(resolvedLocale, 'rating.label'));
452
+ const labelledBy = $derived(ariaLabelledBy ?? labelId ?? undefined);
453
+ const sliderTabIndex = $derived(isRadioGroup || disabled ? undefined : 0);
454
+ const inlineStyle = $derived(
455
+ `--rating-value: ${currentValue}; --rating-display-value: ${displayValue};` +
456
+ ` --rating-count: ${resolvedCount};${style ? ` ${style}` : ''}`
457
+ );
458
+
459
+ function handleRootFocus(event: FocusEvent) {
460
+ if (isRadioGroup || disabled) return;
461
+ trackInteractionModality(event, rootRef);
462
+ focusedIndex = 0;
463
+ focusVisible = shouldShowFocusVisible(rootRef);
464
+ }
465
+
466
+ function handleRootBlur() {
467
+ if (isRadioGroup) return;
468
+ focusedIndex = null;
469
+ focusVisible = false;
470
+ }
471
+ </script>
472
+
473
+ <!-- The role is a radio group, or a slider at a precision below 1. The slider is the one tab
474
+ stop of the rating, and the checker cannot read a role that the props decide. -->
475
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
476
+ <div
477
+ {...restProps}
478
+ bind:this={rootRef}
479
+ id={rootId}
480
+ role={isRadioGroup ? 'radiogroup' : 'slider'}
481
+ tabindex={sliderTabIndex}
482
+ aria-labelledby={labelledBy}
483
+ aria-label={labelledBy ? undefined : (ariaLabel ?? defaultLabel)}
484
+ aria-describedby={ariaDescribedBy}
485
+ aria-required={required || undefined}
486
+ aria-invalid={invalid || undefined}
487
+ aria-disabled={disabled || undefined}
488
+ aria-readonly={readonly || undefined}
489
+ aria-valuemin={isRadioGroup ? undefined : 0}
490
+ aria-valuemax={isRadioGroup ? undefined : resolvedCount}
491
+ aria-valuenow={isRadioGroup ? undefined : currentValue}
492
+ aria-valuetext={isRadioGroup ? undefined : valueTextOf(currentValue)}
493
+ aria-orientation={isRadioGroup ? undefined : 'horizontal'}
494
+ class={className}
495
+ style={inlineStyle}
496
+ data-rating-root="true"
497
+ data-disabled={disabled || undefined}
498
+ data-readonly={readonly || undefined}
499
+ data-required={required || undefined}
500
+ data-invalid={invalid || undefined}
501
+ data-hovering={hoverValue !== null || undefined}
502
+ data-focus-within={focusedIndex !== null || undefined}
503
+ data-focus-visible={(focusedIndex !== null && focusVisible) || undefined}
504
+ onkeydown={handleKeyDown}
505
+ onkeyup={handleKeyUp}
506
+ onpointerleave={handlePointerLeave}
507
+ onfocus={handleRootFocus}
508
+ onblur={handleRootBlur}
509
+ >
510
+ {#if name}
511
+ <input
512
+ bind:this={hiddenInputRef}
513
+ type="hidden"
514
+ {name}
515
+ {form}
516
+ value={currentValue}
517
+ data-rating-input="true"
518
+ />
519
+ {/if}
520
+ {@render children?.()}
521
+ </div>
@@ -0,0 +1,4 @@
1
+ import type { RatingRootProps } from '../types';
2
+ declare const RatingRoot: import("svelte").Component<RatingRootProps, {}, "value" | "element" | "context">;
3
+ type RatingRoot = ReturnType<typeof RatingRoot>;
4
+ export default RatingRoot;
@@ -0,0 +1,28 @@
1
+ /** The value of one step. A precision above 1, or a precision that is not a number, becomes 1. */
2
+ export declare function normalizeRatingPrecision(precision: number): number;
3
+ /** The count of stars. A count below 1, or a count that is not a number, becomes 5. */
4
+ export declare function normalizeRatingCount(count: number): number;
5
+ /**
6
+ * The nearest value on the scale. The result is a multiple of the precision, between 0 and the
7
+ * count. The multiplication removes the error of the binary fractions: 0.1 + 0.2 is 0.30000000004,
8
+ * and a rating of 0.3 must stay 0.3.
9
+ */
10
+ export declare function snapRatingValue(value: number, count: number, precision: number): number;
11
+ /**
12
+ * The value under the pointer. The measure starts at the left edge of the row, or at the right
13
+ * edge in a right-to-left context. The value is always at least one step: a press on the first
14
+ * star gives one star, and never zero.
15
+ */
16
+ export declare function getRatingValueFromPointer(clientX: number, rect: {
17
+ left: number;
18
+ width: number;
19
+ }, options: {
20
+ count: number;
21
+ precision: number;
22
+ rtl: boolean;
23
+ }): number;
24
+ /**
25
+ * How much of one star the value fills, from 0 to 1. The star at 3 is full while the value is 3
26
+ * or more, it is empty at 2 or less, and it is half full at 2.5.
27
+ */
28
+ export declare function getRatingItemFill(itemValue: number, value: number): number;
@@ -0,0 +1,53 @@
1
+ /** The value of one step. A precision above 1, or a precision that is not a number, becomes 1. */
2
+ export function normalizeRatingPrecision(precision) {
3
+ if (!Number.isFinite(precision) || precision <= 0 || precision > 1)
4
+ return 1;
5
+ return precision;
6
+ }
7
+ /** The count of stars. A count below 1, or a count that is not a number, becomes 5. */
8
+ export function normalizeRatingCount(count) {
9
+ if (!Number.isFinite(count) || count < 1)
10
+ return 5;
11
+ return Math.floor(count);
12
+ }
13
+ /**
14
+ * The nearest value on the scale. The result is a multiple of the precision, between 0 and the
15
+ * count. The multiplication removes the error of the binary fractions: 0.1 + 0.2 is 0.30000000004,
16
+ * and a rating of 0.3 must stay 0.3.
17
+ */
18
+ export function snapRatingValue(value, count, precision) {
19
+ if (!Number.isFinite(value))
20
+ return 0;
21
+ const steps = Math.round(value / precision);
22
+ const maxSteps = Math.round(count / precision);
23
+ const clamped = Math.min(Math.max(steps, 0), maxSteps);
24
+ const decimals = getDecimalCount(precision);
25
+ return Number((clamped * precision).toFixed(decimals));
26
+ }
27
+ function getDecimalCount(precision) {
28
+ const text = String(precision);
29
+ const dot = text.indexOf('.');
30
+ return dot === -1 ? 0 : Math.min(text.length - dot - 1, 10);
31
+ }
32
+ /**
33
+ * The value under the pointer. The measure starts at the left edge of the row, or at the right
34
+ * edge in a right-to-left context. The value is always at least one step: a press on the first
35
+ * star gives one star, and never zero.
36
+ */
37
+ export function getRatingValueFromPointer(clientX, rect, options) {
38
+ const { count, precision, rtl } = options;
39
+ if (rect.width <= 0)
40
+ return precision;
41
+ const distance = rtl ? rect.left + rect.width - clientX : clientX - rect.left;
42
+ const fraction = Math.min(Math.max(distance / rect.width, 0), 1);
43
+ const raw = Math.ceil((fraction * count) / precision) * precision;
44
+ return Math.max(snapRatingValue(raw, count, precision), precision);
45
+ }
46
+ /**
47
+ * How much of one star the value fills, from 0 to 1. The star at 3 is full while the value is 3
48
+ * or more, it is empty at 2 or less, and it is half full at 2.5.
49
+ */
50
+ export function getRatingItemFill(itemValue, value) {
51
+ const fill = value - (itemValue - 1);
52
+ return Math.min(Math.max(fill, 0), 1);
53
+ }