V1.0

Base Components

Slider

Allowing precise selection of a single value within a specified range.

Installation

Install the following dependencies:

terminal
npm install @radix-ui/react-slider

Create a slider.tsx file and paste the following code into it.

/components/ui/slider.tsx
'use client';
 
import * as React from 'react';
import * as SliderPrimitive from '@radix-ui/react-slider';
 
import { cnExt } from '@/utils/cn';
 
const SLIDER_ROOT_NAME = 'SliderRoot';
const SLIDER_THUMB_NAME = 'SliderThumb';
 
const SliderRoot = React.forwardRef<
  React.ComponentRef<typeof SliderPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root>
>(({ className, children, ...rest }, forwardedRef) => (
  <SliderPrimitive.Root
    ref={forwardedRef}
    className={cnExt(
      'relative flex h-4 w-full touch-none select-none items-center',
      className,
    )}
    {...rest}
  >
    <SliderPrimitive.Track className='relative h-1.5 w-full overflow-hidden rounded-full bg-bg-soft-200'>
      <SliderPrimitive.Range className='absolute h-full bg-primary-base' />
    </SliderPrimitive.Track>
    {children}
  </SliderPrimitive.Root>
));
SliderRoot.displayName = SLIDER_ROOT_NAME;
 
const SliderThumb = React.forwardRef<
  React.ComponentRef<typeof SliderPrimitive.Thumb>,
  React.ComponentPropsWithoutRef<typeof SliderPrimitive.Thumb>
>(({ className, ...rest }, forwardedRef) => {
  return (
    <SliderPrimitive.Thumb
      ref={forwardedRef}
      className={cnExt(
        [
          // base
          'box-content block size-1.5 shrink-0 cursor-pointer rounded-full border-[5px] border-static-white bg-primary-base shadow-toggle-switch outline-none',
          // focus
          'focus:outline-none',
        ],
        className,
      )}
      {...rest}
    />
  );
});
SliderThumb.displayName = SLIDER_THUMB_NAME;
 
export { SliderRoot as Root, SliderThumb as Thumb };

Update the import paths to match your project setup.

Examples

Range

With Tooltip

API Reference

This component is based on the Radix UI Slider primitives. Refer to their documentation for the API reference.

© 2024 AlignUI Design System. All rights reserved.