@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
package/README.md CHANGED
@@ -1,84 +1,51 @@
1
1
  # @human-kit/ui
2
2
 
3
- Accessible UI components for **Svelte 5**.
4
-
5
- The components use Svelte 5 runes and have full types. The package is native
6
- ESM, and each component has a subpath export, thus your bundler includes only
7
- the components that you import.
3
+ Headless and accessible UI components for **Svelte 5**. More than 40 components
4
+ give you the behavior, and you give them the styles.
8
5
 
9
6
  **[Documentation and live demos → ui.human-kit.com](https://ui.human-kit.com)**
10
7
 
8
+ ## Features
9
+
10
+ - **Headless.** No component has a style. Each part accepts a `class` attribute
11
+ and shows its state in `data-*` attributes.
12
+ - **Accessible.** The components follow the WAI-ARIA Authoring Practices for
13
+ the roles, the keyboard, and the focus.
14
+ - **Ready for each input.** The components work with a mouse, a touch screen,
15
+ and a keyboard.
16
+ - **International.** `LocaleProvider` gives a locale to the dates, the times,
17
+ and the numbers, and the built-in labels are in six languages.
18
+ - **Small.** Native ESM with one subpath export for each component, and one
19
+ dependency at run time: `@floating-ui/dom`.
20
+
11
21
  ## Installation
12
22
 
13
23
  ```bash
14
24
  npm install @human-kit/ui
15
- # or
16
- pnpm add @human-kit/ui
17
25
  ```
18
26
 
19
- Svelte 5 is a peer dependency:
20
-
21
- ```json
22
- "peerDependencies": {
23
- "svelte": "^5.0.0"
24
- }
25
- ```
27
+ Svelte `^5.0.0` is a peer dependency.
26
28
 
27
29
  ## Usage
28
30
 
29
- Import from the root of the package:
30
-
31
- ```svelte
32
- <script lang="ts">
33
- import { Dialog, ComboBox, Input, Label } from '@human-kit/ui';
34
- </script>
35
- ```
36
-
37
- For the smallest bundle, import one component from its subpath:
31
+ Each component is a namespace of parts. Import it from the root of the package,
32
+ or from its subpath for the smallest bundle:
38
33
 
39
34
  ```svelte
40
35
  <script lang="ts">
41
- import { Dialog } from '@human-kit/ui/dialog';
36
+ import { Switch } from '@human-kit/ui/switch';
42
37
  </script>
43
- ```
44
-
45
- ## Components
46
-
47
- | Category | Components |
48
- | ----------- | ----------------------------------------------------------------------------- |
49
- | Overlays | `Dialog`, `Drawer`, `Popover`, `Menu`, `Portal` |
50
- | Forms | `Input`, `TextArea`, `Label`, `Checkbox`, `Switch`, `NumberField`, `Dropzone` |
51
- | Selection | `ComboBox`, `Autocomplete`, `ListBox`, `Toggle`, `ToggleGroup` |
52
- | Date & time | `Calendar`, `Clock`, `DatePicker`, `DateRangePicker`, `TimePicker` |
53
- | Layout | `Accordion`, `Collapsible`, `Tabs`, `Table`, `Tree`, `OverflowRow` |
54
- | Actions | `Button` |
55
- | Utilities | `LocaleProvider`, primitives, and the `cn` class function |
56
-
57
- Each component also has a subpath export, for example `@human-kit/ui/calendar`.
58
38
 
59
- ## Styles
60
-
61
- The components are headless: they have no CSS, and they do not need a CSS
62
- framework. Each part accepts a `class` attribute. Each part also shows its state
63
- in `data-*` attributes, for example `data-state`, `data-disabled`,
64
- `data-focus-visible`, and `data-pressed`. Thus you can write the styles in plain
65
- CSS, in Tailwind, or in a different tool. Each component page in the
66
- documentation gives the full list of the data attributes.
67
-
68
- ```svelte
69
- <Button.Root class="rounded-md bg-black px-3 py-1.5 text-white data-[pressed]:opacity-80">
70
- Save
71
- </Button.Root>
39
+ <Switch.Root
40
+ aria-label="Notifications"
41
+ class="h-5 w-9 rounded-full bg-neutral-300 p-0.5 data-[checked=true]:bg-black"
42
+ >
43
+ <Switch.Thumb class="block size-4 rounded-full bg-white data-[checked=true]:translate-x-4" />
44
+ </Switch.Root>
72
45
  ```
73
46
 
74
- The library has one dependency at run time:
75
- [`@floating-ui/dom`](https://floating-ui.com). Only the components that put an
76
- element against an anchor use it.
77
-
78
- ## Requirements
79
-
80
- - Svelte `^5.0.0`
81
- - Node.js version 20 or later
47
+ Each component page in the documentation gives the anatomy, the data
48
+ attributes, and the full API reference.
82
49
 
83
50
  ## License
84
51
 
@@ -74,5 +74,6 @@ The following components implement this contract:
74
74
  - **ListBox** — items with roving tabindex.
75
75
  - **TransferList** — two ListBoxes plus move buttons. Focus is placed explicitly after every move, because the rows the user was on stop existing: it stays on the button while that button still has work, and otherwise follows the items to the destination list; a double-clicked row hands focus to whichever row took its place.
76
76
  - **NumberField** — spinbutton input, stepper buttons, wheel, and pointer scrub area.
77
+ - **SearchField** — search input and clear button. Only the input shows `data-focused` and `data-focus-visible`; the root shows `data-focus-within`, and `data-focus-visible` while the input shows it. The clear button never takes the focus.
77
78
  - **Slider** — one native range input per thumb. The thumb shows `data-focused` and `data-focus-visible` for its input, and the root shows `data-focus-within` and `data-focus-visible`. A press on the track focuses the thumb with the pointer modality, and a key press after it turns the ring on through `watchFocusVisible`.
78
79
  - **Tooltip** — never takes the focus, and never moves it. It opens on a focus of the trigger only when `shouldShowFocusVisible` says the focus came from the keyboard, thus a focus that a script gave with the pointer modality opens nothing.
@@ -0,0 +1,78 @@
1
+ # ColorPicker
2
+
3
+ ## Description
4
+
5
+ `ColorPicker` is a form field for one color. It holds a square of saturation against brightness, and a slider for each channel. It also holds a hex field, a number field for each channel, a set of swatches and the eye dropper of the browser. The color is held as hue, saturation and brightness, and the value that goes out is text.
6
+
7
+ ## Anatomy
8
+
9
+ - `ColorPicker.Root`
10
+ - `ColorPicker.Label`
11
+ - `ColorPicker.Area`
12
+ - `ColorPicker.AreaThumb`
13
+ - `ColorPicker.Slider`
14
+ - `ColorPicker.SliderThumb`
15
+ - `ColorPicker.HexField`
16
+ - `ColorPicker.ChannelField`
17
+ - `ColorPicker.SwatchList`
18
+ - `ColorPicker.Swatch`
19
+ - `ColorPicker.Preview`
20
+ - `ColorPicker.EyeDropper`
21
+
22
+ ```svelte
23
+ <ColorPicker.Root name="brand" defaultValue="#3366cc">
24
+ <ColorPicker.Label>Brand color</ColorPicker.Label>
25
+ <ColorPicker.Area>
26
+ <ColorPicker.AreaThumb />
27
+ </ColorPicker.Area>
28
+ <ColorPicker.Slider channel="hue">
29
+ <ColorPicker.SliderThumb />
30
+ </ColorPicker.Slider>
31
+ <ColorPicker.HexField />
32
+ <ColorPicker.Preview />
33
+ </ColorPicker.Root>
34
+ ```
35
+
36
+ ## Usage guidelines
37
+
38
+ - Use `bind:value` for the state, or `value` with `onChange` and `controlledValue` to hold it yourself.
39
+ - `format` decides the text of the value: `hex`, `rgb`, `hsl` or `hsb`. The value that comes in can be in any of them.
40
+ - Use `alpha` for a color with an alpha. `ColorPicker.Slider` then accepts `channel="alpha"`.
41
+ - Use `onChangeEnd` for work that must not run on each move of a drag.
42
+ - Give the area a size and the thumb a size. Each thumb is positioned in percent.
43
+ - The CSS custom properties paint the picker: `--color-picker-value` on the root, `--color-picker-hue-color` for the square, `--color-picker-slider-start` and `--color-picker-slider-end` for a track, and `--color-picker-swatch-color` on a swatch and on the preview.
44
+ - Test for the eye dropper before you make it the one way to choose a color: a browser without it gets `data-unsupported`.
45
+
46
+ ## API reference
47
+
48
+ - `ColorPicker.Root`
49
+ - `value?: string`
50
+ - `defaultValue?: string`
51
+ - `controlledValue?: boolean`
52
+ - `onChange?: (value, details) => void`
53
+ - `onChangeEnd?: (value, details) => void`
54
+ - `format?: 'hex' | 'rgb' | 'hsl' | 'hsb'`
55
+ - `alpha?: boolean`
56
+ - `disabled?: boolean`, `readonly?: boolean`, `invalid?: boolean`
57
+ - `name?: string`, `form?: string`
58
+ - `ColorPicker.Area`
59
+ - `xChannel?: ColorChannel`, `yChannel?: ColorChannel`
60
+ - `ColorPicker.Slider`
61
+ - `channel: ColorChannel`
62
+ - `orientation?: 'horizontal' | 'vertical'`
63
+ - `ColorPicker.ChannelField`
64
+ - `channel: ColorChannel`
65
+ - `ColorPicker.Swatch`
66
+ - `color: string`
67
+ - `aria-label?: string`
68
+
69
+ ## Accessibility
70
+
71
+ - The root is a `role="group"` named by `ColorPicker.Label`, `aria-labelledby` or `aria-label`.
72
+ - `ColorPicker.AreaThumb` holds two native sliders, one for each axis, each with the name of its channel and with `aria-valuetext`. That gives a two-dimensional control a value a screen reader can read. The arrows move both axes from either of them.
73
+ - `ColorPicker.SliderThumb` holds one native slider with the name of its channel.
74
+ - The keyboard: the arrows step, `Shift` with an arrow and `PageUp` or `PageDown` move a large step, and `Home` and `End` go to the ends. The horizontal arrows follow the text direction.
75
+ - `ColorPicker.HexField` reads its text at `Enter` and when the focus leaves. A text that names no color goes back to the color of the picker.
76
+ - `ColorPicker.SwatchList` is a `role="listbox"`, and each swatch is an option. One swatch is in the tab order, the arrows move between them, and `Enter` or the space bar takes the color.
77
+ - `ColorPicker.Preview` has no name and no role: the color is already in the fields and in the sliders.
78
+ - The color in a form comes from a hidden input. A `<form>` reset takes the first color back.
@@ -0,0 +1,195 @@
1
+ <script lang="ts">
2
+ import { isRtl } from '../../internal/rtl';
3
+ import { withColorChannel } from '../root/color';
4
+ import { useColorPickerContext, setColorPickerAreaContext } from '../root/context';
5
+ import type { ColorPickerAreaProps } from '../types';
6
+
7
+ /**
8
+ * ColorPicker.Area — the square of two channels.
9
+ *
10
+ * The horizontal axis is the saturation, and the vertical one is the brightness, which counts
11
+ * from the bottom: white is at the top left and black is at the bottom. The area paints
12
+ * nothing of its own. It gives your CSS `--color-picker-hue-color`, and the position of the
13
+ * thumb in `--color-picker-area-x` and `--color-picker-area-y`.
14
+ */
15
+ let {
16
+ xChannel = 'saturation',
17
+ yChannel = 'brightness',
18
+ children,
19
+ class: className = '',
20
+ element = $bindable<HTMLDivElement | null>(null),
21
+ style,
22
+ onpointerdown,
23
+ ...restProps
24
+ }: ColorPickerAreaProps = $props();
25
+
26
+ const ctx = useColorPickerContext('ColorPicker.Area');
27
+
28
+ let areaRef: HTMLDivElement | null = $state(null);
29
+ let dragging = $state(false);
30
+ let rtl = $state(false);
31
+
32
+ const areaId = $derived(ctx.getPartId('area'));
33
+ const xPercent = $derived(ctx.getChannelPercent(xChannel));
34
+ const yPercent = $derived(ctx.getChannelPercent(yChannel));
35
+
36
+ $effect(() => {
37
+ element = areaRef;
38
+ });
39
+
40
+ // The rect is read one time at the press, and again only when the page scrolls: a layout read
41
+ // on each move is what a drag does most.
42
+ let dragRect: DOMRect | null = null;
43
+ let dragPointerId: number | null = null;
44
+ let dragTarget: HTMLElement | null = null;
45
+ let dragChanged = false;
46
+
47
+ function applyPointer(event: PointerEvent) {
48
+ if (!areaRef) return;
49
+ dragRect ??= areaRef.getBoundingClientRect();
50
+ const rect = dragRect;
51
+ if (rect.width <= 0 || rect.height <= 0) return;
52
+
53
+ const rawX = (event.clientX - rect.left) / rect.width;
54
+ const fractionX = Math.min(Math.max(rtl ? 1 - rawX : rawX, 0), 1);
55
+ // The vertical axis counts from the bottom: the top of the square is the highest value.
56
+ const fractionY = Math.min(Math.max((rect.bottom - event.clientY) / rect.height, 0), 1);
57
+
58
+ const xRange = ctx.getChannelRange(xChannel);
59
+ const yRange = ctx.getChannelRange(yChannel);
60
+ const nextX = xRange.min + fractionX * (xRange.max - xRange.min);
61
+ const nextY = yRange.min + fractionY * (yRange.max - yRange.min);
62
+
63
+ // One write for the two axes. Two writes report two changes for one move of the pointer,
64
+ // and the color between them is a color the pointer was never on.
65
+ const next = withColorChannel(withColorChannel(ctx.color, xChannel, nextX), yChannel, nextY);
66
+ if (ctx.setColor(next, { reason: 'pointer', event })) dragChanged = true;
67
+ }
68
+
69
+ function beginDrag(event: PointerEvent, target: HTMLElement) {
70
+ if (ctx.isDisabled || ctx.isReadOnly || event.button !== 0 || dragPointerId !== null) return;
71
+ // The press must not select the page, and the thumb takes the focus below.
72
+ event.preventDefault();
73
+ rtl = isRtl(areaRef);
74
+ dragRect = areaRef?.getBoundingClientRect() ?? null;
75
+ dragPointerId = event.pointerId;
76
+ dragTarget = target;
77
+ dragChanged = false;
78
+ dragging = true;
79
+
80
+ try {
81
+ target.setPointerCapture(event.pointerId);
82
+ } catch {
83
+ // Synthetic test events and older browsers can fail pointer capture.
84
+ }
85
+ target.addEventListener('pointermove', handleDragMove);
86
+ target.addEventListener('pointerup', handleDragEnd);
87
+ target.addEventListener('pointercancel', handleDragEnd);
88
+ target.addEventListener('lostpointercapture', handleDragEnd);
89
+ window.addEventListener('scroll', handleDragScroll, true);
90
+ }
91
+
92
+ function handleDragScroll() {
93
+ dragRect = null;
94
+ }
95
+
96
+ function handleDragMove(event: PointerEvent) {
97
+ if (event.pointerId !== dragPointerId) return;
98
+ applyPointer(event);
99
+ }
100
+
101
+ function handleDragEnd(event: PointerEvent) {
102
+ if (event.pointerId !== dragPointerId) return;
103
+ const changed = dragChanged;
104
+ stopDrag();
105
+ if (changed) ctx.commit({ reason: 'pointer', event });
106
+ }
107
+
108
+ function stopDrag() {
109
+ const target = dragTarget;
110
+ if (target) {
111
+ target.removeEventListener('pointermove', handleDragMove);
112
+ target.removeEventListener('pointerup', handleDragEnd);
113
+ target.removeEventListener('pointercancel', handleDragEnd);
114
+ target.removeEventListener('lostpointercapture', handleDragEnd);
115
+ window.removeEventListener('scroll', handleDragScroll, true);
116
+ if (dragPointerId !== null && target.hasPointerCapture?.(dragPointerId)) {
117
+ try {
118
+ target.releasePointerCapture(dragPointerId);
119
+ } catch {
120
+ // The capture is gone already.
121
+ }
122
+ }
123
+ }
124
+ dragPointerId = null;
125
+ dragTarget = null;
126
+ dragChanged = false;
127
+ dragRect = null;
128
+ dragging = false;
129
+ }
130
+
131
+ $effect(() => () => stopDrag());
132
+
133
+ function handlePointerDown(
134
+ event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
135
+ ) {
136
+ onpointerdown?.(event);
137
+ if (event.defaultPrevented) return;
138
+ const target = event.currentTarget;
139
+ beginDrag(event, target);
140
+ if (dragging) applyPointer(event);
141
+ }
142
+
143
+ setColorPickerAreaContext({
144
+ get xChannel() {
145
+ return xChannel;
146
+ },
147
+ get yChannel() {
148
+ return yChannel;
149
+ },
150
+ get xPercent() {
151
+ return xPercent;
152
+ },
153
+ get yPercent() {
154
+ return yPercent;
155
+ },
156
+ get isDragging() {
157
+ return dragging;
158
+ },
159
+ get isRtl() {
160
+ return rtl;
161
+ },
162
+ get areaId() {
163
+ return areaId;
164
+ },
165
+ startDrag(event) {
166
+ const target = event.currentTarget;
167
+ if (!(target instanceof HTMLElement)) return;
168
+ // The thumb keeps its color at the press; only a move changes it.
169
+ beginDrag(event, target);
170
+ }
171
+ });
172
+
173
+ const inlineStyle = $derived(
174
+ `position: relative; touch-action: none;` +
175
+ ` --color-picker-area-x: ${xPercent}%; --color-picker-area-y: ${yPercent}%;` +
176
+ `${style ? ` ${style}` : ''}`
177
+ );
178
+ </script>
179
+
180
+ <div
181
+ {...restProps}
182
+ bind:this={areaRef}
183
+ id={areaId}
184
+ class={className}
185
+ style={inlineStyle}
186
+ data-color-picker-area="true"
187
+ data-x-channel={xChannel}
188
+ data-y-channel={yChannel}
189
+ data-dragging={dragging || undefined}
190
+ data-disabled={ctx.isDisabled || undefined}
191
+ data-readonly={ctx.isReadOnly || undefined}
192
+ onpointerdown={handlePointerDown}
193
+ >
194
+ {@render children?.()}
195
+ </div>
@@ -0,0 +1,4 @@
1
+ import type { ColorPickerAreaProps } from '../types';
2
+ declare const ColorPickerArea: import("svelte").Component<ColorPickerAreaProps, {}, "element">;
3
+ type ColorPickerArea = ReturnType<typeof ColorPickerArea>;
4
+ export default ColorPickerArea;
@@ -0,0 +1,231 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import { isRtl } from '../../internal/rtl';
4
+ import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
5
+ import { focusWithModality, shouldShowFocusVisible } from '../../primitives/input-modality';
6
+ import { useColorPickerAreaContext, useColorPickerContext } from '../root/context';
7
+ import type { ColorPickerAreaThumbProps, ColorPickerAreaThumbRenderState } from '../types';
8
+
9
+ /**
10
+ * ColorPicker.AreaThumb — the handle of the square.
11
+ *
12
+ * The element the user sees is a `<div>` in the square. Inside it are two native sliders,
13
+ * invisible: one for each axis. A screen reader reads each of them with its channel and its
14
+ * value, and the arrows move both axes from either one. That is the one shape that gives a
15
+ * two-dimensional control a name and a value a screen reader can read.
16
+ */
17
+ let {
18
+ children,
19
+ class: className = '',
20
+ element = $bindable<HTMLDivElement | null>(null),
21
+ style,
22
+ onpointerdown,
23
+ ...restProps
24
+ }: ColorPickerAreaThumbProps = $props();
25
+
26
+ const ctx = useColorPickerContext('ColorPicker.AreaThumb');
27
+ const area = useColorPickerAreaContext('ColorPicker.AreaThumb');
28
+
29
+ let thumbRef: HTMLDivElement | null = $state(null);
30
+ let xInputRef: HTMLInputElement | null = $state(null);
31
+ let yInputRef: HTMLInputElement | null = $state(null);
32
+ let focusedAxis = $state<'x' | 'y' | null>(null);
33
+ let focusVisible = $state(false);
34
+ const rtl = $derived(isRtl(thumbRef));
35
+
36
+ $effect(() => {
37
+ element = thumbRef;
38
+ });
39
+
40
+ const xRange = $derived(ctx.getChannelRange(area.xChannel));
41
+ const yRange = $derived(ctx.getChannelRange(area.yChannel));
42
+ const xValue = $derived(ctx.getChannelValue(area.xChannel));
43
+ const yValue = $derived(ctx.getChannelValue(area.yChannel));
44
+ const focused = $derived(focusedAxis !== null);
45
+
46
+ // The native sliders keep the value: a screen reader reads it from there. A move that the
47
+ // color refuses leaves the input ahead of the color, thus both are made equal again.
48
+ $effect(() => {
49
+ if (xInputRef && xInputRef.value !== String(xValue)) xInputRef.value = String(xValue);
50
+ if (yInputRef && yInputRef.value !== String(yValue)) yInputRef.value = String(yValue);
51
+ });
52
+
53
+ const renderState = $derived<ColorPickerAreaThumbRenderState>({
54
+ xPercent: area.xPercent,
55
+ yPercent: area.yPercent,
56
+ dragging: area.isDragging,
57
+ focused
58
+ });
59
+
60
+ watchFocusVisible({
61
+ isFocused: () => focused,
62
+ element: () => (focusedAxis === 'y' ? yInputRef : xInputRef),
63
+ set: (visible) => {
64
+ focusVisible = visible;
65
+ }
66
+ });
67
+
68
+ // The keyboard reports `onChange` on each key press, and `onChangeEnd` one time at the
69
+ // release, thus a held key does not report an end on each repeat.
70
+ let keyboardChanged = false;
71
+
72
+ function commitKeyboard(event: Event) {
73
+ if (!keyboardChanged) return;
74
+ keyboardChanged = false;
75
+ ctx.commit({ reason: 'keyboard', event });
76
+ }
77
+
78
+ /**
79
+ * The keys of one axis act on the axis that has the focus. The arrows name their own axis,
80
+ * thus they move the square from either input. `Home`, `End` and the page keys do not, and
81
+ * they moved the saturation while the brightness had the focus.
82
+ */
83
+ function handleKeyDown(axis: 'x' | 'y', event: KeyboardEvent) {
84
+ if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
85
+ const large = event.shiftKey;
86
+ const axisChannel = axis === 'x' ? area.xChannel : area.yChannel;
87
+ const axisRange = axis === 'x' ? xRange : yRange;
88
+ let changed = false;
89
+
90
+ switch (event.key) {
91
+ case 'ArrowRight':
92
+ changed = ctx.stepChannel(area.xChannel, rtl ? -1 : 1, { large, event });
93
+ break;
94
+ case 'ArrowLeft':
95
+ changed = ctx.stepChannel(area.xChannel, rtl ? 1 : -1, { large, event });
96
+ break;
97
+ case 'ArrowUp':
98
+ changed = ctx.stepChannel(area.yChannel, 1, { large, event });
99
+ break;
100
+ case 'ArrowDown':
101
+ changed = ctx.stepChannel(area.yChannel, -1, { large, event });
102
+ break;
103
+ case 'PageUp':
104
+ changed = ctx.stepChannel(axisChannel, 1, { large: true, event });
105
+ break;
106
+ case 'PageDown':
107
+ changed = ctx.stepChannel(axisChannel, -1, { large: true, event });
108
+ break;
109
+ case 'Home':
110
+ changed = ctx.setChannel(axisChannel, axisRange.min, { reason: 'keyboard', event });
111
+ break;
112
+ case 'End':
113
+ changed = ctx.setChannel(axisChannel, axisRange.max, { reason: 'keyboard', event });
114
+ break;
115
+ default:
116
+ return;
117
+ }
118
+
119
+ event.preventDefault();
120
+ if (changed) keyboardChanged = true;
121
+ }
122
+
123
+ function handleInput(axis: 'x' | 'y', event: Event) {
124
+ const input = event.currentTarget as HTMLInputElement;
125
+ const channel = axis === 'x' ? area.xChannel : area.yChannel;
126
+ const next = Number(input.value);
127
+ // A screen reader moves the native slider, and the swipe of a mobile one arrives here.
128
+ if (!Number.isNaN(next) && ctx.setChannel(channel, next, { reason: 'input', event })) {
129
+ ctx.commit({ reason: 'input', event });
130
+ }
131
+ input.value = String(axis === 'x' ? xValue : yValue);
132
+ }
133
+
134
+ function handleFocus(axis: 'x' | 'y') {
135
+ focusedAxis = axis;
136
+ focusVisible = shouldShowFocusVisible(axis === 'x' ? xInputRef : yInputRef);
137
+ }
138
+
139
+ function handleBlur(axis: 'x' | 'y', event: FocusEvent) {
140
+ // A key still held when the focus leaves has no release to wait for.
141
+ commitKeyboard(event);
142
+ if (focusedAxis === axis) {
143
+ focusedAxis = null;
144
+ focusVisible = false;
145
+ }
146
+ }
147
+
148
+ function handlePointerDown(
149
+ event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
150
+ ) {
151
+ onpointerdown?.(event);
152
+ if (event.defaultPrevented) return;
153
+ area.startDrag(event);
154
+ // With the modality, and not a plain focus: the focus ring belongs to the keyboard, and a
155
+ // press that leaves it on paints a ring the reader did not ask for.
156
+ if (xInputRef) focusWithModality(xInputRef, 'pointer');
157
+ }
158
+
159
+ const inputStyle =
160
+ 'position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;border:0;opacity:0;' +
161
+ 'cursor:inherit;pointer-events:none;';
162
+ const positionStyle = $derived(
163
+ `position: absolute; left: ${rtl ? 100 - area.xPercent : area.xPercent}%;` +
164
+ ` bottom: ${area.yPercent}%; translate: -50% 50%; touch-action: none;`
165
+ );
166
+ const resolvedStyle = $derived(`${positionStyle}${style ? ` ${style}` : ''}`);
167
+ </script>
168
+
169
+ <div
170
+ {...restProps}
171
+ bind:this={thumbRef}
172
+ class={className}
173
+ style={resolvedStyle}
174
+ data-color-picker-area-thumb="true"
175
+ data-dragging={area.isDragging || undefined}
176
+ data-disabled={ctx.isDisabled || undefined}
177
+ data-readonly={ctx.isReadOnly || undefined}
178
+ data-focused={focused || undefined}
179
+ data-focus-visible={(focused && focusVisible) || undefined}
180
+ onpointerdown={handlePointerDown}
181
+ >
182
+ <input
183
+ bind:this={xInputRef}
184
+ type="range"
185
+ id={ctx.getPartId('area-x')}
186
+ min={xRange.min}
187
+ max={xRange.max}
188
+ step={xRange.step}
189
+ value={String(xValue)}
190
+ disabled={ctx.isDisabled}
191
+ aria-label={ctx.getChannelLabel(area.xChannel)}
192
+ aria-valuetext={ctx.getChannelValueText(area.xChannel)}
193
+ aria-describedby={ctx.ariaDescribedBy}
194
+ aria-readonly={ctx.isReadOnly || undefined}
195
+ aria-invalid={ctx.isInvalid || undefined}
196
+ data-color-picker-area-input="true"
197
+ data-axis="x"
198
+ style={inputStyle}
199
+ onkeydown={(event) => handleKeyDown('x', event)}
200
+ onkeyup={commitKeyboard}
201
+ oninput={(event) => handleInput('x', event)}
202
+ onfocus={() => handleFocus('x')}
203
+ onblur={(event) => handleBlur('x', event)}
204
+ />
205
+ <input
206
+ bind:this={yInputRef}
207
+ type="range"
208
+ id={ctx.getPartId('area-y')}
209
+ min={yRange.min}
210
+ max={yRange.max}
211
+ step={yRange.step}
212
+ value={String(yValue)}
213
+ disabled={ctx.isDisabled}
214
+ aria-label={ctx.getChannelLabel(area.yChannel)}
215
+ aria-valuetext={ctx.getChannelValueText(area.yChannel)}
216
+ aria-describedby={ctx.ariaDescribedBy}
217
+ aria-readonly={ctx.isReadOnly || undefined}
218
+ aria-invalid={ctx.isInvalid || undefined}
219
+ data-color-picker-area-input="true"
220
+ data-axis="y"
221
+ style={inputStyle}
222
+ onkeydown={(event) => handleKeyDown('y', event)}
223
+ onkeyup={commitKeyboard}
224
+ oninput={(event) => handleInput('y', event)}
225
+ onfocus={() => handleFocus('y')}
226
+ onblur={(event) => handleBlur('y', event)}
227
+ />
228
+ {#if children}
229
+ {@render (children as Snippet<[ColorPickerAreaThumbRenderState]>)(renderState)}
230
+ {/if}
231
+ </div>
@@ -0,0 +1,4 @@
1
+ import type { ColorPickerAreaThumbProps } from '../types';
2
+ declare const ColorPickerAreaThumb: import("svelte").Component<ColorPickerAreaThumbProps, {}, "element">;
3
+ type ColorPickerAreaThumb = ReturnType<typeof ColorPickerAreaThumb>;
4
+ export default ColorPickerAreaThumb;