Skip to main content

Slider

Import

import { Slider } from "@sparrowengg/twigs-react";

Usage

Result
Loading...
<Slider
  defaultValue={[0]}
  min={0}
  max={100}
  labels={{
    left: "Min",
    right: "Max",
  }}
  labelPlacement="bottom"
/>

Range slider

The slider can be made a range slider by passing a second value to the defaultValue or value array.

Result
Loading...
<Slider
  defaultValue={[10, 20]}
  min={0}
  max={100}
  labels={{
    left: "Min",
    right: "Max",
  }}
  labelPlacement="bottom"
  components={{
    ThumbLeft: () => (
      <SliderThumb
        css={{
          borderRadius: 0,
          backgroundColor: "$negative900",
        }}
      ></SliderThumb>
    ),
    ThumbRight: () => (
      <SliderThumb
        css={{
          backgroundColor: "$black900",
          borderRadius: 0,
        }}
      ></SliderThumb>
    ),
  }}
/>

Customizing components

Track, Range and Thumb can be customized by using the components property. Make sure the custom component is wrapped by the corresponding default component as shown below

Result
Loading...
<Slider
  components={{
    Thumb: () => (
      <SliderThumb
        css={{
          backgroundColor: "$negative900",
          borderRadius: 0,
        }}
      />
    ),
    Track: ({ children }) => (
      <SliderTrack>
        <Box
          css={{
            width: "100%",
            height: "100%",
            background: "linear-gradient(to right, red, blue)",
            position: "absolute",
          }}
        />
        {children}
      </SliderTrack>
    ),
  }}
/>

The two thumbs can be customized by passing components to ThumbLeft and ThumbRight in components prop.

Props

PropertyDescriptionTypeDefault
defaultValueDefault value of slider. It should be an arraynumber[]
valueControlled value for slidernumber[]
minMinimum valuenumber
maxMaximum valuenumber
labelsCustomize left and right labels{ left: string; right: string }
labelPlacementPosition of the left & right labels'bottom' ǀ 'top''top'
componentsCustomize individual componentsRecord<Track ǀ Range ǀ Thumb ǀ ThumbLeft ǀ ThumbRight, ReactNode>