useColorSliderState
Provides state management for a color slider component. Color sliders allow users to adjust an individual channel of a color value.
| install | yarn add @react-stately/color |
|---|---|
| version | 3.0.0-beta.4 |
| usage | import {useColorSliderState} from '@react-stately/color' |
API#
useColorSliderState(
(props: ColorSliderStateOptions
)): ColorSliderStateInterface#
Properties
| Name | Type | Description |
value | Color | The current color value represented by the color slider. |
values | number[] | Values managed by the slider by thumb index. |
focusedThumb | number | undefined | Currently-focused thumb index. |
step | number | The step amount for the slider. |
Methods
| Method | Description |
setValue(
(value: string
| | Color
)): void | Sets the current color value. If a string is passed, it will be parsed to a Color. |
getDisplayColor(): Color | Returns the color that should be displayed in the slider instead of value or the optional parameter. |
getThumbValue(
(index: number
)): number | Get the value for the specified thumb. |
setThumbValue(
(index: number,
, value: number
)): void | Sets the value for the specified thumb. The actual value set will be clamped and rounded according to min/max/step. |
setThumbPercent(
(index: number,
, percent: number
)): void | Sets value for the specified thumb by percent offset (between 0 and 1). |
isThumbDragging(
(index: number
)): boolean | Whether the specific thumb is being dragged. |
setThumbDragging(
(index: number,
, dragging: boolean
)): void | Set is dragging on the specified thumb. |
setFocusedThumb(
(index: number
| | undefined
)): void | Set focused true on specified thumb. This will remove focus from any thumb that had it before. |
getThumbPercent(
(index: number
)): number | Returns the specified thumb's value as a percentage from 0 to 1. |
getValuePercent(
(value: number
)): number | Returns the value as a percent between the min and max of the slider. |
getThumbValueLabel(
(index: number
)): string | Returns the string label for the specified thumb's value, per props.formatOptions. |
getFormattedValue(
(value: number
)): string | Returns the string label for the value, per props.formatOptions. |
getThumbMinValue(
(index: number
)): number | Returns the min allowed value for the specified thumb. |
getThumbMaxValue(
(index: number
)): number | Returns the max allowed value for the specified thumb. |
getPercentValue(
(percent: number
)): number | Converts a percent along track (between 0 and 1) to the corresponding value. |
isThumbEditable(
(index: number
)): boolean | Returns if the specified thumb is editable. |
setThumbEditable(
(index: number,
, editable: boolean
)): void | Set the specified thumb's editable state. |
Example#
See the docs for useColorSlider in react-aria for an example of useColorSliderState.
| Name | Type | Default | Description |
locale | string | — | The locale to use for formatting the color channel value. |
channel | ColorChannel | — | The color channel that the slider manipulates. |
onChange | (
(value: Color
)) => void | — | Handler that is called when the value changes, as the user drags. |
onChangeEnd | (
(value: Color
)) => void | — | Handler that is called when the user stops dragging. |
orientation | Orientation | 'horizontal' | The orientation of the Slider. |
isDisabled | boolean | — | Whether the whole Slider is disabled. |
step | number | 1 | The slider's step value. |
value | string | Color | — | The current value (controlled). |
defaultValue | string | Color | — | The default value (uncontrolled). |
label | ReactNode | — | The content to display as the label. |
A list of color channels.
'hue'
| 'saturation'
| 'brightness'
| 'lightness'
| 'red'
| 'green'
| 'blue'
| 'alpha'Represents a color value.
| Method | Description |
toFormat(
(format: ColorFormat
)): Color | Converts the color to the given color format, and returns a new Color object. |
toHexInt(): number | Converts the color to hex, and returns an integer representation. |
getChannelValue(
(channel: ColorChannel
)): number | Returns the numeric value for a given channel. Throws an error if the channel is unsupported in the current color format. |
withChannelValue(
(channel: ColorChannel,
, value: number
)): Color | Sets the numeric value for a given channel, and returns a new Color object. Throws an error if the channel is unsupported in the current color format. |
getChannelRange(
(channel: ColorChannel
)): ColorChannelRange | Returns the minimum, maximum, and step values for a given channel. |
getChannelName(
(channel: ColorChannel,
, locale: string
)): string | Returns a localized color channel name for a given channel and locale, for use in visual or accessibility labels. |
formatChannelValue(
(channel: ColorChannel,
, locale: string
)): string | Formats the numeric value for a given channel for display according to the provided locale. |
A list of supported color formats.
'hex'
| 'hexa'
| 'rgb'
| 'rgba'
| 'hsl'
| 'hsla'
| 'hsb'
| 'hsba'| Name | Type | Description |
minValue | number | The minimum value of the color channel. |
maxValue | number | The maximum value of the color channel. |
step | number | The step value of the color channel, used when incrementing and decrementing. |
Properties
| Name | Type | Description |
value | Color | The current color value represented by the color slider. |
values | number[] | Values managed by the slider by thumb index. |
focusedThumb | number | undefined | Currently-focused thumb index. |
step | number | The step amount for the slider. |
Methods
| Method | Description |
setValue(
(value: string
| | Color
)): void | Sets the current color value. If a string is passed, it will be parsed to a Color. |
getDisplayColor(): Color | Returns the color that should be displayed in the slider instead of value or the optional parameter. |
getThumbValue(
(index: number
)): number | Get the value for the specified thumb. |
setThumbValue(
(index: number,
, value: number
)): void | Sets the value for the specified thumb. The actual value set will be clamped and rounded according to min/max/step. |
setThumbPercent(
(index: number,
, percent: number
)): void | Sets value for the specified thumb by percent offset (between 0 and 1). |
isThumbDragging(
(index: number
)): boolean | Whether the specific thumb is being dragged. |
setThumbDragging(
(index: number,
, dragging: boolean
)): void | Set is dragging on the specified thumb. |
setFocusedThumb(
(index: number
| | undefined
)): void | Set focused true on specified thumb. This will remove focus from any thumb that had it before. |
getThumbPercent(
(index: number
)): number | Returns the specified thumb's value as a percentage from 0 to 1. |
getValuePercent(
(value: number
)): number | Returns the value as a percent between the min and max of the slider. |
getThumbValueLabel(
(index: number
)): string | Returns the string label for the specified thumb's value, per props.formatOptions. |
getFormattedValue(
(value: number
)): string | Returns the string label for the value, per props.formatOptions. |
getThumbMinValue(
(index: number
)): number | Returns the min allowed value for the specified thumb. |
getThumbMaxValue(
(index: number
)): number | Returns the max allowed value for the specified thumb. |
getPercentValue(
(percent: number
)): number | Converts a percent along track (between 0 and 1) to the corresponding value. |
isThumbEditable(
(index: number
)): boolean | Returns if the specified thumb is editable. |
setThumbEditable(
(index: number,
, editable: boolean
)): void | Set the specified thumb's editable state. |