-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(slider): ♻️ update slider state & track handlers (#278)
- Loading branch information
1 parent
b97630a
commit a12c676
Showing
37 changed files
with
1,524 additions
and
1,615 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { useNumberFormatter } from "@react-aria/i18n"; | ||
import { useSliderState } from "@react-stately/slider"; | ||
|
||
export interface SliderBaseState { | ||
/** | ||
* Values managed by the slider by thumb index. | ||
*/ | ||
values: number[]; | ||
|
||
/** | ||
* Currently-focused thumb index. | ||
*/ | ||
focusedThumb: number | undefined; | ||
|
||
/** | ||
* The slider's step value. | ||
* @default 1 | ||
*/ | ||
step: number; | ||
|
||
/** Whether the whole Slider is disabled. */ | ||
isDisabled: boolean; | ||
} | ||
|
||
export interface SliderBaseAction { | ||
/** | ||
* Get Thumb value based on its index | ||
* @param index | ||
*/ | ||
getThumbValue(index: number): number; | ||
|
||
/** | ||
* Sets the value for the specified thumb. | ||
* The actual value set will be clamped and rounded according to min/max/step. | ||
* @param index | ||
* @param value | ||
*/ | ||
setThumbValue(index: number, value: number): void; | ||
|
||
/** | ||
* Sets value for the specified thumb by percent offset (between 0 and 1). | ||
* @param index | ||
* @param percent | ||
*/ | ||
setThumbPercent(index: number, percent: number): void; | ||
|
||
/** | ||
* Whether the specific thumb is being dragged. | ||
* @param index | ||
*/ | ||
isThumbDragging(index: number): boolean; | ||
|
||
/** | ||
* Set is dragging on the specified thumb. | ||
* @param index | ||
* @param dragging | ||
*/ | ||
setThumbDragging(index: number, dragging: boolean): void; | ||
|
||
/** | ||
* Set focused true on specified thumb. This will remove focus from | ||
* any thumb that had it before. | ||
* @param index | ||
*/ | ||
setFocusedThumb(index: number | undefined): void; | ||
|
||
/** | ||
* Returns the specified thumb's value as a percentage from 0 to 1. | ||
* @param index | ||
*/ | ||
getThumbPercent(index: number): number; | ||
|
||
/** | ||
* Returns the value as a percent between the min and max of the slider. | ||
* @param index | ||
*/ | ||
getValuePercent(value: number): number; | ||
|
||
/** | ||
* Returns the string label for the specified thumb's value, per props.formatOptions. | ||
* @param index | ||
*/ | ||
getThumbValueLabel(index: number): string; | ||
|
||
/** | ||
* Returns the string label for the value, per props.formatOptions. | ||
* @param index | ||
*/ | ||
getFormattedValue(value: number): string; | ||
|
||
/** | ||
* Returns the min allowed value for the specified thumb. | ||
* @param index | ||
*/ | ||
getThumbMinValue(index: number): number; | ||
|
||
/** | ||
* Returns the max allowed value for the specified thumb. | ||
* @param index | ||
*/ | ||
getThumbMaxValue(index: number): number; | ||
|
||
/** | ||
* Converts a percent along track (between 0 and 1) to the corresponding value. | ||
* @param percent | ||
*/ | ||
getPercentValue(percent: number): number; | ||
|
||
/** | ||
* Returns if the specified thumb is editable. | ||
* @param index | ||
*/ | ||
isThumbEditable(index: number): boolean; | ||
|
||
/** | ||
* Set the specified thumb's editable state. | ||
* @param index | ||
* @param editable | ||
*/ | ||
setThumbEditable(index: number, editable: boolean): void; | ||
} | ||
|
||
export type SliderBaseInitialState = Partial< | ||
Pick<SliderBaseState, "step" | "isDisabled"> | ||
> & { | ||
/** The current value (controlled). */ | ||
value?: number[]; | ||
|
||
/** | ||
* The slider's minimum value. | ||
* @default 0 | ||
*/ | ||
minValue?: number; | ||
|
||
/** | ||
* The slider's maximum value. | ||
* @default 100 | ||
*/ | ||
maxValue?: number; | ||
|
||
/** The default value (uncontrolled). */ | ||
defaultValue?: number[]; | ||
|
||
/** Handler that is called when the value changes. */ | ||
onChange?: (value: number[]) => void; | ||
|
||
/** | ||
* Get the value when dragging has ended. | ||
*/ | ||
onChangeEnd?: (value: number[]) => void; | ||
|
||
/** | ||
* The display format of the value label. | ||
*/ | ||
formatOptions?: Intl.NumberFormatOptions; | ||
}; | ||
|
||
export type SliderBaseStateReturn = SliderBaseState & SliderBaseAction; | ||
|
||
export function useSliderBaseState( | ||
props: SliderBaseInitialState = {}, | ||
): SliderBaseStateReturn { | ||
const { isDisabled = false } = props; | ||
|
||
const numberFormatter = useNumberFormatter(props.formatOptions); | ||
const state = useSliderState({ ...props, isDisabled, numberFormatter }); | ||
|
||
return { | ||
...state, | ||
isDisabled, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createComponent, createHook } from "reakit-system"; | ||
import { GroupHTMLProps, GroupOptions, useGroup } from "reakit"; | ||
import { mergeProps } from "@react-aria/utils"; | ||
|
||
import { SLIDER_GROUP_KEYS } from "./__keys"; | ||
import { SliderStateReturn } from "./SliderState"; | ||
|
||
export type SliderGroupOptions = GroupOptions & | ||
Pick<SliderStateReturn, "groupProps">; | ||
|
||
export type SliderGroupHTMLProps = GroupHTMLProps; | ||
|
||
export type SliderGroupProps = SliderGroupOptions & SliderGroupHTMLProps; | ||
|
||
export const useSliderGroup = createHook< | ||
SliderGroupOptions, | ||
SliderGroupHTMLProps | ||
>({ | ||
name: "SliderGroup", | ||
compose: useGroup, | ||
keys: SLIDER_GROUP_KEYS, | ||
|
||
useOptions(options, htmlProps) { | ||
return options; | ||
}, | ||
|
||
useProps(options, htmlProps) { | ||
return mergeProps(options.groupProps, htmlProps); | ||
}, | ||
}); | ||
|
||
export const SliderGroup = createComponent({ | ||
as: "div", | ||
memo: true, | ||
useHook: useSliderGroup, | ||
}); |
Oops, something went wrong.
a12c676
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: