slider.tsx
Overview
slider.tsx provides reusable React slider components built on top of the @radix-ui/react-slider primitive. It defines two main components:
Slider: A low-level, styled slider component that wraps Radix UI's slider primitive, applying custom styles and forwarding refs.FormSlider: A higher-level controlled slider component designed for forms, managing single value state and change handling in a simpler API.
This file focuses on accessibility, custom styling, and integration with React forms by abstracting the complexity of the multi-value Radix slider into a simpler single-value controlled component.
Components and Types
1. Slider
A styled wrapper around Radix UI's SliderPrimitive.Root component. It provides a horizontal slider with:
A track that visually indicates the slider's range.
A thumb handle for user interaction.
Custom styling matching the application's theme.
Proper accessibility and keyboard navigation inherited from Radix UI.
Type Definition:
Uses React.forwardRef to forward refs to the underlying Radix slider root element.
Props:
Extends all props from SliderPrimitive.Root, allowing full customization. Includes a className prop to add custom CSS classes.
Usage Example:
import { Slider } from './slider';
function Example() {
return (
<Slider
defaultValue={[50]}
max={100}
step={1}
onValueChange={(value) => console.log(value)}
/>
);
}
Implementation Details:
Uses the utility function
cnto concatenate and conditionally apply CSS classes.The slider track is styled with a rounded background bar.
The slider range (selected value) is indicated with a colored bar within the track.
The thumb is styled as a circular handle with focus-visible ring styles and disabled state handling.
2. FormSlider
A controlled slider component tailored for form usage, simplifying the multi-thumb Radix slider into a single-value slider.
Type:
type SliderProps = Omit<
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root>,
'onChange' | 'value'
> & {
onChange: (value: number) => void;
value: number;
};
Props:
value: number— The current value of the slider.onChange: (value: number) => void— Change handler called when slider value updates.Other slider primitive props (except Radix's
onChangeandvalue, which are replaced).
Behavior:
Internally maps the single numeric
valueto Radix's multi-thumb array format[value].Calls
onChangewith the first value from Radix's array when the slider changes.
Usage Example:
import { FormSlider } from './slider';
import React, { useState } from 'react';
function VolumeControl() {
const [volume, setVolume] = useState(30);
return (
<FormSlider
value={volume}
max={100}
onChange={(val) => setVolume(val)}
/>
);
}
Important Implementation Details
The file uses React's
forwardRefto expose refs on bothSliderandFormSlider, enabling parent components to access native slider elements (e.g., for focus or measurements).The file imports and relies on
@radix-ui/react-sliderfor accessibility and core slider behaviors (keyboard, pointer, ARIA).Styling classes used (e.g.,
bg-primary,ring-ring) are presumably from a Tailwind CSS or similar utility-first CSS framework, ensuring consistent theming.The utility function
cn(class name joiner) merges static and dynamic class names cleanly.FormSliderabstracts Radix's multi-thumb slider complexity by exposing a simple single-value API, improving usability in typical form contexts.
Interaction with Other Parts of the System
@radix-ui/react-slider: This external dependency provides the core slider primitives with accessibility and behavior out of the box.cnutility: Imported from@/lib/utils, this function manages class name concatenation, likely shared across the project for consistent class management.Styling system: The CSS classes (e.g.,
bg-primary,ring-offset-background, etc.) indicate integration with a design system or Tailwind CSS config.Parent components or forms:
FormSlideris intended to be used in controlled form components that manage slider values and handle updates.
Mermaid Component Diagram
componentDiagram
component Slider {
+ref: React.Ref
+props: SliderPrimitive.Root props
- renders SliderPrimitive.Root
- applies custom styles and structure
}
component FormSlider {
+ref: React.Ref
+value: number
+onChange(value: number): void
- Uses Slider internally
- Converts single value <-> Radix multi-value
}
FormSlider --> Slider : uses
Slider --> SliderPrimitive.Root : wraps
Summary
The slider.tsx file provides accessible, styled slider components for React applications, leveraging Radix UI slider primitives. It offers a flexible Slider component for full customization, and a simplified FormSlider for common form use cases. The file emphasizes clean abstraction, consistent theming, and ease of integration with React ref forwarding and controlled component patterns.