reburn-ui 0.4.3 → 0.4.4

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.
@@ -3,7 +3,7 @@
3
3
  import type { MantineRadius, MantineSize } from '../../MantineProvider/theme.types.js';
4
4
  import { getRadius, getSize } from '../../utils/get-size.js';
5
5
  import type { CloseButtonProps } from '../CloseButton/CloseButton.svelte';
6
- import { CloseButton } from '../index.js';
6
+ import CloseButton from '../CloseButton/CloseButton.svelte';
7
7
  import { getPillGroupContext } from './PillGroupContext.svelte.js';
8
8
 
9
9
  export type PillVariant = 'default' | 'contrast';
@@ -0,0 +1,274 @@
1
+ <script lang="ts" module>
2
+ import type { DivElement } from '../../element-types.js';
3
+ import type { MantineRadius, MantineSize, MantineSpacing } from '../../MantineProvider/theme.types.js';
4
+ import type { HTMLInputAttributes } from 'svelte/elements';
5
+
6
+ export interface PinInputProps extends DivElement {
7
+ /** Hidden input `name` attribute */
8
+ name?: string;
9
+
10
+ /** Hidden input `form` attribute */
11
+ form?: string;
12
+
13
+ /** Key of `theme.spacing` or any valid CSS value to set `gap` between inputs, numbers are converted to rem, `'md'` by default */
14
+ gap?: MantineSpacing;
15
+
16
+ /** Key of `theme.radius` or any valid CSS value to set `border-radius`, numbers are converted to rem, `theme.defaultRadius` by default */
17
+ radius?: MantineRadius;
18
+
19
+ /** Controls inputs `width` and `height`, `'sm'` by default */
20
+ size?: MantineSize;
21
+
22
+ /** If set, the first input is focused when component is mounted, `false` by default */
23
+ autoFocus?: boolean;
24
+
25
+ /** Controlled component value */
26
+ value?: string;
27
+
28
+ /** Uncontrolled component default value */
29
+ defaultValue?: string;
30
+
31
+ /** Called when value changes */
32
+ onChange?: (value: string) => void;
33
+
34
+ /** Called when all inputs have value */
35
+ onComplete?: (value: string) => void;
36
+
37
+ /** Inputs placeholder, `'○'` by default */
38
+ placeholder?: string;
39
+
40
+ /** Determines whether focus should be moved automatically to the next input once filled, `true` by default */
41
+ manageFocus?: boolean;
42
+
43
+ /** Determines whether `autocomplete="one-time-code"` attribute should be set on all inputs, `true` by default */
44
+ oneTimeCode?: boolean;
45
+
46
+ /** Base id used for all inputs. By default, inputs' ids are generated randomly. */
47
+ id?: string;
48
+
49
+ /** If set, `disabled` attribute is added to all inputs */
50
+ disabled?: boolean;
51
+
52
+ /** If set, adds error styles and `aria-invalid` attribute to all inputs */
53
+ error?: boolean;
54
+
55
+ /** Determines which values can be entered, `'alphanumeric'` by default */
56
+ type?: 'alphanumeric' | 'number' | RegExp;
57
+
58
+ /** Changes input type to `"password"`, `false` by default */
59
+ mask?: boolean;
60
+
61
+ /** Number of inputs, `4` by default */
62
+ length?: number;
63
+
64
+ /** If set, the user cannot edit the value */
65
+ readOnly?: boolean;
66
+
67
+ /** Inputs `type` attribute, inferred from the `type` prop if not specified */
68
+ inputType?: HTMLInputAttributes['type'];
69
+
70
+ /** `inputmode` attribute, inferred from the `type` prop if not specified */
71
+ inputMode?: 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search' | undefined;
72
+
73
+ /** `aria-label` for the inputs */
74
+ ariaLabel?: string;
75
+
76
+ /** Props passed down to the hidden input */
77
+ hiddenInputProps?: React.ComponentPropsWithoutRef<'input'>;
78
+
79
+ /** Assigns ref of the root element */
80
+ rootRef?: React.ForwardedRef<HTMLDivElement>;
81
+
82
+ /** Props added to the input element depending on its index */
83
+ // getInputProps?: (index: number) => InputProps & ElementProps<'input', 'size'>;
84
+ }
85
+ </script>
86
+
87
+ <script lang="ts">
88
+ import { getSize } from '../../utils/get-size.js';
89
+ import Group from '../Group/Group.svelte';
90
+ import Input from '../Input/Input.svelte';
91
+ import { createPinArray } from './create-pin-array.js';
92
+
93
+ let {
94
+ gap = 'sm',
95
+ length = 4,
96
+ manageFocus = true,
97
+ oneTimeCode = true,
98
+ placeholder = '○',
99
+ type = 'alphanumeric',
100
+ ariaLabel = 'PinInput',
101
+ value = $bindable(''),
102
+ inputMode,
103
+ size = 'md',
104
+ radius = 'sm',
105
+ error,
106
+ autoFocus,
107
+ name,
108
+ onChange,
109
+ }: PinInputProps = $props();
110
+
111
+ let _value = $state(createPinArray(length, value));
112
+
113
+ const regex = {
114
+ number: /^[0-9]+$/,
115
+ alphanumeric: /^[a-zA-Z0-9]+$/i,
116
+ };
117
+
118
+ let inputsRef = $state<HTMLInputElement[]>([]);
119
+ let focusedIndex = $state(-1);
120
+
121
+ const valueToString = $derived(_value.join(''));
122
+
123
+ function setValues(values: string[]) {
124
+ _value = values;
125
+ value = valueToString;
126
+ onChange?.(valueToString);
127
+ }
128
+
129
+ function focusInputField(dir: 'next' | 'prev', index: number, event?: KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement }) {
130
+ if (!manageFocus) {
131
+ event?.preventDefault();
132
+ return;
133
+ }
134
+
135
+ if (dir === 'next') {
136
+ const nextIndex = index + 1;
137
+ const canFocusNext = nextIndex < (length ?? 0);
138
+ if (canFocusNext) {
139
+ event?.preventDefault();
140
+ inputsRef[nextIndex]?.focus();
141
+ }
142
+ }
143
+
144
+ if (dir === 'prev') {
145
+ const nextIndex = index - 1;
146
+ const canFocusNext = nextIndex > -1;
147
+ if (canFocusNext) {
148
+ event?.preventDefault();
149
+ inputsRef[nextIndex]?.focus();
150
+ }
151
+ }
152
+ }
153
+
154
+ function validate(code: string) {
155
+ const re = type instanceof RegExp ? type : type && type in regex ? regex[type] : null;
156
+
157
+ return re?.test(code);
158
+ }
159
+
160
+ function setFieldValue(val: string, index: number) {
161
+ const values = [..._value];
162
+ values[index] = val;
163
+ setValues(values);
164
+ }
165
+
166
+ function handleChange(newVal: string, index: number) {
167
+ const nextCharOrValue = newVal.length === 2 ? newVal.split('')[newVal.length - 1] : newVal;
168
+
169
+ const isValid = validate(nextCharOrValue);
170
+
171
+ if (nextCharOrValue.length < 2) {
172
+ if (isValid) {
173
+ setFieldValue(nextCharOrValue, index);
174
+ focusInputField('next', index);
175
+ } else {
176
+ setFieldValue('', index);
177
+ }
178
+ } else if (isValid) {
179
+ setValues(createPinArray(length ?? 0, newVal));
180
+ }
181
+ }
182
+
183
+ function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement }, index: number) {
184
+ const { ctrlKey, metaKey, key, shiftKey, target } = event;
185
+ const inputValue = (target as HTMLInputElement).value;
186
+
187
+ if (inputMode === 'numeric') {
188
+ const canTypeSign =
189
+ key === 'Backspace' || key === 'Tab' || key === 'Control' || key === 'Delete' || (ctrlKey && key === 'v') || (metaKey && key === 'v')
190
+ ? true
191
+ : !Number.isNaN(Number(key));
192
+
193
+ if (!canTypeSign) {
194
+ event.preventDefault();
195
+ }
196
+ }
197
+
198
+ if (key === 'ArrowLeft' || (shiftKey && key === 'Tab')) {
199
+ focusInputField('prev', index, event);
200
+ } else if (key === 'ArrowRight' || key === 'Tab' || key === ' ') {
201
+ focusInputField('next', index, event);
202
+ } else if (key === 'Delete') {
203
+ setFieldValue('', index);
204
+ } else if (key === 'Backspace') {
205
+ if (index !== 0) {
206
+ setFieldValue('', index);
207
+ if (length === index + 1) {
208
+ if ((event.target as HTMLInputElement).value === '') {
209
+ focusInputField('prev', index, event);
210
+ }
211
+ } else {
212
+ focusInputField('prev', index, event);
213
+ }
214
+ }
215
+ } else if (inputValue.length > 0 && key === _value[index]) {
216
+ focusInputField('next', index, event);
217
+ }
218
+ }
219
+
220
+ function handleFocus(event: FocusEvent & { currentTarget: EventTarget & HTMLInputElement }, index: number) {
221
+ (event.target as HTMLInputElement).select();
222
+ focusedIndex = index;
223
+ }
224
+
225
+ function handleBlur() {
226
+ focusedIndex = -1;
227
+ }
228
+
229
+ function handlePaste(event: ClipboardEvent & { currentTarget: EventTarget & HTMLInputElement }) {
230
+ event.preventDefault();
231
+ const copyValue = event.clipboardData?.getData('text/plain')?.replace(/[\n\r\s]+/g, '') ?? '';
232
+ const isValid = validate(copyValue.trim());
233
+
234
+ if (isValid) {
235
+ const copyValueToPinArray = createPinArray(length ?? 0, copyValue);
236
+ setValues(copyValueToPinArray);
237
+ focusInputField('next', copyValueToPinArray.length - 2);
238
+ }
239
+ }
240
+ </script>
241
+
242
+ <Group wrap="nowrap" class="root-39a5Oo67" {gap}>
243
+ {#each { length: length }, index}
244
+ <Input
245
+ style={`--input-padding: 0; --input-text-align: center; --pin-input-size: ${getSize(size, 'pin-input-size')}; width: var(--pin-input-size); height: var(--pin-input-size);`}
246
+ {size}
247
+ inputMode={inputMode || (type === 'number' ? 'numeric' : 'text')}
248
+ onfocus={(event) => handleFocus(event, index)}
249
+ onblur={handleBlur}
250
+ onkeydown={(event) => handleKeyDown(event, index)}
251
+ onchange={(event) => handleChange(event, index)}
252
+ onpaste={handlePaste}
253
+ bind:value={() => _value[index], (newVal) => handleChange(newVal, index)}
254
+ {radius}
255
+ {error}
256
+ bind:ref={inputsRef[index]}
257
+ autofocus={autoFocus && index === 0}
258
+ aria-label={ariaLabel}
259
+ placeholder={focusedIndex === index ? '' : placeholder}
260
+ autocomplete={oneTimeCode ? 'one-time-code' : 'off'}
261
+ />
262
+ {/each}
263
+
264
+ <input type="hidden" {name} value={valueToString} />
265
+ </Group>
266
+
267
+ <style>:global(.root-39a5Oo67) {
268
+ --pin-input-size-xs: calc(1.875rem * var(--mantine-scale));
269
+ --pin-input-size-sm: calc(2.25rem * var(--mantine-scale));
270
+ --pin-input-size-md: calc(2.625rem * var(--mantine-scale));
271
+ --pin-input-size-lg: calc(3.125rem * var(--mantine-scale));
272
+ --pin-input-size-xl: calc(3.75rem * var(--mantine-scale));
273
+ --pin-input-size: var(--pin-input-size-sm);
274
+ }</style>
@@ -0,0 +1,58 @@
1
+ import type { DivElement } from '../../element-types.js';
2
+ import type { MantineRadius, MantineSize, MantineSpacing } from '../../MantineProvider/theme.types.js';
3
+ import type { HTMLInputAttributes } from 'svelte/elements';
4
+ export interface PinInputProps extends DivElement {
5
+ /** Hidden input `name` attribute */
6
+ name?: string;
7
+ /** Hidden input `form` attribute */
8
+ form?: string;
9
+ /** Key of `theme.spacing` or any valid CSS value to set `gap` between inputs, numbers are converted to rem, `'md'` by default */
10
+ gap?: MantineSpacing;
11
+ /** Key of `theme.radius` or any valid CSS value to set `border-radius`, numbers are converted to rem, `theme.defaultRadius` by default */
12
+ radius?: MantineRadius;
13
+ /** Controls inputs `width` and `height`, `'sm'` by default */
14
+ size?: MantineSize;
15
+ /** If set, the first input is focused when component is mounted, `false` by default */
16
+ autoFocus?: boolean;
17
+ /** Controlled component value */
18
+ value?: string;
19
+ /** Uncontrolled component default value */
20
+ defaultValue?: string;
21
+ /** Called when value changes */
22
+ onChange?: (value: string) => void;
23
+ /** Called when all inputs have value */
24
+ onComplete?: (value: string) => void;
25
+ /** Inputs placeholder, `'○'` by default */
26
+ placeholder?: string;
27
+ /** Determines whether focus should be moved automatically to the next input once filled, `true` by default */
28
+ manageFocus?: boolean;
29
+ /** Determines whether `autocomplete="one-time-code"` attribute should be set on all inputs, `true` by default */
30
+ oneTimeCode?: boolean;
31
+ /** Base id used for all inputs. By default, inputs' ids are generated randomly. */
32
+ id?: string;
33
+ /** If set, `disabled` attribute is added to all inputs */
34
+ disabled?: boolean;
35
+ /** If set, adds error styles and `aria-invalid` attribute to all inputs */
36
+ error?: boolean;
37
+ /** Determines which values can be entered, `'alphanumeric'` by default */
38
+ type?: 'alphanumeric' | 'number' | RegExp;
39
+ /** Changes input type to `"password"`, `false` by default */
40
+ mask?: boolean;
41
+ /** Number of inputs, `4` by default */
42
+ length?: number;
43
+ /** If set, the user cannot edit the value */
44
+ readOnly?: boolean;
45
+ /** Inputs `type` attribute, inferred from the `type` prop if not specified */
46
+ inputType?: HTMLInputAttributes['type'];
47
+ /** `inputmode` attribute, inferred from the `type` prop if not specified */
48
+ inputMode?: 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search' | undefined;
49
+ /** `aria-label` for the inputs */
50
+ ariaLabel?: string;
51
+ /** Props passed down to the hidden input */
52
+ hiddenInputProps?: React.ComponentPropsWithoutRef<'input'>;
53
+ /** Assigns ref of the root element */
54
+ rootRef?: React.ForwardedRef<HTMLDivElement>;
55
+ }
56
+ declare const PinInput: import("svelte").Component<PinInputProps, {}, "value">;
57
+ type PinInput = ReturnType<typeof PinInput>;
58
+ export default PinInput;
@@ -0,0 +1 @@
1
+ export declare function createPinArray(length: number, value: string): string[];
@@ -0,0 +1,13 @@
1
+ export function createPinArray(length, value) {
2
+ if (length < 1) {
3
+ return [];
4
+ }
5
+ const values = new Array(length).fill('');
6
+ if (value) {
7
+ const splitted = value.trim().split('');
8
+ for (let i = 0; i < Math.min(length, splitted.length); i += 1) {
9
+ values[i] = splitted[i] === ' ' ? '' : splitted[i];
10
+ }
11
+ }
12
+ return values;
13
+ }
@@ -0,0 +1,10 @@
1
+ export { TdB as icon_plugin };
2
+ declare function TdB(): {
3
+ name: string;
4
+ markup: ({ content: Q, filename: B }: {
5
+ content: any;
6
+ filename: any;
7
+ }) => Promise<{
8
+ code: any;
9
+ }>;
10
+ };