Choose a number value within a range.

Importing

The component can be imported via:

import { Slider } from '@customerio/pluma-components/react';

Usage

A PlumaSlider accepts a number value, and two callbacks:

  • onInput - called when the value changes, while dragging the slider
  • onChange - called when the value is "committed", when the slider is released

Additionally, like other form components, the component requires a label (or, otherwise, an ariaLabel or ariaLabelledby).

A simple slider might look like this:

The value is: 10

import { Slider } from '@customerio/pluma-components/react';
import { useState } from 'react'; 

export default function Example() {
	const [value, setValue] = useState(10);

	return <>
		<Slider
			label="Flock size"
			value={value}
			onChange={(value) => {
				setValue(value);
			}}
		/>

		<Paragraph mb="100">
			The value is: {value}
		</Paragraph>
	</>
}

This example demonstrates when the onChange and onInput callbacks are called:

The "onInput" value is: 10

The "onChange" value is: 10

import { Slider } from '@customerio/pluma-components/react';
import { useState } from 'react'; 

export default function Example() {
	const [onInputValue, setOnInputValue] = useState(10);
	const [onChangeValue, setOnChangeValue] = useState(10);

	return <>
			<Slider
				label="Flock size"
				value={onChangeValue}
				onChange={(value) => {
					setOnChangeValue(value);
				}}
				onInput={(value) => {
					setOnInputValue(value);
				}}
			/>

			<Paragraph mb="100">
				The "onInput" value is: {onInputValue}
			</Paragraph>
			<Paragraph mb="100">
				The "onChange" value is: {onChangeValue}
			</Paragraph>
		</>
	}

onInput can be used when we need to update the UI immediately - for example, when there are other elements that depend on this value. onChange (without onInput) can be used when we only need the value later, for example, when saving the state.

Min, max and step

Internally, the component uses an input of type="range", and accepts a minimum, maximum and step value via the minValue, maxValue and step props.

The "onInput" value is: 150

The "onChange" value is: 150

import { Slider } from '@customerio/pluma-components/react';
import { useState } from 'react'; 

export default function Example() {
	const [onInputValue, setOnInputValue] = useState(150);
	const [onChangeValue, setOnChangeValue] = useState(150);

	return <>
		<Slider
			label="Flock size"
			minValue={100}
			maxValue={500}
			step={25}
			value={onChangeValue}
			onChange={(value) => {
				setOnChangeValue(value);
			}}
			onInput={(value) => {
				setOnInputValue(value);
			}}
		/>

		<Paragraph mb="100">
			The "onInput" value is: {onInputValue}
		</Paragraph>
		<Paragraph mb="100">
			The "onChange" value is: {onChangeValue}
		</Paragraph>
	</>
}

Customizing labels

If a string label is not sufficient, a component can be used to render the label.

In React, the label prop accepts any ReactNode.

import { Slider, Icon } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(50);

  return <Slider
    label={<>Custom label <Icon name="campaigns" isInline={true} size="sm" /></>}
    value={value}
    onChange={(value) => {
      setValue(value);
    }}
  />
}

Customizing descriptions

If a string description is not sufficient, a component can be used to render the description.

In React, the description prop accepts any ReactNode.

import { Slider, Link } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(50);

  return <Slider
    ariaLabel="Custom description input"
    description={<>Custom description, <Link href="https://docs.customer.io" isExternal={true} variant="secondary">see docs for details</Link></>}
    value={value}
    onChange={(value) => {
      setValue(value);
    }}
  />
}

Customizing errors

If a string error is not sufficient, a component can be used to render the error.

In React, the error prop accepts any ReactNode.

import { Slider, Link } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(50);

  return <Slider
    ariaLabel="Custom error input"
    error={<>Custom error, <Link href="https://docs.customer.io" isExternal={true} variant="secondary">see docs for help</Link></>}
    value={value}
    onChange={(value) => {
      setValue(value);
    }}
  />
}

API

Element (or elements) that describe the form field. Also used for error messages, as aria-errormessage isn't supported in all assistive technologies: https://a11ysupport.io/tech/aria/aria-errormessage_attribute

String value that labels the form field.

Element (or elements) that label the form field.

A description for the field, providing additional context or hints.

An error message associated with the form field. Boolean: Ember-only. If the error named block is used, this prop can be set to true to indicate an error is present, which will make the error block render.

An optional id to be assigned to the input element. Also used to generate ids for labels and error messages. If one isn't provided, it will be generated.

Whether the form field is disabled.

Whether the form field is in an invalid state.

The label text to show with the form field.

Default:100

The maximum value.

Default:0

The minimum value.

The name attribute is used for HTML forms

Called when the value change is committed (when the slider is let go).

Whether to allow browser autofill and password managers. When false (default), adds attributes to disable password managers (1Password, LastPass, Bitwarden, Dashlane). Set to true for fields where autofill is desired (e.g., email, password, address fields).

Whether the generated description id should be included in the input's aria-describedby attribute. Disable this when a label`` tag surrounds the input as well as it's description text, for example in an OptionCard` component.

Whether the generated label id should be included in the input's aria-labelledby attribute. Disable this when the label tag surrounds the input as well as it's label text, for example in an OptionCard component.

By default, form elements will throw an error if no label or aria-label is provided. Disable this to suppress the error, for example when building a custom form element, and you want to handle labeling yourself.

Default:1

The step value.

Additional classes to be applied to the description element.

Additional classes to be applied to the error element.

Additional classes to be applied to the field node.

Additional classes to be applied to the footer element.

Ember-only: this is used internally to detect whether a label named block is present, and to ignore empty label props if so.

Additional classes to be applied to the input node.

Sets the size attribute on the input element. This controls the visible width of the input in characters.

Additional classes to be applied to the label node.

The current value.

Whether to apply the center-baseline variant.