poe-svelte-ui-lib 1.0.1 → 1.0.2
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/LICENSE +3 -3
- package/dist/Accordion/Accordion.svelte +53 -53
- package/dist/Accordion/AccordionProps.svelte +70 -70
- package/dist/Button/Button.svelte +144 -144
- package/dist/Button/ButtonProps.svelte +200 -200
- package/dist/ColorPicker/ColorPicker.svelte +207 -207
- package/dist/ColorPicker/ColorPickerProps.svelte +100 -100
- package/dist/FileAttach/FileAttach.svelte +103 -103
- package/dist/Graph/Graph.svelte +270 -270
- package/dist/Graph/GraphProps.svelte +56 -56
- package/dist/Input/Input.svelte +239 -239
- package/dist/Input/InputProps.svelte +221 -221
- package/dist/Loader.svelte +12 -12
- package/dist/MessageModal.svelte +54 -54
- package/dist/ProgressBar/ProgressBar.svelte +48 -48
- package/dist/ProgressBar/ProgressBarProps.svelte +145 -145
- package/dist/Select/Select.svelte +191 -191
- package/dist/Select/SelectProps.svelte +260 -260
- package/dist/Slider/Slider.svelte +260 -260
- package/dist/Slider/SliderProps.svelte +161 -161
- package/dist/Switch/Switch.svelte +83 -83
- package/dist/Switch/SwitchProps.svelte +144 -144
- package/dist/Table/Table.svelte +276 -276
- package/dist/Table/TableProps.svelte +286 -286
- package/dist/TextField/TextField.svelte +22 -22
- package/dist/TextField/TextFieldProps.svelte +92 -92
- package/dist/{appIcons → libIcons}/ButtonAdd.svelte +10 -10
- package/dist/{appIcons → libIcons}/ButtonDelete.svelte +13 -13
- package/dist/{appIcons → libIcons}/LoaderRotate.svelte +9 -9
- package/dist/locales/CircleFlagsEn.svelte +14 -14
- package/dist/locales/CircleFlagsRu.svelte +8 -8
- package/dist/locales/CircleFlagsZh.svelte +8 -8
- package/package.json +49 -47
- /package/dist/{appIcons → libIcons}/ButtonAdd.svelte.d.ts +0 -0
- /package/dist/{appIcons → libIcons}/ButtonDelete.svelte.d.ts +0 -0
- /package/dist/{appIcons → libIcons}/LoaderRotate.svelte.d.ts +0 -0
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
<!-- $lib/ElementsUI/SwitchProps.svelte -->
|
|
2
|
-
<script lang="ts">
|
|
3
|
-
import { getContext } from 'svelte'
|
|
4
|
-
import { t } from '../locales/i18n'
|
|
5
|
-
import type { UIComponent, ISliderProps } from '../types'
|
|
6
|
-
import * as UI from '../index'
|
|
7
|
-
import { optionsStore } from '../options'
|
|
8
|
-
|
|
9
|
-
const { component, onPropertyChange } = $props<{
|
|
10
|
-
component: UIComponent & { properties: Partial<ISliderProps> }
|
|
11
|
-
onPropertyChange: (value: string | object) => void
|
|
12
|
-
}>()
|
|
13
|
-
|
|
14
|
-
const DeviceVariables = getContext<{ value: string; name: string }[]>('DeviceVariables')
|
|
15
|
-
let VARIABLE_OPTIONS = $derived(
|
|
16
|
-
DeviceVariables.map((variable: { value: string; name: string }) => ({
|
|
17
|
-
id: variable.name,
|
|
18
|
-
value: variable.value,
|
|
19
|
-
name: `${variable.value} | ${variable.name}`,
|
|
20
|
-
})),
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
const initialAlign = $derived(
|
|
24
|
-
$optionsStore.ALIGN_OPTIONS.find((a) =>
|
|
25
|
-
(a.value as string).includes(component.properties.label?.class?.split(' ').find((cls: string) => cls.startsWith('text-'))),
|
|
26
|
-
),
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
const initialColor = $derived(
|
|
30
|
-
$optionsStore.COLOR_OPTIONS.find((c) =>
|
|
31
|
-
(c.value as string).includes(component.properties.wrapperClass?.split(' ').find((cls: string) => cls.startsWith('bg-'))),
|
|
32
|
-
),
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
const handleLabelAlign = (align: string) => {
|
|
36
|
-
let labelClass = component.properties.label.class || ''
|
|
37
|
-
labelClass = labelClass
|
|
38
|
-
.split(' ')
|
|
39
|
-
.filter((cls: string) => !cls.startsWith('text-'))
|
|
40
|
-
.join(' ')
|
|
41
|
-
if (align) {
|
|
42
|
-
labelClass += ` ${align}`
|
|
43
|
-
}
|
|
44
|
-
updateProperty('label.class', labelClass)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const handleOptionColorChange = (color: string) => {
|
|
48
|
-
let wrapperClass = component.properties.wrapperClass || ''
|
|
49
|
-
wrapperClass = wrapperClass
|
|
50
|
-
.split(' ')
|
|
51
|
-
.filter((cls: string) => !cls.startsWith('bg-'))
|
|
52
|
-
.join(' ')
|
|
53
|
-
if (color) {
|
|
54
|
-
wrapperClass += ` ${color}`
|
|
55
|
-
}
|
|
56
|
-
updateProperty('wrapperClass', wrapperClass)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/* Обновление свойства */
|
|
60
|
-
const updateProperty = (path: string, value: number | string | object) => {
|
|
61
|
-
const newProperties = JSON.parse(JSON.stringify(component.properties))
|
|
62
|
-
const parts = path.split('.')
|
|
63
|
-
let obj = newProperties
|
|
64
|
-
|
|
65
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
66
|
-
const part = parts[i]
|
|
67
|
-
if (!obj[part]) obj[part] = {}
|
|
68
|
-
obj = obj[part]
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
obj[parts[parts.length - 1]] = value
|
|
72
|
-
onPropertyChange(newProperties)
|
|
73
|
-
}
|
|
74
|
-
</script>
|
|
75
|
-
|
|
76
|
-
{#if component && component.properties}
|
|
77
|
-
<div class="relative flex flex-row items-start justify-center">
|
|
78
|
-
<div class="flex w-1/3 flex-col items-center px-2">
|
|
79
|
-
<UI.Select
|
|
80
|
-
label={{ name: $t('service.constructor.props.variable') }}
|
|
81
|
-
options={VARIABLE_OPTIONS}
|
|
82
|
-
value={VARIABLE_OPTIONS.find((opt) => opt.value === component.properties.id.value)}
|
|
83
|
-
onUpdate={(value) => {
|
|
84
|
-
updateProperty('id.name', (value.name as string).split('|')[1].trim())
|
|
85
|
-
updateProperty('id.value', value.value as string)
|
|
86
|
-
updateProperty('eventHandler.Variables', value.value as string)
|
|
87
|
-
}}
|
|
88
|
-
/>
|
|
89
|
-
<UI.Select
|
|
90
|
-
wrapperClass="w-full"
|
|
91
|
-
label={{ name: $t('service.constructor.props.action') }}
|
|
92
|
-
type="buttons"
|
|
93
|
-
value={$optionsStore.SHORT_ARGUMENT_OPTION.find((h) => h.value === component.properties.eventHandler.Argument)}
|
|
94
|
-
options={$optionsStore.SHORT_ARGUMENT_OPTION}
|
|
95
|
-
onUpdate={(option) => {
|
|
96
|
-
updateProperty('eventHandler.Argument', option.value as string)
|
|
97
|
-
}}
|
|
98
|
-
/>
|
|
99
|
-
</div>
|
|
100
|
-
<div class="flex w-1/3 flex-col px-2">
|
|
101
|
-
<UI.Select
|
|
102
|
-
wrapperClass="w-full"
|
|
103
|
-
label={{ name: $t('service.constructor.props.type') }}
|
|
104
|
-
type="buttons"
|
|
105
|
-
value={$optionsStore.SLIDER_TYPE_OPTIONS.find((opt) => opt.value === (component.properties.type || 'single'))}
|
|
106
|
-
options={$optionsStore.SLIDER_TYPE_OPTIONS}
|
|
107
|
-
onUpdate={(type) => {
|
|
108
|
-
updateProperty('value', type.value === 'single' ? 5 : [2, 7])
|
|
109
|
-
updateProperty('type', type.value as string)
|
|
110
|
-
}}
|
|
111
|
-
/>
|
|
112
|
-
<UI.Input
|
|
113
|
-
wrapperClass="w-full"
|
|
114
|
-
label={{ name: $t('service.constructor.props.minnum') }}
|
|
115
|
-
value={component.properties.number.minNum as number}
|
|
116
|
-
type="number"
|
|
117
|
-
onUpdate={(value) => updateProperty('number.minNum', Number(value))}
|
|
118
|
-
/>
|
|
119
|
-
<UI.Input
|
|
120
|
-
wrapperClass="w-full"
|
|
121
|
-
label={{ name: $t('service.constructor.props.maxnum') }}
|
|
122
|
-
value={component.properties.number.maxNum as number}
|
|
123
|
-
type="number"
|
|
124
|
-
onUpdate={(value) => updateProperty('number.maxNum', Number(value))}
|
|
125
|
-
/>
|
|
126
|
-
<UI.Input
|
|
127
|
-
wrapperClass="w-full"
|
|
128
|
-
label={{ name: $t('service.constructor.props.step') }}
|
|
129
|
-
value={component.properties.number.step as number}
|
|
130
|
-
type="number"
|
|
131
|
-
onUpdate={(value) => updateProperty('number.step', Number(value))}
|
|
132
|
-
/>
|
|
133
|
-
</div>
|
|
134
|
-
<div class="flex w-1/3 flex-col px-2">
|
|
135
|
-
<UI.Input
|
|
136
|
-
wrapperClass="w-full"
|
|
137
|
-
label={{ name: $t('service.constructor.props.label') }}
|
|
138
|
-
value={component.properties.label.name}
|
|
139
|
-
type="text"
|
|
140
|
-
componentClass="w-full"
|
|
141
|
-
onUpdate={(value) => updateProperty('label.name', value as string)}
|
|
142
|
-
/>
|
|
143
|
-
<UI.Select
|
|
144
|
-
wrapperClass="w-full"
|
|
145
|
-
label={{ name: $t('service.constructor.props.align') }}
|
|
146
|
-
type="buttons"
|
|
147
|
-
value={initialAlign}
|
|
148
|
-
options={$optionsStore.ALIGN_OPTIONS}
|
|
149
|
-
onUpdate={(option) => handleLabelAlign(option.value as string)}
|
|
150
|
-
/>
|
|
151
|
-
<UI.Select
|
|
152
|
-
wrapperClass="!h-14"
|
|
153
|
-
label={{ name: $t('service.constructor.props.colors') }}
|
|
154
|
-
type="buttons"
|
|
155
|
-
options={$optionsStore.COLOR_OPTIONS}
|
|
156
|
-
value={initialColor}
|
|
157
|
-
onUpdate={(option) => handleOptionColorChange(option.value as string)}
|
|
158
|
-
/>
|
|
159
|
-
</div>
|
|
160
|
-
</div>
|
|
161
|
-
{/if}
|
|
1
|
+
<!-- $lib/ElementsUI/SwitchProps.svelte -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { getContext } from 'svelte'
|
|
4
|
+
import { t } from '../locales/i18n'
|
|
5
|
+
import type { UIComponent, ISliderProps } from '../types'
|
|
6
|
+
import * as UI from '../index'
|
|
7
|
+
import { optionsStore } from '../options'
|
|
8
|
+
|
|
9
|
+
const { component, onPropertyChange } = $props<{
|
|
10
|
+
component: UIComponent & { properties: Partial<ISliderProps> }
|
|
11
|
+
onPropertyChange: (value: string | object) => void
|
|
12
|
+
}>()
|
|
13
|
+
|
|
14
|
+
const DeviceVariables = getContext<{ value: string; name: string }[]>('DeviceVariables')
|
|
15
|
+
let VARIABLE_OPTIONS = $derived(
|
|
16
|
+
DeviceVariables.map((variable: { value: string; name: string }) => ({
|
|
17
|
+
id: variable.name,
|
|
18
|
+
value: variable.value,
|
|
19
|
+
name: `${variable.value} | ${variable.name}`,
|
|
20
|
+
})),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
const initialAlign = $derived(
|
|
24
|
+
$optionsStore.ALIGN_OPTIONS.find((a) =>
|
|
25
|
+
(a.value as string).includes(component.properties.label?.class?.split(' ').find((cls: string) => cls.startsWith('text-'))),
|
|
26
|
+
),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
const initialColor = $derived(
|
|
30
|
+
$optionsStore.COLOR_OPTIONS.find((c) =>
|
|
31
|
+
(c.value as string).includes(component.properties.wrapperClass?.split(' ').find((cls: string) => cls.startsWith('bg-'))),
|
|
32
|
+
),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const handleLabelAlign = (align: string) => {
|
|
36
|
+
let labelClass = component.properties.label.class || ''
|
|
37
|
+
labelClass = labelClass
|
|
38
|
+
.split(' ')
|
|
39
|
+
.filter((cls: string) => !cls.startsWith('text-'))
|
|
40
|
+
.join(' ')
|
|
41
|
+
if (align) {
|
|
42
|
+
labelClass += ` ${align}`
|
|
43
|
+
}
|
|
44
|
+
updateProperty('label.class', labelClass)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const handleOptionColorChange = (color: string) => {
|
|
48
|
+
let wrapperClass = component.properties.wrapperClass || ''
|
|
49
|
+
wrapperClass = wrapperClass
|
|
50
|
+
.split(' ')
|
|
51
|
+
.filter((cls: string) => !cls.startsWith('bg-'))
|
|
52
|
+
.join(' ')
|
|
53
|
+
if (color) {
|
|
54
|
+
wrapperClass += ` ${color}`
|
|
55
|
+
}
|
|
56
|
+
updateProperty('wrapperClass', wrapperClass)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* Обновление свойства */
|
|
60
|
+
const updateProperty = (path: string, value: number | string | object) => {
|
|
61
|
+
const newProperties = JSON.parse(JSON.stringify(component.properties))
|
|
62
|
+
const parts = path.split('.')
|
|
63
|
+
let obj = newProperties
|
|
64
|
+
|
|
65
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
66
|
+
const part = parts[i]
|
|
67
|
+
if (!obj[part]) obj[part] = {}
|
|
68
|
+
obj = obj[part]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
obj[parts[parts.length - 1]] = value
|
|
72
|
+
onPropertyChange(newProperties)
|
|
73
|
+
}
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
{#if component && component.properties}
|
|
77
|
+
<div class="relative flex flex-row items-start justify-center">
|
|
78
|
+
<div class="flex w-1/3 flex-col items-center px-2">
|
|
79
|
+
<UI.Select
|
|
80
|
+
label={{ name: $t('service.constructor.props.variable') }}
|
|
81
|
+
options={VARIABLE_OPTIONS}
|
|
82
|
+
value={VARIABLE_OPTIONS.find((opt) => opt.value === component.properties.id.value)}
|
|
83
|
+
onUpdate={(value) => {
|
|
84
|
+
updateProperty('id.name', (value.name as string).split('|')[1].trim())
|
|
85
|
+
updateProperty('id.value', value.value as string)
|
|
86
|
+
updateProperty('eventHandler.Variables', value.value as string)
|
|
87
|
+
}}
|
|
88
|
+
/>
|
|
89
|
+
<UI.Select
|
|
90
|
+
wrapperClass="w-full"
|
|
91
|
+
label={{ name: $t('service.constructor.props.action') }}
|
|
92
|
+
type="buttons"
|
|
93
|
+
value={$optionsStore.SHORT_ARGUMENT_OPTION.find((h) => h.value === component.properties.eventHandler.Argument)}
|
|
94
|
+
options={$optionsStore.SHORT_ARGUMENT_OPTION}
|
|
95
|
+
onUpdate={(option) => {
|
|
96
|
+
updateProperty('eventHandler.Argument', option.value as string)
|
|
97
|
+
}}
|
|
98
|
+
/>
|
|
99
|
+
</div>
|
|
100
|
+
<div class="flex w-1/3 flex-col px-2">
|
|
101
|
+
<UI.Select
|
|
102
|
+
wrapperClass="w-full"
|
|
103
|
+
label={{ name: $t('service.constructor.props.type') }}
|
|
104
|
+
type="buttons"
|
|
105
|
+
value={$optionsStore.SLIDER_TYPE_OPTIONS.find((opt) => opt.value === (component.properties.type || 'single'))}
|
|
106
|
+
options={$optionsStore.SLIDER_TYPE_OPTIONS}
|
|
107
|
+
onUpdate={(type) => {
|
|
108
|
+
updateProperty('value', type.value === 'single' ? 5 : [2, 7])
|
|
109
|
+
updateProperty('type', type.value as string)
|
|
110
|
+
}}
|
|
111
|
+
/>
|
|
112
|
+
<UI.Input
|
|
113
|
+
wrapperClass="w-full"
|
|
114
|
+
label={{ name: $t('service.constructor.props.minnum') }}
|
|
115
|
+
value={component.properties.number.minNum as number}
|
|
116
|
+
type="number"
|
|
117
|
+
onUpdate={(value) => updateProperty('number.minNum', Number(value))}
|
|
118
|
+
/>
|
|
119
|
+
<UI.Input
|
|
120
|
+
wrapperClass="w-full"
|
|
121
|
+
label={{ name: $t('service.constructor.props.maxnum') }}
|
|
122
|
+
value={component.properties.number.maxNum as number}
|
|
123
|
+
type="number"
|
|
124
|
+
onUpdate={(value) => updateProperty('number.maxNum', Number(value))}
|
|
125
|
+
/>
|
|
126
|
+
<UI.Input
|
|
127
|
+
wrapperClass="w-full"
|
|
128
|
+
label={{ name: $t('service.constructor.props.step') }}
|
|
129
|
+
value={component.properties.number.step as number}
|
|
130
|
+
type="number"
|
|
131
|
+
onUpdate={(value) => updateProperty('number.step', Number(value))}
|
|
132
|
+
/>
|
|
133
|
+
</div>
|
|
134
|
+
<div class="flex w-1/3 flex-col px-2">
|
|
135
|
+
<UI.Input
|
|
136
|
+
wrapperClass="w-full"
|
|
137
|
+
label={{ name: $t('service.constructor.props.label') }}
|
|
138
|
+
value={component.properties.label.name}
|
|
139
|
+
type="text"
|
|
140
|
+
componentClass="w-full"
|
|
141
|
+
onUpdate={(value) => updateProperty('label.name', value as string)}
|
|
142
|
+
/>
|
|
143
|
+
<UI.Select
|
|
144
|
+
wrapperClass="w-full"
|
|
145
|
+
label={{ name: $t('service.constructor.props.align') }}
|
|
146
|
+
type="buttons"
|
|
147
|
+
value={initialAlign}
|
|
148
|
+
options={$optionsStore.ALIGN_OPTIONS}
|
|
149
|
+
onUpdate={(option) => handleLabelAlign(option.value as string)}
|
|
150
|
+
/>
|
|
151
|
+
<UI.Select
|
|
152
|
+
wrapperClass="!h-14"
|
|
153
|
+
label={{ name: $t('service.constructor.props.colors') }}
|
|
154
|
+
type="buttons"
|
|
155
|
+
options={$optionsStore.COLOR_OPTIONS}
|
|
156
|
+
value={initialColor}
|
|
157
|
+
onUpdate={(option) => handleOptionColorChange(option.value as string)}
|
|
158
|
+
/>
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
{/if}
|
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
<!-- $lib/ElementsUI/Switch.svelte -->
|
|
2
|
-
<script lang="ts">
|
|
3
|
-
import type { ISwitchProps } from '../types'
|
|
4
|
-
|
|
5
|
-
let {
|
|
6
|
-
id = { name: '', value: crypto.randomUUID() },
|
|
7
|
-
wrapperClass = '',
|
|
8
|
-
disabled = false,
|
|
9
|
-
label = { name: '', captionLeft: '', captionRight: '', class: '' },
|
|
10
|
-
height = '2rem',
|
|
11
|
-
value = $bindable(),
|
|
12
|
-
onChange = () => {},
|
|
13
|
-
}: ISwitchProps = $props()
|
|
14
|
-
|
|
15
|
-
const options = [1, 2]
|
|
16
|
-
let checked = $derived(value === options[1])
|
|
17
|
-
|
|
18
|
-
let knobTransform = $derived(checked ? `translateX(calc(${height} * 0.9))` : 'translateX(0)')
|
|
19
|
-
|
|
20
|
-
$effect(() => {
|
|
21
|
-
if (value === undefined || value === null) value = options[0]
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const handleToggle = () => {
|
|
25
|
-
if (disabled) return
|
|
26
|
-
const newValue = checked ? options[1] : options[0]
|
|
27
|
-
|
|
28
|
-
value = newValue
|
|
29
|
-
|
|
30
|
-
onChange(newValue)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const handleCaptionClick = (newValue: number) => {
|
|
34
|
-
if (disabled || value === newValue) return
|
|
35
|
-
value = newValue
|
|
36
|
-
onChange(newValue)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const maxCaptionWidth = $derived(
|
|
40
|
-
Math.max(label.captionLeft?.length ?? 0, label.captionRight?.length ?? 0) > 0
|
|
41
|
-
? `${Math.max(label.captionLeft?.length ?? 0, label.captionRight?.length ?? 0)}ch`
|
|
42
|
-
: 'auto',
|
|
43
|
-
)
|
|
44
|
-
</script>
|
|
45
|
-
|
|
46
|
-
<div class="bg-blue relative flex w-full flex-col items-center justify-center {wrapperClass}">
|
|
47
|
-
{#if label.name}
|
|
48
|
-
<h5 class={`w-full px-4 text-center ${label.class}`}>{label.name}</h5>
|
|
49
|
-
{/if}
|
|
50
|
-
|
|
51
|
-
<div class="relative flex w-full grow items-center justify-center !bg-transparent" id={id.value}>
|
|
52
|
-
<button class="mr-2 {disabled ? 'opacity-60' : 'cursor-pointer'}" style="width: {maxCaptionWidth}; text-align: end;" onclick={() => handleCaptionClick(1)}
|
|
53
|
-
>{label.captionLeft}</button
|
|
54
|
-
>
|
|
55
|
-
<label
|
|
56
|
-
class="relative flex items-center justify-between rounded-full border-1
|
|
57
|
-
{checked ? '!border-[var(--bg-color)]' : '!border-[var(--gray-color)]'}
|
|
58
|
-
{disabled ? 'opacity-60' : ''}"
|
|
59
|
-
>
|
|
60
|
-
<input
|
|
61
|
-
type="checkbox"
|
|
62
|
-
class="absolute left-1/2 h-full w-full -translate-x-1/2 cursor-pointer appearance-none rounded-md"
|
|
63
|
-
bind:checked
|
|
64
|
-
{disabled}
|
|
65
|
-
onchange={handleToggle}
|
|
66
|
-
/>
|
|
67
|
-
<span
|
|
68
|
-
class="relative flex items-center rounded-full transition-all duration-250 {checked ? '!bg-[var(--bg-color)]' : '!bg-[var(--gray-color)]'} {disabled
|
|
69
|
-
? ''
|
|
70
|
-
: 'cursor-pointer'}"
|
|
71
|
-
style="width: {`calc(${height} * 2)`}; height: {height};"
|
|
72
|
-
>
|
|
73
|
-
<span
|
|
74
|
-
class="absolute rounded-full bg-[var(--back-color)] transition-all duration-250 {disabled ? 'opacity-60' : 'cursor-pointer'}"
|
|
75
|
-
style="width: {`calc(${height} * 0.8)`}; height: {`calc(${height} * 0.8)`}; margin: 0 {`calc(${height} * 0.1)`}; transform: {knobTransform};"
|
|
76
|
-
></span>
|
|
77
|
-
</span>
|
|
78
|
-
</label>
|
|
79
|
-
<button class="ml-2 {disabled ? 'opacity-60' : 'cursor-pointer'}" style="width: {maxCaptionWidth}; text-align: start;" onclick={() => handleCaptionClick(2)}
|
|
80
|
-
>{label.captionRight}</button
|
|
81
|
-
>
|
|
82
|
-
</div>
|
|
83
|
-
</div>
|
|
1
|
+
<!-- $lib/ElementsUI/Switch.svelte -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import type { ISwitchProps } from '../types'
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
id = { name: '', value: crypto.randomUUID() },
|
|
7
|
+
wrapperClass = '',
|
|
8
|
+
disabled = false,
|
|
9
|
+
label = { name: '', captionLeft: '', captionRight: '', class: '' },
|
|
10
|
+
height = '2rem',
|
|
11
|
+
value = $bindable(),
|
|
12
|
+
onChange = () => {},
|
|
13
|
+
}: ISwitchProps = $props()
|
|
14
|
+
|
|
15
|
+
const options = [1, 2]
|
|
16
|
+
let checked = $derived(value === options[1])
|
|
17
|
+
|
|
18
|
+
let knobTransform = $derived(checked ? `translateX(calc(${height} * 0.9))` : 'translateX(0)')
|
|
19
|
+
|
|
20
|
+
$effect(() => {
|
|
21
|
+
if (value === undefined || value === null) value = options[0]
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const handleToggle = () => {
|
|
25
|
+
if (disabled) return
|
|
26
|
+
const newValue = checked ? options[1] : options[0]
|
|
27
|
+
|
|
28
|
+
value = newValue
|
|
29
|
+
|
|
30
|
+
onChange(newValue)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const handleCaptionClick = (newValue: number) => {
|
|
34
|
+
if (disabled || value === newValue) return
|
|
35
|
+
value = newValue
|
|
36
|
+
onChange(newValue)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const maxCaptionWidth = $derived(
|
|
40
|
+
Math.max(label.captionLeft?.length ?? 0, label.captionRight?.length ?? 0) > 0
|
|
41
|
+
? `${Math.max(label.captionLeft?.length ?? 0, label.captionRight?.length ?? 0)}ch`
|
|
42
|
+
: 'auto',
|
|
43
|
+
)
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<div class="bg-blue relative flex w-full flex-col items-center justify-center {wrapperClass}">
|
|
47
|
+
{#if label.name}
|
|
48
|
+
<h5 class={`w-full px-4 text-center ${label.class}`}>{label.name}</h5>
|
|
49
|
+
{/if}
|
|
50
|
+
|
|
51
|
+
<div class="relative flex w-full grow items-center justify-center !bg-transparent" id={id.value}>
|
|
52
|
+
<button class="mr-2 {disabled ? 'opacity-60' : 'cursor-pointer'}" style="width: {maxCaptionWidth}; text-align: end;" onclick={() => handleCaptionClick(1)}
|
|
53
|
+
>{label.captionLeft}</button
|
|
54
|
+
>
|
|
55
|
+
<label
|
|
56
|
+
class="relative flex items-center justify-between rounded-full border-1
|
|
57
|
+
{checked ? '!border-[var(--bg-color)]' : '!border-[var(--gray-color)]'}
|
|
58
|
+
{disabled ? 'opacity-60' : ''}"
|
|
59
|
+
>
|
|
60
|
+
<input
|
|
61
|
+
type="checkbox"
|
|
62
|
+
class="absolute left-1/2 h-full w-full -translate-x-1/2 cursor-pointer appearance-none rounded-md"
|
|
63
|
+
bind:checked
|
|
64
|
+
{disabled}
|
|
65
|
+
onchange={handleToggle}
|
|
66
|
+
/>
|
|
67
|
+
<span
|
|
68
|
+
class="relative flex items-center rounded-full transition-all duration-250 {checked ? '!bg-[var(--bg-color)]' : '!bg-[var(--gray-color)]'} {disabled
|
|
69
|
+
? ''
|
|
70
|
+
: 'cursor-pointer'}"
|
|
71
|
+
style="width: {`calc(${height} * 2)`}; height: {height};"
|
|
72
|
+
>
|
|
73
|
+
<span
|
|
74
|
+
class="absolute rounded-full bg-[var(--back-color)] transition-all duration-250 {disabled ? 'opacity-60' : 'cursor-pointer'}"
|
|
75
|
+
style="width: {`calc(${height} * 0.8)`}; height: {`calc(${height} * 0.8)`}; margin: 0 {`calc(${height} * 0.1)`}; transform: {knobTransform};"
|
|
76
|
+
></span>
|
|
77
|
+
</span>
|
|
78
|
+
</label>
|
|
79
|
+
<button class="ml-2 {disabled ? 'opacity-60' : 'cursor-pointer'}" style="width: {maxCaptionWidth}; text-align: start;" onclick={() => handleCaptionClick(2)}
|
|
80
|
+
>{label.captionRight}</button
|
|
81
|
+
>
|
|
82
|
+
</div>
|
|
83
|
+
</div>
|