Back to React Spectrum

Opacity Slider

packages/react-aria-components/docs/examples/opacity-slider.mdx

2022-12-163.2 KB
Original Source

{/* Copyright 2023 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}

import {ExampleLayout} from '@react-spectrum/docs'; export default ExampleLayout;

import docs from 'docs:react-aria-components'; import {TypeLink} from '@react-spectrum/docs'; import styles from '@react-spectrum/docs/src/docs.css'; import Slider from '@react-spectrum/docs/pages/assets/component-illustrations/Slider.svg'; import {ExampleCard} from '@react-spectrum/docs/src/ExampleCard'; import ChevronRight from '@spectrum-icons/workflow/ChevronRight';


keywords: [example, slider, aria, accessibility, react, component] type: component image: opacity-slider.png description: An opacity slider styled with Tailwind CSS.

Opacity Slider

An opacity Slider styled with Tailwind CSS.

Example

tsx
import './tailwind.global.css';
tsx
import {Slider, Label, SliderOutput, SliderTrack, SliderThumb} from 'react-aria-components';

<div className="bg-linear-to-r from-purple-600 to-pink-600 p-12 rounded-lg flex justify-center">
  <Slider defaultValue={30} className="w-[250px]">
    <div className="flex text-white">
      <Label className="flex-1">Opacity</Label>
      <SliderOutput />
    </div>
    <SliderTrack className="relative w-full h-7">
      {({state}) => <>
        <div className="absolute h-2 top-[50%] translate-y-[-50%] w-full rounded-full bg-white/40" />
        <div className="absolute h-2 top-[50%] translate-y-[-50%] rounded-full bg-white" style={{width: state.getThumbPercent(0) * 100 + '%'}} />
        <SliderThumb className="h-7 w-7 top-[50%] rounded-full border border-solid border-purple-800/75 bg-white transition dragging:bg-purple-100 outline-hidden focus-visible:ring-2 ring-black" />
      </>}
    </SliderTrack>
  </Slider>
</div>

Tailwind config

This example uses the tailwindcss-react-aria-components plugin. When using Tailwind v4, add it to your CSS:

css
@import "tailwindcss";
@plugin "tailwindcss-react-aria-components";
<details> <summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Tailwind v3</summary>

When using Tailwind v3, add the plugin to your tailwind.config.js instead:

tsx
module.exports = {
  // ...
  plugins: [
    require('tailwindcss-react-aria-components')
  ]
};

Note: When using Tailwind v3, install tailwindcss-react-aria-components version 1.x instead of 2.x.

</details>

Components

<section className={styles.cardGroup} data-size="small">

<ExampleCard url="../Slider.html" title="Slider" description="A slider allows a user to select one or more values within a range."> <Slider /> </ExampleCard>

</section>