@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.
- package/README.md +27 -60
- package/dist/FOCUS_STATE_CONTRACT.md +1 -0
- package/dist/color-picker/README.md +78 -0
- package/dist/color-picker/area/color-picker-area.svelte +195 -0
- package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
- package/dist/color-picker/index.d.ts +16 -0
- package/dist/color-picker/index.js +15 -0
- package/dist/color-picker/index.parts.d.ts +12 -0
- package/dist/color-picker/index.parts.js +12 -0
- package/dist/color-picker/label/color-picker-label.svelte +34 -0
- package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
- package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
- package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
- package/dist/color-picker/root/color-picker-root.svelte +365 -0
- package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
- package/dist/color-picker/root/color.d.ts +68 -0
- package/dist/color-picker/root/color.js +266 -0
- package/dist/color-picker/root/context.d.ts +127 -0
- package/dist/color-picker/root/context.js +44 -0
- package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
- package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
- package/dist/color-picker/types.d.ts +176 -0
- package/dist/color-picker/types.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/internal/localized-strings.d.ts +176 -0
- package/dist/internal/localized-strings.js +176 -0
- package/dist/pin-input/README.md +55 -0
- package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
- package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
- package/dist/pin-input/index.d.ts +6 -0
- package/dist/pin-input/index.js +5 -0
- package/dist/pin-input/index.parts.d.ts +3 -0
- package/dist/pin-input/index.parts.js +3 -0
- package/dist/pin-input/label/pin-input-label.svelte +43 -0
- package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
- package/dist/pin-input/root/context.d.ts +70 -0
- package/dist/pin-input/root/context.js +15 -0
- package/dist/pin-input/root/pin-input-root.svelte +337 -0
- package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
- package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
- package/dist/pin-input/root/pin-input-utils.js +44 -0
- package/dist/pin-input/types.d.ts +84 -0
- package/dist/pin-input/types.js +1 -0
- package/dist/primitives/aria-hide-outside.d.ts +13 -0
- package/dist/primitives/aria-hide-outside.js +7 -1
- package/dist/primitives/focus-trap.d.ts +2 -0
- package/dist/primitives/focus-trap.js +7 -1
- package/dist/primitives/presence.svelte.js +6 -1
- package/dist/rating/README.md +58 -0
- package/dist/rating/index.d.ts +7 -0
- package/dist/rating/index.js +6 -0
- package/dist/rating/index.parts.d.ts +4 -0
- package/dist/rating/index.parts.js +4 -0
- package/dist/rating/item/rating-item.svelte +177 -0
- package/dist/rating/item/rating-item.svelte.d.ts +4 -0
- package/dist/rating/label/rating-label.svelte +42 -0
- package/dist/rating/label/rating-label.svelte.d.ts +4 -0
- package/dist/rating/output/rating-output.svelte +38 -0
- package/dist/rating/output/rating-output.svelte.d.ts +4 -0
- package/dist/rating/root/context.d.ts +87 -0
- package/dist/rating/root/context.js +15 -0
- package/dist/rating/root/rating-root.svelte +521 -0
- package/dist/rating/root/rating-root.svelte.d.ts +4 -0
- package/dist/rating/root/rating-utils.d.ts +28 -0
- package/dist/rating/root/rating-utils.js +53 -0
- package/dist/rating/types.d.ts +124 -0
- package/dist/rating/types.js +1 -0
- package/dist/search-field/README.md +29 -0
- package/dist/search-field/clear/README.md +5 -0
- package/dist/search-field/clear/search-field-clear.svelte +99 -0
- package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
- package/dist/search-field/index.d.ts +15 -0
- package/dist/search-field/index.js +6 -0
- package/dist/search-field/index.parts.d.ts +4 -0
- package/dist/search-field/index.parts.js +4 -0
- package/dist/search-field/input/README.md +5 -0
- package/dist/search-field/input/search-field-input.svelte +158 -0
- package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
- package/dist/search-field/label/README.md +3 -0
- package/dist/search-field/label/search-field-label.svelte +27 -0
- package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
- package/dist/search-field/root/README.md +5 -0
- package/dist/search-field/root/context.d.ts +28 -0
- package/dist/search-field/root/context.js +15 -0
- package/dist/search-field/root/search-field-root.svelte +230 -0
- package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
- package/dist/toast/README.md +75 -0
- package/dist/toast/action/toast-action.svelte +42 -0
- package/dist/toast/action/toast-action.svelte.d.ts +4 -0
- package/dist/toast/close/toast-close.svelte +41 -0
- package/dist/toast/close/toast-close.svelte.d.ts +4 -0
- package/dist/toast/content/toast-content.svelte +24 -0
- package/dist/toast/content/toast-content.svelte.d.ts +4 -0
- package/dist/toast/description/toast-description.svelte +41 -0
- package/dist/toast/description/toast-description.svelte.d.ts +4 -0
- package/dist/toast/index.d.ts +34 -0
- package/dist/toast/index.js +15 -0
- package/dist/toast/index.parts.d.ts +9 -0
- package/dist/toast/index.parts.js +9 -0
- package/dist/toast/positioner/toast-positioner.svelte +75 -0
- package/dist/toast/positioner/toast-positioner.svelte.d.ts +4 -0
- package/dist/toast/provider/context.d.ts +36 -0
- package/dist/toast/provider/context.js +22 -0
- package/dist/toast/provider/toast-manager.svelte.d.ts +113 -0
- package/dist/toast/provider/toast-manager.svelte.js +210 -0
- package/dist/toast/provider/toast-provider.svelte +98 -0
- package/dist/toast/provider/toast-provider.svelte.d.ts +4 -0
- package/dist/toast/root/context.d.ts +18 -0
- package/dist/toast/root/context.js +15 -0
- package/dist/toast/root/toast-root.svelte +296 -0
- package/dist/toast/root/toast-root.svelte.d.ts +4 -0
- package/dist/toast/title/toast-title.svelte +38 -0
- package/dist/toast/title/toast-title.svelte.d.ts +4 -0
- package/dist/toast/types.d.ts +100 -0
- package/dist/toast/types.js +1 -0
- package/dist/toast/viewport/toast-viewport.svelte +389 -0
- package/dist/toast/viewport/toast-viewport.svelte.d.ts +4 -0
- package/dist/tooltip/root/tooltip-root.svelte +16 -11
- package/package.json +26 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { Color, ColorChannel, ColorChannelRange, ColorFormat } from './color';
|
|
2
|
+
export type { Color, ColorChannel, ColorChannelRange, ColorFormat };
|
|
3
|
+
/** How a color changed. */
|
|
4
|
+
export type ColorPickerChangeReason = 'pointer' | 'keyboard' | 'input' | 'swatch' | 'eye-dropper' | 'form-reset';
|
|
5
|
+
export type ColorPickerChangeDetails = {
|
|
6
|
+
reason: ColorPickerChangeReason;
|
|
7
|
+
/** The channel that moved, when one channel moved. */
|
|
8
|
+
channel?: ColorChannel;
|
|
9
|
+
event?: Event;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Context shared between ColorPicker.Root and its parts.
|
|
13
|
+
*/
|
|
14
|
+
export type ColorPickerContext = {
|
|
15
|
+
/** The instance id. Every ARIA id of the parts is made from it. */
|
|
16
|
+
instanceId: string;
|
|
17
|
+
/** The id of the root element. */
|
|
18
|
+
rootId: string;
|
|
19
|
+
/** The id of the `ColorPicker.Label` element, when one is in the DOM. */
|
|
20
|
+
labelId: string | null;
|
|
21
|
+
/** The color: hue, saturation, brightness and alpha. */
|
|
22
|
+
color: Color;
|
|
23
|
+
/** The text of the color, in the format of the root. */
|
|
24
|
+
text: string;
|
|
25
|
+
/** The color as CSS, always with its alpha. */
|
|
26
|
+
cssColor: string;
|
|
27
|
+
/** The color of the hue, at full saturation and brightness. */
|
|
28
|
+
hueColor: string;
|
|
29
|
+
/** The text format of the value. */
|
|
30
|
+
format: ColorFormat;
|
|
31
|
+
/** Whether the picker holds an alpha. */
|
|
32
|
+
hasAlpha: boolean;
|
|
33
|
+
isDisabled: boolean;
|
|
34
|
+
isReadOnly: boolean;
|
|
35
|
+
isInvalid: boolean;
|
|
36
|
+
/** The `aria-label` given to the root. */
|
|
37
|
+
ariaLabel: string | undefined;
|
|
38
|
+
/** The `aria-labelledby` given to the root. */
|
|
39
|
+
ariaLabelledBy: string | undefined;
|
|
40
|
+
/** The `aria-describedby` given to the root. Each control carries it. */
|
|
41
|
+
ariaDescribedBy: string | undefined;
|
|
42
|
+
/** Registers the id of `ColorPicker.Label`. Returns the unregister function. */
|
|
43
|
+
registerLabel: (id: string) => () => void;
|
|
44
|
+
/** The id of one control of the picker, for example `area-thumb` or `channel-red`. */
|
|
45
|
+
getPartId: (part: string) => string;
|
|
46
|
+
/** The limits and the steps of one channel. */
|
|
47
|
+
getChannelRange: (channel: ColorChannel) => ColorChannelRange;
|
|
48
|
+
/** The value of one channel, rounded as a field shows it. */
|
|
49
|
+
getChannelValue: (channel: ColorChannel) => number;
|
|
50
|
+
/** The position of one channel between its limits, from 0 to 100. */
|
|
51
|
+
getChannelPercent: (channel: ColorChannel) => number;
|
|
52
|
+
/** The name of one channel in the locale, for example "Saturation". */
|
|
53
|
+
getChannelLabel: (channel: ColorChannel) => string;
|
|
54
|
+
/** The text a screen reader reads for one channel, for example "50%". */
|
|
55
|
+
getChannelValueText: (channel: ColorChannel) => string;
|
|
56
|
+
/** The color with one channel at another value, as CSS. It paints the track of a slider. */
|
|
57
|
+
getChannelColor: (channel: ColorChannel, value: number) => string;
|
|
58
|
+
/** Writes one channel. Returns whether the color changed. */
|
|
59
|
+
setChannel: (channel: ColorChannel, value: number, details: ColorPickerChangeDetails) => boolean;
|
|
60
|
+
/** Moves one channel by a step, or by the large step. */
|
|
61
|
+
stepChannel: (channel: ColorChannel, direction: -1 | 1, options: {
|
|
62
|
+
large?: boolean;
|
|
63
|
+
event?: Event;
|
|
64
|
+
}) => boolean;
|
|
65
|
+
/** Writes the whole color. Returns whether the color changed. */
|
|
66
|
+
setColor: (color: Color, details: ColorPickerChangeDetails) => boolean;
|
|
67
|
+
/** Reads a text into the color. Returns whether the text named a color. */
|
|
68
|
+
setText: (text: string, details: ColorPickerChangeDetails) => boolean;
|
|
69
|
+
/** Reports the end of a sequence of changes: a key press, a drag. */
|
|
70
|
+
commit: (details: ColorPickerChangeDetails) => void;
|
|
71
|
+
};
|
|
72
|
+
export declare function setColorPickerContext(ctx: ColorPickerContext): void;
|
|
73
|
+
export declare function getColorPickerContext(): ColorPickerContext | undefined;
|
|
74
|
+
export declare function useColorPickerContext(part: string): ColorPickerContext;
|
|
75
|
+
/** Context of one `ColorPicker.Area`, for its thumb. */
|
|
76
|
+
export type ColorPickerAreaContext = {
|
|
77
|
+
xChannel: ColorChannel;
|
|
78
|
+
yChannel: ColorChannel;
|
|
79
|
+
/** The position of the thumb, from 0 to 100. The y axis counts from the bottom. */
|
|
80
|
+
xPercent: number;
|
|
81
|
+
yPercent: number;
|
|
82
|
+
/** Whether a pointer moves the thumb. */
|
|
83
|
+
isDragging: boolean;
|
|
84
|
+
/** Whether the area is in a right-to-left context. */
|
|
85
|
+
isRtl: boolean;
|
|
86
|
+
areaId: string;
|
|
87
|
+
/** Starts a pointer drag from the thumb. The color does not change until the pointer moves. */
|
|
88
|
+
startDrag: (event: PointerEvent) => void;
|
|
89
|
+
};
|
|
90
|
+
export declare function setColorPickerAreaContext(ctx: ColorPickerAreaContext): void;
|
|
91
|
+
export declare function useColorPickerAreaContext(part: string): ColorPickerAreaContext;
|
|
92
|
+
export type ColorPickerSliderOrientation = 'horizontal' | 'vertical';
|
|
93
|
+
/** Context of one `ColorPicker.Slider`, for its thumb. */
|
|
94
|
+
export type ColorPickerSliderContext = {
|
|
95
|
+
channel: ColorChannel;
|
|
96
|
+
orientation: ColorPickerSliderOrientation;
|
|
97
|
+
/** The position of the thumb on the track, from 0 to 100. */
|
|
98
|
+
percent: number;
|
|
99
|
+
isDragging: boolean;
|
|
100
|
+
isRtl: boolean;
|
|
101
|
+
sliderId: string;
|
|
102
|
+
startDrag: (event: PointerEvent) => void;
|
|
103
|
+
};
|
|
104
|
+
export declare function setColorPickerSliderContext(ctx: ColorPickerSliderContext): void;
|
|
105
|
+
export declare function useColorPickerSliderContext(part: string): ColorPickerSliderContext;
|
|
106
|
+
/** Context of one `ColorPicker.SwatchList`, for its swatches. */
|
|
107
|
+
export type ColorPickerSwatchListContext = {
|
|
108
|
+
listId: string;
|
|
109
|
+
/** Registers a swatch. Returns its index and the unregister function. */
|
|
110
|
+
register: (options: {
|
|
111
|
+
elementRef: () => HTMLElement | null;
|
|
112
|
+
color: () => string;
|
|
113
|
+
}) => {
|
|
114
|
+
index: number;
|
|
115
|
+
unregister: () => void;
|
|
116
|
+
};
|
|
117
|
+
/** Whether one swatch is the one the tab order lands on. */
|
|
118
|
+
isTabStop: (index: number) => boolean;
|
|
119
|
+
/** Moves the focus between the swatches. */
|
|
120
|
+
moveFocus: (from: number, step: number | 'first' | 'last') => void;
|
|
121
|
+
/** Answers a press or a key on one swatch. */
|
|
122
|
+
select: (color: string, event?: Event) => void;
|
|
123
|
+
/** Whether the color of a swatch is the color of the picker. */
|
|
124
|
+
isSelected: (color: string) => boolean;
|
|
125
|
+
};
|
|
126
|
+
export declare function setColorPickerSwatchListContext(ctx: ColorPickerSwatchListContext): void;
|
|
127
|
+
export declare function getColorPickerSwatchListContext(): ColorPickerSwatchListContext | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const COLOR_PICKER_KEY = Symbol('color-picker');
|
|
3
|
+
export function setColorPickerContext(ctx) {
|
|
4
|
+
setContext(COLOR_PICKER_KEY, ctx);
|
|
5
|
+
}
|
|
6
|
+
export function getColorPickerContext() {
|
|
7
|
+
return getContext(COLOR_PICKER_KEY);
|
|
8
|
+
}
|
|
9
|
+
export function useColorPickerContext(part) {
|
|
10
|
+
const ctx = getColorPickerContext();
|
|
11
|
+
if (!ctx) {
|
|
12
|
+
throw new Error(`${part} must be used inside a ColorPicker.Root`);
|
|
13
|
+
}
|
|
14
|
+
return ctx;
|
|
15
|
+
}
|
|
16
|
+
const COLOR_PICKER_AREA_KEY = Symbol('color-picker-area');
|
|
17
|
+
export function setColorPickerAreaContext(ctx) {
|
|
18
|
+
setContext(COLOR_PICKER_AREA_KEY, ctx);
|
|
19
|
+
}
|
|
20
|
+
export function useColorPickerAreaContext(part) {
|
|
21
|
+
const ctx = getContext(COLOR_PICKER_AREA_KEY);
|
|
22
|
+
if (!ctx) {
|
|
23
|
+
throw new Error(`${part} must be used inside a ColorPicker.Area`);
|
|
24
|
+
}
|
|
25
|
+
return ctx;
|
|
26
|
+
}
|
|
27
|
+
const COLOR_PICKER_SLIDER_KEY = Symbol('color-picker-slider');
|
|
28
|
+
export function setColorPickerSliderContext(ctx) {
|
|
29
|
+
setContext(COLOR_PICKER_SLIDER_KEY, ctx);
|
|
30
|
+
}
|
|
31
|
+
export function useColorPickerSliderContext(part) {
|
|
32
|
+
const ctx = getContext(COLOR_PICKER_SLIDER_KEY);
|
|
33
|
+
if (!ctx) {
|
|
34
|
+
throw new Error(`${part} must be used inside a ColorPicker.Slider`);
|
|
35
|
+
}
|
|
36
|
+
return ctx;
|
|
37
|
+
}
|
|
38
|
+
const COLOR_PICKER_SWATCH_LIST_KEY = Symbol('color-picker-swatch-list');
|
|
39
|
+
export function setColorPickerSwatchListContext(ctx) {
|
|
40
|
+
setContext(COLOR_PICKER_SWATCH_LIST_KEY, ctx);
|
|
41
|
+
}
|
|
42
|
+
export function getColorPickerSwatchListContext() {
|
|
43
|
+
return getContext(COLOR_PICKER_SWATCH_LIST_KEY);
|
|
44
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { isRtl } from '../../internal/rtl';
|
|
3
|
+
import { setColorPickerSliderContext, useColorPickerContext } from '../root/context';
|
|
4
|
+
import type { ColorPickerSliderProps } from '../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ColorPicker.Slider — the track of one channel.
|
|
8
|
+
*
|
|
9
|
+
* A press anywhere on the track moves the thumb there. The track paints nothing of its own:
|
|
10
|
+
* your CSS draws the gradient of the channel, and `--color-picker-slider-start` and
|
|
11
|
+
* `--color-picker-slider-end` give it the two ends of that channel in the color of now.
|
|
12
|
+
*/
|
|
13
|
+
let {
|
|
14
|
+
channel,
|
|
15
|
+
orientation = 'horizontal',
|
|
16
|
+
children,
|
|
17
|
+
class: className = '',
|
|
18
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
19
|
+
style,
|
|
20
|
+
onpointerdown,
|
|
21
|
+
...restProps
|
|
22
|
+
}: ColorPickerSliderProps = $props();
|
|
23
|
+
|
|
24
|
+
const ctx = useColorPickerContext('ColorPicker.Slider');
|
|
25
|
+
|
|
26
|
+
let trackRef: HTMLDivElement | null = $state(null);
|
|
27
|
+
let dragging = $state(false);
|
|
28
|
+
let rtl = $state(false);
|
|
29
|
+
|
|
30
|
+
const sliderId = $derived(ctx.getPartId(`slider-${channel}`));
|
|
31
|
+
const range = $derived(ctx.getChannelRange(channel));
|
|
32
|
+
const percent = $derived(ctx.getChannelPercent(channel));
|
|
33
|
+
|
|
34
|
+
$effect(() => {
|
|
35
|
+
element = trackRef;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// The rect is read one time at the press, and again only when the page scrolls: a layout read
|
|
39
|
+
// on each move is what a drag does most.
|
|
40
|
+
let dragRect: DOMRect | null = null;
|
|
41
|
+
let dragPointerId: number | null = null;
|
|
42
|
+
let dragTarget: HTMLElement | null = null;
|
|
43
|
+
let dragChanged = false;
|
|
44
|
+
|
|
45
|
+
function applyPointer(event: PointerEvent) {
|
|
46
|
+
if (!trackRef) return;
|
|
47
|
+
dragRect ??= trackRef.getBoundingClientRect();
|
|
48
|
+
const rect = dragRect;
|
|
49
|
+
|
|
50
|
+
let fraction: number;
|
|
51
|
+
if (orientation === 'vertical') {
|
|
52
|
+
if (rect.height <= 0) return;
|
|
53
|
+
fraction = (rect.bottom - event.clientY) / rect.height;
|
|
54
|
+
} else {
|
|
55
|
+
if (rect.width <= 0) return;
|
|
56
|
+
const raw = (event.clientX - rect.left) / rect.width;
|
|
57
|
+
fraction = rtl ? 1 - raw : raw;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const next = range.min + Math.min(Math.max(fraction, 0), 1) * (range.max - range.min);
|
|
61
|
+
if (ctx.setChannel(channel, next, { reason: 'pointer', event })) dragChanged = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function beginDrag(event: PointerEvent, target: HTMLElement) {
|
|
65
|
+
if (ctx.isDisabled || ctx.isReadOnly || event.button !== 0 || dragPointerId !== null) return;
|
|
66
|
+
// The press must not select the page, and the thumb takes the focus below.
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
rtl = isRtl(trackRef);
|
|
69
|
+
dragRect = trackRef?.getBoundingClientRect() ?? null;
|
|
70
|
+
dragPointerId = event.pointerId;
|
|
71
|
+
dragTarget = target;
|
|
72
|
+
dragChanged = false;
|
|
73
|
+
dragging = true;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
target.setPointerCapture(event.pointerId);
|
|
77
|
+
} catch {
|
|
78
|
+
// Synthetic test events and older browsers can fail pointer capture.
|
|
79
|
+
}
|
|
80
|
+
target.addEventListener('pointermove', handleDragMove);
|
|
81
|
+
target.addEventListener('pointerup', handleDragEnd);
|
|
82
|
+
target.addEventListener('pointercancel', handleDragEnd);
|
|
83
|
+
target.addEventListener('lostpointercapture', handleDragEnd);
|
|
84
|
+
window.addEventListener('scroll', handleDragScroll, true);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function handleDragScroll() {
|
|
88
|
+
dragRect = null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function handleDragMove(event: PointerEvent) {
|
|
92
|
+
if (event.pointerId !== dragPointerId) return;
|
|
93
|
+
applyPointer(event);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function handleDragEnd(event: PointerEvent) {
|
|
97
|
+
if (event.pointerId !== dragPointerId) return;
|
|
98
|
+
const changed = dragChanged;
|
|
99
|
+
stopDrag();
|
|
100
|
+
if (changed) ctx.commit({ reason: 'pointer', channel, event });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function stopDrag() {
|
|
104
|
+
const target = dragTarget;
|
|
105
|
+
if (target) {
|
|
106
|
+
target.removeEventListener('pointermove', handleDragMove);
|
|
107
|
+
target.removeEventListener('pointerup', handleDragEnd);
|
|
108
|
+
target.removeEventListener('pointercancel', handleDragEnd);
|
|
109
|
+
target.removeEventListener('lostpointercapture', handleDragEnd);
|
|
110
|
+
window.removeEventListener('scroll', handleDragScroll, true);
|
|
111
|
+
if (dragPointerId !== null && target.hasPointerCapture?.(dragPointerId)) {
|
|
112
|
+
try {
|
|
113
|
+
target.releasePointerCapture(dragPointerId);
|
|
114
|
+
} catch {
|
|
115
|
+
// The capture is gone already.
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
dragPointerId = null;
|
|
120
|
+
dragTarget = null;
|
|
121
|
+
dragChanged = false;
|
|
122
|
+
dragRect = null;
|
|
123
|
+
dragging = false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
$effect(() => () => stopDrag());
|
|
127
|
+
|
|
128
|
+
function handlePointerDown(
|
|
129
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
130
|
+
) {
|
|
131
|
+
onpointerdown?.(event);
|
|
132
|
+
if (event.defaultPrevented) return;
|
|
133
|
+
beginDrag(event, event.currentTarget);
|
|
134
|
+
if (dragging) applyPointer(event);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
setColorPickerSliderContext({
|
|
138
|
+
get channel() {
|
|
139
|
+
return channel;
|
|
140
|
+
},
|
|
141
|
+
get orientation() {
|
|
142
|
+
return orientation;
|
|
143
|
+
},
|
|
144
|
+
get percent() {
|
|
145
|
+
return percent;
|
|
146
|
+
},
|
|
147
|
+
get isDragging() {
|
|
148
|
+
return dragging;
|
|
149
|
+
},
|
|
150
|
+
get isRtl() {
|
|
151
|
+
return rtl;
|
|
152
|
+
},
|
|
153
|
+
get sliderId() {
|
|
154
|
+
return sliderId;
|
|
155
|
+
},
|
|
156
|
+
startDrag(event) {
|
|
157
|
+
const target = event.currentTarget;
|
|
158
|
+
if (!(target instanceof HTMLElement)) return;
|
|
159
|
+
// The thumb keeps its value at the press; only a move changes it.
|
|
160
|
+
beginDrag(event, target);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const inlineStyle = $derived(
|
|
165
|
+
`position: relative; touch-action: none;` +
|
|
166
|
+
` --color-picker-slider-percent: ${percent}%;` +
|
|
167
|
+
` --color-picker-slider-start: ${ctx.getChannelColor(channel, range.min)};` +
|
|
168
|
+
` --color-picker-slider-end: ${ctx.getChannelColor(channel, range.max)};` +
|
|
169
|
+
`${style ? ` ${style}` : ''}`
|
|
170
|
+
);
|
|
171
|
+
</script>
|
|
172
|
+
|
|
173
|
+
<div
|
|
174
|
+
{...restProps}
|
|
175
|
+
bind:this={trackRef}
|
|
176
|
+
id={sliderId}
|
|
177
|
+
class={className}
|
|
178
|
+
style={inlineStyle}
|
|
179
|
+
data-color-picker-slider="true"
|
|
180
|
+
data-channel={channel}
|
|
181
|
+
data-orientation={orientation}
|
|
182
|
+
data-dragging={dragging || undefined}
|
|
183
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
184
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
185
|
+
onpointerdown={handlePointerDown}
|
|
186
|
+
>
|
|
187
|
+
{@render children?.()}
|
|
188
|
+
</div>
|
|
@@ -0,0 +1,208 @@
|
|
|
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 { useColorPickerContext, useColorPickerSliderContext } from '../root/context';
|
|
7
|
+
import type { ColorPickerSliderThumbProps, ColorPickerSliderThumbRenderState } from '../types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* ColorPicker.SliderThumb — the handle of one channel.
|
|
11
|
+
*
|
|
12
|
+
* The element the user sees is a `<div>` on the track. Inside it, a native slider fills the
|
|
13
|
+
* thumb, invisible, and it is what has the focus and the value. The keyboard is answered
|
|
14
|
+
* here, thus the steps and the text direction are the same in each browser.
|
|
15
|
+
*/
|
|
16
|
+
let {
|
|
17
|
+
children,
|
|
18
|
+
class: className = '',
|
|
19
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
20
|
+
style,
|
|
21
|
+
onpointerdown,
|
|
22
|
+
...restProps
|
|
23
|
+
}: ColorPickerSliderThumbProps = $props();
|
|
24
|
+
|
|
25
|
+
const ctx = useColorPickerContext('ColorPicker.SliderThumb');
|
|
26
|
+
const slider = useColorPickerSliderContext('ColorPicker.SliderThumb');
|
|
27
|
+
|
|
28
|
+
let thumbRef: HTMLDivElement | null = $state(null);
|
|
29
|
+
let inputRef: HTMLInputElement | null = $state(null);
|
|
30
|
+
let focused = $state(false);
|
|
31
|
+
let focusVisible = $state(false);
|
|
32
|
+
const rtl = $derived(isRtl(thumbRef));
|
|
33
|
+
|
|
34
|
+
$effect(() => {
|
|
35
|
+
element = thumbRef;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const range = $derived(ctx.getChannelRange(slider.channel));
|
|
39
|
+
const value = $derived(ctx.getChannelValue(slider.channel));
|
|
40
|
+
|
|
41
|
+
// The native slider keeps the value: a screen reader reads it from there. A move that the
|
|
42
|
+
// color refuses leaves the input ahead of the color, thus both are made equal again.
|
|
43
|
+
$effect(() => {
|
|
44
|
+
if (inputRef && inputRef.value !== String(value)) inputRef.value = String(value);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const renderState = $derived<ColorPickerSliderThumbRenderState>({
|
|
48
|
+
percent: slider.percent,
|
|
49
|
+
value,
|
|
50
|
+
dragging: slider.isDragging,
|
|
51
|
+
focused
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
watchFocusVisible({
|
|
55
|
+
isFocused: () => focused,
|
|
56
|
+
element: () => inputRef,
|
|
57
|
+
set: (visible) => {
|
|
58
|
+
focusVisible = visible;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// The keyboard reports `onChange` on each key press, and `onChangeEnd` one time at the
|
|
63
|
+
// release, thus a held key does not report an end on each repeat.
|
|
64
|
+
let keyboardChanged = false;
|
|
65
|
+
|
|
66
|
+
function commitKeyboard(event: Event) {
|
|
67
|
+
if (!keyboardChanged) return;
|
|
68
|
+
keyboardChanged = false;
|
|
69
|
+
ctx.commit({ reason: 'keyboard', channel: slider.channel, event });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
73
|
+
if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
|
|
74
|
+
const vertical = slider.orientation === 'vertical';
|
|
75
|
+
const large = event.shiftKey;
|
|
76
|
+
let changed = false;
|
|
77
|
+
|
|
78
|
+
switch (event.key) {
|
|
79
|
+
case 'ArrowRight':
|
|
80
|
+
changed = ctx.stepChannel(slider.channel, vertical || !rtl ? 1 : -1, { large, event });
|
|
81
|
+
break;
|
|
82
|
+
case 'ArrowLeft':
|
|
83
|
+
changed = ctx.stepChannel(slider.channel, vertical || !rtl ? -1 : 1, { large, event });
|
|
84
|
+
break;
|
|
85
|
+
case 'ArrowUp':
|
|
86
|
+
changed = ctx.stepChannel(slider.channel, 1, { large, event });
|
|
87
|
+
break;
|
|
88
|
+
case 'ArrowDown':
|
|
89
|
+
changed = ctx.stepChannel(slider.channel, -1, { large, event });
|
|
90
|
+
break;
|
|
91
|
+
case 'PageUp':
|
|
92
|
+
changed = ctx.stepChannel(slider.channel, 1, { large: true, event });
|
|
93
|
+
break;
|
|
94
|
+
case 'PageDown':
|
|
95
|
+
changed = ctx.stepChannel(slider.channel, -1, { large: true, event });
|
|
96
|
+
break;
|
|
97
|
+
case 'Home':
|
|
98
|
+
changed = ctx.setChannel(slider.channel, range.min, {
|
|
99
|
+
reason: 'keyboard',
|
|
100
|
+
channel: slider.channel,
|
|
101
|
+
event
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
case 'End':
|
|
105
|
+
changed = ctx.setChannel(slider.channel, range.max, {
|
|
106
|
+
reason: 'keyboard',
|
|
107
|
+
channel: slider.channel,
|
|
108
|
+
event
|
|
109
|
+
});
|
|
110
|
+
break;
|
|
111
|
+
default:
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
event.preventDefault();
|
|
116
|
+
if (changed) keyboardChanged = true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function handleInput(event: Event) {
|
|
120
|
+
const input = event.currentTarget as HTMLInputElement;
|
|
121
|
+
const next = Number(input.value);
|
|
122
|
+
// A screen reader moves the native slider, and the swipe of a mobile one arrives here.
|
|
123
|
+
if (
|
|
124
|
+
!Number.isNaN(next) &&
|
|
125
|
+
ctx.setChannel(slider.channel, next, { reason: 'input', channel: slider.channel, event })
|
|
126
|
+
) {
|
|
127
|
+
ctx.commit({ reason: 'input', channel: slider.channel, event });
|
|
128
|
+
}
|
|
129
|
+
input.value = String(value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function handleFocus() {
|
|
133
|
+
focused = true;
|
|
134
|
+
focusVisible = shouldShowFocusVisible(inputRef);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function handleBlur(event: FocusEvent) {
|
|
138
|
+
// A key still held when the focus leaves has no release to wait for.
|
|
139
|
+
commitKeyboard(event);
|
|
140
|
+
focused = false;
|
|
141
|
+
focusVisible = false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function handlePointerDown(
|
|
145
|
+
event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
146
|
+
) {
|
|
147
|
+
onpointerdown?.(event);
|
|
148
|
+
if (event.defaultPrevented) return;
|
|
149
|
+
slider.startDrag(event);
|
|
150
|
+
// With the modality, and not a plain focus: the focus ring belongs to the keyboard, and a
|
|
151
|
+
// press that leaves it on paints a ring the reader did not ask for.
|
|
152
|
+
if (inputRef) focusWithModality(inputRef, 'pointer');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const positionStyle = $derived.by(() => {
|
|
156
|
+
if (slider.orientation === 'vertical') {
|
|
157
|
+
return `position: absolute; bottom: ${slider.percent}%; left: 50%; translate: -50% 50%;`;
|
|
158
|
+
}
|
|
159
|
+
// A physical `left`, because the `translate` that centers the thumb is physical too.
|
|
160
|
+
const left = rtl ? 100 - slider.percent : slider.percent;
|
|
161
|
+
return `position: absolute; left: ${left}%; top: 50%; translate: -50% -50%;`;
|
|
162
|
+
});
|
|
163
|
+
const resolvedStyle = $derived(`${positionStyle} touch-action: none;${style ? ` ${style}` : ''}`);
|
|
164
|
+
</script>
|
|
165
|
+
|
|
166
|
+
<div
|
|
167
|
+
{...restProps}
|
|
168
|
+
bind:this={thumbRef}
|
|
169
|
+
class={className}
|
|
170
|
+
style={resolvedStyle}
|
|
171
|
+
data-color-picker-slider-thumb="true"
|
|
172
|
+
data-channel={slider.channel}
|
|
173
|
+
data-orientation={slider.orientation}
|
|
174
|
+
data-dragging={slider.isDragging || undefined}
|
|
175
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
176
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
177
|
+
data-focused={focused || undefined}
|
|
178
|
+
data-focus-visible={(focused && focusVisible) || undefined}
|
|
179
|
+
onpointerdown={handlePointerDown}
|
|
180
|
+
>
|
|
181
|
+
<input
|
|
182
|
+
bind:this={inputRef}
|
|
183
|
+
type="range"
|
|
184
|
+
id={ctx.getPartId(`slider-input-${slider.channel}`)}
|
|
185
|
+
min={range.min}
|
|
186
|
+
max={range.max}
|
|
187
|
+
step={range.step}
|
|
188
|
+
value={String(value)}
|
|
189
|
+
disabled={ctx.isDisabled}
|
|
190
|
+
aria-label={ctx.getChannelLabel(slider.channel)}
|
|
191
|
+
aria-valuetext={ctx.getChannelValueText(slider.channel)}
|
|
192
|
+
aria-describedby={ctx.ariaDescribedBy}
|
|
193
|
+
aria-orientation={slider.orientation}
|
|
194
|
+
aria-readonly={ctx.isReadOnly || undefined}
|
|
195
|
+
aria-invalid={ctx.isInvalid || undefined}
|
|
196
|
+
data-color-picker-slider-input="true"
|
|
197
|
+
data-channel={slider.channel}
|
|
198
|
+
style="position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;border:0;opacity:0;cursor:inherit;pointer-events:none;"
|
|
199
|
+
onkeydown={handleKeyDown}
|
|
200
|
+
onkeyup={commitKeyboard}
|
|
201
|
+
oninput={handleInput}
|
|
202
|
+
onfocus={handleFocus}
|
|
203
|
+
onblur={handleBlur}
|
|
204
|
+
/>
|
|
205
|
+
{#if children}
|
|
206
|
+
{@render (children as Snippet<[ColorPickerSliderThumbRenderState]>)(renderState)}
|
|
207
|
+
{/if}
|
|
208
|
+
</div>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ColorPickerSliderThumbProps } from '../types';
|
|
2
|
+
declare const ColorPickerSliderThumb: import("svelte").Component<ColorPickerSliderThumbProps, {}, "element">;
|
|
3
|
+
type ColorPickerSliderThumb = ReturnType<typeof ColorPickerSliderThumb>;
|
|
4
|
+
export default ColorPickerSliderThumb;
|