@makolabs/ripple 0.0.1-dev.6 → 0.0.1-dev.8
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 +393 -53
- package/dist/button/Button.svelte +5 -3
- package/dist/button/Button.svelte.d.ts +1 -1
- package/dist/button/button.d.ts +40 -63
- package/dist/button/button.js +15 -14
- package/dist/charts/Chart.svelte +533 -0
- package/dist/charts/Chart.svelte.d.ts +4 -0
- package/dist/drawer/Drawer.svelte +13 -2
- package/dist/drawer/Drawer.svelte.d.ts +1 -1
- package/dist/drawer/drawer.d.ts +0 -17
- package/dist/elements/alert/Alert.svelte +53 -0
- package/dist/elements/alert/Alert.svelte.d.ts +4 -0
- package/dist/elements/badge/Badge.svelte +13 -5
- package/dist/elements/badge/Badge.svelte.d.ts +1 -1
- package/dist/elements/badge/badge.d.ts +0 -12
- package/dist/elements/dropdown/Dropdown.svelte +32 -37
- package/dist/elements/dropdown/Dropdown.svelte.d.ts +1 -1
- package/dist/elements/dropdown/Select.svelte +143 -59
- package/dist/elements/dropdown/Select.svelte.d.ts +1 -1
- package/dist/elements/dropdown/dropdown.d.ts +34 -57
- package/dist/elements/dropdown/dropdown.js +10 -4
- package/dist/elements/dropdown/select.d.ts +34 -54
- package/dist/elements/dropdown/select.js +22 -14
- package/dist/elements/file-upload/FileUpload.svelte +213 -0
- package/dist/elements/file-upload/FileUpload.svelte.d.ts +4 -0
- package/dist/elements/progress/Progress.svelte +87 -0
- package/dist/elements/progress/Progress.svelte.d.ts +4 -0
- package/dist/elements/timeline/Timeline.svelte +92 -0
- package/dist/elements/timeline/Timeline.svelte.d.ts +7 -0
- package/dist/forms/Checkbox.svelte +54 -0
- package/dist/forms/Checkbox.svelte.d.ts +4 -0
- package/dist/forms/DateRange.svelte +493 -0
- package/dist/forms/DateRange.svelte.d.ts +4 -0
- package/dist/forms/Form.svelte +39 -0
- package/dist/forms/Form.svelte.d.ts +4 -0
- package/dist/forms/Input.svelte +86 -0
- package/dist/forms/Input.svelte.d.ts +4 -0
- package/dist/forms/NumberInput.svelte +159 -0
- package/dist/forms/NumberInput.svelte.d.ts +4 -0
- package/dist/forms/RadioInputs.svelte +64 -0
- package/dist/forms/RadioInputs.svelte.d.ts +4 -0
- package/dist/forms/RadioPill.svelte +66 -0
- package/dist/forms/RadioPill.svelte.d.ts +4 -0
- package/dist/forms/Slider.svelte +342 -0
- package/dist/forms/Slider.svelte.d.ts +4 -0
- package/dist/forms/Tags.svelte +181 -0
- package/dist/forms/Tags.svelte.d.ts +4 -0
- package/dist/forms/Toggle.svelte +132 -0
- package/dist/forms/Toggle.svelte.d.ts +4 -0
- package/dist/forms/slider.d.ts +143 -0
- package/dist/forms/slider.js +62 -0
- package/dist/header/Breadcrumbs.svelte +2 -1
- package/dist/header/Breadcrumbs.svelte.d.ts +1 -1
- package/dist/header/PageHeader.svelte +2 -2
- package/dist/header/PageHeader.svelte.d.ts +1 -1
- package/dist/header/breadcrumbs.d.ts +20 -14
- package/dist/header/breadcrumbs.js +6 -0
- package/dist/header/pageheaders.d.ts +1 -1
- package/dist/helper/date.d.ts +7 -0
- package/dist/helper/date.js +15 -0
- package/dist/index.d.ts +742 -9
- package/dist/index.js +59 -16
- package/dist/layout/card/Card.svelte +5 -8
- package/dist/layout/card/Card.svelte.d.ts +1 -1
- package/dist/layout/card/StatsCard.svelte +116 -87
- package/dist/layout/card/card.d.ts +22 -33
- package/dist/layout/card/card.js +9 -8
- package/dist/layout/card/stats-card.d.ts +23 -25
- package/dist/layout/card/stats-card.js +13 -13
- package/dist/layout/navbar/navbar.d.ts +0 -23
- package/dist/layout/sidebar/NavGroup.svelte +33 -41
- package/dist/layout/sidebar/NavGroup.svelte.d.ts +1 -1
- package/dist/layout/sidebar/NavItem.svelte +1 -1
- package/dist/layout/sidebar/NavItem.svelte.d.ts +1 -1
- package/dist/layout/sidebar/Sidebar.svelte +19 -25
- package/dist/layout/sidebar/Sidebar.svelte.d.ts +1 -1
- package/dist/layout/table/table.d.ts +1 -1
- package/dist/layout/tabs/tabs.d.ts +1 -1
- package/dist/modal/Modal.svelte +2 -1
- package/dist/modal/Modal.svelte.d.ts +1 -1
- package/dist/modal/modal.d.ts +0 -23
- package/dist/sonner/sonner.svelte +13 -0
- package/dist/sonner/sonner.svelte.d.ts +4 -0
- package/dist/types/variants.d.ts +1 -21
- package/dist/types/variants.js +1 -19
- package/dist/variants.d.ts +20 -0
- package/dist/variants.js +19 -0
- package/package.json +6 -2
- package/dist/button/index.d.ts +0 -1
- package/dist/button/index.js +0 -1
- package/dist/drawer/index.d.ts +0 -2
- package/dist/drawer/index.js +0 -1
- package/dist/elements/badge/index.d.ts +0 -2
- package/dist/elements/badge/index.js +0 -2
- package/dist/elements/dropdown/index.d.ts +0 -3
- package/dist/elements/dropdown/index.js +0 -2
- package/dist/header/index.d.ts +0 -4
- package/dist/header/index.js +0 -2
- package/dist/layout/card/index.d.ts +0 -4
- package/dist/layout/card/index.js +0 -2
- package/dist/layout/index.d.ts +0 -5
- package/dist/layout/index.js +0 -5
- package/dist/layout/navbar/index.d.ts +0 -2
- package/dist/layout/navbar/index.js +0 -2
- package/dist/layout/sidebar/index.d.ts +0 -2
- package/dist/layout/sidebar/index.js +0 -1
- package/dist/layout/sidebar/sidebar.d.ts +0 -46
- package/dist/layout/sidebar/sidebar.js +0 -1
- package/dist/layout/table/index.d.ts +0 -3
- package/dist/layout/table/index.js +0 -2
- package/dist/layout/tabs/index.d.ts +0 -3
- package/dist/layout/tabs/index.js +0 -3
- package/dist/modal/index.d.ts +0 -1
- package/dist/modal/index.js +0 -1
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '../helper/cls.js';
|
|
3
|
+
import { Size } from '../variants.js';
|
|
4
|
+
import type { NumberInputProps } from '../index.js';
|
|
5
|
+
|
|
6
|
+
let {
|
|
7
|
+
value = $bindable(0),
|
|
8
|
+
unit = $bindable('USD'),
|
|
9
|
+
name,
|
|
10
|
+
label,
|
|
11
|
+
placeholder = 'Enter a number',
|
|
12
|
+
size = Size.BASE,
|
|
13
|
+
class: className = '',
|
|
14
|
+
units = [],
|
|
15
|
+
errors,
|
|
16
|
+
disabled = false,
|
|
17
|
+
dropdownicon: DropdownIcon,
|
|
18
|
+
onunitchange: onUnitChange,
|
|
19
|
+
...restProps
|
|
20
|
+
}: NumberInputProps = $props();
|
|
21
|
+
|
|
22
|
+
let showUnitDropdown = $state(false);
|
|
23
|
+
let containerRef = $state<HTMLDivElement | null>(null);
|
|
24
|
+
|
|
25
|
+
const selectedOption = $derived(units.find((u) => u.value === unit));
|
|
26
|
+
|
|
27
|
+
const containerClass = $derived(
|
|
28
|
+
cn(
|
|
29
|
+
'relative flex items-center gap-1 rounded-lg border bg-white shadow-sm',
|
|
30
|
+
{
|
|
31
|
+
'border-danger-300': errors?.length,
|
|
32
|
+
'cursor-not-allowed opacity-50': disabled
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
'h-8': size === Size.SM,
|
|
36
|
+
'h-10': size === Size.BASE,
|
|
37
|
+
'h-12': size === Size.LG
|
|
38
|
+
},
|
|
39
|
+
'border-default-300 focus-within:border-primary-500 focus-within:ring-2 focus-within:ring-primary-500 focus-within:ring-offset-2',
|
|
40
|
+
className
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const inputClass = $derived(
|
|
45
|
+
cn('w-full bg-transparent outline-none disabled:cursor-not-allowed px-3', {
|
|
46
|
+
'text-sm': size === Size.SM,
|
|
47
|
+
'text-base': size === Size.BASE,
|
|
48
|
+
'text-lg': size === Size.LG
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const dropdownClass = cn(
|
|
53
|
+
'absolute right-0 top-full z-50 mt-1 max-h-[200px] w-[100px] overflow-auto rounded-md border bg-white shadow-lg'
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const iconClass = $derived(
|
|
57
|
+
cn('h-4 w-4 transition-transform', { 'rotate-180': showUnitDropdown })
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
function handleUnitSelect(selectedUnit: string) {
|
|
61
|
+
onUnitChange?.(unit, selectedUnit);
|
|
62
|
+
unit = selectedUnit;
|
|
63
|
+
showUnitDropdown = false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function handleClickOutside(event: MouseEvent) {
|
|
67
|
+
const target = event.target as Node;
|
|
68
|
+
if (containerRef && !containerRef.contains(target)) {
|
|
69
|
+
showUnitDropdown = false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function handleUnitToggle() {
|
|
74
|
+
showUnitDropdown = !showUnitDropdown;
|
|
75
|
+
}
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<svelte:window onclick={handleClickOutside} />
|
|
79
|
+
|
|
80
|
+
<div class="space-y-1">
|
|
81
|
+
{#if label}
|
|
82
|
+
<label for={name} class="block text-sm font-medium text-gray-700">{label}</label>
|
|
83
|
+
{/if}
|
|
84
|
+
<div class={containerClass} bind:this={containerRef}>
|
|
85
|
+
<svg
|
|
86
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
87
|
+
width="24"
|
|
88
|
+
height="24"
|
|
89
|
+
viewBox="0 0 24 24"
|
|
90
|
+
class="text-default-500 ml-3 size-4 flex-shrink-0"
|
|
91
|
+
>
|
|
92
|
+
<path
|
|
93
|
+
fill="none"
|
|
94
|
+
stroke="currentColor"
|
|
95
|
+
stroke-linecap="round"
|
|
96
|
+
stroke-linejoin="round"
|
|
97
|
+
stroke-width="2"
|
|
98
|
+
d="M17 9V7a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2m2 4h10a2 2 0 0 0 2-2v-6a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2m7-5a2 2 0 1 1-4 0a2 2 0 0 1 4 0"
|
|
99
|
+
/>
|
|
100
|
+
</svg>
|
|
101
|
+
<input
|
|
102
|
+
{name}
|
|
103
|
+
id={name}
|
|
104
|
+
bind:value
|
|
105
|
+
type="number"
|
|
106
|
+
{placeholder}
|
|
107
|
+
{disabled}
|
|
108
|
+
class={inputClass}
|
|
109
|
+
{...restProps}
|
|
110
|
+
/>
|
|
111
|
+
|
|
112
|
+
<button
|
|
113
|
+
type="button"
|
|
114
|
+
class="hover:bg-default-100 flex items-center gap-1 rounded px-1"
|
|
115
|
+
onclick={handleUnitToggle}
|
|
116
|
+
{disabled}
|
|
117
|
+
>
|
|
118
|
+
{#if selectedOption?.icon}
|
|
119
|
+
{@const Icon = selectedOption.icon}
|
|
120
|
+
<Icon />
|
|
121
|
+
{/if}
|
|
122
|
+
<span class="text-sm">{unit}</span>
|
|
123
|
+
{#if DropdownIcon}
|
|
124
|
+
<DropdownIcon class={iconClass} />
|
|
125
|
+
{:else}
|
|
126
|
+
<svg class={iconClass} fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
127
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" />
|
|
128
|
+
</svg>
|
|
129
|
+
{/if}
|
|
130
|
+
</button>
|
|
131
|
+
|
|
132
|
+
{#if showUnitDropdown}
|
|
133
|
+
<div class={dropdownClass}>
|
|
134
|
+
{#each units as unitOption}
|
|
135
|
+
<button
|
|
136
|
+
type="button"
|
|
137
|
+
class="hover:bg-default-100 w-full px-3 py-1.5 text-left text-sm"
|
|
138
|
+
class:bg-default-50={unit === unitOption.value}
|
|
139
|
+
onclick={() => handleUnitSelect(unitOption.value)}
|
|
140
|
+
>
|
|
141
|
+
<div class="flex items-center gap-2">
|
|
142
|
+
{#if unitOption.icon}
|
|
143
|
+
{@const Icon = unitOption.icon}
|
|
144
|
+
<Icon />
|
|
145
|
+
{/if}
|
|
146
|
+
{unitOption.value}
|
|
147
|
+
</div>
|
|
148
|
+
</button>
|
|
149
|
+
{/each}
|
|
150
|
+
</div>
|
|
151
|
+
{/if}
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
{#if errors?.length}
|
|
155
|
+
{#each errors as error}
|
|
156
|
+
<p class="text-danger-600 text-sm">{error}</p>
|
|
157
|
+
{/each}
|
|
158
|
+
{/if}
|
|
159
|
+
</div>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '../helper/cls.js';
|
|
3
|
+
import type { RadioInputsProps } from '../index.js';
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
name,
|
|
7
|
+
label,
|
|
8
|
+
options,
|
|
9
|
+
value = $bindable(),
|
|
10
|
+
disabled = false,
|
|
11
|
+
class: className = '',
|
|
12
|
+
errors = [],
|
|
13
|
+
required = false
|
|
14
|
+
}: RadioInputsProps = $props();
|
|
15
|
+
|
|
16
|
+
const containerClass = $derived(cn('space-y-2', className));
|
|
17
|
+
|
|
18
|
+
const radioClass = $derived(
|
|
19
|
+
cn('w-4 h-4 text-primary-600 border-gray-300 focus:ring-primary-500', {
|
|
20
|
+
'opacity-50 cursor-not-allowed': disabled,
|
|
21
|
+
'border-red-300 focus:ring-red-500': errors.length
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const labelClass = $derived(
|
|
26
|
+
cn('block text-sm font-medium', {
|
|
27
|
+
'text-gray-700': !errors.length,
|
|
28
|
+
'text-red-600': errors.length
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<div class={containerClass}>
|
|
34
|
+
{#if label}
|
|
35
|
+
<label class={labelClass} for={`${name}-${value || options[0].value}`}>{label}</label>
|
|
36
|
+
{/if}
|
|
37
|
+
<div class="space-y-2">
|
|
38
|
+
{#each options as option}
|
|
39
|
+
<div class="flex items-center gap-2">
|
|
40
|
+
<input
|
|
41
|
+
type="radio"
|
|
42
|
+
{name}
|
|
43
|
+
id={`${name}-${option.value}`}
|
|
44
|
+
value={option.value}
|
|
45
|
+
bind:group={value}
|
|
46
|
+
class={radioClass}
|
|
47
|
+
{disabled}
|
|
48
|
+
{required}
|
|
49
|
+
aria-describedby={errors.length ? `${name}-errors` : undefined}
|
|
50
|
+
/>
|
|
51
|
+
<label for={`${name}-${option.value}`} class="text-sm text-gray-700">
|
|
52
|
+
{option.label}
|
|
53
|
+
</label>
|
|
54
|
+
</div>
|
|
55
|
+
{/each}
|
|
56
|
+
</div>
|
|
57
|
+
{#if errors.length}
|
|
58
|
+
{#each errors as error (error)}
|
|
59
|
+
<p id={`${name}-errors`} class="mt-1 text-sm text-red-600" role="alert">
|
|
60
|
+
{error}
|
|
61
|
+
</p>
|
|
62
|
+
{/each}
|
|
63
|
+
{/if}
|
|
64
|
+
</div>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '../helper/cls.js';
|
|
3
|
+
import type { RadioPillProps } from '../index.js';
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
options = [],
|
|
7
|
+
value = $bindable(options.length > 0 ? options[0].value : ''),
|
|
8
|
+
name = '',
|
|
9
|
+
class: className = '',
|
|
10
|
+
errors = [],
|
|
11
|
+
onchange = undefined,
|
|
12
|
+
label = ''
|
|
13
|
+
}: RadioPillProps = $props();
|
|
14
|
+
|
|
15
|
+
function handleSelect(optionValue: string) {
|
|
16
|
+
value = optionValue;
|
|
17
|
+
if (onchange) onchange(optionValue);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let pillDivRef: HTMLDivElement;
|
|
21
|
+
const hasError = $derived(errors && errors.length > 0);
|
|
22
|
+
|
|
23
|
+
$effect(() => {
|
|
24
|
+
if (errors.length) {
|
|
25
|
+
pillDivRef.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<div class={cn('flex flex-col space-y-2', className)} bind:this={pillDivRef}>
|
|
31
|
+
{#if hasError}
|
|
32
|
+
<div class="text-danger-500 text-sm">{errors[0]}</div>
|
|
33
|
+
{/if}
|
|
34
|
+
{#if label}
|
|
35
|
+
<label
|
|
36
|
+
class={cn('text-sm font-medium', hasError ? 'text-danger-500' : 'text-default-700')}
|
|
37
|
+
for={value}>{label}</label
|
|
38
|
+
>
|
|
39
|
+
{/if}
|
|
40
|
+
<div class="inline-flex flex-wrap gap-2">
|
|
41
|
+
{#each options as option}
|
|
42
|
+
<button
|
|
43
|
+
id={option.value}
|
|
44
|
+
type="button"
|
|
45
|
+
onclick={() => handleSelect(option.value)}
|
|
46
|
+
class={cn(
|
|
47
|
+
'border-default-200 cursor-pointer rounded-lg border px-4 py-2 text-sm font-medium transition-all',
|
|
48
|
+
{
|
|
49
|
+
'border-info-600 bg-info-600 text-white': value === option.value && !hasError,
|
|
50
|
+
'border-danger-500 bg-danger-500 text-white': value === option.value && hasError,
|
|
51
|
+
'text-default-800 hover:border-default-300 bg-white':
|
|
52
|
+
value !== option.value && !hasError,
|
|
53
|
+
'border-danger-500 text-danger-500 hover:bg-danger-50':
|
|
54
|
+
value !== option.value && hasError,
|
|
55
|
+
'text-default-400': value !== option.value && option.value === '128' && !hasError
|
|
56
|
+
}
|
|
57
|
+
)}
|
|
58
|
+
aria-pressed={value === option.value}
|
|
59
|
+
>
|
|
60
|
+
{option.label}
|
|
61
|
+
</button>
|
|
62
|
+
{/each}
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
|
|
66
|
+
<input type="hidden" {name} {value} />
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
<!-- A unified slider component supporting enum, single value, and range selection -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { cn } from '../helper/cls.js';
|
|
4
|
+
import { slider } from './slider.js';
|
|
5
|
+
import { Size } from '../variants.js';
|
|
6
|
+
import type { SliderProps } from '../index.js';
|
|
7
|
+
|
|
8
|
+
interface EnumOption {
|
|
9
|
+
value: string | number;
|
|
10
|
+
label: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type SliderMode = 'single' | 'range' | 'enum';
|
|
14
|
+
type ThumbType = 'start' | 'end' | 'single';
|
|
15
|
+
type NotationType = 'standard' | 'compact' | 'scientific' | 'engineering';
|
|
16
|
+
|
|
17
|
+
let {
|
|
18
|
+
name,
|
|
19
|
+
label,
|
|
20
|
+
mode = 'single' as SliderMode,
|
|
21
|
+
disabled = false,
|
|
22
|
+
size = Size.BASE,
|
|
23
|
+
errors = [],
|
|
24
|
+
class: className = '',
|
|
25
|
+
min = 0,
|
|
26
|
+
max = 100,
|
|
27
|
+
step = 1,
|
|
28
|
+
value = $bindable(min),
|
|
29
|
+
valueStart = $bindable(min),
|
|
30
|
+
valueEnd = $bindable(max),
|
|
31
|
+
showValue = true,
|
|
32
|
+
valuePrefix = '',
|
|
33
|
+
valueSuffix = '',
|
|
34
|
+
options = [] as EnumOption[],
|
|
35
|
+
formatOptions = {
|
|
36
|
+
notation: 'standard' as NotationType,
|
|
37
|
+
maximumFractionDigits: 1,
|
|
38
|
+
minimumFractionDigits: 0
|
|
39
|
+
}
|
|
40
|
+
}: SliderProps = $props();
|
|
41
|
+
|
|
42
|
+
$effect(() => {
|
|
43
|
+
if (mode === 'enum' && options.length > 0 && value === min) {
|
|
44
|
+
value = options[0].value;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const {
|
|
49
|
+
base,
|
|
50
|
+
track,
|
|
51
|
+
range,
|
|
52
|
+
thumb,
|
|
53
|
+
mark,
|
|
54
|
+
label: labelClass,
|
|
55
|
+
value: valueClass
|
|
56
|
+
} = $derived(
|
|
57
|
+
slider({
|
|
58
|
+
size,
|
|
59
|
+
disabled,
|
|
60
|
+
hasError: errors.length > 0
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const baseClass = $derived(cn(base(), { 'mb-12': mode === 'enum' }, className));
|
|
65
|
+
const trackClass = $derived(cn(track()));
|
|
66
|
+
const rangeClass = $derived(cn(range()));
|
|
67
|
+
const thumbClass = $derived(cn(thumb()));
|
|
68
|
+
const markClass = $derived(cn(mark()));
|
|
69
|
+
const labelClass_ = $derived(cn(labelClass()));
|
|
70
|
+
const valueClass_ = $derived(cn(valueClass()));
|
|
71
|
+
|
|
72
|
+
let isDragging = $state(false);
|
|
73
|
+
let activeThumb = $state<ThumbType>('single');
|
|
74
|
+
let trackElement: HTMLDivElement;
|
|
75
|
+
|
|
76
|
+
function getEnumStepSize() {
|
|
77
|
+
return options.length > 1 ? 100 / (options.length - 1) : 100;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getEnumIndex() {
|
|
81
|
+
const index = options.findIndex((opt) => opt.value === value);
|
|
82
|
+
return index >= 0 ? index : 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getSingleThumbPosition() {
|
|
86
|
+
if (typeof value !== 'number') return '0%';
|
|
87
|
+
return `${((value - min) / (max - min)) * 100}%`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getSingleRangeWidth() {
|
|
91
|
+
if (typeof value !== 'number') return '0%';
|
|
92
|
+
return `${((value - min) / (max - min)) * 100}%`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function getEnumThumbPosition() {
|
|
96
|
+
const index = getEnumIndex();
|
|
97
|
+
const stepSize = getEnumStepSize();
|
|
98
|
+
return `${index * stepSize}%`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function getEnumRangeWidth() {
|
|
102
|
+
const index = getEnumIndex();
|
|
103
|
+
const stepSize = getEnumStepSize();
|
|
104
|
+
return `${index * stepSize}%`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function getRangeThumbStartPosition() {
|
|
108
|
+
return `${((valueStart - min) / (max - min)) * 100}%`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function getRangeThumbEndPosition() {
|
|
112
|
+
return `${((valueEnd - min) / (max - min)) * 100}%`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getRangeWidth() {
|
|
116
|
+
return `${((valueEnd - valueStart) / (max - min)) * 100}%`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function getRangeLeft() {
|
|
120
|
+
return `${((valueStart - min) / (max - min)) * 100}%`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function formatValue(val: number | string): string {
|
|
124
|
+
if (typeof val === 'string') return val;
|
|
125
|
+
|
|
126
|
+
const formatter = new Intl.NumberFormat('en-US', formatOptions);
|
|
127
|
+
return `${valuePrefix}${formatter.format(val)}${valueSuffix}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function calculateValueFromPosition(x: number): number {
|
|
131
|
+
const rect = trackElement.getBoundingClientRect();
|
|
132
|
+
const percentage = Math.max(0, Math.min(100, ((x - rect.left) / rect.width) * 100));
|
|
133
|
+
const rawValue = (percentage / 100) * (max - min) + min;
|
|
134
|
+
return Math.min(Math.max(min, Math.round(rawValue / step) * step), max);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function handleTrackClick(event: MouseEvent) {
|
|
138
|
+
if (disabled) return;
|
|
139
|
+
|
|
140
|
+
const newValue = calculateValueFromPosition(event.clientX);
|
|
141
|
+
|
|
142
|
+
if (mode === 'enum') {
|
|
143
|
+
const percentage = ((newValue - min) / (max - min)) * 100;
|
|
144
|
+
const index = Math.round(percentage / getEnumStepSize());
|
|
145
|
+
value = options[Math.min(Math.max(0, index), options.length - 1)].value;
|
|
146
|
+
} else if (mode === 'range') {
|
|
147
|
+
const distanceToStart = Math.abs(valueStart - newValue);
|
|
148
|
+
const distanceToEnd = Math.abs(valueEnd - newValue);
|
|
149
|
+
|
|
150
|
+
if (distanceToStart < distanceToEnd) {
|
|
151
|
+
valueStart = Math.min(newValue, valueEnd - step);
|
|
152
|
+
} else {
|
|
153
|
+
valueEnd = Math.max(newValue, valueStart + step);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
value = newValue;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function handleThumbMouseDown(event: MouseEvent, thumb: ThumbType = 'single') {
|
|
161
|
+
if (disabled) return;
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
|
|
164
|
+
isDragging = true;
|
|
165
|
+
activeThumb = thumb;
|
|
166
|
+
|
|
167
|
+
window.addEventListener('mousemove', handleGlobalMouseMove);
|
|
168
|
+
window.addEventListener('mouseup', handleGlobalMouseUp);
|
|
169
|
+
document.body.style.userSelect = 'none';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function handleGlobalMouseMove(event: MouseEvent) {
|
|
173
|
+
if (!isDragging || disabled) return;
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
|
|
176
|
+
const newValue = calculateValueFromPosition(event.clientX);
|
|
177
|
+
|
|
178
|
+
if (mode === 'enum') {
|
|
179
|
+
const percentage = ((newValue - min) / (max - min)) * 100;
|
|
180
|
+
const index = Math.round(percentage / getEnumStepSize());
|
|
181
|
+
if (index >= 0 && index < options.length) {
|
|
182
|
+
value = options[index].value;
|
|
183
|
+
}
|
|
184
|
+
} else if (mode === 'range') {
|
|
185
|
+
if (activeThumb === 'start') {
|
|
186
|
+
valueStart = Math.min(newValue, valueEnd - step);
|
|
187
|
+
} else {
|
|
188
|
+
valueEnd = Math.max(newValue, valueStart + step);
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
value = newValue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function handleGlobalMouseUp(event: MouseEvent) {
|
|
196
|
+
if (!isDragging) return;
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
|
|
199
|
+
isDragging = false;
|
|
200
|
+
window.removeEventListener('mousemove', handleGlobalMouseMove);
|
|
201
|
+
window.removeEventListener('mouseup', handleGlobalMouseUp);
|
|
202
|
+
document.body.style.userSelect = '';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function handleKeydown(event: KeyboardEvent, thumb: ThumbType = 'single') {
|
|
206
|
+
if (disabled) return;
|
|
207
|
+
|
|
208
|
+
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
const direction = event.key === 'ArrowLeft' ? -1 : 1;
|
|
211
|
+
|
|
212
|
+
if (mode === 'enum') {
|
|
213
|
+
const currentIndex = options.findIndex((opt) => opt.value === value);
|
|
214
|
+
const newIndex = Math.min(Math.max(0, currentIndex + direction), options.length - 1);
|
|
215
|
+
value = options[newIndex].value;
|
|
216
|
+
} else if (mode === 'range') {
|
|
217
|
+
if (thumb === 'start') {
|
|
218
|
+
const newValue = valueStart + direction * step;
|
|
219
|
+
valueStart = Math.min(Math.max(min, newValue), valueEnd - step);
|
|
220
|
+
} else {
|
|
221
|
+
const newValue = valueEnd + direction * step;
|
|
222
|
+
valueEnd = Math.max(Math.min(max, newValue), valueStart + step);
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
const newValue = (typeof value === 'number' ? value : min) + direction * step;
|
|
226
|
+
value = Math.min(Math.max(min, newValue), max);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
$effect(() => {
|
|
232
|
+
if (errors.length) {
|
|
233
|
+
trackElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
</script>
|
|
237
|
+
|
|
238
|
+
<input type="hidden" name={`${name}[value]`} bind:value />
|
|
239
|
+
{#if mode === 'range'}
|
|
240
|
+
<input type="hidden" name={`${name}[start]`} bind:value={valueStart} />
|
|
241
|
+
<input type="hidden" name={`${name}[end]`} bind:value={valueEnd} />
|
|
242
|
+
{/if}
|
|
243
|
+
<input type="hidden" name={`${name}[mode]`} bind:value={mode} />
|
|
244
|
+
|
|
245
|
+
<div class={baseClass}>
|
|
246
|
+
<div class="flex items-center justify-between">
|
|
247
|
+
{#if label}
|
|
248
|
+
<label for={name} class={labelClass_}>{label}</label>
|
|
249
|
+
{/if}
|
|
250
|
+
{#if showValue && mode !== 'enum'}
|
|
251
|
+
<div class={valueClass_}>
|
|
252
|
+
{#if mode === 'range'}
|
|
253
|
+
<span>{formatValue(valueStart)}</span>
|
|
254
|
+
<span class="mx-1">-</span>
|
|
255
|
+
<span>{formatValue(valueEnd)}</span>
|
|
256
|
+
{:else}
|
|
257
|
+
<span>{formatValue(value)}</span>
|
|
258
|
+
{/if}
|
|
259
|
+
</div>
|
|
260
|
+
{:else if showValue && mode === 'enum'}
|
|
261
|
+
<div class={valueClass_}>
|
|
262
|
+
<span>{options[getEnumIndex()]?.label || ''}</span>
|
|
263
|
+
</div>
|
|
264
|
+
{/if}
|
|
265
|
+
</div>
|
|
266
|
+
|
|
267
|
+
<div
|
|
268
|
+
bind:this={trackElement}
|
|
269
|
+
class={trackClass}
|
|
270
|
+
role={mode === 'range' ? 'group' : 'slider'}
|
|
271
|
+
aria-disabled={disabled}
|
|
272
|
+
aria-label={label}
|
|
273
|
+
onclick={handleTrackClick}
|
|
274
|
+
>
|
|
275
|
+
{#if mode === 'range'}
|
|
276
|
+
<div class={rangeClass} style="width: {getRangeWidth()}; left: {getRangeLeft()}"></div>
|
|
277
|
+
<div
|
|
278
|
+
class={thumbClass}
|
|
279
|
+
style="left: {getRangeThumbStartPosition()}"
|
|
280
|
+
role="slider"
|
|
281
|
+
aria-label="Minimum value"
|
|
282
|
+
aria-valuemin={min}
|
|
283
|
+
aria-valuemax={max}
|
|
284
|
+
aria-valuenow={valueStart}
|
|
285
|
+
tabindex={disabled ? -1 : 0}
|
|
286
|
+
onkeydown={(e) => handleKeydown(e, 'start')}
|
|
287
|
+
onmousedown={(e) => handleThumbMouseDown(e, 'start')}
|
|
288
|
+
></div>
|
|
289
|
+
<div
|
|
290
|
+
class={thumbClass}
|
|
291
|
+
style="left: {getRangeThumbEndPosition()}"
|
|
292
|
+
role="slider"
|
|
293
|
+
aria-label="Maximum value"
|
|
294
|
+
aria-valuemin={min}
|
|
295
|
+
aria-valuemax={max}
|
|
296
|
+
aria-valuenow={valueEnd}
|
|
297
|
+
tabindex={disabled ? -1 : 0}
|
|
298
|
+
onkeydown={(e) => handleKeydown(e, 'end')}
|
|
299
|
+
onmousedown={(e) => handleThumbMouseDown(e, 'end')}
|
|
300
|
+
></div>
|
|
301
|
+
{:else if mode === 'enum'}
|
|
302
|
+
<div class={rangeClass} style="width: {getEnumRangeWidth()}"></div>
|
|
303
|
+
<div
|
|
304
|
+
class={thumbClass}
|
|
305
|
+
style="left: {getEnumThumbPosition()}"
|
|
306
|
+
role="slider"
|
|
307
|
+
aria-valuemin={0}
|
|
308
|
+
aria-valuemax={options.length - 1}
|
|
309
|
+
aria-valuenow={getEnumIndex()}
|
|
310
|
+
tabindex={disabled ? -1 : 0}
|
|
311
|
+
onkeydown={handleKeydown}
|
|
312
|
+
onmousedown={(e) => handleThumbMouseDown(e, 'single')}
|
|
313
|
+
></div>
|
|
314
|
+
{#each options as option, i}
|
|
315
|
+
<div class={markClass} style="left: {(i / (options.length - 1)) * 100}%">
|
|
316
|
+
{option.label}
|
|
317
|
+
</div>
|
|
318
|
+
{/each}
|
|
319
|
+
{:else}
|
|
320
|
+
<div class={rangeClass} style="width: {getSingleRangeWidth()}"></div>
|
|
321
|
+
<div
|
|
322
|
+
class={thumbClass}
|
|
323
|
+
style="left: {getSingleThumbPosition()}"
|
|
324
|
+
role="slider"
|
|
325
|
+
aria-valuemin={min}
|
|
326
|
+
aria-valuemax={max}
|
|
327
|
+
aria-valuenow={typeof value === 'number' ? value : min}
|
|
328
|
+
tabindex={disabled ? -1 : 0}
|
|
329
|
+
onkeydown={handleKeydown}
|
|
330
|
+
onmousedown={(e) => handleThumbMouseDown(e, 'single')}
|
|
331
|
+
></div>
|
|
332
|
+
{/if}
|
|
333
|
+
</div>
|
|
334
|
+
|
|
335
|
+
{#if errors.length}
|
|
336
|
+
{#each errors as error}
|
|
337
|
+
<p class="mt-2.5 text-sm text-red-600" role="alert">
|
|
338
|
+
{error}
|
|
339
|
+
</p>
|
|
340
|
+
{/each}
|
|
341
|
+
{/if}
|
|
342
|
+
</div>
|