Slider

The Slider component allows users to select a value from a continuous or stepped range by dragging a thumb.

Installation

$ cbui-cli install @crossbuildui/slider

Import

index.tsx
1import { Slider } from '@/crossbuildui/slider'; 2// import Icon from 'react-native-vector-icons/MaterialIcons'; // Or your preferred icon library for start/end content

Slider Component

Basic Usage

A simple slider with a default value and a label.

MyComponent.tsx
1<Slider defaultValue={50} label="Volume" />

Sizes

Adjust slider track and thumb size with sm, md (default), or lg.

MyComponent.tsx
1<View style={{gap: 24}}> 2 <Slider defaultValue={30} size="sm" label="Small Slider" /> 3 <Slider defaultValue={50} size="md" label="Medium Slider (Default)" /> 4 <Slider defaultValue={70} size="lg" label="Large Slider" /> 5</View>

Colors

Set the thematic color of the slider's filled track and thumb using the color prop.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider defaultValue={25} color="primary" label="Primary" /> 3 <Slider defaultValue={40} color="secondary" label="Secondary" /> 4 <Slider defaultValue={55} color="success" label="Success" /> 5 <Slider defaultValue={70} color="warning" label="Warning" /> 6 <Slider defaultValue={85} color="danger" label="Danger" /> 7 <Slider defaultValue={50} color="foreground" label="Foreground" /> 8</View>

Radius

Control the border radius of the track and thumb with none, sm, md (default), lg, or full.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider defaultValue={50} radius="none" label="No Radius" /> 3 <Slider defaultValue={50} radius="sm" label="Small Radius" /> 4 <Slider defaultValue={50} radius="md" label="Medium Radius (Default)" /> 5 <Slider defaultValue={50} radius="lg" label="Large Radius" /> 6 <Slider defaultValue={50} radius="full" label="Full Radius" /> 7</View>

Orientation

Sliders can be horizontal (default) or vertical.

MyComponent.tsx
1<View style={{flexDirection: 'row', gap: 40, alignItems: 'center', height: 250}}> 2 <Slider defaultValue={60} label="Horizontal (Default)" /> 3 <Slider 4 orientation="vertical" 5 defaultValue={75} 6 label="Vertical Slider" 7 minValue={0} 8 maxValue={100} 9 /> 10</View>

Step, Min & Max Values

Define the range and increment of the slider using minValue, maxValue, and step.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider 3 label="Temperature" 4 defaultValue={20} 5 minValue={0} 6 maxValue={40} 7 step={0.5} 8 formatOptions={{ style: 'unit', unit: 'celsius' }} 9 /> 10 <Slider 11 label="Brightness" 12 defaultValue={75} 13 minValue={0} 14 maxValue={100} 15 step={5} 16 /> 17</View>

Controlled and Uncontrolled

Manage slider state externally using value and onChange, or use defaultValue for an uncontrolled component. onChangeEnd fires when the gesture ends.

MyComponent.tsx
1function ControlledSlider() { 2 const [value, setValue] = React.useState(60); 3 4 return ( 5 <View style={{gap: 8}}> 6 <Slider 7 label="Controlled Slider" 8 value={value} 9 onChange={setValue} 10 minValue={0} 11 maxValue={100} 12 /> 13 <Text>Current Value: {value}</Text> 14 </View> 15 ); 16} 17 18// Uncontrolled (uses defaultValue) 19// <Slider label="Uncontrolled" defaultValue={30} /> 20

Label and Value Display

Provide a label, control value visibility with hideValue, and format the displayed value with formatOptions.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider defaultValue={50} label="Slider with Label" /> 3 <Slider defaultValue={70} label="Hide Value" hideValue /> 4 <Slider 5 defaultValue={25} 6 label="Custom Format" 7 formatOptions={{ style: 'currency', currency: 'USD' }} 8 /> 9</View>

Tooltip

Enable showTooltip to display the current value above the thumb while dragging.

MyComponent.tsx
1<Slider 2 defaultValue={65} 3 label="Drag to see Tooltip" 4 showTooltip 5/>

Show Steps

Display visual indicators for each step on the track by enabling showSteps.

MyComponent.tsx
1<Slider 2 defaultValue={50} 3 label="Slider with Steps" 4 minValue={0} 5 maxValue={100} 6 step={10} 7 showSteps 8/>

Marks

Provide an array of marks to display custom labels and indicators at specific values along the track.

MyComponent.tsx
1const marks = [ 2 { value: 0, label: '0°C' }, 3 { value: 20, label: '20°C' }, 4 { value: 37, label: '37°C' }, 5 { value: 100, label: '100°C' }, 6]; 7 8<Slider 9 label="Temperature with Marks" 10 defaultValue={20} 11 minValue={0} 12 maxValue={100} 13 marks={marks} 14 step={1} 15/>

Fill Offset

Use fillOffset to change the starting point of the filled portion of the track. Useful for sliders representing a range that doesn't start from the minimum value (e.g., a balance slider centered at 0).

MyComponent.tsx
1<View style={{gap: 24}}> 2 <Slider 3 label="Balance (-50 to 50, fill from 0)" 4 defaultValue={10} 5 minValue={-50} 6 maxValue={50} 7 fillOffset={0} 8 step={1} 9 /> 10 <Slider 11 label="Range (0 to 100, fill from 50)" 12 defaultValue={75} 13 minValue={0} 14 maxValue={100} 15 fillOffset={50} 16 /> 17</View>

Start and End Content

Add decorative or informative elements like icons or text at the beginning and end of the slider using startContent and endContent.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider 3 label="Volume" 4 defaultValue={40} 5 startContent={<Icon name="volume-mute" size={20} color="gray" />} 6 endContent={<Icon name="volume-high" size={20} color="gray" />} 7 /> 8 <Slider 9 label="Rating" 10 defaultValue={3} 11 minValue={1} 12 maxValue={5} 13 step={1} 14 startContent={<Text style={{fontSize: 12, color: 'gray'}}>Min</Text>} 15 endContent={<Text style={{fontSize: 12, color: 'gray'}}>Max</Text>} 16 /> 17</View>

Disabled State

Disable the slider interaction using the isDisabled prop.

MyComponent.tsx
1<Slider 2 defaultValue={50} 3 label="Disabled Slider" 4 isDisabled 5/>

Thumb Customization

Customize the thumb's appearance and behavior with hideThumb, showOutline, and disableThumbScale.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Slider 3 defaultValue={50} 4 label="Hidden Thumb" 5 hideThumb 6 /> 7 <Slider 8 defaultValue={60} 9 label="Thumb with Outline" 10 showOutline 11 /> 12 <Slider 13 defaultValue={70} 14 label="Thumb Scale Animation Disabled" 15 disableThumbScale 16 /> 17</View>

Props Overview

PROPTYPEDEFAULTDESCRIPTION
labelReactNode-Label for the slider.
sizeSliderSize'md'Size of the slider.
colorSliderColor'primary'Thematic color.
radiusSliderRadius'md'Border radius.
stepnumber1Increment value.
valuenumber-Controlled value.
defaultValuenumber0Uncontrolled initial value.
onChange(val: number) => void-Callback on value change.
onChangeEnd(val: number) => void-Callback on gesture end.
minValuenumber0Minimum value.
maxValuenumber100Maximum value.
orientationSliderOrientation'horizontal'Orientation.
fillOffsetnumberminValueStart point for the fill.
showStepsbooleanfalseShow step indicators.
showTooltipbooleanfalseShow tooltip on drag.
marksSliderMark[]-Marks to display on the track.
startContentReactNode-Content at the start of the slider.
endContentReactNode-Content at the end of the slider.
formatOptionsIntl.NumberFormatOptions-Options for formatting the displayed value.
tooltipPropsany-Props for the tooltip (e.g., style).
showOutlinebooleanfalseShow outline around thumb.
hideValuebooleanfalseHide displayed value.
hideThumbbooleanfalseHide the thumb.
disableThumbScalebooleanfalseDisable thumb scale animation.
isDisabledbooleanfalseDisable the slider.
stylesSliderSlotsStyles-Custom styles for slider slots.
styleStyleProp<ViewStyle>-Style for the base container.

Styling

Customize the Slider appearance using the style prop for the main wrapper or the styles prop for more granular control over internal slots:

  • style: Applied to the outermost container (View) of the entire slider component.
  • styles.base: Styles for the outermost container.
  • styles.label: Styles for the label text.
  • styles.sliderWrapper: Styles for the container of the track, thumb, and content.
  • styles.track: Styles for the slider track.
  • styles.filler: Styles for the filled part of the track.
  • styles.thumb: Styles for the draggable thumb.
  • styles.thumbOutline: Styles for the thumb's outline.
  • styles.step: Styles for step indicators.
  • styles.markLabel: Styles for mark labels.
  • styles.markIndicator: Styles for mark indicator lines.
  • styles.tooltip: Styles for the tooltip.
  • styles.tooltipText: Styles for text inside the tooltip.
  • styles.startContentWrapper: Styles for the start content container.
  • styles.endContentWrapper: Styles for the end content container.
  • styles.valueText: Styles for the displayed value text.
MyComponent.tsx
1<Slider 2 label="Custom Styled Slider" 3 defaultValue={45} 4 style={{ marginVertical: 20, padding: 10, backgroundColor: '#e6e6fa' }} // Styles the outermost 'base' container 5 styles={{ 6 base: { borderWidth: 1, borderColor: 'purple' }, 7 label: { color: 'navy', fontWeight: 'bold', fontStyle: 'italic' }, 8 sliderWrapper: { marginTop: 15 }, 9 track: { backgroundColor: 'lightgray', height: 10 }, 10 filler: { backgroundColor: 'teal' }, 11 thumb: { 12 width: 28, 13 height: 28, 14 borderRadius: 6, // Square-ish thumb 15 backgroundColor: 'darkorange', 16 borderWidth: 2, 17 borderColor: 'maroon' 18 }, 19 valueText: { color: 'green', fontSize: 16 }, 20 tooltip: { backgroundColor: 'black', borderRadius: 8 }, 21 tooltipText: { color: 'white', fontWeight: 'bold' } 22 }} 23 showTooltip 24/>

Accessibility

The Slider component aims to be accessible:

  • The component is built using GestureDetector for touch interactions.
  • The label prop provides context for the slider.
  • When isDisabled, interactions are prevented and opacity is reduced.
  • Consider providing accessibilityLabel, accessibilityValue, and accessibilityHint props directly to the root View or via the style/styles.base if further customization is needed for screen readers, although these are not explicitly handled by the component itself. Standard React Native accessibility props can be applied to the root element.