@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,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
+ }