@nqmcreative/ui 0.3.0 → 0.4.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 +209 -15
- package/cli/index.mjs +247 -70
- package/dist/core/color.d.ts +24 -0
- package/dist/core/color.js +39 -0
- package/dist/core/index.d.ts +8 -1
- package/dist/core/index.js +5 -1
- package/dist/core/locale.svelte.d.ts +8 -0
- package/dist/core/locale.svelte.js +14 -0
- package/dist/core/number.d.ts +22 -0
- package/dist/core/number.js +33 -0
- package/dist/core/pin.d.ts +23 -0
- package/dist/core/pin.js +38 -0
- package/dist/core/tags.d.ts +37 -0
- package/dist/core/tags.js +51 -0
- package/dist/core/time.d.ts +30 -0
- package/dist/core/time.js +78 -0
- package/dist/styles/matte/ColorInput.svelte +110 -0
- package/dist/styles/matte/ColorInput.svelte.d.ts +16 -0
- package/dist/styles/matte/CurrencyInput.svelte +108 -0
- package/dist/styles/matte/CurrencyInput.svelte.d.ts +26 -0
- package/dist/styles/matte/FileInput.svelte +194 -0
- package/dist/styles/matte/FileInput.svelte.d.ts +32 -0
- package/dist/styles/matte/InputAddon.svelte +36 -0
- package/dist/styles/matte/InputAddon.svelte.d.ts +13 -0
- package/dist/styles/matte/PinInput.svelte +137 -0
- package/dist/styles/matte/PinInput.svelte.d.ts +25 -0
- package/dist/styles/matte/SearchInput.svelte +87 -0
- package/dist/styles/matte/SearchInput.svelte.d.ts +24 -0
- package/dist/styles/matte/TagsInput.svelte +173 -0
- package/dist/styles/matte/TagsInput.svelte.d.ts +29 -0
- package/dist/styles/matte/TimeInput.svelte +129 -0
- package/dist/styles/matte/TimeInput.svelte.d.ts +21 -0
- package/dist/styles/matte/index.d.ts +8 -0
- package/dist/styles/matte/index.js +8 -0
- package/dist/styles/paper/ColorInput.svelte +112 -0
- package/dist/styles/paper/ColorInput.svelte.d.ts +16 -0
- package/dist/styles/paper/CurrencyInput.svelte +108 -0
- package/dist/styles/paper/CurrencyInput.svelte.d.ts +26 -0
- package/dist/styles/paper/FileInput.svelte +204 -0
- package/dist/styles/paper/FileInput.svelte.d.ts +32 -0
- package/dist/styles/paper/InputAddon.svelte +36 -0
- package/dist/styles/paper/InputAddon.svelte.d.ts +13 -0
- package/dist/styles/paper/PinInput.svelte +137 -0
- package/dist/styles/paper/PinInput.svelte.d.ts +25 -0
- package/dist/styles/paper/SearchInput.svelte +92 -0
- package/dist/styles/paper/SearchInput.svelte.d.ts +24 -0
- package/dist/styles/paper/TagsInput.svelte +178 -0
- package/dist/styles/paper/TagsInput.svelte.d.ts +29 -0
- package/dist/styles/paper/TimeInput.svelte +155 -0
- package/dist/styles/paper/TimeInput.svelte.d.ts +21 -0
- package/dist/styles/paper/index.d.ts +8 -0
- package/dist/styles/paper/index.js +8 -0
- package/dist/styles/sprout/ColorInput.svelte +112 -0
- package/dist/styles/sprout/ColorInput.svelte.d.ts +16 -0
- package/dist/styles/sprout/CurrencyInput.svelte +108 -0
- package/dist/styles/sprout/CurrencyInput.svelte.d.ts +26 -0
- package/dist/styles/sprout/FileInput.svelte +205 -0
- package/dist/styles/sprout/FileInput.svelte.d.ts +32 -0
- package/dist/styles/sprout/InputAddon.svelte +37 -0
- package/dist/styles/sprout/InputAddon.svelte.d.ts +13 -0
- package/dist/styles/sprout/PinInput.svelte +138 -0
- package/dist/styles/sprout/PinInput.svelte.d.ts +25 -0
- package/dist/styles/sprout/SearchInput.svelte +92 -0
- package/dist/styles/sprout/SearchInput.svelte.d.ts +24 -0
- package/dist/styles/sprout/TagsInput.svelte +179 -0
- package/dist/styles/sprout/TagsInput.svelte.d.ts +29 -0
- package/dist/styles/sprout/TimeInput.svelte +155 -0
- package/dist/styles/sprout/TimeInput.svelte.d.ts +21 -0
- package/dist/styles/sprout/index.d.ts +8 -0
- package/dist/styles/sprout/index.js +8 -0
- package/package.json +98 -2
- package/registry.json +143 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { formatSize as toReadableSize, sortFiles, type RejectedFile } from '../../core/files.js';
|
|
3
|
+
import { useLocale } from '../../core/locale.svelte.js';
|
|
4
|
+
import { toneFocusWithinBorder, type Tone } from '../../core/tones.js';
|
|
5
|
+
import { edge } from './lift.js';
|
|
6
|
+
import type { InputSize } from './Input.svelte';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
/** Bindable list of accepted files. */
|
|
10
|
+
files?: File[];
|
|
11
|
+
/** Same syntax as the input's `accept` — `'image/*,.pdf'`. */
|
|
12
|
+
accept?: string;
|
|
13
|
+
multiple?: boolean;
|
|
14
|
+
/** Per-file ceiling in bytes. `0` means no limit. */
|
|
15
|
+
maxSize?: number;
|
|
16
|
+
/** Cap on how many files may be held at once. `0` means no limit. */
|
|
17
|
+
maxFiles?: number;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
size?: InputSize;
|
|
20
|
+
tone?: Tone;
|
|
21
|
+
invalid?: boolean;
|
|
22
|
+
/** Text on the button. Defaults to the locale's `chooseFile`. */
|
|
23
|
+
label?: string;
|
|
24
|
+
/** Shown while nothing is chosen. Defaults to the locale's `noFile`. */
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
/** Show the list of held files below the row. */
|
|
27
|
+
showList?: boolean;
|
|
28
|
+
/** The `name` the hidden input posts under, for a plain form submit. */
|
|
29
|
+
name?: string;
|
|
30
|
+
onaccept?: (files: File[]) => void;
|
|
31
|
+
onreject?: (rejected: RejectedFile[]) => void;
|
|
32
|
+
class?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let {
|
|
36
|
+
files = $bindable([]),
|
|
37
|
+
accept,
|
|
38
|
+
multiple = false,
|
|
39
|
+
maxSize = 0,
|
|
40
|
+
maxFiles = 0,
|
|
41
|
+
disabled = false,
|
|
42
|
+
size = 'md',
|
|
43
|
+
tone = 'brand',
|
|
44
|
+
invalid = false,
|
|
45
|
+
label,
|
|
46
|
+
placeholder,
|
|
47
|
+
showList = true,
|
|
48
|
+
name,
|
|
49
|
+
onaccept,
|
|
50
|
+
onreject,
|
|
51
|
+
class: className = ''
|
|
52
|
+
}: Props = $props();
|
|
53
|
+
|
|
54
|
+
const t = useLocale();
|
|
55
|
+
|
|
56
|
+
let input: HTMLInputElement | null = $state(null);
|
|
57
|
+
let rejected = $state<RejectedFile[]>([]);
|
|
58
|
+
|
|
59
|
+
const formatSize = (bytes: number) => toReadableSize(bytes, t.current.byteUnits);
|
|
60
|
+
|
|
61
|
+
/** One file reads as its name and weight; several read as a count. */
|
|
62
|
+
const summary = $derived.by(() => {
|
|
63
|
+
if (files.length === 0) return placeholder ?? t.current.noFile;
|
|
64
|
+
if (files.length === 1) return `${files[0].name} · ${formatSize(files[0].size)}`;
|
|
65
|
+
return `${files.length} ${t.current.selected}`;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
function add(incoming: FileList | File[] | null) {
|
|
69
|
+
if (!incoming || disabled) return;
|
|
70
|
+
|
|
71
|
+
const { accepted, rejected: bad } = sortFiles(incoming, {
|
|
72
|
+
accept,
|
|
73
|
+
maxSize,
|
|
74
|
+
maxFiles,
|
|
75
|
+
multiple,
|
|
76
|
+
held: files.length
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
rejected = bad;
|
|
80
|
+
if (bad.length) onreject?.(bad);
|
|
81
|
+
if (accepted.length === 0) return;
|
|
82
|
+
|
|
83
|
+
files = multiple ? [...files, ...accepted] : accepted.slice(0, 1);
|
|
84
|
+
onaccept?.(accepted);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function clear() {
|
|
88
|
+
files = [];
|
|
89
|
+
rejected = [];
|
|
90
|
+
if (input) input.value = '';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const reasons: Record<RejectedFile['reason'], string> = $derived({
|
|
94
|
+
type: t.current.rejectedType,
|
|
95
|
+
size: t.current.rejectedSize,
|
|
96
|
+
count: t.current.rejectedCount
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const sizes: Record<InputSize, string> = {
|
|
100
|
+
sm: 'h-8 text-[13px]',
|
|
101
|
+
md: 'h-10 text-sm',
|
|
102
|
+
lg: 'h-11 text-[15px]'
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const buttons: Record<InputSize, string> = {
|
|
106
|
+
sm: 'h-6 px-2 text-[11px]',
|
|
107
|
+
md: 'h-7 px-2.5 text-xs',
|
|
108
|
+
lg: 'h-8 px-3 text-xs'
|
|
109
|
+
};
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<div class="flex w-full flex-col gap-3 {className}">
|
|
113
|
+
<span
|
|
114
|
+
class="inline-flex w-full items-center gap-2 rounded-xl border bg-bg px-1 {edge} transition-colors duration-150 ease-brand-out
|
|
115
|
+
{sizes[size]}
|
|
116
|
+
{invalid
|
|
117
|
+
? 'border-danger focus-within:border-danger'
|
|
118
|
+
: `border-hairline-strong ${toneFocusWithinBorder[tone]}`}
|
|
119
|
+
{disabled ? 'pointer-events-none opacity-50' : ''}"
|
|
120
|
+
>
|
|
121
|
+
<button
|
|
122
|
+
type="button"
|
|
123
|
+
{disabled}
|
|
124
|
+
onclick={() => input?.click()}
|
|
125
|
+
class="shrink-0 rounded-lg border border-hairline-strong bg-bg-alt font-sans font-medium text-text-secondary transition-colors duration-150 hover:bg-bg-inset hover:text-text focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-current
|
|
126
|
+
{buttons[size]}"
|
|
127
|
+
>
|
|
128
|
+
{label ?? t.current.chooseFile}
|
|
129
|
+
</button>
|
|
130
|
+
|
|
131
|
+
<span
|
|
132
|
+
class="min-w-0 flex-1 truncate font-sans {files.length ? 'text-text' : 'text-text-muted'}"
|
|
133
|
+
>
|
|
134
|
+
{summary}
|
|
135
|
+
</span>
|
|
136
|
+
|
|
137
|
+
{#if files.length}
|
|
138
|
+
<button
|
|
139
|
+
type="button"
|
|
140
|
+
onclick={clear}
|
|
141
|
+
aria-label={t.current.remove}
|
|
142
|
+
class="mr-1 shrink-0 rounded p-1 text-text-muted transition-colors duration-150 hover:bg-bg-inset hover:text-danger focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-danger"
|
|
143
|
+
>
|
|
144
|
+
<svg class="size-3.5" viewBox="0 0 14 14" fill="none" aria-hidden="true">
|
|
145
|
+
<path
|
|
146
|
+
d="m2 2 10 10M12 2 2 12"
|
|
147
|
+
stroke="currentColor"
|
|
148
|
+
stroke-width="1.6"
|
|
149
|
+
stroke-linecap="round"
|
|
150
|
+
/>
|
|
151
|
+
</svg>
|
|
152
|
+
</button>
|
|
153
|
+
{/if}
|
|
154
|
+
</span>
|
|
155
|
+
|
|
156
|
+
<input
|
|
157
|
+
bind:this={input}
|
|
158
|
+
type="file"
|
|
159
|
+
{name}
|
|
160
|
+
{accept}
|
|
161
|
+
{multiple}
|
|
162
|
+
{disabled}
|
|
163
|
+
aria-invalid={invalid ? 'true' : undefined}
|
|
164
|
+
class="sr-only"
|
|
165
|
+
onchange={(event) => add(event.currentTarget.files)}
|
|
166
|
+
/>
|
|
167
|
+
|
|
168
|
+
{#if rejected.length}
|
|
169
|
+
<ul class="flex flex-col gap-1">
|
|
170
|
+
{#each rejected as item (item.file.name + item.reason)}
|
|
171
|
+
<li class="font-sans text-sm text-danger">
|
|
172
|
+
{item.file.name} — {reasons[item.reason]}
|
|
173
|
+
</li>
|
|
174
|
+
{/each}
|
|
175
|
+
</ul>
|
|
176
|
+
{/if}
|
|
177
|
+
|
|
178
|
+
{#if showList && multiple && files.length > 1}
|
|
179
|
+
<ul class="flex flex-col rounded-[20px] border border-hairline bg-bg {edge}">
|
|
180
|
+
{#each files as file, i (file.name + file.size + i)}
|
|
181
|
+
<li
|
|
182
|
+
class="flex items-center gap-3 border-b border-hairline px-4 py-2.5 font-sans last:border-b-0"
|
|
183
|
+
>
|
|
184
|
+
<span class="min-w-0 flex-1 truncate text-sm text-text">{file.name}</span>
|
|
185
|
+
<span class="shrink-0 text-xs text-text-muted tabular-nums">{formatSize(file.size)}</span>
|
|
186
|
+
<button
|
|
187
|
+
type="button"
|
|
188
|
+
onclick={() => (files = files.filter((_, at) => at !== i))}
|
|
189
|
+
aria-label="{t.current.remove} {file.name}"
|
|
190
|
+
class="-m-1 shrink-0 rounded p-1 text-text-muted transition-colors duration-150 hover:bg-bg-inset hover:text-danger focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-danger"
|
|
191
|
+
>
|
|
192
|
+
<svg class="size-3.5" viewBox="0 0 14 14" fill="none" aria-hidden="true">
|
|
193
|
+
<path
|
|
194
|
+
d="m2 2 10 10M12 2 2 12"
|
|
195
|
+
stroke="currentColor"
|
|
196
|
+
stroke-width="1.6"
|
|
197
|
+
stroke-linecap="round"
|
|
198
|
+
/>
|
|
199
|
+
</svg>
|
|
200
|
+
</button>
|
|
201
|
+
</li>
|
|
202
|
+
{/each}
|
|
203
|
+
</ul>
|
|
204
|
+
{/if}
|
|
205
|
+
</div>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type RejectedFile } from '../../core/files.js';
|
|
2
|
+
import { type Tone } from '../../core/tones.js';
|
|
3
|
+
import type { InputSize } from './Input.svelte';
|
|
4
|
+
interface Props {
|
|
5
|
+
/** Bindable list of accepted files. */
|
|
6
|
+
files?: File[];
|
|
7
|
+
/** Same syntax as the input's `accept` — `'image/*,.pdf'`. */
|
|
8
|
+
accept?: string;
|
|
9
|
+
multiple?: boolean;
|
|
10
|
+
/** Per-file ceiling in bytes. `0` means no limit. */
|
|
11
|
+
maxSize?: number;
|
|
12
|
+
/** Cap on how many files may be held at once. `0` means no limit. */
|
|
13
|
+
maxFiles?: number;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
size?: InputSize;
|
|
16
|
+
tone?: Tone;
|
|
17
|
+
invalid?: boolean;
|
|
18
|
+
/** Text on the button. Defaults to the locale's `chooseFile`. */
|
|
19
|
+
label?: string;
|
|
20
|
+
/** Shown while nothing is chosen. Defaults to the locale's `noFile`. */
|
|
21
|
+
placeholder?: string;
|
|
22
|
+
/** Show the list of held files below the row. */
|
|
23
|
+
showList?: boolean;
|
|
24
|
+
/** The `name` the hidden input posts under, for a plain form submit. */
|
|
25
|
+
name?: string;
|
|
26
|
+
onaccept?: (files: File[]) => void;
|
|
27
|
+
onreject?: (rejected: RejectedFile[]) => void;
|
|
28
|
+
class?: string;
|
|
29
|
+
}
|
|
30
|
+
declare const FileInput: import("svelte").Component<Props, {}, "files">;
|
|
31
|
+
type FileInput = ReturnType<typeof FileInput>;
|
|
32
|
+
export default FileInput;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
import { edge } from './lift.js';
|
|
5
|
+
import type { InputSize } from './Input.svelte';
|
|
6
|
+
|
|
7
|
+
interface Props extends HTMLAttributes<HTMLSpanElement> {
|
|
8
|
+
/** Match the Input it sits beside — the heights have to agree. */
|
|
9
|
+
size?: InputSize;
|
|
10
|
+
/** Paint it like a field instead of a fixed label. */
|
|
11
|
+
variant?: 'muted' | 'plain';
|
|
12
|
+
children: Snippet;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let {
|
|
16
|
+
size = 'md',
|
|
17
|
+
variant = 'muted',
|
|
18
|
+
class: className = '',
|
|
19
|
+
children,
|
|
20
|
+
...rest
|
|
21
|
+
}: Props = $props();
|
|
22
|
+
|
|
23
|
+
const sizes: Record<InputSize, string> = {
|
|
24
|
+
sm: 'h-8 px-2.5 text-[13px]',
|
|
25
|
+
md: 'h-10 px-3 text-sm',
|
|
26
|
+
lg: 'h-11 px-3 text-[15px]'
|
|
27
|
+
};
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<span
|
|
31
|
+
class="inline-flex shrink-0 items-center gap-1.5 rounded-xl border border-hairline-strong font-sans whitespace-nowrap text-text-secondary {edge}
|
|
32
|
+
{sizes[size]}
|
|
33
|
+
{variant === 'muted' ? 'bg-bg-alt' : 'bg-bg'} {className}"
|
|
34
|
+
{...rest}
|
|
35
|
+
>
|
|
36
|
+
{@render children()}
|
|
37
|
+
</span>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import type { InputSize } from './Input.svelte';
|
|
4
|
+
interface Props extends HTMLAttributes<HTMLSpanElement> {
|
|
5
|
+
/** Match the Input it sits beside — the heights have to agree. */
|
|
6
|
+
size?: InputSize;
|
|
7
|
+
/** Paint it like a field instead of a fixed label. */
|
|
8
|
+
variant?: 'muted' | 'plain';
|
|
9
|
+
children: Snippet;
|
|
10
|
+
}
|
|
11
|
+
declare const InputAddon: import("svelte").Component<Props, {}, "">;
|
|
12
|
+
type InputAddon = ReturnType<typeof InputAddon>;
|
|
13
|
+
export default InputAddon;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useLocale } from '../../core/locale.svelte.js';
|
|
3
|
+
import { caretIndex, clearFrom, fillFrom, sanitisePin, type PinMode } from '../../core/pin.js';
|
|
4
|
+
import { toneFocusBorder, type Tone } from '../../core/tones.js';
|
|
5
|
+
import { edge } from './lift.js';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
/** Bindable code. Shorter than `length` means still incomplete. */
|
|
9
|
+
value?: string;
|
|
10
|
+
/** How many boxes. */
|
|
11
|
+
length?: number;
|
|
12
|
+
/** What a box accepts. */
|
|
13
|
+
mode?: PinMode;
|
|
14
|
+
/** Render the characters as dots. */
|
|
15
|
+
mask?: boolean;
|
|
16
|
+
/** Draw a gap after this many boxes — `3` gives `123 456`. */
|
|
17
|
+
groupAfter?: number;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
invalid?: boolean;
|
|
20
|
+
tone?: Tone;
|
|
21
|
+
/** `name` for a plain form submit — one hidden field holding the code. */
|
|
22
|
+
name?: string;
|
|
23
|
+
/** Fires once the last box is filled. */
|
|
24
|
+
oncomplete?: (value: string) => void;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let {
|
|
29
|
+
value = $bindable(''),
|
|
30
|
+
length = 6,
|
|
31
|
+
mode = 'numeric',
|
|
32
|
+
mask = false,
|
|
33
|
+
groupAfter = 0,
|
|
34
|
+
disabled = false,
|
|
35
|
+
invalid = false,
|
|
36
|
+
tone = 'brand',
|
|
37
|
+
name,
|
|
38
|
+
oncomplete,
|
|
39
|
+
class: className = ''
|
|
40
|
+
}: Props = $props();
|
|
41
|
+
|
|
42
|
+
const t = useLocale();
|
|
43
|
+
|
|
44
|
+
let boxes: HTMLInputElement[] = $state([]);
|
|
45
|
+
|
|
46
|
+
const chars = $derived(Array.from({ length }, (_, i) => value[i] ?? ''));
|
|
47
|
+
|
|
48
|
+
function focusAt(index: number) {
|
|
49
|
+
boxes[Math.max(0, Math.min(length - 1, index))]?.focus();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The DOM is written back on every keystroke, not only when the value
|
|
54
|
+
* changed: a refused character leaves the box holding text the state never
|
|
55
|
+
* accepted, and nothing else would clear it.
|
|
56
|
+
*/
|
|
57
|
+
function sync(box: HTMLInputElement, index: number) {
|
|
58
|
+
box.value = value[index] ?? '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function commit(next: string, from: number) {
|
|
62
|
+
const was = value;
|
|
63
|
+
value = next;
|
|
64
|
+
if (value.length === length && was.length < length) oncomplete?.(value);
|
|
65
|
+
// Land on the first empty box, or the one after whatever was just typed.
|
|
66
|
+
focusAt(Math.min(Math.max(from, caretIndex(value, length)), length - 1));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function onInput(event: Event & { currentTarget: HTMLInputElement }, index: number) {
|
|
70
|
+
const typed = sanitisePin(event.currentTarget.value, mode);
|
|
71
|
+
if (!typed) {
|
|
72
|
+
sync(event.currentTarget, index);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const next = fillFrom(value, index, typed, length, mode);
|
|
76
|
+
commit(next, index + typed.length);
|
|
77
|
+
for (const [at, box] of boxes.entries()) sync(box, at);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function onKeydown(event: KeyboardEvent, index: number) {
|
|
81
|
+
if (event.key === 'Backspace') {
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
// An empty box deletes the one before it, the way a caret would.
|
|
84
|
+
const target = value[index] ? index : index - 1;
|
|
85
|
+
if (target < 0) return;
|
|
86
|
+
value = clearFrom(value, target);
|
|
87
|
+
for (const [at, box] of boxes.entries()) sync(box, at);
|
|
88
|
+
focusAt(target);
|
|
89
|
+
} else if (event.key === 'ArrowLeft') {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
focusAt(index - 1);
|
|
92
|
+
} else if (event.key === 'ArrowRight') {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
focusAt(index + 1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function onPaste(event: ClipboardEvent, index: number) {
|
|
99
|
+
event.preventDefault();
|
|
100
|
+
const text = event.clipboardData?.getData('text') ?? '';
|
|
101
|
+
const clean = sanitisePin(text, mode);
|
|
102
|
+
if (!clean) return;
|
|
103
|
+
commit(fillFrom(value, index, clean, length, mode), index + clean.length);
|
|
104
|
+
for (const [at, box] of boxes.entries()) sync(box, at);
|
|
105
|
+
}
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<div class="flex items-center gap-2 {className}">
|
|
109
|
+
{#each chars as char, index (index)}
|
|
110
|
+
{#if groupAfter && index > 0 && index % groupAfter === 0}
|
|
111
|
+
<span class="h-px w-2 bg-hairline-strong" aria-hidden="true"></span>
|
|
112
|
+
{/if}
|
|
113
|
+
<input
|
|
114
|
+
bind:this={boxes[index]}
|
|
115
|
+
value={char}
|
|
116
|
+
{disabled}
|
|
117
|
+
type={mask ? 'password' : 'text'}
|
|
118
|
+
inputmode={mode === 'numeric' ? 'numeric' : 'text'}
|
|
119
|
+
autocomplete={index === 0 ? 'one-time-code' : 'off'}
|
|
120
|
+
autocapitalize="characters"
|
|
121
|
+
spellcheck="false"
|
|
122
|
+
maxlength={length}
|
|
123
|
+
aria-label="{t.current.digit} {index + 1}"
|
|
124
|
+
aria-invalid={invalid ? 'true' : undefined}
|
|
125
|
+
oninput={(event) => onInput(event, index)}
|
|
126
|
+
onkeydown={(event) => onKeydown(event, index)}
|
|
127
|
+
onpaste={(event) => onPaste(event, index)}
|
|
128
|
+
onfocus={(event) => event.currentTarget.select()}
|
|
129
|
+
class="size-11 rounded-xl border bg-bg text-center font-sans text-base font-medium text-text tabular-nums {edge} transition-colors duration-150 ease-brand-out focus:outline-none
|
|
130
|
+
{invalid ? 'border-danger' : `border-hairline-strong ${toneFocusBorder[tone]}`}
|
|
131
|
+
{disabled ? 'pointer-events-none opacity-50' : ''}"
|
|
132
|
+
/>
|
|
133
|
+
{/each}
|
|
134
|
+
|
|
135
|
+
{#if name}
|
|
136
|
+
<input type="hidden" {name} {value} />
|
|
137
|
+
{/if}
|
|
138
|
+
</div>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PinMode } from '../../core/pin.js';
|
|
2
|
+
import { type Tone } from '../../core/tones.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
/** Bindable code. Shorter than `length` means still incomplete. */
|
|
5
|
+
value?: string;
|
|
6
|
+
/** How many boxes. */
|
|
7
|
+
length?: number;
|
|
8
|
+
/** What a box accepts. */
|
|
9
|
+
mode?: PinMode;
|
|
10
|
+
/** Render the characters as dots. */
|
|
11
|
+
mask?: boolean;
|
|
12
|
+
/** Draw a gap after this many boxes — `3` gives `123 456`. */
|
|
13
|
+
groupAfter?: number;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
invalid?: boolean;
|
|
16
|
+
tone?: Tone;
|
|
17
|
+
/** `name` for a plain form submit — one hidden field holding the code. */
|
|
18
|
+
name?: string;
|
|
19
|
+
/** Fires once the last box is filled. */
|
|
20
|
+
oncomplete?: (value: string) => void;
|
|
21
|
+
class?: string;
|
|
22
|
+
}
|
|
23
|
+
declare const PinInput: import("svelte").Component<Props, {}, "value">;
|
|
24
|
+
type PinInput = ReturnType<typeof PinInput>;
|
|
25
|
+
export default PinInput;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
4
|
+
import { useLocale } from '../../core/locale.svelte.js';
|
|
5
|
+
import type { Tone } from '../../core/tones.js';
|
|
6
|
+
import Input, { type InputSize } from './Input.svelte';
|
|
7
|
+
|
|
8
|
+
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix' | 'type'> {
|
|
9
|
+
value?: string;
|
|
10
|
+
size?: InputSize;
|
|
11
|
+
tone?: Tone;
|
|
12
|
+
invalid?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Milliseconds to wait after the last keystroke before `onsearch` fires.
|
|
15
|
+
* `0` fires on every one.
|
|
16
|
+
*/
|
|
17
|
+
debounce?: number;
|
|
18
|
+
/** The debounced value. The bound `value` still updates immediately. */
|
|
19
|
+
onsearch?: (value: string) => void;
|
|
20
|
+
onclear?: () => void;
|
|
21
|
+
/** Replaces the magnifier. */
|
|
22
|
+
icon?: Snippet;
|
|
23
|
+
class?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let {
|
|
27
|
+
value = $bindable(''),
|
|
28
|
+
size = 'md',
|
|
29
|
+
tone = 'brand',
|
|
30
|
+
invalid = false,
|
|
31
|
+
disabled = false,
|
|
32
|
+
debounce = 0,
|
|
33
|
+
onsearch,
|
|
34
|
+
onclear,
|
|
35
|
+
icon,
|
|
36
|
+
class: className = '',
|
|
37
|
+
...rest
|
|
38
|
+
}: Props = $props();
|
|
39
|
+
|
|
40
|
+
const t = useLocale();
|
|
41
|
+
|
|
42
|
+
// The timer is cleared by the effect's own teardown, so a keystroke that
|
|
43
|
+
// lands mid-wait restarts the clock rather than queueing a second call.
|
|
44
|
+
$effect(() => {
|
|
45
|
+
const next = value;
|
|
46
|
+
if (!onsearch) return;
|
|
47
|
+
if (debounce <= 0) {
|
|
48
|
+
onsearch(next);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const timer = setTimeout(() => onsearch(next), debounce);
|
|
52
|
+
return () => clearTimeout(timer);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function clear() {
|
|
56
|
+
value = '';
|
|
57
|
+
onclear?.();
|
|
58
|
+
}
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<Input bind:value type="search" {size} {tone} {invalid} {disabled} class={className} {...rest}>
|
|
62
|
+
{#snippet prefix()}
|
|
63
|
+
{#if icon}
|
|
64
|
+
{@render icon()}
|
|
65
|
+
{:else}
|
|
66
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" aria-hidden="true">
|
|
67
|
+
<circle cx="9" cy="9" r="5.5" stroke="currentColor" stroke-width="1.6" />
|
|
68
|
+
<path d="m13 13 3.5 3.5" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" />
|
|
69
|
+
</svg>
|
|
70
|
+
{/if}
|
|
71
|
+
{/snippet}
|
|
72
|
+
|
|
73
|
+
{#snippet suffix()}
|
|
74
|
+
{#if value}
|
|
75
|
+
<button
|
|
76
|
+
type="button"
|
|
77
|
+
onclick={clear}
|
|
78
|
+
aria-label={t.current.clearSearch}
|
|
79
|
+
class="-mr-1 rounded p-1 text-text-muted transition-colors duration-150 hover:bg-bg-inset hover:text-text focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-current"
|
|
80
|
+
>
|
|
81
|
+
<svg class="size-3.5" viewBox="0 0 14 14" fill="none" aria-hidden="true">
|
|
82
|
+
<path
|
|
83
|
+
d="m2 2 10 10M12 2 2 12"
|
|
84
|
+
stroke="currentColor"
|
|
85
|
+
stroke-width="1.6"
|
|
86
|
+
stroke-linecap="round"
|
|
87
|
+
/>
|
|
88
|
+
</svg>
|
|
89
|
+
</button>
|
|
90
|
+
{/if}
|
|
91
|
+
{/snippet}
|
|
92
|
+
</Input>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
3
|
+
import type { Tone } from '../../core/tones.js';
|
|
4
|
+
import { type InputSize } from './Input.svelte';
|
|
5
|
+
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix' | 'type'> {
|
|
6
|
+
value?: string;
|
|
7
|
+
size?: InputSize;
|
|
8
|
+
tone?: Tone;
|
|
9
|
+
invalid?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Milliseconds to wait after the last keystroke before `onsearch` fires.
|
|
12
|
+
* `0` fires on every one.
|
|
13
|
+
*/
|
|
14
|
+
debounce?: number;
|
|
15
|
+
/** The debounced value. The bound `value` still updates immediately. */
|
|
16
|
+
onsearch?: (value: string) => void;
|
|
17
|
+
onclear?: () => void;
|
|
18
|
+
/** Replaces the magnifier. */
|
|
19
|
+
icon?: Snippet;
|
|
20
|
+
class?: string;
|
|
21
|
+
}
|
|
22
|
+
declare const SearchInput: import("svelte").Component<Props, {}, "value">;
|
|
23
|
+
type SearchInput = ReturnType<typeof SearchInput>;
|
|
24
|
+
export default SearchInput;
|