From 147df04c229a9af85a85e0accf78716d37f4eb04 Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Wed, 18 Feb 2026 16:55:11 +0300 Subject: [PATCH] feat(Slider): tweak styles for a knob and add slider label --- .../ui/ComboControlV2/ComboControlV2.svelte | 32 +++---- src/shared/ui/Slider/Slider.svelte | 89 +++++++++++++------ 2 files changed, 76 insertions(+), 45 deletions(-) diff --git a/src/shared/ui/ComboControlV2/ComboControlV2.svelte b/src/shared/ui/ComboControlV2/ComboControlV2.svelte index a7ad0cb..efbbbc9 100644 --- a/src/shared/ui/ComboControlV2/ComboControlV2.svelte +++ b/src/shared/ui/ComboControlV2/ComboControlV2.svelte @@ -98,11 +98,11 @@ const handleInputChange: ChangeEventHandler = event => { function calculateScale(index: number): number | string { const calculate = () => orientation === 'horizontal' - ? (control.min + (index * (control.max - control.min) / 4)) - : (control.max - (index * (control.max - control.min) / 4)); + ? control.min + (index * (control.max - control.min)) / 4 + : control.max - (index * (control.max - control.min)) / 4; return Number.isInteger(control.step) ? Math.round(calculate()) - : (calculate()).toFixed(2); + : calculate().toFixed(2); } @@ -111,7 +111,9 @@ function calculateScale(index: number): number | string { class={cn( 'flex gap-4 sm:py-4 sm:px-1 rounded-xl transition-all duration-300', '', - orientation === 'horizontal' ? 'flex-row items-end w-full' : 'flex-col items-center h-full', + orientation === 'horizontal' + ? 'flex-row items-end w-full' + : 'flex-col items-center h-full', className, )} > @@ -120,7 +122,9 @@ function calculateScale(index: number): number | string {
{#each Array(5) as _, i} @@ -133,7 +137,12 @@ function calculateScale(index: number): number | string { {calculateScale(i)} -
+
{/each} @@ -146,6 +155,7 @@ function calculateScale(index: number): number | string { min={control.min} max={control.max} step={control.step} + {label} {orientation} />
@@ -162,16 +172,6 @@ function calculateScale(index: number): number | string { variant="ghost" /> {/if} - - {#if label} -
-
-
- - {label} - -
- {/if} {/snippet} diff --git a/src/shared/ui/Slider/Slider.svelte b/src/shared/ui/Slider/Slider.svelte index b03ec6c..20e68bb 100644 --- a/src/shared/ui/Slider/Slider.svelte +++ b/src/shared/ui/Slider/Slider.svelte @@ -9,28 +9,43 @@ import { type SliderRootProps, } from 'bits-ui'; -type Props = Omit & { - /** - * Slider value, numeric. - */ - value: number; - /** - * A callback function called when the value changes. - * @param newValue - number - */ - onValueChange?: (newValue: number) => void; - /** - * A callback function called when the user stops dragging the thumb and the value is committed. - * @param newValue - number - */ - onValueCommit?: (newValue: number) => void; -}; +type Props = + & Omit< + SliderRootProps, + 'type' | 'onValueChange' | 'onValueCommit' + > + & { + /** + * Slider value, numeric. + */ + value: number; + /** + * Optional label displayed inline on the track before the filled range. + */ + label?: string; + /** + * A callback function called when the value changes. + * @param newValue - number + */ + onValueChange?: (newValue: number) => void; + /** + * A callback function called when the user stops dragging the thumb and the value is committed. + * @param newValue - number + */ + onValueCommit?: (newValue: number) => void; + }; -let { value = $bindable(), orientation = 'horizontal', class: className, ...rest }: Props = $props(); +let { + value = $bindable(), + orientation = 'horizontal', + class: className, + label, + ...rest +}: Props = $props(); {#snippet children(props)} + {#if label && orientation === 'horizontal'} + + {label} + + {/if} - -