UNPKG

8.04 kBTypeScriptView Raw
1import * as React from 'react';
2export interface UseSliderParameters {
3 /**
4 * The id of the element containing a label for the slider.
5 */
6 'aria-labelledby'?: string;
7 /**
8 * The default value. Use when the component is not controlled.
9 */
10 defaultValue?: number | ReadonlyArray<number>;
11 /**
12 * If `true`, the component is disabled.
13 * @default false
14 */
15 disabled?: boolean;
16 /**
17 * If `true`, the active thumb doesn't swap when moving pointer over a thumb while dragging another thumb.
18 * @default false
19 */
20 disableSwap?: boolean;
21 /**
22 * If `true` the Slider will be rendered right-to-left (with the lowest value on the right-hand side).
23 * @default false
24 */
25 isRtl?: boolean;
26 /**
27 * Marks indicate predetermined values to which the user can move the slider.
28 * If `true` the marks are spaced according the value of the `step` prop.
29 * If an array, it should contain objects with `value` and an optional `label` keys.
30 * @default false
31 */
32 marks?: boolean | ReadonlyArray<Mark>;
33 /**
34 * The maximum allowed value of the slider.
35 * Should not be equal to min.
36 * @default 100
37 */
38 max?: number;
39 /**
40 * The minimum allowed value of the slider.
41 * Should not be equal to max.
42 * @default 0
43 */
44 min?: number;
45 /**
46 * Name attribute of the hidden `input` element.
47 */
48 name?: string;
49 /**
50 * Callback function that is fired when the slider's value changed.
51 *
52 * @param {Event} event The event source of the callback.
53 * You can pull out the new value by accessing `event.target.value` (any).
54 * **Warning**: This is a generic event not a change event.
55 * @param {number | number[]} value The new value.
56 * @param {number} activeThumb Index of the currently moved thumb.
57 */
58 onChange?: (event: Event, value: number | number[], activeThumb: number) => void;
59 /**
60 * Callback function that is fired when the `mouseup` is triggered.
61 *
62 * @param {React.SyntheticEvent | Event} event The event source of the callback. **Warning**: This is a generic event not a change event.
63 * @param {number | number[]} value The new value.
64 */
65 onChangeCommitted?: (event: React.SyntheticEvent | Event, value: number | number[]) => void;
66 /**
67 * The component orientation.
68 * @default 'horizontal'
69 */
70 orientation?: 'horizontal' | 'vertical';
71 /**
72 * The ref attached to the root of the Slider.
73 */
74 rootRef?: React.Ref<Element>;
75 /**
76 * A transformation function, to change the scale of the slider.
77 * @param {any} x
78 * @returns {any}
79 * @default function Identity(x) {
80 * return x;
81 * }
82 */
83 scale?: (value: number) => number;
84 /**
85 * The granularity with which the slider can step through values when using Page Up/Page Down or Shift + Arrow Up/Arrow Down.
86 * @default 10
87 */
88 shiftStep?: number;
89 /**
90 * The granularity with which the slider can step through values. (A "discrete" slider.)
91 * The `min` prop serves as the origin for the valid values.
92 * We recommend (max - min) to be evenly divisible by the step.
93 *
94 * When step is `null`, the thumb can only be slid onto marks provided with the `marks` prop.
95 * @default 1
96 */
97 step?: number | null;
98 /**
99 * Tab index attribute of the hidden `input` element.
100 */
101 tabIndex?: number;
102 /**
103 * The value of the slider.
104 * For ranged sliders, provide an array with two values.
105 */
106 value?: number | ReadonlyArray<number>;
107}
108export interface Mark {
109 value: number;
110 label?: React.ReactNode;
111}
112export type UseSliderRootSlotOwnProps = {
113 onMouseDown: React.MouseEventHandler;
114 ref: React.RefCallback<Element> | null;
115};
116export type UseSliderRootSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseSliderRootSlotOwnProps> & UseSliderRootSlotOwnProps;
117export type UseSliderThumbSlotOwnProps = {
118 onMouseLeave: React.MouseEventHandler;
119 onMouseOver: React.MouseEventHandler;
120};
121export type UseSliderThumbSlotProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseSliderThumbSlotOwnProps> & UseSliderThumbSlotOwnProps;
122export type UseSliderHiddenInputOwnProps = {
123 'aria-labelledby'?: string;
124 'aria-orientation'?: React.AriaAttributes['aria-orientation'];
125 'aria-valuemax'?: React.AriaAttributes['aria-valuemax'];
126 'aria-valuemin'?: React.AriaAttributes['aria-valuemin'];
127 disabled: boolean;
128 name?: string;
129 onBlur: React.FocusEventHandler;
130 onChange: React.ChangeEventHandler;
131 onFocus: React.FocusEventHandler;
132 step?: number | 'any';
133 style: React.CSSProperties;
134 tabIndex?: number;
135 type?: React.InputHTMLAttributes<HTMLInputElement>['type'];
136};
137export type UseSliderHiddenInputProps<ExternalProps = {}> = Omit<ExternalProps, keyof UseSliderHiddenInputOwnProps> & UseSliderHiddenInputOwnProps;
138export type Axis = 'horizontal' | 'vertical' | 'horizontal-reverse';
139export interface AxisProps<T extends Axis> {
140 offset: (percent: number) => T extends 'horizontal' ? {
141 left: string;
142 } : T extends 'vertical' ? {
143 bottom: string;
144 } : T extends 'horizontal-reverse' ? {
145 right: string;
146 } : never;
147 leap: (percent: number) => T extends 'horizontal' | 'horizontal-reverse' ? {
148 width: string;
149 } : T extends 'vertical' ? {
150 height: string;
151 } : never;
152}
153export interface UseSliderReturnValue {
154 /**
155 * The active index of the slider.
156 */
157 active: number;
158 /**
159 * The orientation of the slider.
160 */
161 axis: Axis;
162 /**
163 * Returns the `offset` and `leap` methods to calculate the positioning styles based on the slider axis.
164 */
165 axisProps: {
166 [key in Axis]: AxisProps<key>;
167 };
168 /**
169 * If `true`, the slider is being dragged.
170 */
171 dragging: boolean;
172 /**
173 * The index of the thumb which is focused on the slider.
174 */
175 focusedThumbIndex: number;
176 /**
177 * Resolver for the hidden input slot's props.
178 * @param externalProps props for the hidden input slot
179 * @returns props that should be spread on the hidden input slot
180 */
181 getHiddenInputProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSliderHiddenInputProps<ExternalProps>;
182 /**
183 * Resolver for the root slot's props.
184 * @param externalProps props for the root slot
185 * @returns props that should be spread on the root slot
186 */
187 getRootProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSliderRootSlotProps<ExternalProps>;
188 /**
189 * Resolver for the thumb slot's props.
190 * @param externalProps props for the thumb slot
191 * @returns props that should be spread on the thumb slot
192 */
193 getThumbProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseSliderThumbSlotProps<ExternalProps>;
194 /**
195 * Resolver for the thumb slot's style prop.
196 * @param index of the currently moved thumb
197 * @returns props that should be spread on the style prop of thumb slot
198 */
199 getThumbStyle: (index: number) => object;
200 /**
201 * The marks of the slider. Marks indicate predetermined values to which the user can move the slider.
202 */
203 marks: Mark[];
204 /**
205 * The thumb index for the current value when in hover state.
206 */
207 open: number;
208 /**
209 * If `true`, the slider is a range slider when the `value` prop passed is an array.
210 */
211 range: boolean;
212 /**
213 * Ref to the root slot's DOM node.
214 */
215 rootRef: React.RefCallback<Element> | null;
216 /**
217 * The track leap for the current value of the slider.
218 */
219 trackLeap: number;
220 /**
221 * The track offset for the current value of the slider.
222 */
223 trackOffset: number;
224 /**
225 * The possible values of the slider.
226 */
227 values: number[];
228}