Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Slider/Slider.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
{
name: 'min',
description: 'Minimum allowed slider value.',
description: 'Minimum allowed slider value. Negative values enable bidirectional fill from zero.',
required: false,
type: 'number',
default: '0'
Expand Down
29 changes: 29 additions & 0 deletions src/components/Slider/Slider.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,33 @@ describe('Slider', () => {
cy.get('#sl-err').should('have.attr', 'data-state', 'invalid')
})
})

describe('bidirectional fill', () => {
// Targets the range element: the only absolutely-positioned element inside the control.
const range = () => cy.get('[data-slot="control"] .absolute')

it('fills from zero-crossing to a positive value', () => {
cy.mount(Slider, { props: { modelValue: [50], min: -100, max: 100 } })
range()
.should('have.attr', 'style')
.and('include', 'left: 50%')
.and('include', 'right: 25%')
})

it('fills from a negative value to the zero-crossing', () => {
cy.mount(Slider, { props: { modelValue: [-50], min: -100, max: 100 } })
range()
.should('have.attr', 'style')
.and('include', 'left: 25%')
.and('include', 'right: 50%')
})

it('renders zero-width fill when value is at zero', () => {
cy.mount(Slider, { props: { modelValue: [0], min: -100, max: 100 } })
range()
.should('have.attr', 'style')
.and('include', 'left: 50%')
.and('include', 'right: 50%')
})
})
})
6 changes: 6 additions & 0 deletions src/components/Slider/Slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Use a two-element `modelValue` to render two thumbs.

<ComponentPreview name="Slider-Range" />

## Negative Values

When `min` is negative the slider fills bidirectionally from the zero-crossing, so positive and negative values are visually distinct.

<ComponentPreview name="Slider-NegativeValues" />

## Labeling

<ComponentPreview name="Slider-Labeling" />
Expand Down
41 changes: 35 additions & 6 deletions src/components/Slider/Slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ const {
disabled: () => props.disabled,
})

const isBidirectional = computed(() => props.min < 0 && props.max > 0)

const bidirectionalRangeStyles = computed(() => {
const { min, max } = props
const range = max - min
const zeroPos = -min / range
const thumbPositions = sliderValue.value.map((v) => (v - min) / range)
const allPositions =
thumbPositions.length === 1 ? [zeroPos, thumbPositions[0]] : thumbPositions
return {
left: `${Math.min(...allPositions) * 100}%`,
right: `${(1 - Math.max(...allPositions)) * 100}%`,
}
})

const trackClasses = computed(() => {
return [
'relative grow rounded',
Expand All @@ -68,6 +83,13 @@ const rangeClasses = computed(() => {
]
})

const rootClasses = computed(() => {
return [
'relative flex w-full select-none touch-none items-center',
props.size === 'md' ? 'h-5' : 'h-4',
]
})

const thumbClasses = computed(() => {
return [
'rounded-full bg-surface-white shadow-md ring-gray-600/20 transition-shadow duration-200 ease-out hover:ring-[6px] focus:outline-none dark:bg-surface-gray-7 dark:ring-gray-100/20',
Expand All @@ -85,10 +107,10 @@ const onValueCommit = (value: SliderValue) => {
const hasLabeling = computed(() => {
return Boolean(
props.label ||
slots.label ||
showDescription.value ||
slots.description ||
hasError.value,
slots.label ||
showDescription.value ||
slots.description ||
hasError.value,
)
})
</script>
Expand All @@ -110,7 +132,7 @@ const hasLabeling = computed(() => {
<SliderRoot
:id="inputId"
v-model="sliderValue"
class="relative flex w-full select-none touch-none items-center"
:class="rootClasses"
:max="props.max"
:min="props.min"
:step="props.step"
Expand All @@ -125,7 +147,14 @@ const hasLabeling = computed(() => {
@value-commit="onValueCommit"
>
<SliderTrack :class="trackClasses">
<SliderRange :class="rangeClasses" />
<SliderRange v-if="!isBidirectional" :class="rangeClasses" />
<div
v-else
:class="rangeClasses"
:style="bidirectionalRangeStyles"
data-orientation="horizontal"
:data-disabled="props.disabled ? '' : undefined"
/>
</SliderTrack>

<SliderThumb
Expand Down
12 changes: 12 additions & 0 deletions src/components/Slider/stories/NegativeValues.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup lang="ts">
import { ref } from 'vue'
import { Slider } from 'frappe-ui'

const value = ref([25])
</script>

<template>
<div class="w-full max-w-md">
<Slider v-model="value" :min="-50" :max="100" />
</div>
</template>
2 changes: 1 addition & 1 deletion src/components/Slider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface SliderProps extends InputLabelingProps {
/** Maximum allowed slider value. */
max?: number

/** Minimum allowed slider value. */
/** Minimum allowed slider value. Negative values enable bidirectional fill from zero. */
min?: number

/** Visual size of the slider. */
Expand Down
Loading