/
types.ts
271 lines (242 loc) · 6.41 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Dispatch, HTMLAttributes, ReactNode, SetStateAction } from "react";
import { PropsWithRef } from "@react-md/utils";
/**
* @remarks \@since 2.5.0
*/
export type ThumbIndex = 0 | 1;
/**
* @remarks \@since 2.5.0
*/
export type SliderThumbIndex = ThumbIndex | null;
/**
* @remarks \@since 2.5.0
*/
export type SliderDragEvent =
| MouseEvent
| TouchEvent
| React.MouseEvent
| React.TouchEvent;
/**
* @remarks \@since 2.5.0
*/
export type SliderDraggingBy = "mouse" | "touch" | null;
/**
* @remarks \@since 2.5.0
*/
export type SliderValue = number;
/**
* @remarks \@since 2.5.0
*/
export type SliderDefaultValue = SliderValue | (() => SliderValue);
/**
* An object containing the functions required to update the `Slider`'s value
* when the user interacts with the slider. These functions are provided by the
* `useSlider` hook.
*
* @remarks \@since 2.5.0
* @internal
*/
export interface SliderControls {
persist(): void;
minimum(): void;
maximum(): void;
increment(): void;
incrementJump(): void;
decrement(): void;
decrementJump(): void;
setValue: Dispatch<SetStateAction<SliderValue>>;
}
/**
* @remarks \@since 2.5.0
*/
export type RangeSliderValue = readonly [number, number];
/**
* @remarks \@since 2.5.0
*/
export type RangeSliderDefaultValue =
| RangeSliderValue
| (() => RangeSliderValue);
/**
* An object containing the functions required to update the `RangeSlider`'s
* value when the user interacts with the slider. These functions are provided by
* the `useRangeSlider` hook.
*
* @remarks \@since 2.5.0
* @internal
*/
export interface RangeSliderControls {
persist(): void;
minimum(index: ThumbIndex): void;
maximum(index: ThumbIndex): void;
increment(index: ThumbIndex): void;
incrementJump(index: ThumbIndex): void;
decrement(index: ThumbIndex): void;
decrementJump(index: ThumbIndex): void;
setValue: Dispatch<SetStateAction<RangeSliderValue>>;
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderValueOptions {
/**
* The min value for the slider.
*/
min?: number;
/**
* The max value for the slider.
*/
max?: number;
/**
* A positive number representing the value to "jump" while incrementing or
* decrementing the slider's value. This should normally stay as the default
* value of `1`, but can also be decimal values if needed.
*/
step?: number;
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderStepOptions extends SliderValueOptions {
/**
* An optional amount to jump by when using the `PageUp` or `PageDown` keys.
* When this is omitted, it will try to default to 10% of the full range to
* the nearest step
*/
jump?: number;
/**
* Determines when the `value` should be updated from the `useSlider` and
* `useRangeSlider` hooks. When this is set to `"change"`, the `value` will
* update immediately as the user interacts with the slider. When this is set
* to `"blur"`, the `value` will only be updated once the user has tabbed away
* from the slider or completed the drag via mouse/touch.
*
* It is recommended to set this to `"blur"` when the value does not need to
* be used immediately.
*/
updateOn?: "change" | "blur";
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderPresentation {
/**
* Boolean if the slider is rendered vertically instead of horizontally.
*/
vertical?: boolean;
/**
* Boolean if the slider is disabled and the values cannot be changed.
*/
disabled?: boolean;
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderThumbPresentation {
/**
* Boolean if the slider should act as a discrete slider which will render a
* tooltip above the thumb while dragging to visualize the current value for
* the slider.
*/
discrete?: boolean;
/**
* The duration that it takes for the slider animation to complete for a new
* value. This is just used to help make things look smoother while dragging
* compared to jumping to new values.
*/
animationDuration?: number;
/**
* A function that is used to help with accessibility by creating a better
* value string if just a number isn't representative enough of your range.
*
* Example:
*
* ```tsx
* const [value, controls] = useSlider(0, {
* // format to be `$100`, etc
* getValueText: value => `$${value}`,
* });
*
* return <Slider baseId="price-slider" label="Price" {...controls} />;
* ```
*/
getValueText?(value: number): string;
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderThumbOptions
extends Omit<SliderValueOptions, "step">,
SliderPresentation,
SliderThumbPresentation {}
/**
* @remarks \@since 2.5.0
*/
export type DefinedSliderValueOptions = Required<SliderValueOptions>;
/**
* @remarks \@since 2.5.0
*/
export type SliderEventHandlerNames =
| "onBlur"
| "onKeyDown"
| "onMouseDown"
| "onTouchStart";
/**
* @remarks \@since 2.5.0
*/
export type SliderEventHandlers = Pick<
HTMLAttributes<HTMLSpanElement>,
SliderEventHandlerNames
>;
/**
* @remarks \@since 2.5.0
*/
export interface SliderAddons {
/**
* An optional addon to render before the slider's track. This can be anything
* and will be wrapped in the `TextIconSpacing` component from
* `@react-md/icon`.
*
* When the `vertical` prop is enabled, the addon will appear underneath the
* track.
*/
beforeAddon?: ReactNode;
/**
* An optional addon to render after the slider's track. This can be anything
* and will be wrapped in the `TextIconSpacing` component from
* `@react-md/icon`.
*
* When the `vertical` prop is enabled, the addon will appear above the track.
*/
afterAddon?: ReactNode;
}
/**
* These are the shared and common props required for both the `Slider` and
* `RangeSlider` components.
*
* @remarks \@since 2.5.0
*/
export interface BaseSliderProps
extends HTMLAttributes<HTMLDivElement>,
SliderAddons,
SliderLabelProps,
SliderPresentation,
SliderThumbPresentation {
/**
* An id for the slider and different parts which is required for a11y.
*/
baseId: string;
}
/**
* @remarks \@since 2.5.0
*/
export interface SliderLabelProps {
/**
* An optional label to display with the slider. This should normally be a
* short (1-4 word) description for this slider.
*/
label?: ReactNode;
/**
* Optional props to pass to the component wrapping the `label` content.
*/
labelProps?: PropsWithRef<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
}