2026-01-18 15:55:07 +03:00
|
|
|
<!--
|
|
|
|
|
Component: ComboControl
|
|
|
|
|
Provides multiple ways to change certain value
|
|
|
|
|
- via Increase/Decrease buttons
|
|
|
|
|
- via Slider
|
|
|
|
|
- via Input
|
|
|
|
|
-->
|
2026-01-05 09:03:31 +03:00
|
|
|
<script lang="ts">
|
2026-01-07 16:53:17 +03:00
|
|
|
import type { TypographyControl } from '$shared/lib';
|
2026-01-05 09:03:31 +03:00
|
|
|
import { Button } from '$shared/shadcn/ui/button';
|
|
|
|
|
import * as ButtonGroup from '$shared/shadcn/ui/button-group';
|
|
|
|
|
import { Input } from '$shared/shadcn/ui/input';
|
|
|
|
|
import * as Popover from '$shared/shadcn/ui/popover';
|
|
|
|
|
import { Slider } from '$shared/shadcn/ui/slider';
|
|
|
|
|
import MinusIcon from '@lucide/svelte/icons/minus';
|
|
|
|
|
import PlusIcon from '@lucide/svelte/icons/plus';
|
|
|
|
|
import type { ChangeEventHandler } from 'svelte/elements';
|
|
|
|
|
|
|
|
|
|
interface ComboControlProps {
|
|
|
|
|
/**
|
|
|
|
|
* Text for increase button aria-label
|
|
|
|
|
*/
|
|
|
|
|
increaseLabel?: string;
|
|
|
|
|
/**
|
|
|
|
|
* Text for decrease button aria-label
|
|
|
|
|
*/
|
|
|
|
|
decreaseLabel?: string;
|
|
|
|
|
/**
|
|
|
|
|
* Text for control button aria-label
|
|
|
|
|
*/
|
|
|
|
|
controlLabel?: string;
|
|
|
|
|
/**
|
2026-01-07 16:53:17 +03:00
|
|
|
* Control instance
|
2026-01-05 09:03:31 +03:00
|
|
|
*/
|
2026-01-07 16:53:17 +03:00
|
|
|
control: TypographyControl;
|
2026-01-05 09:03:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {
|
2026-01-07 16:53:17 +03:00
|
|
|
control,
|
2026-01-05 09:03:31 +03:00
|
|
|
decreaseLabel,
|
2026-01-07 16:53:17 +03:00
|
|
|
increaseLabel,
|
2026-01-05 09:03:31 +03:00
|
|
|
controlLabel,
|
|
|
|
|
}: ComboControlProps = $props();
|
|
|
|
|
|
|
|
|
|
// Local state for the slider to prevent infinite loops
|
2026-01-08 13:14:04 +03:00
|
|
|
// svelte-ignore state_referenced_locally - $state captures initial value, $effect syncs updates
|
2026-01-07 16:53:17 +03:00
|
|
|
let sliderValue = $state(Number(control.value));
|
2026-01-05 09:03:31 +03:00
|
|
|
|
|
|
|
|
// Sync sliderValue when external value changes
|
|
|
|
|
$effect(() => {
|
2026-01-07 16:53:17 +03:00
|
|
|
sliderValue = Number(control.value);
|
2026-01-05 09:03:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleInputChange: ChangeEventHandler<HTMLInputElement> = event => {
|
|
|
|
|
const parsedValue = parseFloat(event.currentTarget.value);
|
|
|
|
|
if (!isNaN(parsedValue)) {
|
2026-01-07 16:53:17 +03:00
|
|
|
control.value = parsedValue;
|
2026-01-05 09:03:31 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle slider value change.
|
|
|
|
|
* The Slider component passes the value as a number directly.
|
|
|
|
|
*/
|
2026-01-07 16:53:17 +03:00
|
|
|
const handleSliderChange = (newValue: number) => {
|
|
|
|
|
control.value = newValue;
|
2026-01-05 09:03:31 +03:00
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<ButtonGroup.Root>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
aria-label={decreaseLabel}
|
2026-01-07 16:53:17 +03:00
|
|
|
onclick={control.decrease}
|
|
|
|
|
disabled={control.isAtMin}
|
2026-01-05 09:03:31 +03:00
|
|
|
>
|
|
|
|
|
<MinusIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
<Popover.Root>
|
|
|
|
|
<Popover.Trigger>
|
|
|
|
|
{#snippet child({ props })}
|
|
|
|
|
<Button
|
|
|
|
|
{...props}
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
aria-label={controlLabel}
|
|
|
|
|
>
|
2026-01-07 16:53:17 +03:00
|
|
|
{control.value}
|
2026-01-05 09:03:31 +03:00
|
|
|
</Button>
|
|
|
|
|
{/snippet}
|
|
|
|
|
</Popover.Trigger>
|
|
|
|
|
<Popover.Content class="w-auto p-4">
|
|
|
|
|
<div class="flex flex-col items-center gap-3">
|
|
|
|
|
<Slider
|
2026-01-07 16:53:17 +03:00
|
|
|
min={control.min}
|
|
|
|
|
max={control.max}
|
|
|
|
|
step={control.step}
|
2026-01-05 09:03:31 +03:00
|
|
|
value={sliderValue}
|
|
|
|
|
onValueChange={handleSliderChange}
|
|
|
|
|
type="single"
|
|
|
|
|
orientation="vertical"
|
|
|
|
|
class="h-48"
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
2026-01-07 16:53:17 +03:00
|
|
|
value={control.value}
|
2026-01-05 09:03:31 +03:00
|
|
|
onchange={handleInputChange}
|
2026-01-07 16:53:17 +03:00
|
|
|
min={control.min}
|
|
|
|
|
max={control.max}
|
2026-01-05 09:03:31 +03:00
|
|
|
class="w-16 text-center"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</Popover.Content>
|
|
|
|
|
</Popover.Root>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
aria-label={increaseLabel}
|
2026-01-07 16:53:17 +03:00
|
|
|
onclick={control.increase}
|
|
|
|
|
disabled={control.isAtMax}
|
2026-01-05 09:03:31 +03:00
|
|
|
>
|
|
|
|
|
<PlusIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</ButtonGroup.Root>
|