reburn-ui 0.4.3 → 0.4.5
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/adapter/adapter-bun/adapter.js +2 -2
- package/dist/adapter/adapter-bun/index.d.ts +6 -0
- package/dist/core/CodeHighlight/CodeHighlight.context.svelte.d.ts +13 -0
- package/dist/core/CodeHighlight/CodeHighlight.context.svelte.js +13 -0
- package/dist/core/CodeHighlight/CodeHighlight.svelte +196 -0
- package/dist/core/CodeHighlight/CodeHighlight.svelte.d.ts +48 -0
- package/dist/core/CodeHighlight/CodeHighlightAdapterProvider.context.svelte.d.ts +17 -0
- package/dist/core/CodeHighlight/CodeHighlightAdapterProvider.context.svelte.js +20 -0
- package/dist/core/CodeHighlight/CodeHighlightControl.svelte +37 -0
- package/dist/core/CodeHighlight/CodeHighlightControl.svelte.d.ts +11 -0
- package/dist/core/CodeHighlight/CopyCodeButton/CopyCodeButton.svelte +23 -0
- package/dist/core/CodeHighlight/CopyCodeButton/CopyCodeButton.svelte.d.ts +8 -0
- package/dist/core/CodeHighlight/CopyCodeButton/CopyIcon.svelte +31 -0
- package/dist/core/CodeHighlight/CopyCodeButton/CopyIcon.svelte.d.ts +8 -0
- package/dist/core/CodeHighlight/ExpandCodeButton/ExpandCodeButton.svelte +23 -0
- package/dist/core/CodeHighlight/ExpandCodeButton/ExpandCodeButton.svelte.d.ts +9 -0
- package/dist/core/CodeHighlight/ExpandCodeButton/ExpandIcon.svelte +32 -0
- package/dist/core/CodeHighlight/ExpandCodeButton/ExpandIcon.svelte.d.ts +8 -0
- package/dist/core/CodeHighlight/code-highlight.css +93 -0
- package/dist/core/CodeHighlight/index.d.ts +2 -0
- package/dist/core/CodeHighlight/index.js +2 -0
- package/dist/core/Notifications/NotificationContainer.svelte +2 -2
- package/dist/core/Notifications/Notifications.svelte +2 -2
- package/dist/core/charts/ChartRoot/ChartRoot.svelte +3 -3
- package/dist/core/charts/ChartRoot/ChartRoot.svelte.d.ts +5 -5
- package/dist/core/charts/ChartRoot/ChartXGrid.svelte +2 -2
- package/dist/core/charts/ChartRoot/ChartXLabels.svelte +3 -3
- package/dist/core/charts/ChartRoot/ChartYGrid.svelte +2 -2
- package/dist/core/charts/ChartRoot/ChartYLabels.svelte +3 -3
- package/dist/core/charts/ChartTooltip/ChartTooltip.svelte +45 -47
- package/dist/core/charts/LineChart/LineChart.svelte +1 -1
- package/dist/core/charts/LineChart/LineChartInner.svelte +1 -1
- package/dist/core/charts/ScatterChart/ScatterChartInner.svelte +1 -1
- package/dist/core/components/ColorPicker/ColorSlider.svelte +0 -1
- package/dist/core/components/Floating/CreateFloating.svelte.d.ts +0 -1
- package/dist/core/components/Floating/CreateFloating.svelte.js +4 -4
- package/dist/core/components/Pill/Pill.svelte +4 -2
- package/dist/core/components/PinInput/PinInput.svelte +274 -0
- package/dist/core/components/PinInput/PinInput.svelte.d.ts +58 -0
- package/dist/core/components/PinInput/create-pin-array.d.ts +1 -0
- package/dist/core/components/PinInput/create-pin-array.js +13 -0
- package/dist/core/components/Tooltip/Tooltip.svelte +3 -5
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/docs/UsageDemo.svelte +113 -0
- package/dist/docs/UsageDemo.svelte.d.ts +41 -0
- package/dist/styles/_mixins.scss +1 -1
- package/dist/styles/_variables.scss +145 -5
- package/package.json +34 -25
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
autoClose = 4000,
|
|
51
51
|
transitionDuration = 250,
|
|
52
52
|
containerWidth = 440,
|
|
53
|
-
notificationMaxHeight = 200,
|
|
54
|
-
limit = 5,
|
|
53
|
+
// notificationMaxHeight = 200,
|
|
54
|
+
// limit = 5,
|
|
55
55
|
zIndex = getDefaultZIndex('overlay'),
|
|
56
56
|
store = notifications,
|
|
57
57
|
withinPortal = true,
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
}
|
|
111
111
|
</script>
|
|
112
112
|
|
|
113
|
-
<script lang="ts" generics="T extends Record<string,
|
|
113
|
+
<script lang="ts" generics="T extends Record<string, unknown>">
|
|
114
114
|
import { setContext, type Snippet } from 'svelte';
|
|
115
115
|
|
|
116
116
|
let {
|
|
@@ -142,8 +142,8 @@
|
|
|
142
142
|
dataKey: () => dataKey,
|
|
143
143
|
yAxisProps: () => yAxisProps,
|
|
144
144
|
xAxisProps: () => xAxisProps,
|
|
145
|
-
xScale: () =>
|
|
146
|
-
yScale: () =>
|
|
145
|
+
xScale: () => xScale,
|
|
146
|
+
yScale: () => yScale,
|
|
147
147
|
xGrid: () => xGrid,
|
|
148
148
|
yGrid: () => yGrid,
|
|
149
149
|
});
|
|
@@ -63,14 +63,14 @@ export interface ChartRootProps<T extends Record<string, unknown>> extends Omit<
|
|
|
63
63
|
svgProps?: SvgElement;
|
|
64
64
|
}
|
|
65
65
|
import { type Snippet } from 'svelte';
|
|
66
|
-
declare function $$render<T extends Record<string,
|
|
66
|
+
declare function $$render<T extends Record<string, unknown>>(): {
|
|
67
67
|
props: ChartRootProps<T>;
|
|
68
68
|
exports: {};
|
|
69
69
|
bindings: "";
|
|
70
70
|
slots: {};
|
|
71
71
|
events: {};
|
|
72
72
|
};
|
|
73
|
-
declare class __sveltets_Render<T extends Record<string,
|
|
73
|
+
declare class __sveltets_Render<T extends Record<string, unknown>> {
|
|
74
74
|
props(): ReturnType<typeof $$render<T>>['props'];
|
|
75
75
|
events(): ReturnType<typeof $$render<T>>['events'];
|
|
76
76
|
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
@@ -78,12 +78,12 @@ declare class __sveltets_Render<T extends Record<string, any>> {
|
|
|
78
78
|
exports(): {};
|
|
79
79
|
}
|
|
80
80
|
interface $$IsomorphicComponent {
|
|
81
|
-
new <T extends Record<string,
|
|
81
|
+
new <T extends Record<string, unknown>>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
82
82
|
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
83
83
|
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
84
|
-
<T extends Record<string,
|
|
84
|
+
<T extends Record<string, unknown>>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
85
85
|
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
86
86
|
}
|
|
87
87
|
declare const ChartRoot: $$IsomorphicComponent;
|
|
88
|
-
type ChartRoot<T extends Record<string,
|
|
88
|
+
type ChartRoot<T extends Record<string, unknown>> = InstanceType<typeof ChartRoot<T>>;
|
|
89
89
|
export default ChartRoot;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
</script>
|
|
6
6
|
|
|
7
7
|
{#if typeof ctx.xGrid === 'boolean'}
|
|
8
|
-
{#each ctx.xTicks.labels as
|
|
9
|
-
<line class="grid-lines" x1={
|
|
8
|
+
{#each ctx.xTicks.labels as { position }}
|
|
9
|
+
<line class="grid-lines" x1={position} x2={position} y1={0} y2={ctx.chartHeight} />
|
|
10
10
|
{/each}
|
|
11
11
|
{:else if typeof ctx.xGrid === 'string'}
|
|
12
12
|
{#if ctx.xGrid.includes('top')}
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
</script>
|
|
6
6
|
|
|
7
7
|
<div class="container">
|
|
8
|
-
{#each ctx.xTicks.labels as
|
|
9
|
-
<span class="label" style:left="{
|
|
10
|
-
{
|
|
8
|
+
{#each ctx.xTicks.labels as { position, label }}
|
|
9
|
+
<span class="label" style:left="{position}px">
|
|
10
|
+
{label}
|
|
11
11
|
</span>
|
|
12
12
|
{/each}
|
|
13
13
|
</div>
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
</script>
|
|
6
6
|
|
|
7
7
|
{#if typeof ctx.yGrid === 'boolean'}
|
|
8
|
-
{#each ctx.yTicks.labels as
|
|
9
|
-
<line class="grid-lines" x1={0} x2={ctx.chartWidth} y1={
|
|
8
|
+
{#each ctx.yTicks.labels as { position }}
|
|
9
|
+
<line class="grid-lines" x1={0} x2={ctx.chartWidth} y1={position} y2={position} />
|
|
10
10
|
{/each}
|
|
11
11
|
{:else if typeof ctx.yGrid === 'string'}
|
|
12
12
|
{#if ctx.yGrid.includes('top')}
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
</script>
|
|
6
6
|
|
|
7
7
|
<div class="container" style:width="{ctx.yTicks.maxLength * 8}px">
|
|
8
|
-
{#each ctx.yTicks.labels as
|
|
9
|
-
<span class="label" style:top="{
|
|
10
|
-
{
|
|
8
|
+
{#each ctx.yTicks.labels as { position, label }}
|
|
9
|
+
<span class="label" style:top="{position}px">
|
|
10
|
+
{label}
|
|
11
11
|
</span>
|
|
12
12
|
{/each}
|
|
13
13
|
</div>
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
<script lang="ts" generics="T extends ChartData">
|
|
19
19
|
import { CreateFloating, type UpdatePositionProps } from '../../components/Floating/CreateFloating.svelte.js';
|
|
20
|
-
import { Paper
|
|
20
|
+
import { Paper } from '../../components/index.js';
|
|
21
21
|
import { getThemeColor } from '../../MantineProvider/color-functions/get-theme-color.js';
|
|
22
22
|
import { flip, offset } from '@floating-ui/dom';
|
|
23
23
|
import { on } from 'svelte/events';
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
let dots = $state<{ top: number; left: number; color: string }[]>([]);
|
|
42
42
|
|
|
43
43
|
function calcLeft(x: number, rect: DOMRect) {
|
|
44
|
-
return x + rect.left
|
|
44
|
+
return x + rect.left;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
function calcTop(y: number, rect: DOMRect) {
|
|
48
|
-
return y
|
|
48
|
+
return y;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// TODO: refactor this
|
|
@@ -141,51 +141,49 @@
|
|
|
141
141
|
}
|
|
142
142
|
</script>
|
|
143
143
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
{
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
<
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
<div class="item">
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
<
|
|
159
|
-
<
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
<div class="item-name">{series.name}</div>
|
|
166
|
-
</div>
|
|
167
|
-
|
|
168
|
-
<ChartTooltipData {series} {element} />
|
|
144
|
+
{#if show && chartRect}
|
|
145
|
+
<div class="root" bind:this={tooltipRef} style:top="{tooltipY}px" style:left="{tooltipX}px">
|
|
146
|
+
{#if children && element}
|
|
147
|
+
{@render children(element)}
|
|
148
|
+
{:else if element}
|
|
149
|
+
<Paper withBorder shadow="md">
|
|
150
|
+
<div class="label">{ctx.xFormatLabel(ctx.xAccessor(element as T))}</div>
|
|
151
|
+
|
|
152
|
+
<div class="body">
|
|
153
|
+
{#each ctx.series as series (series.name)}
|
|
154
|
+
<div class="item">
|
|
155
|
+
<div class="item-body">
|
|
156
|
+
{#if showColor}
|
|
157
|
+
<div class="item-color">
|
|
158
|
+
<svg>
|
|
159
|
+
<circle r={6} fill={getThemeColor(series.color)} width={12} height={12} cx={6} cy={6} />
|
|
160
|
+
</svg>
|
|
161
|
+
</div>
|
|
162
|
+
{/if}
|
|
163
|
+
|
|
164
|
+
<div class="item-name">{series.name}</div>
|
|
169
165
|
</div>
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
{
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
{
|
|
188
|
-
|
|
166
|
+
|
|
167
|
+
<ChartTooltipData {series} {element} />
|
|
168
|
+
</div>
|
|
169
|
+
{/each}
|
|
170
|
+
</div>
|
|
171
|
+
</Paper>
|
|
172
|
+
{/if}
|
|
173
|
+
</div>
|
|
174
|
+
|
|
175
|
+
<div
|
|
176
|
+
class="line"
|
|
177
|
+
style:top="{tooltipLine.top}px"
|
|
178
|
+
style:left="{tooltipLine.left}px"
|
|
179
|
+
style:height="{tooltipLine.height}px"
|
|
180
|
+
bind:this={tooltipLineRef}
|
|
181
|
+
></div>
|
|
182
|
+
|
|
183
|
+
{#each dots as dot}
|
|
184
|
+
<div class="dot" style:top="{dot.top}px" style:left="{dot.left}px" style:background-color={dot.color}></div>
|
|
185
|
+
{/each}
|
|
186
|
+
{/if}
|
|
189
187
|
|
|
190
188
|
<style>.root {
|
|
191
189
|
position: absolute;
|
|
@@ -17,14 +17,14 @@ export class CreateFloating {
|
|
|
17
17
|
$effect(() => {
|
|
18
18
|
if (!this.#elementsSet())
|
|
19
19
|
return;
|
|
20
|
-
const cleanup = autoUpdate(this.#referenceElement, this.#floatingElement, this
|
|
20
|
+
const cleanup = autoUpdate(this.#referenceElement, this.#floatingElement, this.#updatePosition);
|
|
21
21
|
return () => cleanup();
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
|
-
#elementsSet
|
|
24
|
+
#elementsSet() {
|
|
25
25
|
return this.#referenceElement && this.#floatingElement;
|
|
26
|
-
}
|
|
27
|
-
updatePosition = () => {
|
|
26
|
+
}
|
|
27
|
+
#updatePosition = () => {
|
|
28
28
|
if (!this.#elementsSet())
|
|
29
29
|
return;
|
|
30
30
|
computePosition(this.#referenceElement, this.#floatingElement, {
|
|
@@ -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
|
|
6
|
+
import CloseButton from '../CloseButton/CloseButton.svelte';
|
|
7
7
|
import { getPillGroupContext } from './PillGroupContext.svelte.js';
|
|
8
8
|
|
|
9
9
|
export type PillVariant = 'default' | 'contrast';
|
|
@@ -72,9 +72,11 @@
|
|
|
72
72
|
onremove?.();
|
|
73
73
|
removeButtonProps?.onclick?.(event);
|
|
74
74
|
}}
|
|
75
|
-
/>
|
|
75
|
+
/>
|
|
76
|
+
{/if}
|
|
76
77
|
</div>
|
|
77
78
|
|
|
79
|
+
<!-- svelte-ignore css_unused_selector -->
|
|
78
80
|
<style>.root {
|
|
79
81
|
--pill-fz-xs: calc(0.625rem * var(--mantine-scale));
|
|
80
82
|
--pill-fz-sm: calc(0.75rem * var(--mantine-scale));
|
|
@@ -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[];
|