@tableslayer/ui 0.2.8 → 0.3.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/dist/components/Autocomplete/Autocomplete.svelte +874 -0
- package/dist/components/Autocomplete/Autocomplete.svelte.d.ts +26 -0
- package/dist/components/Autocomplete/index.d.ts +2 -0
- package/dist/components/Autocomplete/index.js +2 -0
- package/dist/components/Autocomplete/types.d.ts +81 -0
- package/dist/components/Autocomplete/types.js +1 -0
- package/dist/components/Dialog/Dialog.svelte +174 -0
- package/dist/components/Dialog/Dialog.svelte.d.ts +23 -0
- package/dist/components/Dialog/index.d.ts +2 -0
- package/dist/components/Dialog/index.js +2 -0
- package/dist/components/Dialog/types.d.ts +20 -0
- package/dist/components/Dialog/types.js +1 -0
- package/dist/components/Input/FileInput.svelte.d.ts +1 -1
- package/dist/components/Input/InputCheckbox.svelte.d.ts +1 -1
- package/dist/components/NumberInput/NumberInput.svelte +241 -0
- package/dist/components/NumberInput/NumberInput.svelte.d.ts +16 -0
- package/dist/components/NumberInput/index.d.ts +2 -0
- package/dist/components/NumberInput/index.js +2 -0
- package/dist/components/NumberInput/types.d.ts +18 -0
- package/dist/components/NumberInput/types.js +1 -0
- package/dist/components/Tabs/Tabs.svelte +204 -0
- package/dist/components/Tabs/Tabs.svelte.d.ts +26 -0
- package/dist/components/Tabs/index.d.ts +2 -0
- package/dist/components/Tabs/index.js +2 -0
- package/dist/components/Tabs/types.d.ts +25 -0
- package/dist/components/Tabs/types.js +1 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +4 -0
- package/dist/components/types.d.ts +1 -0
- package/dist/components/types.js +1 -1
- package/package.json +11 -7
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AutocompleteProps } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* ## Autocomplete
|
|
4
|
+
*
|
|
5
|
+
* A combo box: a field that holds what has been chosen as pills, with a place to type beside
|
|
6
|
+
* them, and under it a list of the options that match what is typed. One choice or several.
|
|
7
|
+
* `selected` is the values chosen, as it is for `Select`. The options are a list, or a function
|
|
8
|
+
* that finds those matching the text. With `onCreate`, text that matches nothing can be made
|
|
9
|
+
* into a new option; with a `delimiter`, several can be typed or pasted at once.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```svelte
|
|
13
|
+
* <Autocomplete
|
|
14
|
+
* options={[
|
|
15
|
+
* { value: 'woods', label: 'Woods' },
|
|
16
|
+
* { value: 'hills', label: 'Hills', description: 'Rough going' }
|
|
17
|
+
* ]}
|
|
18
|
+
* bind:selected
|
|
19
|
+
* isMultiple
|
|
20
|
+
* placeholder="Terrain"
|
|
21
|
+
* />
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare const Autocomplete: import("svelte").Component<AutocompleteProps, {}, "selected" | "query" | "isOpen" | "element">;
|
|
25
|
+
type Autocomplete = ReturnType<typeof Autocomplete>;
|
|
26
|
+
export default Autocomplete;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { OffsetOptions, Placement } from '@floating-ui/dom';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
4
|
+
export type AutocompleteOption = {
|
|
5
|
+
value: string;
|
|
6
|
+
label: string;
|
|
7
|
+
/** Options with the same group, given one after another, are listed under it as a heading. */
|
|
8
|
+
group?: string;
|
|
9
|
+
/** A second, quieter line of text beside the label. It is searched as the label is. */
|
|
10
|
+
description?: string;
|
|
11
|
+
isDisabled?: boolean;
|
|
12
|
+
/** A colour for the option's pill once it is chosen: any CSS colour. Text over a hex colour is made to contrast. */
|
|
13
|
+
color?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
};
|
|
16
|
+
/** The options to choose from, or a function that finds those matching what has been typed. */
|
|
17
|
+
export type AutocompleteSource = AutocompleteOption[] | ((query: string) => Promise<AutocompleteOption[]>);
|
|
18
|
+
export type AutocompleteProps = {
|
|
19
|
+
options: AutocompleteSource;
|
|
20
|
+
/** The values chosen: one at most unless `isMultiple`. */
|
|
21
|
+
selected: string[];
|
|
22
|
+
onSelectedChange?: (selected: string[]) => void;
|
|
23
|
+
isMultiple?: boolean;
|
|
24
|
+
/** With one choice, shows it as plain text in the field and not as a pill. For a table cell or a tight form. */
|
|
25
|
+
isPlainText?: boolean;
|
|
26
|
+
variant?: 'default' | 'transparent';
|
|
27
|
+
size?: 'sm' | 'md';
|
|
28
|
+
isDisabled?: boolean;
|
|
29
|
+
/** What has been typed to search with. It is never the name of what is chosen. */
|
|
30
|
+
query?: string;
|
|
31
|
+
onQueryChange?: (query: string) => void;
|
|
32
|
+
isOpen?: boolean;
|
|
33
|
+
onIsOpenChange?: (open: boolean) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Lets text that matches no option be made into one: on Enter, on the row that offers it,
|
|
36
|
+
* on leaving the field, or at each `delimiter`. The function is given the text and returns
|
|
37
|
+
* the option made of it, which is then chosen; returning nothing turns the text down.
|
|
38
|
+
*/
|
|
39
|
+
onCreate?: (text: string) => AutocompleteOption | void | Promise<AutocompleteOption | void>;
|
|
40
|
+
/** The wording of the row that offers it. */
|
|
41
|
+
createLabel?: (text: string) => string;
|
|
42
|
+
/** Whether there is a button that clears what is chosen, and Backspace may take a single choice away. On unless turned off. */
|
|
43
|
+
isClearable?: boolean;
|
|
44
|
+
/** Decides whether an option matches what has been typed, in place of looking for the text in its label and description. */
|
|
45
|
+
filter?: (option: AutocompleteOption, query: string) => boolean;
|
|
46
|
+
/** Whether capitals matter in matching. */
|
|
47
|
+
isCaseSensitive?: boolean;
|
|
48
|
+
/** `startsWith` lists the options that begin with the text before those that only hold it. */
|
|
49
|
+
sortMatchesBy?: 'none' | 'startsWith';
|
|
50
|
+
/** A character that ends an option as it is typed or pasted, usually a comma: "a, b, c" pasted is three choices. */
|
|
51
|
+
delimiter?: string;
|
|
52
|
+
/** Whether there is a list of options. Turned off, the field only takes what is typed, with `onCreate`. On unless turned off. */
|
|
53
|
+
showSuggestions?: boolean;
|
|
54
|
+
/** Shows the list as busy, for options being fetched by the caller. A function given as `options` does this itself. */
|
|
55
|
+
isLoading?: boolean;
|
|
56
|
+
/** How long to wait after typing stops before asking a function for options, in milliseconds. */
|
|
57
|
+
debounce?: number;
|
|
58
|
+
/** Said when there are no options at all. */
|
|
59
|
+
emptyText?: string;
|
|
60
|
+
/** Said when the text typed matches none. */
|
|
61
|
+
noMatchText?: (text: string) => string;
|
|
62
|
+
/** Said when every option has been chosen. */
|
|
63
|
+
allSelectedText?: string;
|
|
64
|
+
/** Said when the text typed names an option already chosen. */
|
|
65
|
+
alreadyAddedText?: (label: string) => string;
|
|
66
|
+
loadingText?: string;
|
|
67
|
+
/** Shown before the text field, inside it. */
|
|
68
|
+
start?: Snippet;
|
|
69
|
+
/** How one option is drawn, in place of its label and description. */
|
|
70
|
+
option?: Snippet<[{
|
|
71
|
+
option: AutocompleteOption;
|
|
72
|
+
isActive: boolean;
|
|
73
|
+
isSelected: boolean;
|
|
74
|
+
query: string;
|
|
75
|
+
}]>;
|
|
76
|
+
positioning?: {
|
|
77
|
+
placement?: Placement;
|
|
78
|
+
offset?: OffsetOptions;
|
|
79
|
+
};
|
|
80
|
+
element?: HTMLInputElement;
|
|
81
|
+
} & Omit<HTMLInputAttributes, 'value' | 'size' | 'disabled' | 'multiple'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { IconX } from '@tabler/icons-svelte';
|
|
3
|
+
import { Icon } from '../Icon';
|
|
4
|
+
import { Title } from '../Title';
|
|
5
|
+
import type { DialogProps } from './types';
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
isOpen = $bindable(false),
|
|
9
|
+
onIsOpenChange,
|
|
10
|
+
title,
|
|
11
|
+
size = 'md',
|
|
12
|
+
isClosedOnOutsideClick = true,
|
|
13
|
+
showCloseButton = true,
|
|
14
|
+
children,
|
|
15
|
+
footer,
|
|
16
|
+
...restProps
|
|
17
|
+
}: DialogProps = $props();
|
|
18
|
+
|
|
19
|
+
const uid = $props.id();
|
|
20
|
+
let dialog = $state<HTMLDialogElement | null>(null);
|
|
21
|
+
|
|
22
|
+
const close = () => {
|
|
23
|
+
if (!isOpen) return;
|
|
24
|
+
isOpen = false;
|
|
25
|
+
onIsOpenChange?.(false);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// The browser's own modal dialog: it holds the focus, dims the page and closes on Escape.
|
|
29
|
+
$effect(() => {
|
|
30
|
+
if (!dialog) return;
|
|
31
|
+
if (isOpen && !dialog.open) dialog.showModal();
|
|
32
|
+
else if (!isOpen && dialog.open) dialog.close();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/** Where a press began, so that a drag out of the dialog that ends on the page behind does not close it. */
|
|
36
|
+
let pressedOutside = false;
|
|
37
|
+
const classes = $derived(['dialog', `dialog--${size}`, restProps.class ?? '']);
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<!--
|
|
41
|
+
@component
|
|
42
|
+
## Dialog
|
|
43
|
+
|
|
44
|
+
A modal dialog over the page, for something that must be dealt with before going on. It is
|
|
45
|
+
the browser's `<dialog>`, so focus is held inside it and Escape closes it.
|
|
46
|
+
|
|
47
|
+
@example
|
|
48
|
+
```svelte
|
|
49
|
+
<Dialog bind:isOpen title="Rename the scenario">
|
|
50
|
+
{#snippet children()}
|
|
51
|
+
<Input bind:value={name} />
|
|
52
|
+
{/snippet}
|
|
53
|
+
{#snippet footer({ close })}
|
|
54
|
+
<Button variant="ghost" onclick={close}>Cancel</Button>
|
|
55
|
+
<Button onclick={save}>Rename</Button>
|
|
56
|
+
{/snippet}
|
|
57
|
+
</Dialog>
|
|
58
|
+
```
|
|
59
|
+
-->
|
|
60
|
+
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_noninteractive_element_interactions -->
|
|
61
|
+
<dialog
|
|
62
|
+
{...restProps}
|
|
63
|
+
bind:this={dialog}
|
|
64
|
+
class={classes}
|
|
65
|
+
aria-labelledby={title ? `${uid}-title` : undefined}
|
|
66
|
+
onclose={close}
|
|
67
|
+
onpointerdown={(event) => (pressedOutside = event.target === dialog)}
|
|
68
|
+
onclick={(event) => {
|
|
69
|
+
if (isClosedOnOutsideClick && pressedOutside && event.target === dialog) close();
|
|
70
|
+
}}
|
|
71
|
+
>
|
|
72
|
+
{#if isOpen}
|
|
73
|
+
<div class="dialog__box">
|
|
74
|
+
{#if title || showCloseButton}
|
|
75
|
+
<header class="dialog__header">
|
|
76
|
+
{#if title}
|
|
77
|
+
<Title as="h2" size="sm" id={`${uid}-title`}>{title}</Title>
|
|
78
|
+
{/if}
|
|
79
|
+
{#if showCloseButton}
|
|
80
|
+
<button type="button" class="dialog__close" aria-label="Close" onclick={close}>
|
|
81
|
+
<Icon Icon={IconX} size="1rem" />
|
|
82
|
+
</button>
|
|
83
|
+
{/if}
|
|
84
|
+
</header>
|
|
85
|
+
{/if}
|
|
86
|
+
<div class="dialog__content">
|
|
87
|
+
{@render children({ close })}
|
|
88
|
+
</div>
|
|
89
|
+
{#if footer}
|
|
90
|
+
<footer class="dialog__footer">
|
|
91
|
+
{@render footer({ close })}
|
|
92
|
+
</footer>
|
|
93
|
+
{/if}
|
|
94
|
+
</div>
|
|
95
|
+
{/if}
|
|
96
|
+
</dialog>
|
|
97
|
+
|
|
98
|
+
<style>
|
|
99
|
+
.dialog {
|
|
100
|
+
width: 32rem;
|
|
101
|
+
max-width: calc(100vw - 2rem);
|
|
102
|
+
max-height: calc(100dvh - 2rem);
|
|
103
|
+
padding: 0;
|
|
104
|
+
color: var(--fg);
|
|
105
|
+
background: var(--popoverBg);
|
|
106
|
+
border: var(--borderThin);
|
|
107
|
+
border-radius: var(--radius-2);
|
|
108
|
+
box-shadow: var(--shadow-3);
|
|
109
|
+
overflow: hidden;
|
|
110
|
+
}
|
|
111
|
+
.dialog--sm {
|
|
112
|
+
width: 22rem;
|
|
113
|
+
}
|
|
114
|
+
.dialog--lg {
|
|
115
|
+
width: 48rem;
|
|
116
|
+
}
|
|
117
|
+
.dialog--full {
|
|
118
|
+
width: calc(100vw - 2rem);
|
|
119
|
+
height: calc(100dvh - 2rem);
|
|
120
|
+
}
|
|
121
|
+
.dialog::backdrop {
|
|
122
|
+
background: rgb(0 0 0 / 0.55);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.dialog__box {
|
|
126
|
+
display: flex;
|
|
127
|
+
flex-direction: column;
|
|
128
|
+
height: 100%;
|
|
129
|
+
max-height: calc(100dvh - 2rem);
|
|
130
|
+
}
|
|
131
|
+
.dialog__header {
|
|
132
|
+
display: flex;
|
|
133
|
+
align-items: center;
|
|
134
|
+
flex: none;
|
|
135
|
+
gap: 0.75rem;
|
|
136
|
+
padding: 0.5rem 0.5rem 0.5rem 0.75rem;
|
|
137
|
+
border-bottom: var(--borderThin);
|
|
138
|
+
}
|
|
139
|
+
.dialog__close {
|
|
140
|
+
display: flex;
|
|
141
|
+
align-items: center;
|
|
142
|
+
justify-content: center;
|
|
143
|
+
width: 1.5rem;
|
|
144
|
+
height: 1.5rem;
|
|
145
|
+
margin-left: auto;
|
|
146
|
+
padding: 0;
|
|
147
|
+
color: var(--fgMuted);
|
|
148
|
+
background: transparent;
|
|
149
|
+
border: none;
|
|
150
|
+
border-radius: var(--radius-1);
|
|
151
|
+
cursor: pointer;
|
|
152
|
+
}
|
|
153
|
+
.dialog__close:hover {
|
|
154
|
+
color: var(--fg);
|
|
155
|
+
background: var(--contrastLow);
|
|
156
|
+
}
|
|
157
|
+
.dialog__content {
|
|
158
|
+
flex: 1;
|
|
159
|
+
min-height: 0;
|
|
160
|
+
padding: 0.75rem;
|
|
161
|
+
overflow-y: auto;
|
|
162
|
+
}
|
|
163
|
+
.dialog--full .dialog__content {
|
|
164
|
+
padding: 0;
|
|
165
|
+
}
|
|
166
|
+
.dialog__footer {
|
|
167
|
+
display: flex;
|
|
168
|
+
flex: none;
|
|
169
|
+
justify-content: flex-end;
|
|
170
|
+
gap: 0.5rem;
|
|
171
|
+
padding: 0.5rem 0.75rem;
|
|
172
|
+
border-top: var(--borderThin);
|
|
173
|
+
}
|
|
174
|
+
</style>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DialogProps } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* ## Dialog
|
|
4
|
+
*
|
|
5
|
+
* A modal dialog over the page, for something that must be dealt with before going on. It is
|
|
6
|
+
* the browser's `<dialog>`, so focus is held inside it and Escape closes it.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```svelte
|
|
10
|
+
* <Dialog bind:isOpen title="Rename the scenario">
|
|
11
|
+
* {#snippet children()}
|
|
12
|
+
* <Input bind:value={name} />
|
|
13
|
+
* {/snippet}
|
|
14
|
+
* {#snippet footer({ close })}
|
|
15
|
+
* <Button variant="ghost" onclick={close}>Cancel</Button>
|
|
16
|
+
* <Button onclick={save}>Rename</Button>
|
|
17
|
+
* {/snippet}
|
|
18
|
+
* </Dialog>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare const Dialog: import("svelte").Component<DialogProps, {}, "isOpen">;
|
|
22
|
+
type Dialog = ReturnType<typeof Dialog>;
|
|
23
|
+
export default Dialog;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLDialogAttributes } from 'svelte/elements';
|
|
3
|
+
export type DialogProps = {
|
|
4
|
+
isOpen?: boolean;
|
|
5
|
+
onIsOpenChange?: (open: boolean) => void;
|
|
6
|
+
/** The heading. Left out, the dialog must be named some other way (`aria-label`). */
|
|
7
|
+
title?: string;
|
|
8
|
+
size?: 'sm' | 'md' | 'lg' | 'full';
|
|
9
|
+
/** Whether a click on the dimmed page behind closes it. Escape always does. */
|
|
10
|
+
isClosedOnOutsideClick?: boolean;
|
|
11
|
+
/** Whether the heading has a button that closes it. */
|
|
12
|
+
showCloseButton?: boolean;
|
|
13
|
+
children: Snippet<[{
|
|
14
|
+
close: () => void;
|
|
15
|
+
}]>;
|
|
16
|
+
/** A row of buttons along the foot. */
|
|
17
|
+
footer?: Snippet<[{
|
|
18
|
+
close: () => void;
|
|
19
|
+
}]>;
|
|
20
|
+
} & Omit<HTMLDialogAttributes, 'children' | 'open' | 'title'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -16,6 +16,6 @@ declare const FileInput: $$__sveltets_2_IsomorphicComponent<FileInputProps, {
|
|
|
16
16
|
fileSelect: CustomEvent<File>;
|
|
17
17
|
} & {
|
|
18
18
|
[evt: string]: CustomEvent<any>;
|
|
19
|
-
}, {}, {}, "
|
|
19
|
+
}, {}, {}, "value" | "files">;
|
|
20
20
|
type FileInput = InstanceType<typeof FileInput>;
|
|
21
21
|
export default FileInput;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { InputCheckboxProps } from './types';
|
|
2
|
-
declare const InputCheckbox: import("svelte").Component<InputCheckboxProps, {}, "
|
|
2
|
+
declare const InputCheckbox: import("svelte").Component<InputCheckboxProps, {}, "checked" | "element">;
|
|
3
3
|
type InputCheckbox = ReturnType<typeof InputCheckbox>;
|
|
4
4
|
export default InputCheckbox;
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { IconChevronDown, IconChevronUp } from '@tabler/icons-svelte';
|
|
3
|
+
import { Icon } from '../Icon';
|
|
4
|
+
import { untrack } from 'svelte';
|
|
5
|
+
import type { NumberInputProps } from './types';
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
value = $bindable(),
|
|
9
|
+
onValueChange,
|
|
10
|
+
min,
|
|
11
|
+
max,
|
|
12
|
+
step = 1,
|
|
13
|
+
isEmptyAllowed = false,
|
|
14
|
+
variant = 'default',
|
|
15
|
+
size = 'md',
|
|
16
|
+
showStepper = true,
|
|
17
|
+
isDisabled = false,
|
|
18
|
+
element = $bindable(),
|
|
19
|
+
...restProps
|
|
20
|
+
}: NumberInputProps = $props();
|
|
21
|
+
|
|
22
|
+
const show = (number: number | null) => (number === null || number === undefined ? '' : String(number));
|
|
23
|
+
/** What is in the field: the number, or whatever is part-way typed. */
|
|
24
|
+
let text = $state(untrack(() => show(value)));
|
|
25
|
+
let isEditing = $state(false);
|
|
26
|
+
$effect(() => {
|
|
27
|
+
const shown = show(value);
|
|
28
|
+
if (!untrack(() => isEditing)) text = shown;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** As many decimal places as the step has, so that stepping by 0.1 does not wander. */
|
|
32
|
+
const places = $derived((String(step).split('.')[1] ?? '').length);
|
|
33
|
+
/** Held within the least and the most, with the dust of decimal arithmetic rounded off. */
|
|
34
|
+
const fitted = (number: number) =>
|
|
35
|
+
Number(Math.min(max ?? Infinity, Math.max(min ?? -Infinity, number)).toFixed(Math.max(places, 6)));
|
|
36
|
+
|
|
37
|
+
const settle = (next: number | null) => {
|
|
38
|
+
isEditing = false;
|
|
39
|
+
text = show(next);
|
|
40
|
+
if (next === value) return;
|
|
41
|
+
value = next;
|
|
42
|
+
onValueChange?.(next);
|
|
43
|
+
};
|
|
44
|
+
const commit = () => {
|
|
45
|
+
const typed = text.trim().replace(',', '.');
|
|
46
|
+
if (typed === '') return settle(isEmptyAllowed ? null : value);
|
|
47
|
+
const number = Number(typed);
|
|
48
|
+
settle(Number.isFinite(number) ? fitted(number) : value);
|
|
49
|
+
};
|
|
50
|
+
const stepBy = (by: number) => {
|
|
51
|
+
if (isDisabled) return;
|
|
52
|
+
const typed = Number(text.trim().replace(',', '.'));
|
|
53
|
+
const from = text.trim() !== '' && Number.isFinite(typed) ? typed : (value ?? min ?? 0);
|
|
54
|
+
settle(fitted(Number((from + by).toFixed(places))));
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const onkeydown = (event: KeyboardEvent & { currentTarget: HTMLInputElement }) => {
|
|
58
|
+
restProps.onkeydown?.(event);
|
|
59
|
+
if (event.defaultPrevented) return;
|
|
60
|
+
const many = event.shiftKey ? 10 : 1;
|
|
61
|
+
if (event.key === 'ArrowUp') {
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
stepBy(step * many);
|
|
64
|
+
} else if (event.key === 'ArrowDown') {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
stepBy(-step * many);
|
|
67
|
+
} else if (event.key === 'Enter') {
|
|
68
|
+
commit();
|
|
69
|
+
} else if (event.key === 'Escape' && isEditing) {
|
|
70
|
+
event.stopPropagation();
|
|
71
|
+
settle(value);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const classes = $derived([
|
|
76
|
+
'numberInput',
|
|
77
|
+
`numberInput--${variant}`,
|
|
78
|
+
`numberInput--${size}`,
|
|
79
|
+
isDisabled && 'numberInput--isDisabled',
|
|
80
|
+
restProps.class ?? ''
|
|
81
|
+
]);
|
|
82
|
+
</script>
|
|
83
|
+
|
|
84
|
+
<!--
|
|
85
|
+
@component
|
|
86
|
+
## NumberInput
|
|
87
|
+
|
|
88
|
+
A field for a number, with buttons and arrow keys that step it (Shift steps by ten). The
|
|
89
|
+
number is held within `min` and `max` and told through `onValueChange` once it is settled,
|
|
90
|
+
not on every key.
|
|
91
|
+
|
|
92
|
+
@example
|
|
93
|
+
```svelte
|
|
94
|
+
<NumberInput bind:value={cost} min={0} max={9} step={0.5} />
|
|
95
|
+
```
|
|
96
|
+
-->
|
|
97
|
+
<div class={classes}>
|
|
98
|
+
<input
|
|
99
|
+
{...restProps}
|
|
100
|
+
bind:this={element}
|
|
101
|
+
class="numberInput__input"
|
|
102
|
+
type="text"
|
|
103
|
+
inputmode="decimal"
|
|
104
|
+
role="spinbutton"
|
|
105
|
+
autocomplete="off"
|
|
106
|
+
aria-valuenow={value ?? undefined}
|
|
107
|
+
aria-valuemin={min}
|
|
108
|
+
aria-valuemax={max}
|
|
109
|
+
disabled={isDisabled}
|
|
110
|
+
value={text}
|
|
111
|
+
oninput={(event) => {
|
|
112
|
+
isEditing = true;
|
|
113
|
+
text = event.currentTarget.value;
|
|
114
|
+
restProps.oninput?.(event);
|
|
115
|
+
}}
|
|
116
|
+
onblur={(event) => {
|
|
117
|
+
if (isEditing) commit();
|
|
118
|
+
restProps.onblur?.(event);
|
|
119
|
+
}}
|
|
120
|
+
{onkeydown}
|
|
121
|
+
/>
|
|
122
|
+
{#if showStepper}
|
|
123
|
+
<div class="numberInput__stepper">
|
|
124
|
+
<button
|
|
125
|
+
type="button"
|
|
126
|
+
class="numberInput__step"
|
|
127
|
+
aria-label="More"
|
|
128
|
+
tabindex="-1"
|
|
129
|
+
disabled={isDisabled || (max !== undefined && value !== null && value >= max)}
|
|
130
|
+
onpointerdown={(event) => event.preventDefault()}
|
|
131
|
+
onclick={() => stepBy(step)}
|
|
132
|
+
>
|
|
133
|
+
<Icon Icon={IconChevronUp} size="0.625rem" />
|
|
134
|
+
</button>
|
|
135
|
+
<button
|
|
136
|
+
type="button"
|
|
137
|
+
class="numberInput__step"
|
|
138
|
+
aria-label="Less"
|
|
139
|
+
tabindex="-1"
|
|
140
|
+
disabled={isDisabled || (min !== undefined && value !== null && value <= min)}
|
|
141
|
+
onpointerdown={(event) => event.preventDefault()}
|
|
142
|
+
onclick={() => stepBy(-step)}
|
|
143
|
+
>
|
|
144
|
+
<Icon Icon={IconChevronDown} size="0.625rem" />
|
|
145
|
+
</button>
|
|
146
|
+
</div>
|
|
147
|
+
{/if}
|
|
148
|
+
</div>
|
|
149
|
+
|
|
150
|
+
<style>
|
|
151
|
+
.numberInput {
|
|
152
|
+
display: flex;
|
|
153
|
+
align-items: stretch;
|
|
154
|
+
width: 100%;
|
|
155
|
+
height: 2rem;
|
|
156
|
+
font-family: var(--font-sans);
|
|
157
|
+
background: var(--inputBg);
|
|
158
|
+
border: var(--borderThin);
|
|
159
|
+
border-color: var(--inputBorderColor);
|
|
160
|
+
border-radius: var(--radius-2);
|
|
161
|
+
overflow: hidden;
|
|
162
|
+
}
|
|
163
|
+
.numberInput:focus-within {
|
|
164
|
+
border-color: var(--fg);
|
|
165
|
+
background: var(--inputFocusBg);
|
|
166
|
+
}
|
|
167
|
+
.numberInput--sm {
|
|
168
|
+
height: 1.5rem;
|
|
169
|
+
font-size: 0.875rem;
|
|
170
|
+
}
|
|
171
|
+
.numberInput--transparent {
|
|
172
|
+
border-color: transparent;
|
|
173
|
+
background: transparent;
|
|
174
|
+
}
|
|
175
|
+
.numberInput--transparent:hover {
|
|
176
|
+
border-color: var(--inputBorderColor);
|
|
177
|
+
}
|
|
178
|
+
.numberInput--transparent:focus-within {
|
|
179
|
+
border-color: var(--fg);
|
|
180
|
+
background: var(--inputFocusBg);
|
|
181
|
+
}
|
|
182
|
+
.numberInput--isDisabled {
|
|
183
|
+
opacity: 0.5;
|
|
184
|
+
cursor: not-allowed;
|
|
185
|
+
}
|
|
186
|
+
.numberInput:has([aria-invalid='true']) {
|
|
187
|
+
border-color: var(--fgDanger);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.numberInput__input {
|
|
191
|
+
flex: 1;
|
|
192
|
+
width: 100%;
|
|
193
|
+
min-width: 0;
|
|
194
|
+
padding: 0 0.25rem 0 0.75rem;
|
|
195
|
+
font: inherit;
|
|
196
|
+
font-variant-numeric: tabular-nums;
|
|
197
|
+
color: inherit;
|
|
198
|
+
background: transparent;
|
|
199
|
+
border: none;
|
|
200
|
+
outline: none;
|
|
201
|
+
}
|
|
202
|
+
.numberInput--sm .numberInput__input {
|
|
203
|
+
padding-left: 0.5rem;
|
|
204
|
+
}
|
|
205
|
+
.numberInput__input:disabled {
|
|
206
|
+
cursor: not-allowed;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.numberInput__stepper {
|
|
210
|
+
display: flex;
|
|
211
|
+
flex: none;
|
|
212
|
+
flex-direction: column;
|
|
213
|
+
width: 1.125rem;
|
|
214
|
+
border-left: var(--borderThin);
|
|
215
|
+
border-color: var(--inputBorderColor);
|
|
216
|
+
opacity: 0;
|
|
217
|
+
}
|
|
218
|
+
.numberInput:hover .numberInput__stepper,
|
|
219
|
+
.numberInput:focus-within .numberInput__stepper {
|
|
220
|
+
opacity: 1;
|
|
221
|
+
}
|
|
222
|
+
.numberInput__step {
|
|
223
|
+
display: flex;
|
|
224
|
+
align-items: center;
|
|
225
|
+
justify-content: center;
|
|
226
|
+
flex: 1;
|
|
227
|
+
padding: 0;
|
|
228
|
+
color: var(--fgMuted);
|
|
229
|
+
background: transparent;
|
|
230
|
+
border: none;
|
|
231
|
+
cursor: pointer;
|
|
232
|
+
}
|
|
233
|
+
.numberInput__step:hover:not(:disabled) {
|
|
234
|
+
color: var(--fg);
|
|
235
|
+
background: var(--contrastLow);
|
|
236
|
+
}
|
|
237
|
+
.numberInput__step:disabled {
|
|
238
|
+
opacity: 0.4;
|
|
239
|
+
cursor: not-allowed;
|
|
240
|
+
}
|
|
241
|
+
</style>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NumberInputProps } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* ## NumberInput
|
|
4
|
+
*
|
|
5
|
+
* A field for a number, with buttons and arrow keys that step it (Shift steps by ten). The
|
|
6
|
+
* number is held within `min` and `max` and told through `onValueChange` once it is settled,
|
|
7
|
+
* not on every key.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```svelte
|
|
11
|
+
* <NumberInput bind:value={cost} min={0} max={9} step={0.5} />
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare const NumberInput: import("svelte").Component<NumberInputProps, {}, "value" | "element">;
|
|
15
|
+
type NumberInput = ReturnType<typeof NumberInput>;
|
|
16
|
+
export default NumberInput;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
2
|
+
export type NumberInputProps = {
|
|
3
|
+
/** The number, or null while the field is empty. */
|
|
4
|
+
value: number | null;
|
|
5
|
+
/** Told once the number is settled: on a step, on Enter, or on leaving the field. Not on every key. */
|
|
6
|
+
onValueChange?: (value: number | null) => void;
|
|
7
|
+
min?: number;
|
|
8
|
+
max?: number;
|
|
9
|
+
step?: number;
|
|
10
|
+
/** Whether the field may be left empty. If not, emptying it puts the last number back. */
|
|
11
|
+
isEmptyAllowed?: boolean;
|
|
12
|
+
variant?: 'default' | 'transparent';
|
|
13
|
+
size?: 'sm' | 'md';
|
|
14
|
+
/** Whether the buttons that step the number up and down are shown. The arrow keys step it either way. */
|
|
15
|
+
showStepper?: boolean;
|
|
16
|
+
isDisabled?: boolean;
|
|
17
|
+
element?: HTMLInputElement;
|
|
18
|
+
} & Omit<HTMLInputAttributes, 'value' | 'min' | 'max' | 'step' | 'size' | 'disabled' | 'type'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|