UNPKG

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