@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.
- 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 +8 -0
- package/dist/index.js +8 -0
- package/dist/internal/localized-strings.d.ts +152 -0
- package/dist/internal/localized-strings.js +152 -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/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/tooltip/root/tooltip-root.svelte +16 -11
- package/package.json +21 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import type { RatingChangeDetails, RatingContext } from './root/context';
|
|
4
|
+
export type { RatingChangeDetails, RatingContext };
|
|
5
|
+
export type RatingRootProps = {
|
|
6
|
+
/** A stable id, from which the component makes its internal ARIA ids. Give one on a server. */
|
|
7
|
+
id?: string;
|
|
8
|
+
/** The value, from 0 to `count`. 0 is no rating. You can bind it with `bind:value`. */
|
|
9
|
+
value?: number;
|
|
10
|
+
/** The value at the start, for when you give no `value`. The default is 0. */
|
|
11
|
+
defaultValue?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Give your own code full control of the value. The component stops to write back to `value`,
|
|
14
|
+
* and it reports only through `onChange`. Thus the parent can refuse a change.
|
|
15
|
+
*/
|
|
16
|
+
controlledValue?: boolean;
|
|
17
|
+
/** The component calls it on each change. */
|
|
18
|
+
onChange?: (value: number, details: RatingChangeDetails) => void;
|
|
19
|
+
/**
|
|
20
|
+
* The component calls it when a sequence of changes ends: at the release of a key, and at the
|
|
21
|
+
* press on an item. Use it for work that must not run on each key repeat.
|
|
22
|
+
*/
|
|
23
|
+
onChangeEnd?: (value: number, details: RatingChangeDetails) => void;
|
|
24
|
+
/** The count of items. The default is 5. */
|
|
25
|
+
count?: number;
|
|
26
|
+
/**
|
|
27
|
+
* The distance between two values. The default is 1, which gives whole items. 0.5 gives half
|
|
28
|
+
* items. A precision below 1 makes the rating a slider, because a radio group cannot say 3.5.
|
|
29
|
+
*/
|
|
30
|
+
precision?: number;
|
|
31
|
+
/**
|
|
32
|
+
* A press on the item of the current value puts the value back to 0. `Delete` and `Backspace`
|
|
33
|
+
* do the same. The default is on.
|
|
34
|
+
*/
|
|
35
|
+
allowClear?: boolean;
|
|
36
|
+
/** Disables the rating. The items leave the tab order, and the value does not change. */
|
|
37
|
+
disabled?: boolean;
|
|
38
|
+
/** Keeps the items in the tab order, but the value does not change. */
|
|
39
|
+
readonly?: boolean;
|
|
40
|
+
/** Marks the rating as necessary. The root gets `aria-required`. */
|
|
41
|
+
required?: boolean;
|
|
42
|
+
/** Marks the value as invalid. The root gets `aria-invalid`. */
|
|
43
|
+
invalid?: boolean;
|
|
44
|
+
/** The name of the hidden input that carries the value in a form. */
|
|
45
|
+
name?: string;
|
|
46
|
+
/** The id of the form that the hidden input belongs to, when the rating is outside it. */
|
|
47
|
+
form?: string;
|
|
48
|
+
/**
|
|
49
|
+
* The text of one item, for the screen reader. The default is "3 of 5" in the locale. The
|
|
50
|
+
* second argument is the count.
|
|
51
|
+
*/
|
|
52
|
+
getItemLabel?: (value: number, count: number) => string;
|
|
53
|
+
/**
|
|
54
|
+
* The text of the value, for `Rating.Output` and for the text a screen reader reads. The
|
|
55
|
+
* default is "3 of 5" in the locale, and "No rating" at 0.
|
|
56
|
+
*/
|
|
57
|
+
getValueText?: (value: number, count: number) => string;
|
|
58
|
+
/** The accessible name of the rating, for when there is no `Rating.Label`. */
|
|
59
|
+
'aria-label'?: string;
|
|
60
|
+
/** The id of the element that gives the rating its name, in place of `Rating.Label`. */
|
|
61
|
+
'aria-labelledby'?: string;
|
|
62
|
+
/** The id of the element that describes the rating, for example an error message. */
|
|
63
|
+
'aria-describedby'?: string;
|
|
64
|
+
/** The content: the label, the output and the items. */
|
|
65
|
+
children?: Snippet;
|
|
66
|
+
/** The CSS class names of the root element. */
|
|
67
|
+
class?: string;
|
|
68
|
+
/** A bindable reference to the root element. */
|
|
69
|
+
element?: HTMLDivElement | null;
|
|
70
|
+
/** A bindable reference to the context, for a composition of your own. */
|
|
71
|
+
context?: RatingContext;
|
|
72
|
+
} & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'id' | 'role' | 'aria-label' | 'aria-labelledby' | 'aria-describedby' | 'onchange'>;
|
|
73
|
+
export type RatingLabelProps = Omit<HTMLAttributes<HTMLSpanElement>, 'class'> & {
|
|
74
|
+
/** The CSS class names of the label. */
|
|
75
|
+
class?: string;
|
|
76
|
+
};
|
|
77
|
+
export type RatingOutputRenderState = {
|
|
78
|
+
/** The value. */
|
|
79
|
+
value: number;
|
|
80
|
+
/** The value the user sees: the value under the pointer, or the value. */
|
|
81
|
+
displayValue: number;
|
|
82
|
+
/** The count of items. */
|
|
83
|
+
count: number;
|
|
84
|
+
/** The text of the value, in the locale. */
|
|
85
|
+
text: string;
|
|
86
|
+
};
|
|
87
|
+
export type RatingOutputProps = Omit<HTMLAttributes<HTMLOutputElement>, 'class' | 'children'> & {
|
|
88
|
+
/**
|
|
89
|
+
* The content. As a snippet with one argument, it receives the render state: `value`,
|
|
90
|
+
* `displayValue`, `count` and `text`. Without it, the component shows `text`.
|
|
91
|
+
*/
|
|
92
|
+
children?: Snippet<[RatingOutputRenderState]>;
|
|
93
|
+
/** The CSS class names of the element. */
|
|
94
|
+
class?: string;
|
|
95
|
+
};
|
|
96
|
+
export type RatingItemRenderState = {
|
|
97
|
+
/** The value of the item: 1 for the first one. */
|
|
98
|
+
value: number;
|
|
99
|
+
/** The index of the item: 0 for the first one. */
|
|
100
|
+
index: number;
|
|
101
|
+
/** How much of the item the value fills, from 0 to 1. */
|
|
102
|
+
fill: number;
|
|
103
|
+
/** Whether the item is the value. */
|
|
104
|
+
selected: boolean;
|
|
105
|
+
/** Whether the value, or the pointer, is at this item or past it. */
|
|
106
|
+
highlighted: boolean;
|
|
107
|
+
/** Whether the item has the focus. */
|
|
108
|
+
focused: boolean;
|
|
109
|
+
};
|
|
110
|
+
export type RatingItemProps = Omit<HTMLAttributes<HTMLSpanElement>, 'class' | 'children' | 'aria-label'> & {
|
|
111
|
+
/** The index of the item. Without it, the items take the indexes in mount order. */
|
|
112
|
+
index?: number;
|
|
113
|
+
/** The name of this item, for the screen reader. It replaces `getItemLabel` of the root. */
|
|
114
|
+
'aria-label'?: string;
|
|
115
|
+
/**
|
|
116
|
+
* The content. As a snippet with one argument, it receives the render state: `value`, `index`,
|
|
117
|
+
* `fill`, `selected`, `highlighted` and `focused`.
|
|
118
|
+
*/
|
|
119
|
+
children?: Snippet<[RatingItemRenderState]> | Snippet;
|
|
120
|
+
/** The CSS class names of the item. */
|
|
121
|
+
class?: string;
|
|
122
|
+
/** A bindable reference to the item element. */
|
|
123
|
+
element?: HTMLSpanElement | null;
|
|
124
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# SearchField
|
|
2
|
+
|
|
3
|
+
A text input for a search query. The `Escape` key and a clear button empty the text, and the `Enter` key submits the query.
|
|
4
|
+
|
|
5
|
+
## Anatomy
|
|
6
|
+
|
|
7
|
+
```svelte
|
|
8
|
+
<SearchField.Root>
|
|
9
|
+
<SearchField.Label />
|
|
10
|
+
<SearchField.Input />
|
|
11
|
+
<SearchField.Clear />
|
|
12
|
+
</SearchField.Root>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage guidelines
|
|
16
|
+
|
|
17
|
+
- Give the input an accessible name. Use `SearchField.Label`, or put `aria-label` or `aria-labelledby` on `SearchField.Input`.
|
|
18
|
+
- Use `bind:value` for the state in the two directions. The value is always a string.
|
|
19
|
+
- Use `onSubmit` to start the search on `Enter`. In a form, the form also submits.
|
|
20
|
+
- Use `onClear` to know when the `Escape` key or the clear button empties the text.
|
|
21
|
+
- Use `name` on `Root` to submit the text in an HTML form.
|
|
22
|
+
- Use the `data-empty` attribute to hide the clear button while the text is empty.
|
|
23
|
+
|
|
24
|
+
## Accessibility
|
|
25
|
+
|
|
26
|
+
- `SearchField.Input` makes an `<input type="search">`, which has the `searchbox` role.
|
|
27
|
+
- The `Escape` key empties the text and keeps the key in the field. When the text is already empty, the key goes on, thus a dialog around the field can close.
|
|
28
|
+
- The clear button is not in the tab order, because the `Escape` key does the same operation. A press on it keeps the focus in the input.
|
|
29
|
+
- The localized name of the clear button is "Clear search". Put an `aria-label` on `SearchField.Clear` to change it.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# SearchField.Clear
|
|
2
|
+
|
|
3
|
+
Makes the button that empties the search text.
|
|
4
|
+
|
|
5
|
+
The button is not in the tab order, and a press on it keeps the focus in the input. It is disabled while the text is empty, and while the field is disabled or read-only. If you give no children, it shows an "x" icon.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ComponentProps, Snippet } from 'svelte';
|
|
3
|
+
import { ButtonRoot } from '../../button/index.js';
|
|
4
|
+
import { trackInteractionModality } from '../../primitives/input-modality';
|
|
5
|
+
import { composeEventHandlers } from '../../utils/compose-event-handlers';
|
|
6
|
+
import { useSearchFieldContext } from '../root/context';
|
|
7
|
+
|
|
8
|
+
type SearchFieldClearProps = Omit<
|
|
9
|
+
ComponentProps<typeof ButtonRoot>,
|
|
10
|
+
| 'children'
|
|
11
|
+
| 'class'
|
|
12
|
+
| 'type'
|
|
13
|
+
| 'disabled'
|
|
14
|
+
| 'tabindex'
|
|
15
|
+
| 'pending'
|
|
16
|
+
| 'pressed'
|
|
17
|
+
| 'focusableWhenDisabled'
|
|
18
|
+
> & {
|
|
19
|
+
class?: string;
|
|
20
|
+
/** The content of the button. If you give none, the component shows an "x" icon. */
|
|
21
|
+
children?: Snippet;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* SearchField.Clear — the button that empties the search text.
|
|
26
|
+
*
|
|
27
|
+
* It is not in the tab order, because the `Escape` key in the input does the same operation.
|
|
28
|
+
* A press on it does not take the focus: the focus stays in the input, thus the virtual
|
|
29
|
+
* keyboard of a phone stays open.
|
|
30
|
+
*/
|
|
31
|
+
let {
|
|
32
|
+
class: className,
|
|
33
|
+
children,
|
|
34
|
+
'aria-label': ariaLabel,
|
|
35
|
+
onclick: onClickExternal,
|
|
36
|
+
onmousedown: onMouseDownExternal,
|
|
37
|
+
onpointerdown: onPointerDownExternal,
|
|
38
|
+
...restProps
|
|
39
|
+
}: SearchFieldClearProps = $props();
|
|
40
|
+
|
|
41
|
+
const ctx = useSearchFieldContext('SearchField.Clear');
|
|
42
|
+
const isClearDisabled = $derived(ctx.isDisabled || ctx.isReadOnly || ctx.isEmpty);
|
|
43
|
+
|
|
44
|
+
// Keep the focus where it is. Without this, the press moves the focus to the button and the
|
|
45
|
+
// input loses it.
|
|
46
|
+
function handleMouseDown(event: MouseEvent) {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// On a touch screen the input must have the focus before the press ends, or the virtual
|
|
51
|
+
// keyboard closes and opens again.
|
|
52
|
+
function handlePointerDown(event: PointerEvent) {
|
|
53
|
+
if (isClearDisabled || event.button !== 0) return;
|
|
54
|
+
trackInteractionModality(event, ctx.inputRef);
|
|
55
|
+
ctx.inputRef?.focus();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function handleClick() {
|
|
59
|
+
if (isClearDisabled) return;
|
|
60
|
+
ctx.clear('clear-press');
|
|
61
|
+
ctx.inputRef?.focus();
|
|
62
|
+
}
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<ButtonRoot
|
|
66
|
+
{...restProps}
|
|
67
|
+
type="button"
|
|
68
|
+
tabindex={-1}
|
|
69
|
+
aria-label={ariaLabel ?? ctx.clearAriaLabel}
|
|
70
|
+
aria-controls={ctx.inputId}
|
|
71
|
+
disabled={isClearDisabled}
|
|
72
|
+
class={className}
|
|
73
|
+
data-search-field-clear="true"
|
|
74
|
+
data-empty={ctx.isEmpty || undefined}
|
|
75
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
76
|
+
onmousedown={composeEventHandlers(handleMouseDown, onMouseDownExternal ?? undefined)}
|
|
77
|
+
onpointerdown={composeEventHandlers(handlePointerDown, onPointerDownExternal ?? undefined)}
|
|
78
|
+
onclick={composeEventHandlers(handleClick, onClickExternal ?? undefined)}
|
|
79
|
+
>
|
|
80
|
+
{#if children}
|
|
81
|
+
{@render children()}
|
|
82
|
+
{:else}
|
|
83
|
+
<svg
|
|
84
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
85
|
+
width="16"
|
|
86
|
+
height="16"
|
|
87
|
+
viewBox="0 0 24 24"
|
|
88
|
+
fill="none"
|
|
89
|
+
stroke="currentColor"
|
|
90
|
+
stroke-width="2"
|
|
91
|
+
stroke-linecap="round"
|
|
92
|
+
stroke-linejoin="round"
|
|
93
|
+
aria-hidden="true"
|
|
94
|
+
>
|
|
95
|
+
<path d="M18 6 6 18" />
|
|
96
|
+
<path d="m6 6 12 12" />
|
|
97
|
+
</svg>
|
|
98
|
+
{/if}
|
|
99
|
+
</ButtonRoot>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ComponentProps, Snippet } from 'svelte';
|
|
2
|
+
import { ButtonRoot } from '../../button/index.js';
|
|
3
|
+
type SearchFieldClearProps = Omit<ComponentProps<typeof ButtonRoot>, 'children' | 'class' | 'type' | 'disabled' | 'tabindex' | 'pending' | 'pressed' | 'focusableWhenDisabled'> & {
|
|
4
|
+
class?: string;
|
|
5
|
+
/** The content of the button. If you give none, the component shows an "x" icon. */
|
|
6
|
+
children?: Snippet;
|
|
7
|
+
};
|
|
8
|
+
declare const SearchFieldClear: import("svelte").Component<SearchFieldClearProps, {}, "">;
|
|
9
|
+
type SearchFieldClear = ReturnType<typeof SearchFieldClear>;
|
|
10
|
+
export default SearchFieldClear;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ComponentProps } from 'svelte';
|
|
2
|
+
import type SearchFieldClearComponent from './clear/search-field-clear.svelte';
|
|
3
|
+
import type SearchFieldInputComponent from './input/search-field-input.svelte';
|
|
4
|
+
import type SearchFieldLabelComponent from './label/search-field-label.svelte';
|
|
5
|
+
import type SearchFieldRootComponent from './root/search-field-root.svelte';
|
|
6
|
+
export * as SearchField from './index.parts.js';
|
|
7
|
+
export { default as SearchFieldRoot } from './root/search-field-root.svelte';
|
|
8
|
+
export { default as SearchFieldLabel } from './label/search-field-label.svelte';
|
|
9
|
+
export { default as SearchFieldInput } from './input/search-field-input.svelte';
|
|
10
|
+
export { default as SearchFieldClear } from './clear/search-field-clear.svelte';
|
|
11
|
+
export type SearchFieldRootProps = ComponentProps<typeof SearchFieldRootComponent>;
|
|
12
|
+
export type SearchFieldLabelProps = ComponentProps<typeof SearchFieldLabelComponent>;
|
|
13
|
+
export type SearchFieldInputProps = ComponentProps<typeof SearchFieldInputComponent>;
|
|
14
|
+
export type SearchFieldClearProps = ComponentProps<typeof SearchFieldClearComponent>;
|
|
15
|
+
export { getSearchFieldContext, setSearchFieldContext, useSearchFieldContext, type SearchFieldChangeReason, type SearchFieldContext } from './root/context.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * as SearchField from './index.parts.js';
|
|
2
|
+
export { default as SearchFieldRoot } from './root/search-field-root.svelte';
|
|
3
|
+
export { default as SearchFieldLabel } from './label/search-field-label.svelte';
|
|
4
|
+
export { default as SearchFieldInput } from './input/search-field-input.svelte';
|
|
5
|
+
export { default as SearchFieldClear } from './clear/search-field-clear.svelte';
|
|
6
|
+
export { getSearchFieldContext, setSearchFieldContext, useSearchFieldContext } from './root/context.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default as Root } from './root/search-field-root.svelte';
|
|
2
|
+
export { default as Label } from './label/search-field-label.svelte';
|
|
3
|
+
export { default as Input } from './input/search-field-input.svelte';
|
|
4
|
+
export { default as Clear } from './clear/search-field-clear.svelte';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default as Root } from './root/search-field-root.svelte';
|
|
2
|
+
export { default as Label } from './label/search-field-label.svelte';
|
|
3
|
+
export { default as Input } from './input/search-field-input.svelte';
|
|
4
|
+
export { default as Clear } from './clear/search-field-clear.svelte';
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
3
|
+
import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
|
|
4
|
+
import {
|
|
5
|
+
shouldShowFocusVisible,
|
|
6
|
+
trackInteractionModality
|
|
7
|
+
} from '../../primitives/input-modality';
|
|
8
|
+
import { composeEventHandlers } from '../../utils/compose-event-handlers';
|
|
9
|
+
import { useSearchFieldContext } from '../root/context';
|
|
10
|
+
|
|
11
|
+
type SearchFieldInputProps = Omit<
|
|
12
|
+
HTMLInputAttributes,
|
|
13
|
+
| 'class'
|
|
14
|
+
| 'id'
|
|
15
|
+
| 'value'
|
|
16
|
+
| 'defaultValue'
|
|
17
|
+
| 'name'
|
|
18
|
+
| 'disabled'
|
|
19
|
+
| 'readonly'
|
|
20
|
+
| 'required'
|
|
21
|
+
| 'aria-disabled'
|
|
22
|
+
| 'aria-readonly'
|
|
23
|
+
| 'aria-required'
|
|
24
|
+
| 'aria-invalid'
|
|
25
|
+
> & {
|
|
26
|
+
class?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The type of the input. Keep `search`, because it gives the input the `searchbox` role.
|
|
29
|
+
* Change it only when the field holds a different kind of text, for example an email.
|
|
30
|
+
*/
|
|
31
|
+
type?: HTMLInputAttributes['type'];
|
|
32
|
+
/** A bindable reference to the input element. */
|
|
33
|
+
element?: HTMLInputElement | null;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let {
|
|
37
|
+
class: className,
|
|
38
|
+
type = 'search',
|
|
39
|
+
enterkeyhint = 'search',
|
|
40
|
+
element = $bindable<HTMLInputElement | null>(null),
|
|
41
|
+
oninput: onInputExternal,
|
|
42
|
+
onfocus: onFocusExternal,
|
|
43
|
+
onblur: onBlurExternal,
|
|
44
|
+
onkeydown: onKeyDownExternal,
|
|
45
|
+
onmousedown: onMouseDownExternal,
|
|
46
|
+
onpointerdown: onPointerDownExternal,
|
|
47
|
+
onmouseenter: onMouseEnterExternal,
|
|
48
|
+
onmouseleave: onMouseLeaveExternal,
|
|
49
|
+
...restProps
|
|
50
|
+
}: SearchFieldInputProps = $props();
|
|
51
|
+
|
|
52
|
+
const ctx = useSearchFieldContext('SearchField.Input');
|
|
53
|
+
let inputRef: HTMLInputElement | null = $state(null);
|
|
54
|
+
let hovered = $state(false);
|
|
55
|
+
|
|
56
|
+
$effect(() => {
|
|
57
|
+
element = inputRef;
|
|
58
|
+
ctx.setInputRef(inputRef);
|
|
59
|
+
return () => {
|
|
60
|
+
element = null;
|
|
61
|
+
ctx.setInputRef(null);
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
$effect(() => {
|
|
66
|
+
if (ctx.isDisabled) hovered = false;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
watchFocusVisible({
|
|
70
|
+
isFocused: () => ctx.focused,
|
|
71
|
+
element: () => inputRef,
|
|
72
|
+
set: (visible) => ctx.setFocusVisible(visible)
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
function handleInput(event: Event) {
|
|
76
|
+
ctx.setValue((event.currentTarget as HTMLInputElement).value, 'input');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function handleFocus() {
|
|
80
|
+
if (ctx.isDisabled) return;
|
|
81
|
+
ctx.setFocused(true);
|
|
82
|
+
ctx.setFocusVisible(shouldShowFocusVisible(inputRef));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function handleBlur() {
|
|
86
|
+
ctx.setFocused(false);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
90
|
+
trackInteractionModality(event, inputRef);
|
|
91
|
+
ctx.setFocusVisible(ctx.focused ? true : shouldShowFocusVisible(inputRef));
|
|
92
|
+
|
|
93
|
+
// A key in an IME composition belongs to the composition: `Escape` cancels the candidate
|
|
94
|
+
// text and `Enter` accepts it.
|
|
95
|
+
if (event.isComposing || ctx.isDisabled || ctx.isReadOnly) return;
|
|
96
|
+
|
|
97
|
+
if (event.key === 'Escape') {
|
|
98
|
+
// An empty field lets the key go on, thus a dialog or a popover around it can close.
|
|
99
|
+
if (!ctx.clear('escape-key')) return;
|
|
100
|
+
// The native search input also empties itself on `Escape`. Stop it, and stop the key
|
|
101
|
+
// before it closes a dialog that contains the field.
|
|
102
|
+
event.preventDefault();
|
|
103
|
+
event.stopPropagation();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// `Enter` keeps its native action, thus the field also submits the form that contains it.
|
|
108
|
+
if (event.key === 'Enter') ctx.submit();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function handlePointerModality(event: MouseEvent | PointerEvent) {
|
|
112
|
+
trackInteractionModality(event, inputRef);
|
|
113
|
+
ctx.setFocusVisible(false);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function handleMouseEnter() {
|
|
117
|
+
hovered = !ctx.isDisabled;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function handleMouseLeave() {
|
|
121
|
+
hovered = false;
|
|
122
|
+
}
|
|
123
|
+
</script>
|
|
124
|
+
|
|
125
|
+
<input
|
|
126
|
+
{...restProps}
|
|
127
|
+
bind:this={inputRef}
|
|
128
|
+
id={ctx.inputId}
|
|
129
|
+
{type}
|
|
130
|
+
{enterkeyhint}
|
|
131
|
+
name={ctx.name}
|
|
132
|
+
value={ctx.value}
|
|
133
|
+
defaultValue={ctx.defaultValue}
|
|
134
|
+
disabled={ctx.isDisabled}
|
|
135
|
+
readonly={ctx.isReadOnly}
|
|
136
|
+
required={ctx.isRequired}
|
|
137
|
+
aria-invalid={ctx.isInvalid ? 'true' : undefined}
|
|
138
|
+
aria-readonly={ctx.isReadOnly || undefined}
|
|
139
|
+
aria-required={ctx.isRequired || undefined}
|
|
140
|
+
data-search-field-input="true"
|
|
141
|
+
data-empty={ctx.isEmpty || undefined}
|
|
142
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
143
|
+
data-readonly={ctx.isReadOnly || undefined}
|
|
144
|
+
data-required={ctx.isRequired || undefined}
|
|
145
|
+
data-invalid={ctx.isInvalid || undefined}
|
|
146
|
+
data-hovered={hovered || undefined}
|
|
147
|
+
data-focused={ctx.focused || undefined}
|
|
148
|
+
data-focus-visible={ctx.focusVisible || undefined}
|
|
149
|
+
oninput={composeEventHandlers(handleInput, onInputExternal ?? undefined)}
|
|
150
|
+
onfocus={composeEventHandlers(handleFocus, onFocusExternal ?? undefined)}
|
|
151
|
+
onblur={composeEventHandlers(handleBlur, onBlurExternal ?? undefined)}
|
|
152
|
+
onkeydown={composeEventHandlers(handleKeyDown, onKeyDownExternal ?? undefined)}
|
|
153
|
+
onmousedown={composeEventHandlers(handlePointerModality, onMouseDownExternal ?? undefined)}
|
|
154
|
+
onpointerdown={composeEventHandlers(handlePointerModality, onPointerDownExternal ?? undefined)}
|
|
155
|
+
onmouseenter={composeEventHandlers(handleMouseEnter, onMouseEnterExternal ?? undefined)}
|
|
156
|
+
onmouseleave={composeEventHandlers(handleMouseLeave, onMouseLeaveExternal ?? undefined)}
|
|
157
|
+
class={className}
|
|
158
|
+
/>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
2
|
+
type SearchFieldInputProps = Omit<HTMLInputAttributes, 'class' | 'id' | 'value' | 'defaultValue' | 'name' | 'disabled' | 'readonly' | 'required' | 'aria-disabled' | 'aria-readonly' | 'aria-required' | 'aria-invalid'> & {
|
|
3
|
+
class?: string;
|
|
4
|
+
/**
|
|
5
|
+
* The type of the input. Keep `search`, because it gives the input the `searchbox` role.
|
|
6
|
+
* Change it only when the field holds a different kind of text, for example an email.
|
|
7
|
+
*/
|
|
8
|
+
type?: HTMLInputAttributes['type'];
|
|
9
|
+
/** A bindable reference to the input element. */
|
|
10
|
+
element?: HTMLInputElement | null;
|
|
11
|
+
};
|
|
12
|
+
declare const SearchFieldInput: import("svelte").Component<SearchFieldInputProps, {}, "element">;
|
|
13
|
+
type SearchFieldInput = ReturnType<typeof SearchFieldInput>;
|
|
14
|
+
export default SearchFieldInput;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { HTMLLabelAttributes } from 'svelte/elements';
|
|
3
|
+
import { useSearchFieldContext } from '../root/context';
|
|
4
|
+
|
|
5
|
+
type SearchFieldLabelProps = Omit<HTMLLabelAttributes, 'class' | 'for'> & {
|
|
6
|
+
class?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* SearchField.Label — the accessible name of the input.
|
|
11
|
+
*
|
|
12
|
+
* It is a native `<label>` for the input, thus a click on the text moves the focus to the input.
|
|
13
|
+
*/
|
|
14
|
+
let { children, class: className, ...restProps }: SearchFieldLabelProps = $props();
|
|
15
|
+
|
|
16
|
+
const ctx = useSearchFieldContext('SearchField.Label');
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<label
|
|
20
|
+
{...restProps}
|
|
21
|
+
for={ctx.inputId}
|
|
22
|
+
class={className}
|
|
23
|
+
data-search-field-label="true"
|
|
24
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
25
|
+
>
|
|
26
|
+
{@render children?.()}
|
|
27
|
+
</label>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HTMLLabelAttributes } from 'svelte/elements';
|
|
2
|
+
type SearchFieldLabelProps = Omit<HTMLLabelAttributes, 'class' | 'for'> & {
|
|
3
|
+
class?: string;
|
|
4
|
+
};
|
|
5
|
+
declare const SearchFieldLabel: import("svelte").Component<SearchFieldLabelProps, {}, "">;
|
|
6
|
+
type SearchFieldLabel = ReturnType<typeof SearchFieldLabel>;
|
|
7
|
+
export default SearchFieldLabel;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# SearchField.Root
|
|
2
|
+
|
|
3
|
+
Holds the search text and the state of the field. It gives the shared context to each SearchField part.
|
|
4
|
+
|
|
5
|
+
Use `bind:value` for the state in the two directions. With a `name`, the input submits the text in a form. A form reset puts back `defaultValue`.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** What made the value change. */
|
|
2
|
+
export type SearchFieldChangeReason = 'input' | 'escape-key' | 'clear-press' | 'form-reset';
|
|
3
|
+
export type SearchFieldContext = {
|
|
4
|
+
id: string;
|
|
5
|
+
inputId: string;
|
|
6
|
+
value: string;
|
|
7
|
+
defaultValue: string;
|
|
8
|
+
name: string | undefined;
|
|
9
|
+
isEmpty: boolean;
|
|
10
|
+
isDisabled: boolean;
|
|
11
|
+
isReadOnly: boolean;
|
|
12
|
+
isRequired: boolean;
|
|
13
|
+
isInvalid: boolean;
|
|
14
|
+
clearAriaLabel: string;
|
|
15
|
+
focused: boolean;
|
|
16
|
+
focusVisible: boolean;
|
|
17
|
+
inputRef: HTMLInputElement | null;
|
|
18
|
+
setInputRef: (element: HTMLInputElement | null) => void;
|
|
19
|
+
setFocused: (focused: boolean) => void;
|
|
20
|
+
setFocusVisible: (visible: boolean) => void;
|
|
21
|
+
setValue: (value: string, reason: SearchFieldChangeReason) => void;
|
|
22
|
+
/** Empties the value and calls `onClear`. It does nothing when the value is already empty. */
|
|
23
|
+
clear: (reason: 'escape-key' | 'clear-press') => boolean;
|
|
24
|
+
submit: () => void;
|
|
25
|
+
};
|
|
26
|
+
export declare function setSearchFieldContext(context: SearchFieldContext): void;
|
|
27
|
+
export declare function getSearchFieldContext(): SearchFieldContext | undefined;
|
|
28
|
+
export declare function useSearchFieldContext(part: string): SearchFieldContext;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const KEY = Symbol('search-field');
|
|
3
|
+
export function setSearchFieldContext(context) {
|
|
4
|
+
setContext(KEY, context);
|
|
5
|
+
}
|
|
6
|
+
export function getSearchFieldContext() {
|
|
7
|
+
return getContext(KEY);
|
|
8
|
+
}
|
|
9
|
+
export function useSearchFieldContext(part) {
|
|
10
|
+
const context = getSearchFieldContext();
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error(`${part} must be used within SearchField.Root.`);
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|