Amplify UI

SliderField

SliderField allows users to select a value within a defined range.

Demo

Usage

Import the SliderField component.

import { SliderField } from '@aws-amplify/ui-react';

export const DefaultSliderFieldExample = () => {
  return <SliderField label="Default slider" />;
};

Controlled component

To use the SliderField as a controlled component, handle the current value using the value and onChange props.

import { SliderField } from '@aws-amplify/ui-react';
import * as React from 'react';

export const ControlledSliderFieldExample = () => {
  const [value, setValue] = React.useState(50);

  return (
    <SliderField label="Controlled slider" value={value} onChange={setValue} />
  );
};

Basics

To control the range of the SliderField, use the min and max props. To control the interval between selectable values, use the step prop (defaults to 1). You may also set a defaultValue.

import { SliderField } from '@aws-amplify/ui-react';

export const SliderFieldBasicsExample = () => {
  return (
    <SliderField
      label="Select your favorite odd number between 1 and 9"
      min={1}
      max={9}
      step={2}
      defaultValue={5}
    />
  );
};

Accessibility / Label behavior

You can hide the value to the right of the label with isValueHidden prop.

import { SliderField, Flex, Button } from '@aws-amplify/ui-react';
import * as React from 'react';

export const SliderFieldAccessibilityExample = () => {
  const [labelHidden, setLabelHidden] = React.useState(false);
  const [isValueHidden, setIsValueHidden] = React.useState(false);

  return (
    <>
      <SliderField
        label="Accessibility demo"
        labelHidden={labelHidden}
        isValueHidden={isValueHidden}
        defaultValue={50}
      />
      <Flex>
        <Button onClick={() => setLabelHidden(!labelHidden)}>
          {labelHidden ? 'Show label' : 'Hide label'}
        </Button>
        <Button onClick={() => setIsValueHidden(!isValueHidden)}>
          {isValueHidden ? 'Show value' : 'Hide value'}
        </Button>
      </Flex>
    </>
  );
};

The SliderField will programmatically update the value of aria-valuenow in response to user input. However, a slider sometimes is used to choose a value that is not, semantically, a number. In these cases, the ariaValuetext attribute is used to provide the appropriate text name for the currently selected value. See MDN using the slider role.

Neutral

import { SliderField, Text } from '@aws-amplify/ui-react';
import * as React from 'react';

const userFeedback = [
  'Strongly disagree',
  'Disagree',
  'Neutral',
  'Agree',
  'Strongly agree',
];

export const SliderFieldAriaExample = () => {
  const [index, setIndex] = React.useState(2);

  return (
    <>
      <SliderField
        ariaValuetext={userFeedback[index]}
        label="I tend to be more introverted."
        value={index}
        onChange={setIndex}
        max={4}
        isValueHidden
      />
      <Text>{userFeedback[index]}</Text>
    </>
  );
};

Forms

To use the SliderField in a form, pass a name prop to the SliderField to access its current value. To disable the SliderField, set the isDisabled prop. A disabled SliderField will not be focusable, mutable, or submit a value with form data.

import { SliderField, Button, Flex } from '@aws-amplify/ui-react';
import * as React from 'react';

export const SliderFieldFormExample = () => {
  const [value, setValue] = React.useState(5);
  const [isDisabled, setIsDisabled] = React.useState(false);

  const handleOnSubmit = (event) => {
    event.preventDefault();
    alert(`Waterslide enjoyment: ${event.target.waterslides.value}`);
  };

  return (
    <form onSubmit={handleOnSubmit}>
      <SliderField
        label="On a scale from 1-10, how much do you enjoy waterslides?"
        name="waterslides"
        min={1}
        max={10}
        value={value}
        onChange={setValue}
        isDisabled={isDisabled}
      />
      <Flex>
        <Button type="submit">Submit</Button>
        <Button onClick={() => setIsDisabled(!isDisabled)}>
          {isDisabled ? 'Enable SelectField' : 'Disable SelectField'}
        </Button>
      </Flex>
    </form>
  );
};

Orientation

To change the orientation from horizontal (default) to vertical, use the orientation prop.

import { SliderField } from '@aws-amplify/ui-react';

export const SliderFieldOrientationExample = () => {
  return (
    <SliderField
      label="How tall is your cat (in inches)?"
      orientation="vertical"
      min={3}
      max={20}
      defaultValue={9}
    />
  );
};

Icons

To add icons on either side of the SliderField, you may use the outerStartComponent or outerEndComponent props.

import { SliderField, Icon } from '@aws-amplify/ui-react';

export const SliderFieldIconsExample = () => {
  return (
    <SliderField
      label="Volume slider"
      outerStartComponent={
        <Icon
          ariaLabel="volume-down-icon"
          pathData="M16 7.97V16.02C17.48 15.29 18.5 13.77 18.5 12C18.5 10.23 17.48 8.71 16 7.97ZM5 9V15H9L14 20V4L9 9H5ZM12 8.83V15.17L9.83 13H7V11H9.83L12 8.83Z"
        />
      }
      outerEndComponent={
        <Icon
          ariaLabel="volume-up-icon"
          pathData="M3 8.99998V15H7L12 20V3.99998L7 8.99998H3ZM10 8.82998V15.17L7.83 13H5V11H7.83L10 8.82998ZM16.5 12C16.5 10.23 15.48 8.70998 14 7.96998V16.02C15.48 15.29 16.5 13.77 16.5 12ZM14 3.22998V5.28998C16.89 6.14998 19 8.82998 19 12C19 15.17 16.89 17.85 14 18.71V20.77C18.01 19.86 21 16.28 21 12C21 7.71998 18.01 4.13998 14 3.22998V3.22998Z"
        />
      }
      defaultValue={50}
      size="large"
    />
  );
};

Format value

To format how the value is displayed, you can pass in a render function to formatValue prop.

import { SliderField } from '@aws-amplify/ui-react';

export const SliderFieldFormatValueExample = () => {
  const formatValue = (value: number) => {
    return `${value}%`;
  };
  return (
    <SliderField
      label="SliderField with formatted value"
      defaultValue={50}
      formatValue={formatValue}
    />
  );
};

Validation error

To validate the SliderField input, use the hasError and errorMessage props.

You must be at least 18 years old.

import { SliderField } from '@aws-amplify/ui-react';
import * as React from 'react';

export const SliderFieldValidationExample = () => {
  const [age, setAge] = React.useState(13);

  return (
    <SliderField
      label="Select your age"
      value={age}
      onChange={setAge}
      hasError={age < 18}
      errorMessage="You must be at least 18 years old."
    />
  );
};

CSS Styling

Theme

You can customize the appearance of all SliderField components in your application with a Theme.

SliderField Theme Source

import { SliderField, ThemeProvider, Theme } from '@aws-amplify/ui-react';

const theme: Theme = {
  name: 'slider-theme',
  tokens: {
    components: {
      sliderfield: {
        thumb: {
          width: { value: '{space.xl}' },
          height: { value: '{space.xl}' },
          backgroundColor: { value: '{colors.neutral.90}' },
          borderRadius: { value: '{radii.medium}' },
          _hover: {
            backgroundColor: { value: '{colors.neutral.80}' },
            borderColor: { value: '{colors.neutral.90}' },
          },
          _focus: {
            borderColor: { value: '{colors.green.80}' },
            boxShadow: {
              // @ts-ignore //IGNORE
              value: {
                spreadRadius: '3px',
                color: '{colors.green.20}',
              },
            },
          },
        },
        track: {
          backgroundColor: {
            value: '{colors.blue.20}',
          },
          height: { value: '{fontSizes.medium}' },
        },
        range: {
          backgroundColor: { value: '{colors.blue.80}' },
        },
      },
    },
  },
};

export const SliderFieldThemeExample = () => {
  return (
    <ThemeProvider theme={theme} colorMode="light">
      <SliderField label="Themed Slider" defaultValue={50} labelHidden />
    </ThemeProvider>
  );
};

Target classes

ClassDescription
amplify-sliderfieldTop level element that wraps the SliderField primitive
amplify-sliderfield__groupClass applied to the element that wraps the slider root
amplify-sliderfield__labelClass applied to the slider label
amplify-sliderfield__rangeClass applied to the filled in portion of the slider track
amplify-sliderfield__rootClass applied to the slider root which wraps the track and thumb
amplify-sliderfield__thumbClass applied to the slider thumb
amplify-sliderfield__trackClass applied to the slider track
  • --amplify-components-sliderfield-large-thumb-height
  • --amplify-components-sliderfield-large-thumb-width
  • --amplify-components-sliderfield-large-track-height
  • --amplify-components-sliderfield-padding-block
  • --amplify-components-sliderfield-range-background-color
  • --amplify-components-sliderfield-range-border-radius
  • --amplify-components-sliderfield-range-disabled-background-color
  • --amplify-components-sliderfield-small-thumb-height
  • --amplify-components-sliderfield-small-thumb-width
  • --amplify-components-sliderfield-small-track-height
  • --amplify-components-sliderfield-thumb-background-color
  • --amplify-components-sliderfield-thumb-border-color
  • --amplify-components-sliderfield-thumb-border-radius
  • --amplify-components-sliderfield-thumb-border-style
  • --amplify-components-sliderfield-thumb-border-width
  • --amplify-components-sliderfield-thumb-box-shadow
  • --amplify-components-sliderfield-thumb-disabled-background-color
  • --amplify-components-sliderfield-thumb-disabled-border-color
  • --amplify-components-sliderfield-thumb-disabled-box-shadow
  • --amplify-components-sliderfield-thumb-focus-border-color
  • --amplify-components-sliderfield-thumb-focus-box-shadow
  • --amplify-components-sliderfield-thumb-height
  • --amplify-components-sliderfield-thumb-hover-background-color
  • --amplify-components-sliderfield-thumb-hover-border-color
  • --amplify-components-sliderfield-thumb-width
  • --amplify-components-sliderfield-track-background-color
  • --amplify-components-sliderfield-track-border-radius
  • --amplify-components-sliderfield-track-height
  • --amplify-components-sliderfield-track-min-width

Global styling

To override styling on all SliderField components, you can set the Amplify CSS variables or use the target classes.

/* styles.css */
.amplify-sliderfield {
  --amplify-components-sliderfield-range-background-color: var(
    --amplify-colors-orange-60
  );
}

Local styling

To override styling on a specific SliderField, you can use class selectors or style props.

Using a class selector:

Note that .amplify-sliderfield__range applies to the filled-in portion of the SliderField track, and .amplify-sliderfield__track applies to the empty portion.

/* styles.css */
.custom-slider .amplify-sliderfield__track {
  background-color: var(--amplify-colors-purple-80);
}
import { SliderField } from '@aws-amplify/ui-react';
import './styles.css';

<SliderField
  className="custom-slider"
  label="Classname Slider"
  defaultValue={50}
/>;

Using style props:

There are several props you can use to style different parts of the SliderField:

  • filledTrackColor applies to the filled-in part of the SliderField
  • emptyTrackColor applies to the empty part of the SliderField
  • thumbColor applies to the thumb component that users can slide
  • trackSize applies to the width of the track itself (e.g., 15px)
  • size applies to the overall size of the SliderField, including the thumb (options include 'small', 'large', and default)
import { SliderField, useTheme } from '@aws-amplify/ui-react';

export const SliderFieldStylePropsExample = () => {
  const { tokens } = useTheme();

  return (
    <SliderField
      label="Style Props Slider"
      filledTrackColor={tokens.colors.green[80]}
      emptyTrackColor={tokens.colors.green[20]}
      thumbColor={tokens.colors.red[60]}
      trackSize="15px"
      defaultValue={50}
    />
  );
};

Amplify open source software, documentation and community are supported by Amazon Web Services.

© 2024 Amazon Web Services, Inc. and its affiliates. All rights reserved. View the site terms and privacy policy.

Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.