@mobilestock-native/slider 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +280 -0
- package/index.js +570 -0
- package/package.json +14 -3
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { DefaultTheme } from 'styled-components/native';
|
|
4
|
+
import { ShadedToken } from '@mobilestock-native/tools';
|
|
5
|
+
|
|
6
|
+
declare const markVariantMap: {
|
|
7
|
+
/** @description The default: an 8px dot, surface colour with a subtle border. */
|
|
8
|
+
DOT: {
|
|
9
|
+
width: "0.5rem";
|
|
10
|
+
height: "0.5rem";
|
|
11
|
+
borderRadius: "ROUND";
|
|
12
|
+
fill: "SURFACE";
|
|
13
|
+
};
|
|
14
|
+
/** @description An 8px dot in the primary colour, white inside the active range. */
|
|
15
|
+
FILLED: {
|
|
16
|
+
width: "0.5rem";
|
|
17
|
+
height: "0.5rem";
|
|
18
|
+
borderRadius: "ROUND";
|
|
19
|
+
fill: "PRIMARY_CONTRAST";
|
|
20
|
+
};
|
|
21
|
+
/** @description A 2px by 8px tick, grey outside the active range and white inside it. */
|
|
22
|
+
TICK: {
|
|
23
|
+
width: "0.125rem";
|
|
24
|
+
height: "0.5rem";
|
|
25
|
+
borderRadius: "0.0625rem";
|
|
26
|
+
fill: "MUTED_CONTRAST";
|
|
27
|
+
};
|
|
28
|
+
/** @description A 2px by 16px line crossing the track, grey outside the active range and white inside it. */
|
|
29
|
+
LINE: {
|
|
30
|
+
width: "0.125rem";
|
|
31
|
+
height: "1rem";
|
|
32
|
+
borderRadius: "0.0625rem";
|
|
33
|
+
fill: "MUTED_CONTRAST";
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type SliderMarkVariant = keyof typeof markVariantMap;
|
|
37
|
+
|
|
38
|
+
declare const thumbVariantMap: {
|
|
39
|
+
/** @description The default: a 24px circle, surface colour with a primary border. */
|
|
40
|
+
CIRCLE: {
|
|
41
|
+
width: "1.5rem";
|
|
42
|
+
height: "1.5rem";
|
|
43
|
+
borderRadius: "ROUND";
|
|
44
|
+
fill: "SURFACE";
|
|
45
|
+
};
|
|
46
|
+
/** @description A 24px circle in the primary colour. */
|
|
47
|
+
FILLED: {
|
|
48
|
+
width: "1.5rem";
|
|
49
|
+
height: "1.5rem";
|
|
50
|
+
borderRadius: "ROUND";
|
|
51
|
+
fill: "PRIMARY";
|
|
52
|
+
};
|
|
53
|
+
/** @description A 12px by 28px pill in the primary colour, the handle of the Material and iOS sliders. */
|
|
54
|
+
BAR: {
|
|
55
|
+
width: "0.75rem";
|
|
56
|
+
height: "1.75rem";
|
|
57
|
+
borderRadius: "ROUND";
|
|
58
|
+
fill: "PRIMARY";
|
|
59
|
+
};
|
|
60
|
+
/** @description A 24px rounded square, surface colour with a primary border. */
|
|
61
|
+
SQUARE: {
|
|
62
|
+
width: "1.5rem";
|
|
63
|
+
height: "1.5rem";
|
|
64
|
+
borderRadius: "0.25rem";
|
|
65
|
+
fill: "SURFACE";
|
|
66
|
+
};
|
|
67
|
+
/** @description A 16px dot in the primary colour, for dense screens. */
|
|
68
|
+
DOT: {
|
|
69
|
+
width: "1rem";
|
|
70
|
+
height: "1rem";
|
|
71
|
+
borderRadius: "ROUND";
|
|
72
|
+
fill: "PRIMARY";
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
type SliderThumbVariant = keyof typeof thumbVariantMap;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @description A colour of the track, the thumbs or the marks: a token of `theme.colors.container` with an optional
|
|
79
|
+
* shade, as the `Container` takes (`PRIMARY`, `SUCCESS_LIGHT`, `DANGER_700`).
|
|
80
|
+
*/
|
|
81
|
+
type SliderColor = ShadedToken<Uppercase<keyof DefaultTheme['colors']['container'] & string>>;
|
|
82
|
+
/**
|
|
83
|
+
* @description What `segmentColor` receives for each stretch of track between two consecutive stops: its index, the
|
|
84
|
+
* values at its ends and whether it lies inside the selected range.
|
|
85
|
+
*/
|
|
86
|
+
interface SliderSegmentInfo {
|
|
87
|
+
index: number;
|
|
88
|
+
from: number;
|
|
89
|
+
to: number;
|
|
90
|
+
active: boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @description What a custom mark receives to draw itself: the value it stands on, whether that value is inside the
|
|
94
|
+
* active range, and its index among the marks.
|
|
95
|
+
*/
|
|
96
|
+
interface SliderMarkRenderInfo {
|
|
97
|
+
value: number;
|
|
98
|
+
active: boolean;
|
|
99
|
+
index: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @description The two ends of a range slider, always `[minimum, maximum]` with `minimum <= maximum`.
|
|
103
|
+
*/
|
|
104
|
+
type SliderRange = [number, number];
|
|
105
|
+
/**
|
|
106
|
+
* @description What the slider shows: one number for a single thumb, or `[minimum, maximum]` for two thumbs.
|
|
107
|
+
*/
|
|
108
|
+
type SliderValue = number | SliderRange;
|
|
109
|
+
/**
|
|
110
|
+
* @description One of the thumbs of a range. A slider with one thumb only has the `MAXIMUM` one.
|
|
111
|
+
*/
|
|
112
|
+
type SliderThumb = 'MINIMUM' | 'MAXIMUM';
|
|
113
|
+
/**
|
|
114
|
+
* @description What a custom thumb receives to draw itself: which thumb it is, the value it rests on, and whether it
|
|
115
|
+
* is locked or the slider is disabled.
|
|
116
|
+
*/
|
|
117
|
+
interface SliderThumbRenderInfo {
|
|
118
|
+
kind: SliderThumb;
|
|
119
|
+
value: number;
|
|
120
|
+
locked: boolean;
|
|
121
|
+
disabled: boolean;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @description Keeps `Value` inferred from `value` alone, so a `setState` handed to `onChange` never widens it to
|
|
125
|
+
* the whole union (the built-in `NoInfer` needs TypeScript 5.4).
|
|
126
|
+
*/
|
|
127
|
+
type NoInfer<Type> = [Type][Type extends unknown ? 0 : never];
|
|
128
|
+
interface SliderProps<Value extends SliderValue = SliderValue> {
|
|
129
|
+
/**
|
|
130
|
+
* @description Lowest value of the track.
|
|
131
|
+
* Default: required
|
|
132
|
+
* @example min={8490}
|
|
133
|
+
*/
|
|
134
|
+
min: number;
|
|
135
|
+
/**
|
|
136
|
+
* @description Highest value of the track.
|
|
137
|
+
* Default: required
|
|
138
|
+
* @example max={38900}
|
|
139
|
+
*/
|
|
140
|
+
max: number;
|
|
141
|
+
/**
|
|
142
|
+
* @description Distance between two values a thumb can stop at, counted from `min`. Every value the component
|
|
143
|
+
* delivers is a multiple of it. Ignored when `stops` is given.
|
|
144
|
+
* Default: 1
|
|
145
|
+
* @example step={100}
|
|
146
|
+
*/
|
|
147
|
+
step?: number;
|
|
148
|
+
/**
|
|
149
|
+
* @description The only values a thumb can stop at, in place of the multiples of `step`: the thumb snaps to the
|
|
150
|
+
* closest one, and the keyboard and the screen reader move to the next or the previous. Values outside `[min, max]`
|
|
151
|
+
* are ignored.
|
|
152
|
+
* Default: undefined (the multiples of `step`)
|
|
153
|
+
* @example stops={[0, 5000, 10000, 20000, 50000]}
|
|
154
|
+
*/
|
|
155
|
+
stops?: number[];
|
|
156
|
+
/**
|
|
157
|
+
* @description Current value (controlled component). A number draws one thumb, filled from `min` up to it;
|
|
158
|
+
* `[minimum, maximum]` draws two thumbs with the range between them.
|
|
159
|
+
* Default: required
|
|
160
|
+
* @example value={12000}
|
|
161
|
+
* @example value={[12000, 26000]}
|
|
162
|
+
*/
|
|
163
|
+
value: Value;
|
|
164
|
+
/**
|
|
165
|
+
* @description Called with the new value when the user releases a thumb, or right away for a keyboard or screen
|
|
166
|
+
* reader step. It receives a number or a range, the same shape as `value`.
|
|
167
|
+
* Default: required
|
|
168
|
+
* @example onChange={([minimum, maximum]) => setPriceRange([minimum, maximum])}
|
|
169
|
+
*/
|
|
170
|
+
onChange(value: NoInfer<Value>): void;
|
|
171
|
+
/**
|
|
172
|
+
* @description Called with the value at every move while a thumb is dragged. Pass `value` back through it to keep
|
|
173
|
+
* the labels in step with the thumbs during the drag.
|
|
174
|
+
* Default: undefined
|
|
175
|
+
* @example onChanging={setPriceRange}
|
|
176
|
+
*/
|
|
177
|
+
onChanging?(value: NoInfer<Value>): void;
|
|
178
|
+
/**
|
|
179
|
+
* @description Formats the value shown under each thumb and read by screen readers. Without it no label is shown.
|
|
180
|
+
* Default: undefined (no labels)
|
|
181
|
+
* @example formatLabel={value => formatCurrency(value)}
|
|
182
|
+
*/
|
|
183
|
+
formatLabel?(value: number): string;
|
|
184
|
+
/**
|
|
185
|
+
* @description The look of the thumbs: one of the ready-made variants (`CIRCLE`, `FILLED`, `BAR`, `SQUARE`,
|
|
186
|
+
* `DOT`), or a function that draws your own from the thumb's kind, value and state. A custom thumb is centred in
|
|
187
|
+
* the 44px touch target; give its width in `thumbWidth` so the track math matches it.
|
|
188
|
+
* Default: 'CIRCLE'
|
|
189
|
+
* @example thumb="BAR"
|
|
190
|
+
* @example thumb={({ value }) => <Badge text={String(value)} />}
|
|
191
|
+
*/
|
|
192
|
+
thumb?: SliderThumbVariant | ((info: SliderThumbRenderInfo) => ReactNode);
|
|
193
|
+
/**
|
|
194
|
+
* @description Width of a custom `thumb`, used to keep the thumb inside the track and to place the marks. The
|
|
195
|
+
* ready-made variants bring their own.
|
|
196
|
+
* Default: '1.5rem'
|
|
197
|
+
* @example thumbWidth="2rem"
|
|
198
|
+
*/
|
|
199
|
+
thumbWidth?: `${number}rem`;
|
|
200
|
+
/**
|
|
201
|
+
* @description Colour of the selected stretch of track, of the thumbs and of the marks that follow them.
|
|
202
|
+
* Default: 'PRIMARY'
|
|
203
|
+
* @example activeColor="SUCCESS"
|
|
204
|
+
*/
|
|
205
|
+
activeColor?: SliderColor;
|
|
206
|
+
/**
|
|
207
|
+
* @description Colour of the track outside the selected stretch, and of the border of the `DOT` marks.
|
|
208
|
+
* Default: undefined (the subtle border colour of the theme)
|
|
209
|
+
* @example railColor="MUTED"
|
|
210
|
+
*/
|
|
211
|
+
railColor?: SliderColor;
|
|
212
|
+
/**
|
|
213
|
+
* @description Colours the track stretch by stretch, between consecutive stops: the function receives each segment
|
|
214
|
+
* and returns its colour, or `undefined` to keep `activeColor` inside the range and `railColor` outside it. The
|
|
215
|
+
* colours follow `value`, so pass `onChanging` to update them during the drag. Needs at most 25 stops.
|
|
216
|
+
* Default: undefined (one colour inside the range, one outside)
|
|
217
|
+
* @example segmentColor={({ index }) => (index < 3 ? 'SUCCESS' : index < 6 ? 'NOTICE' : 'DANGER')}
|
|
218
|
+
*/
|
|
219
|
+
segmentColor?(segment: SliderSegmentInfo): SliderColor | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* @description The marks on the track at each value a thumb can stop at: the stops, or `min`, its multiples of
|
|
222
|
+
* `step` and `max`. `true` draws the `DOT` variant, `false` draws none; a variant (`DOT`, `FILLED`, `TICK`, `LINE`)
|
|
223
|
+
* picks a look, and a function draws your own from the mark's value, whether it is inside the active range and its
|
|
224
|
+
* index, centred on the value. With more than 24 marks none is drawn, because they would merge into a dotted line.
|
|
225
|
+
* Default: true
|
|
226
|
+
* @example marks="TICK"
|
|
227
|
+
* @example marks={({ active }) => <Icon name="Circle" size="3XS" color={active ? 'CONTRAST' : 'REGULAR'} />}
|
|
228
|
+
*/
|
|
229
|
+
marks?: boolean | SliderMarkVariant | ((info: SliderMarkRenderInfo) => ReactNode);
|
|
230
|
+
/**
|
|
231
|
+
* @description Dims the slider and ignores every touch, pointer, keyboard and screen reader action.
|
|
232
|
+
* Default: false
|
|
233
|
+
* @example disabled
|
|
234
|
+
*/
|
|
235
|
+
disabled?: boolean;
|
|
236
|
+
/**
|
|
237
|
+
* @description Range only: the thumb that stays where `value` puts it. It is drawn with a muted border, takes no
|
|
238
|
+
* touch, pointer or keyboard, and the other thumb still stops against it.
|
|
239
|
+
* Default: undefined (both thumbs move)
|
|
240
|
+
* @example locked="MINIMUM"
|
|
241
|
+
*/
|
|
242
|
+
locked?: SliderThumb;
|
|
243
|
+
/**
|
|
244
|
+
* @description Accessibility label of the thumb of a single-value slider.
|
|
245
|
+
* Default: 'Valor'
|
|
246
|
+
* @example accessibilityLabel="Quantidade"
|
|
247
|
+
*/
|
|
248
|
+
accessibilityLabel?: string;
|
|
249
|
+
/**
|
|
250
|
+
* @description Accessibility label of the minimum thumb of a range.
|
|
251
|
+
* Default: 'Mínimo'
|
|
252
|
+
* @example minimumAccessibilityLabel="Preço mínimo"
|
|
253
|
+
*/
|
|
254
|
+
minimumAccessibilityLabel?: string;
|
|
255
|
+
/**
|
|
256
|
+
* @description Accessibility label of the maximum thumb of a range.
|
|
257
|
+
* Default: 'Máximo'
|
|
258
|
+
* @example maximumAccessibilityLabel="Preço máximo"
|
|
259
|
+
*/
|
|
260
|
+
maximumAccessibilityLabel?: string;
|
|
261
|
+
/**
|
|
262
|
+
* @description Test identifier of the slider. The track receives `${testID}-track`. With one thumb, the thumb is
|
|
263
|
+
* `${testID}-thumb` and its label `${testID}-label`; with two, the thumbs are `${testID}-minimum` and
|
|
264
|
+
* `${testID}-maximum` and the labels `${testID}-minimum-label` and `${testID}-maximum-label`.
|
|
265
|
+
* Default: 'slider'
|
|
266
|
+
* @example testID="price-range"
|
|
267
|
+
*/
|
|
268
|
+
testID?: string;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* One or two thumbs on one track: a number draws a single thumb filled from `min`, a `[minimum, maximum]` pair draws
|
|
273
|
+
* a range. On Android and iOS each thumb is a pan gesture whose math runs on the UI thread, so the drag follows the
|
|
274
|
+
* finger at the frame rate; the component only comes back to JavaScript to deliver `onChanging` and `onChange`. In
|
|
275
|
+
* the browser the thumbs listen to pointer events and to the keyboard, with the same math. Each step or stop the
|
|
276
|
+
* thumb snaps to gives a short vibration on Android (`Vibration`) and in the browser (`navigator.vibrate`).
|
|
277
|
+
*/
|
|
278
|
+
declare function Slider<Value extends SliderValue>({ min, max, step, stops, value, onChange, onChanging, formatLabel, activeColor, railColor, segmentColor, thumb, thumbWidth, marks, disabled, locked, accessibilityLabel, minimumAccessibilityLabel, maximumAccessibilityLabel, testID }: SliderProps<Value>): react_jsx_runtime.JSX.Element;
|
|
279
|
+
|
|
280
|
+
export { Slider, type SliderColor, type SliderMarkRenderInfo, type SliderMarkVariant, type SliderProps, type SliderRange, type SliderSegmentInfo, type SliderThumb, type SliderThumbRenderInfo, type SliderThumbVariant, type SliderValue };
|
package/index.js
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
|
|
47
|
+
// src/index.tsx
|
|
48
|
+
var src_exports = {};
|
|
49
|
+
__export(src_exports, {
|
|
50
|
+
Slider: () => Slider
|
|
51
|
+
});
|
|
52
|
+
module.exports = __toCommonJS(src_exports);
|
|
53
|
+
|
|
54
|
+
// src/components/Slider/index.tsx
|
|
55
|
+
var import_react = require("react");
|
|
56
|
+
var import_react_native = require("react-native");
|
|
57
|
+
var import_react_native_gesture_handler = require("react-native-gesture-handler");
|
|
58
|
+
var import_react_native_reanimated = __toESM(require("react-native-reanimated"));
|
|
59
|
+
var import_native = require("styled-components/native");
|
|
60
|
+
var import_container = require("@mobilestock-native/container");
|
|
61
|
+
var import_tools = __toESM(require("@mobilestock-native/tools"));
|
|
62
|
+
var import_typography = require("@mobilestock-native/typography");
|
|
63
|
+
|
|
64
|
+
// src/utils/marks.ts
|
|
65
|
+
var markVariantMap = {
|
|
66
|
+
/** @description The default: an 8px dot, surface colour with a subtle border. */
|
|
67
|
+
DOT: { width: "0.5rem", height: "0.5rem", borderRadius: "ROUND", fill: "SURFACE" },
|
|
68
|
+
/** @description An 8px dot in the primary colour, white inside the active range. */
|
|
69
|
+
FILLED: { width: "0.5rem", height: "0.5rem", borderRadius: "ROUND", fill: "PRIMARY_CONTRAST" },
|
|
70
|
+
/** @description A 2px by 8px tick, grey outside the active range and white inside it. */
|
|
71
|
+
TICK: { width: "0.125rem", height: "0.5rem", borderRadius: "0.0625rem", fill: "MUTED_CONTRAST" },
|
|
72
|
+
/** @description A 2px by 16px line crossing the track, grey outside the active range and white inside it. */
|
|
73
|
+
LINE: { width: "0.125rem", height: "1rem", borderRadius: "0.0625rem", fill: "MUTED_CONTRAST" }
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/utils/range.ts
|
|
77
|
+
function thumbValueOf(kind, range) {
|
|
78
|
+
"worklet";
|
|
79
|
+
return kind === "MINIMUM" ? range.minimum : range.maximum;
|
|
80
|
+
}
|
|
81
|
+
function clampValue(value, min, max) {
|
|
82
|
+
"worklet";
|
|
83
|
+
return Math.min(Math.max(value, min), max);
|
|
84
|
+
}
|
|
85
|
+
function snapToStep(value, min, max, step) {
|
|
86
|
+
"worklet";
|
|
87
|
+
const clamped = clampValue(value, min, max);
|
|
88
|
+
if (clamped === max || step <= 0) return clamped;
|
|
89
|
+
const steps = Math.round((clamped - min) / step);
|
|
90
|
+
return clampValue(min + steps * step, min, max);
|
|
91
|
+
}
|
|
92
|
+
function normalizeStops(stops, min, max) {
|
|
93
|
+
if (!stops) return [];
|
|
94
|
+
return Array.from(new Set(stops.filter((stop) => stop >= min && stop <= max))).sort((first, second) => first - second);
|
|
95
|
+
}
|
|
96
|
+
function listStopValues(min, max, step, stops, limit = Number.POSITIVE_INFINITY) {
|
|
97
|
+
if (stops.length > 0) return stops.length <= limit ? stops : [];
|
|
98
|
+
if (max <= min) return [min];
|
|
99
|
+
if (step <= 0) return [min, max];
|
|
100
|
+
const decimals = Math.max(countDecimals(min), countDecimals(step));
|
|
101
|
+
const count = Math.floor((max - min) / step + FLOAT_TOLERANCE);
|
|
102
|
+
if (count + 1 > limit) return [];
|
|
103
|
+
const values = [];
|
|
104
|
+
for (let index = 0; index <= count; index++) values.push(roundTo(min + index * step, decimals));
|
|
105
|
+
if (values[values.length - 1] < max) values.push(max);
|
|
106
|
+
return values.length <= limit ? values : [];
|
|
107
|
+
}
|
|
108
|
+
var FLOAT_TOLERANCE = 1e-9;
|
|
109
|
+
function countDecimals(value) {
|
|
110
|
+
const text = String(value);
|
|
111
|
+
const dot = text.indexOf(".");
|
|
112
|
+
return dot < 0 ? 0 : text.length - dot - 1;
|
|
113
|
+
}
|
|
114
|
+
function roundTo(value, decimals) {
|
|
115
|
+
return Number(value.toFixed(decimals));
|
|
116
|
+
}
|
|
117
|
+
function snapToStops(value, stops) {
|
|
118
|
+
"worklet";
|
|
119
|
+
let closest = stops[0];
|
|
120
|
+
for (let index = 1; index < stops.length; index++) {
|
|
121
|
+
if (Math.abs(stops[index] - value) < Math.abs(closest - value)) closest = stops[index];
|
|
122
|
+
}
|
|
123
|
+
return closest;
|
|
124
|
+
}
|
|
125
|
+
function snapValue(value, min, max, step, stops) {
|
|
126
|
+
"worklet";
|
|
127
|
+
return stops.length > 0 ? snapToStops(value, stops) : snapToStep(value, min, max, step);
|
|
128
|
+
}
|
|
129
|
+
function valueToRatio(value, min, max) {
|
|
130
|
+
"worklet";
|
|
131
|
+
if (max <= min) return 0;
|
|
132
|
+
return (clampValue(value, min, max) - min) / (max - min);
|
|
133
|
+
}
|
|
134
|
+
function ratioToValue(ratio, min, max, step, stops = []) {
|
|
135
|
+
"worklet";
|
|
136
|
+
return snapValue(min + clampValue(ratio, 0, 1) * (max - min), min, max, step, stops);
|
|
137
|
+
}
|
|
138
|
+
function resolveRange(kind, candidate, current) {
|
|
139
|
+
"worklet";
|
|
140
|
+
if (kind === "MINIMUM") return [Math.min(candidate, current[1]), current[1]];
|
|
141
|
+
return [current[0], Math.max(candidate, current[0])];
|
|
142
|
+
}
|
|
143
|
+
function resolveDrag(kind, position, usableWidth, current, min, max, step, stops = []) {
|
|
144
|
+
"worklet";
|
|
145
|
+
if (usableWidth <= 0) return current;
|
|
146
|
+
const candidate = ratioToValue(position / usableWidth, min, max, step, stops);
|
|
147
|
+
return resolveRange(kind, candidate, current);
|
|
148
|
+
}
|
|
149
|
+
function stepValue(value, direction, min, max, step, stops = []) {
|
|
150
|
+
var _a;
|
|
151
|
+
if (stops.length === 0) {
|
|
152
|
+
if (step <= 0) return clampValue(value, min, max);
|
|
153
|
+
const index = (value - min) / step;
|
|
154
|
+
const nextIndex = direction === 1 ? Math.floor(index + FLOAT_TOLERANCE) + 1 : Math.ceil(index - FLOAT_TOLERANCE) - 1;
|
|
155
|
+
const decimals = Math.max(countDecimals(min), countDecimals(step));
|
|
156
|
+
return clampValue(roundTo(min + nextIndex * step, decimals), min, max);
|
|
157
|
+
}
|
|
158
|
+
if (direction === 1) return (_a = stops.find((stop) => stop > value)) != null ? _a : stops[stops.length - 1];
|
|
159
|
+
for (let index = stops.length - 1; index >= 0; index--) {
|
|
160
|
+
if (stops[index] < value) return stops[index];
|
|
161
|
+
}
|
|
162
|
+
return stops[0];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/utils/thumbs.ts
|
|
166
|
+
var thumbVariantMap = {
|
|
167
|
+
/** @description The default: a 24px circle, surface colour with a primary border. */
|
|
168
|
+
CIRCLE: { width: "1.5rem", height: "1.5rem", borderRadius: "ROUND", fill: "SURFACE" },
|
|
169
|
+
/** @description A 24px circle in the primary colour. */
|
|
170
|
+
FILLED: { width: "1.5rem", height: "1.5rem", borderRadius: "ROUND", fill: "PRIMARY" },
|
|
171
|
+
/** @description A 12px by 28px pill in the primary colour, the handle of the Material and iOS sliders. */
|
|
172
|
+
BAR: { width: "0.75rem", height: "1.75rem", borderRadius: "ROUND", fill: "PRIMARY" },
|
|
173
|
+
/** @description A 24px rounded square, surface colour with a primary border. */
|
|
174
|
+
SQUARE: { width: "1.5rem", height: "1.5rem", borderRadius: "0.25rem", fill: "SURFACE" },
|
|
175
|
+
/** @description A 16px dot in the primary colour, for dense screens. */
|
|
176
|
+
DOT: { width: "1rem", height: "1rem", borderRadius: "ROUND", fill: "PRIMARY" }
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/components/Slider/index.tsx
|
|
180
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
181
|
+
var DEFAULT_THUMB_WIDTH = "1.5rem";
|
|
182
|
+
var TOUCH_SIZE = "2.75rem";
|
|
183
|
+
var TRACK_HEIGHT = "0.25rem";
|
|
184
|
+
var MAX_MARKS = 24;
|
|
185
|
+
var STEP_PULSE_MILLISECONDS = 20;
|
|
186
|
+
var RELEASE_PULSE_MILLISECONDS = 30;
|
|
187
|
+
var DISABLED_OPACITY = 0.5;
|
|
188
|
+
var PAN_ACTIVATION_DISTANCE = 8;
|
|
189
|
+
var PAN_FAIL_DISTANCE = 12;
|
|
190
|
+
var ACCESSIBILITY_ACTIONS = [{ name: "increment" }, { name: "decrement" }];
|
|
191
|
+
var KEY_DIRECTIONS = { ArrowRight: 1, ArrowUp: 1, ArrowLeft: -1, ArrowDown: -1 };
|
|
192
|
+
function initialState(value, min) {
|
|
193
|
+
return Array.isArray(value) ? { minimum: value[0], maximum: value[1], dragStart: 0 } : { minimum: min, maximum: value, dragStart: 0 };
|
|
194
|
+
}
|
|
195
|
+
function Slider({
|
|
196
|
+
min,
|
|
197
|
+
max,
|
|
198
|
+
step = 1,
|
|
199
|
+
stops,
|
|
200
|
+
value,
|
|
201
|
+
onChange,
|
|
202
|
+
onChanging,
|
|
203
|
+
formatLabel,
|
|
204
|
+
activeColor = "PRIMARY",
|
|
205
|
+
railColor,
|
|
206
|
+
segmentColor,
|
|
207
|
+
thumb = "CIRCLE",
|
|
208
|
+
thumbWidth = DEFAULT_THUMB_WIDTH,
|
|
209
|
+
marks = true,
|
|
210
|
+
disabled = false,
|
|
211
|
+
locked,
|
|
212
|
+
accessibilityLabel = "Valor",
|
|
213
|
+
minimumAccessibilityLabel = "M\xEDnimo",
|
|
214
|
+
maximumAccessibilityLabel = "M\xE1ximo",
|
|
215
|
+
testID = "slider"
|
|
216
|
+
}) {
|
|
217
|
+
var _a, _b, _c;
|
|
218
|
+
const theme = (0, import_native.useTheme)();
|
|
219
|
+
const state = (0, import_react_native_reanimated.useSharedValue)(initialState(value, min));
|
|
220
|
+
const pointerDragRef = (0, import_react.useRef)(null);
|
|
221
|
+
const nativeDragRef = (0, import_react.useRef)(false);
|
|
222
|
+
const callbacksRef = (0, import_react.useRef)({ onChange, onChanging });
|
|
223
|
+
const [trackWidth, setTrackWidth] = (0, import_react.useState)(0);
|
|
224
|
+
const [, setSyncTick] = (0, import_react.useState)(0);
|
|
225
|
+
const isRange = Array.isArray(value);
|
|
226
|
+
const range = isRange ? value : [min, value];
|
|
227
|
+
const stopValues = (0, import_react.useMemo)(() => normalizeStops(stops, min, max), [stops, min, max]);
|
|
228
|
+
const listsStops = !!marks || !!segmentColor;
|
|
229
|
+
const stopList = (0, import_react.useMemo)(
|
|
230
|
+
() => listsStops ? listStopValues(min, max, step, stopValues, MAX_MARKS + 1) : [],
|
|
231
|
+
[listsStops, min, max, step, stopValues]
|
|
232
|
+
);
|
|
233
|
+
const markVariant = typeof marks === "string" ? marks : "DOT";
|
|
234
|
+
const markValues = marks ? stopList : [];
|
|
235
|
+
const isWeb = import_react_native.Platform.OS === "web";
|
|
236
|
+
const thumbShape = typeof thumb === "string" ? thumbVariantMap[thumb] : void 0;
|
|
237
|
+
const thumbSize = parseFloat(import_tools.default.calculateRemToPx((_a = thumbShape == null ? void 0 : thumbShape.width) != null ? _a : thumbWidth));
|
|
238
|
+
const thumbHeight = thumbShape ? parseFloat(import_tools.default.calculateRemToPx(thumbShape.height)) : thumbSize;
|
|
239
|
+
const touchSize = Math.max(parseFloat(import_tools.default.calculateRemToPx(TOUCH_SIZE)), thumbHeight);
|
|
240
|
+
const touchOffset = (touchSize - thumbSize) / 2;
|
|
241
|
+
const trackHeight = parseFloat(import_tools.default.calculateRemToPx(TRACK_HEIGHT));
|
|
242
|
+
const thumbBorderWidth = parseFloat(theme.borderWidth.thin);
|
|
243
|
+
const usableWidth = Math.max(trackWidth - thumbSize, 0);
|
|
244
|
+
const isMeasured = trackWidth > 0;
|
|
245
|
+
const activeColour = (_b = resolveColour(activeColor)) != null ? _b : theme.colors.container.primary;
|
|
246
|
+
const railColour = (_c = railColor && resolveColour(railColor)) != null ? _c : theme.colors.border.subtle;
|
|
247
|
+
const segmentStops = segmentColor ? stopList : [];
|
|
248
|
+
const hasSegments = segmentStops.length >= 2 && segmentStops.length <= MAX_MARKS + 1;
|
|
249
|
+
const Root = isWeb ? import_react_native.View : import_react_native_gesture_handler.GestureHandlerRootView;
|
|
250
|
+
const rootStyle = { width: "100%", opacity: disabled ? DISABLED_OPACITY : 1 };
|
|
251
|
+
const trackAreaStyle = __spreadValues({
|
|
252
|
+
height: touchSize,
|
|
253
|
+
justifyContent: "center"
|
|
254
|
+
}, isWeb && { userSelect: "none" });
|
|
255
|
+
const railStyle = {
|
|
256
|
+
height: trackHeight,
|
|
257
|
+
borderRadius: trackHeight / 2,
|
|
258
|
+
marginHorizontal: thumbSize / 2,
|
|
259
|
+
backgroundColor: railColour
|
|
260
|
+
};
|
|
261
|
+
const activeRangeStyle = {
|
|
262
|
+
position: "absolute",
|
|
263
|
+
left: thumbSize / 2,
|
|
264
|
+
height: trackHeight,
|
|
265
|
+
borderRadius: trackHeight / 2,
|
|
266
|
+
backgroundColor: activeColour,
|
|
267
|
+
opacity: isMeasured && !hasSegments ? 1 : 0
|
|
268
|
+
};
|
|
269
|
+
const minimumThumbStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({
|
|
270
|
+
transform: [{ translateX: valueToRatio(state.value.minimum, min, max) * usableWidth }]
|
|
271
|
+
}));
|
|
272
|
+
const maximumThumbStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({
|
|
273
|
+
transform: [{ translateX: valueToRatio(state.value.maximum, min, max) * usableWidth }]
|
|
274
|
+
}));
|
|
275
|
+
const activeRangeAnimatedStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => {
|
|
276
|
+
const start = valueToRatio(state.value.minimum, min, max) * usableWidth;
|
|
277
|
+
const end = valueToRatio(state.value.maximum, min, max) * usableWidth;
|
|
278
|
+
return { transform: [{ translateX: start }], width: end - start };
|
|
279
|
+
});
|
|
280
|
+
const topThumb = resolveTopThumb();
|
|
281
|
+
const hasGestures = !isWeb && !disabled;
|
|
282
|
+
const minimumGesture = (0, import_react.useMemo)(
|
|
283
|
+
() => hasGestures ? buildGesture("MINIMUM") : void 0,
|
|
284
|
+
[hasGestures, usableWidth, min, max, step, stopValues]
|
|
285
|
+
);
|
|
286
|
+
const maximumGesture = (0, import_react.useMemo)(
|
|
287
|
+
() => hasGestures ? buildGesture("MAXIMUM") : void 0,
|
|
288
|
+
[hasGestures, usableWidth, min, max, step, stopValues]
|
|
289
|
+
);
|
|
290
|
+
(0, import_react.useEffect)(() => {
|
|
291
|
+
callbacksRef.current = { onChange, onChanging };
|
|
292
|
+
});
|
|
293
|
+
(0, import_react.useEffect)(() => {
|
|
294
|
+
if (pointerDragRef.current || nativeDragRef.current) return;
|
|
295
|
+
state.value = __spreadProps(__spreadValues({}, state.value), { minimum: range[0], maximum: range[1] });
|
|
296
|
+
});
|
|
297
|
+
function resolveColour(token) {
|
|
298
|
+
return import_tools.default.resolveThemeColor(theme.colors.container, token);
|
|
299
|
+
}
|
|
300
|
+
function renderMark(markValue, index) {
|
|
301
|
+
const centre = thumbSize / 2 + valueToRatio(markValue, min, max) * usableWidth;
|
|
302
|
+
const active = markValue >= range[0] && markValue <= range[1];
|
|
303
|
+
const key = `${index}`;
|
|
304
|
+
if (typeof marks === "function") {
|
|
305
|
+
const boxStyle = {
|
|
306
|
+
position: "absolute",
|
|
307
|
+
left: centre - touchSize / 2,
|
|
308
|
+
width: touchSize,
|
|
309
|
+
height: touchSize,
|
|
310
|
+
alignItems: "center",
|
|
311
|
+
justifyContent: "center",
|
|
312
|
+
opacity: isMeasured ? 1 : 0
|
|
313
|
+
};
|
|
314
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { testID: `${testID}-mark`, pointerEvents: "none", style: boxStyle, children: marks({ value: markValue, active, index }) }, key);
|
|
315
|
+
}
|
|
316
|
+
const shape = markVariantMap[markVariant];
|
|
317
|
+
const width = parseFloat(import_tools.default.calculateRemToPx(shape.width));
|
|
318
|
+
const height = parseFloat(import_tools.default.calculateRemToPx(shape.height));
|
|
319
|
+
const outsideColour = shape.fill === "PRIMARY_CONTRAST" ? activeColour : theme.colors.border.strong;
|
|
320
|
+
const markStyle = __spreadValues({
|
|
321
|
+
position: "absolute",
|
|
322
|
+
left: centre - width / 2,
|
|
323
|
+
width,
|
|
324
|
+
height,
|
|
325
|
+
borderRadius: shape.borderRadius === "ROUND" ? Math.max(width, height) / 2 : parseFloat(import_tools.default.calculateRemToPx(shape.borderRadius)),
|
|
326
|
+
opacity: isMeasured ? 1 : 0
|
|
327
|
+
}, shape.fill === "SURFACE" ? {
|
|
328
|
+
borderWidth: thumbBorderWidth,
|
|
329
|
+
borderColor: railColour,
|
|
330
|
+
backgroundColor: theme.colors.container.surface
|
|
331
|
+
} : { backgroundColor: active ? theme.colors.container.surface : outsideColour });
|
|
332
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { testID: `${testID}-mark`, pointerEvents: "none", style: markStyle }, key);
|
|
333
|
+
}
|
|
334
|
+
function isThumbLocked(kind) {
|
|
335
|
+
return isRange && locked === kind;
|
|
336
|
+
}
|
|
337
|
+
function deliver(next) {
|
|
338
|
+
return isRange ? next : next[1];
|
|
339
|
+
}
|
|
340
|
+
function emitChanging(next) {
|
|
341
|
+
var _a2, _b2;
|
|
342
|
+
(_b2 = (_a2 = callbacksRef.current).onChanging) == null ? void 0 : _b2.call(_a2, deliver(next));
|
|
343
|
+
}
|
|
344
|
+
function beginNativeDrag() {
|
|
345
|
+
nativeDragRef.current = true;
|
|
346
|
+
}
|
|
347
|
+
function emitChange(next) {
|
|
348
|
+
nativeDragRef.current = false;
|
|
349
|
+
callbacksRef.current.onChange(deliver(next));
|
|
350
|
+
setSyncTick((tick) => tick + 1);
|
|
351
|
+
}
|
|
352
|
+
function vibrate(milliseconds) {
|
|
353
|
+
var _a2, _b2;
|
|
354
|
+
if (isWeb) {
|
|
355
|
+
;
|
|
356
|
+
(_b2 = (_a2 = globalThis.navigator) == null ? void 0 : _a2.vibrate) == null ? void 0 : _b2.call(_a2, milliseconds);
|
|
357
|
+
} else if (import_react_native.Platform.OS === "android") {
|
|
358
|
+
import_react_native.Vibration.vibrate(milliseconds);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function pulse() {
|
|
362
|
+
vibrate(STEP_PULSE_MILLISECONDS);
|
|
363
|
+
}
|
|
364
|
+
function releaseFeedback() {
|
|
365
|
+
vibrate(RELEASE_PULSE_MILLISECONDS);
|
|
366
|
+
}
|
|
367
|
+
function readRange() {
|
|
368
|
+
return [state.value.minimum, state.value.maximum];
|
|
369
|
+
}
|
|
370
|
+
function readThumbValue(kind) {
|
|
371
|
+
return thumbValueOf(kind, state.value);
|
|
372
|
+
}
|
|
373
|
+
function moveThumb(kind, position) {
|
|
374
|
+
const current = readRange();
|
|
375
|
+
const next = resolveDrag(kind, position, usableWidth, current, min, max, step, stopValues);
|
|
376
|
+
state.value = __spreadProps(__spreadValues({}, state.value), { minimum: next[0], maximum: next[1] });
|
|
377
|
+
if (next[0] !== current[0] || next[1] !== current[1]) pulse();
|
|
378
|
+
return next;
|
|
379
|
+
}
|
|
380
|
+
function commitThumbValue(kind, candidate) {
|
|
381
|
+
emitChange(resolveRange(kind, candidate, readRange()));
|
|
382
|
+
}
|
|
383
|
+
function buildGesture(kind) {
|
|
384
|
+
return import_react_native_gesture_handler.Gesture.Pan().activeOffsetX([-PAN_ACTIVATION_DISTANCE, PAN_ACTIVATION_DISTANCE]).failOffsetY([-PAN_FAIL_DISTANCE, PAN_FAIL_DISTANCE]).onStart(() => {
|
|
385
|
+
"worklet";
|
|
386
|
+
const current = state.value;
|
|
387
|
+
state.value = __spreadProps(__spreadValues({}, current), { dragStart: valueToRatio(thumbValueOf(kind, current), min, max) * usableWidth });
|
|
388
|
+
(0, import_react_native_reanimated.runOnJS)(beginNativeDrag)();
|
|
389
|
+
}).onUpdate((event) => {
|
|
390
|
+
"worklet";
|
|
391
|
+
const current = state.value;
|
|
392
|
+
const next = resolveDrag(
|
|
393
|
+
kind,
|
|
394
|
+
current.dragStart + event.translationX,
|
|
395
|
+
usableWidth,
|
|
396
|
+
[current.minimum, current.maximum],
|
|
397
|
+
min,
|
|
398
|
+
max,
|
|
399
|
+
step,
|
|
400
|
+
stopValues
|
|
401
|
+
);
|
|
402
|
+
state.value = __spreadProps(__spreadValues({}, current), { minimum: next[0], maximum: next[1] });
|
|
403
|
+
if (next[0] !== current.minimum || next[1] !== current.maximum) (0, import_react_native_reanimated.runOnJS)(pulse)();
|
|
404
|
+
(0, import_react_native_reanimated.runOnJS)(emitChanging)(next);
|
|
405
|
+
}).onEnd(() => {
|
|
406
|
+
"worklet";
|
|
407
|
+
(0, import_react_native_reanimated.runOnJS)(releaseFeedback)();
|
|
408
|
+
(0, import_react_native_reanimated.runOnJS)(emitChange)([state.value.minimum, state.value.maximum]);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
function handleLayout(event) {
|
|
412
|
+
setTrackWidth(event.nativeEvent.layout.width);
|
|
413
|
+
}
|
|
414
|
+
function handlePointerDown(kind, event) {
|
|
415
|
+
var _a2;
|
|
416
|
+
if (disabled || isThumbLocked(kind)) return;
|
|
417
|
+
const target = event.nativeEvent.target;
|
|
418
|
+
(_a2 = target == null ? void 0 : target.setPointerCapture) == null ? void 0 : _a2.call(target, event.nativeEvent.pointerId);
|
|
419
|
+
pointerDragRef.current = {
|
|
420
|
+
kind,
|
|
421
|
+
startX: event.nativeEvent.clientX,
|
|
422
|
+
startPosition: valueToRatio(readThumbValue(kind), min, max) * usableWidth
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function handlePointerMove(event) {
|
|
426
|
+
const drag = pointerDragRef.current;
|
|
427
|
+
if (!drag) return;
|
|
428
|
+
const next = moveThumb(drag.kind, drag.startPosition + event.nativeEvent.clientX - drag.startX);
|
|
429
|
+
emitChanging(next);
|
|
430
|
+
}
|
|
431
|
+
function handlePointerUp() {
|
|
432
|
+
if (!pointerDragRef.current) return;
|
|
433
|
+
pointerDragRef.current = null;
|
|
434
|
+
releaseFeedback();
|
|
435
|
+
emitChange(readRange());
|
|
436
|
+
}
|
|
437
|
+
function handleKeyDown(kind, event) {
|
|
438
|
+
var _a2;
|
|
439
|
+
if (disabled || isThumbLocked(kind)) return;
|
|
440
|
+
const key = event.nativeEvent.key;
|
|
441
|
+
const direction = KEY_DIRECTIONS[key];
|
|
442
|
+
const current = readThumbValue(kind);
|
|
443
|
+
if (direction) {
|
|
444
|
+
commitThumbValue(kind, stepValue(current, direction, min, max, step, stopValues));
|
|
445
|
+
} else if (key === "Home") {
|
|
446
|
+
commitThumbValue(kind, snapValue(min, min, max, step, stopValues));
|
|
447
|
+
} else if (key === "End") {
|
|
448
|
+
commitThumbValue(kind, snapValue(max, min, max, step, stopValues));
|
|
449
|
+
} else {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
(_a2 = event.preventDefault) == null ? void 0 : _a2.call(event);
|
|
453
|
+
}
|
|
454
|
+
function handleAccessibilityAction(kind, event) {
|
|
455
|
+
if (disabled || isThumbLocked(kind)) return;
|
|
456
|
+
const direction = event.nativeEvent.actionName === "increment" ? 1 : -1;
|
|
457
|
+
commitThumbValue(kind, stepValue(readThumbValue(kind), direction, min, max, step, stopValues));
|
|
458
|
+
}
|
|
459
|
+
function thumbAccessibilityLabel(kind) {
|
|
460
|
+
if (!isRange) return accessibilityLabel;
|
|
461
|
+
return kind === "MINIMUM" ? minimumAccessibilityLabel : maximumAccessibilityLabel;
|
|
462
|
+
}
|
|
463
|
+
function resolveTopThumb() {
|
|
464
|
+
if (!isRange || locked === "MAXIMUM") return "MINIMUM";
|
|
465
|
+
if (locked === "MINIMUM") return "MAXIMUM";
|
|
466
|
+
if (range[0] === range[1] && valueToRatio(range[0], min, max) > 0.5) return "MINIMUM";
|
|
467
|
+
return "MAXIMUM";
|
|
468
|
+
}
|
|
469
|
+
function thumbTestID(kind) {
|
|
470
|
+
return isRange ? `${testID}-${kind.toLowerCase()}` : `${testID}-thumb`;
|
|
471
|
+
}
|
|
472
|
+
function renderThumbShape(kind, thumbValue, isLocked) {
|
|
473
|
+
if (typeof thumb === "function") return thumb({ kind, value: thumbValue, locked: isLocked, disabled });
|
|
474
|
+
const shape = thumbVariantMap[thumb];
|
|
475
|
+
const primary = isLocked ? theme.colors.border.subtle : activeColour;
|
|
476
|
+
const shapeStyle = {
|
|
477
|
+
width: thumbSize,
|
|
478
|
+
height: thumbHeight,
|
|
479
|
+
borderRadius: shape.borderRadius === "ROUND" ? Math.max(thumbSize, thumbHeight) / 2 : parseFloat(import_tools.default.calculateRemToPx(shape.borderRadius)),
|
|
480
|
+
borderWidth: thumbBorderWidth,
|
|
481
|
+
borderColor: primary,
|
|
482
|
+
backgroundColor: shape.fill === "PRIMARY" ? primary : theme.colors.container.surface
|
|
483
|
+
};
|
|
484
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: shapeStyle });
|
|
485
|
+
}
|
|
486
|
+
function renderSegment(paint, from, to, index) {
|
|
487
|
+
var _a2;
|
|
488
|
+
const active = from >= range[0] && to <= range[1];
|
|
489
|
+
const colour = (_a2 = resolveColour(paint({ index, from, to, active }))) != null ? _a2 : active ? activeColour : railColour;
|
|
490
|
+
const start = valueToRatio(from, min, max) * usableWidth;
|
|
491
|
+
const segmentStyle = {
|
|
492
|
+
position: "absolute",
|
|
493
|
+
left: thumbSize / 2 + start,
|
|
494
|
+
width: valueToRatio(to, min, max) * usableWidth - start,
|
|
495
|
+
height: trackHeight,
|
|
496
|
+
borderRadius: trackHeight / 2,
|
|
497
|
+
backgroundColor: colour,
|
|
498
|
+
opacity: isMeasured ? 1 : 0
|
|
499
|
+
};
|
|
500
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { testID: `${testID}-segment`, pointerEvents: "none", style: segmentStyle }, `${from}-${to}`);
|
|
501
|
+
}
|
|
502
|
+
function renderThumb(kind) {
|
|
503
|
+
const thumbValue = thumbValueOf(kind, { minimum: range[0], maximum: range[1] });
|
|
504
|
+
const isLocked = isThumbLocked(kind);
|
|
505
|
+
const isInert = disabled || isLocked;
|
|
506
|
+
const isOnTop = topThumb === kind;
|
|
507
|
+
const touchTargetStyle = __spreadValues(__spreadValues({
|
|
508
|
+
position: "absolute",
|
|
509
|
+
left: -touchOffset,
|
|
510
|
+
width: touchSize,
|
|
511
|
+
height: touchSize,
|
|
512
|
+
alignItems: "center",
|
|
513
|
+
justifyContent: "center",
|
|
514
|
+
opacity: isMeasured ? 1 : 0,
|
|
515
|
+
zIndex: isOnTop ? 2 : 1
|
|
516
|
+
}, isWeb && { touchAction: "none", userSelect: "none" }), isWeb && !isInert && { cursor: "pointer" });
|
|
517
|
+
const webProps = isWeb && !isLocked ? {
|
|
518
|
+
onKeyDown: (event) => handleKeyDown(kind, event),
|
|
519
|
+
onPointerDown: (event) => handlePointerDown(kind, event),
|
|
520
|
+
onPointerMove: handlePointerMove,
|
|
521
|
+
onPointerUp: handlePointerUp,
|
|
522
|
+
onPointerCancel: handlePointerUp
|
|
523
|
+
} : {};
|
|
524
|
+
const thumb2 = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
525
|
+
import_react_native_reanimated.default.View,
|
|
526
|
+
__spreadProps(__spreadValues({}, webProps), {
|
|
527
|
+
testID: thumbTestID(kind),
|
|
528
|
+
pointerEvents: isLocked ? "none" : "auto",
|
|
529
|
+
accessible: true,
|
|
530
|
+
accessibilityRole: "adjustable",
|
|
531
|
+
accessibilityLabel: thumbAccessibilityLabel(kind),
|
|
532
|
+
accessibilityValue: { min, max, now: thumbValue, text: formatLabel == null ? void 0 : formatLabel(thumbValue) },
|
|
533
|
+
accessibilityState: { disabled: isInert },
|
|
534
|
+
accessibilityActions: ACCESSIBILITY_ACTIONS,
|
|
535
|
+
onAccessibilityAction: (event) => handleAccessibilityAction(kind, event),
|
|
536
|
+
focusable: !isInert,
|
|
537
|
+
style: [touchTargetStyle, kind === "MINIMUM" ? minimumThumbStyle : maximumThumbStyle],
|
|
538
|
+
children: renderThumbShape(kind, thumbValue, isLocked)
|
|
539
|
+
})
|
|
540
|
+
);
|
|
541
|
+
const gesture = kind === "MINIMUM" ? minimumGesture : maximumGesture;
|
|
542
|
+
if (isInert || !gesture) return thumb2;
|
|
543
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_gesture_handler.GestureDetector, { gesture, children: thumb2 });
|
|
544
|
+
}
|
|
545
|
+
function renderLabels() {
|
|
546
|
+
if (!formatLabel) return null;
|
|
547
|
+
if (!isRange) {
|
|
548
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_typography.Typography, { size: "XS", color: "DEFAULT_400", testID: `${testID}-label`, children: formatLabel(range[1]) });
|
|
549
|
+
}
|
|
550
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_container.Container.Horizontal, { align: "BETWEEN_CENTER", gap: "XS", children: [
|
|
551
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_typography.Typography, { size: "XS", color: "DEFAULT_400", testID: `${testID}-minimum-label`, children: formatLabel(range[0]) }),
|
|
552
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_typography.Typography, { size: "XS", color: "DEFAULT_400", testID: `${testID}-maximum-label`, children: formatLabel(range[1]) })
|
|
553
|
+
] });
|
|
554
|
+
}
|
|
555
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Root, { style: rootStyle, testID, children: [
|
|
556
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { testID: `${testID}-track`, style: trackAreaStyle, onLayout: handleLayout, children: [
|
|
557
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { testID: `${testID}-rail`, style: railStyle }),
|
|
558
|
+
segmentColor && hasSegments && segmentStops.slice(1).map((to, index) => renderSegment(segmentColor, segmentStops[index], to, index)),
|
|
559
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { testID: `${testID}-active`, style: [activeRangeStyle, activeRangeAnimatedStyle] }),
|
|
560
|
+
markValues.length <= MAX_MARKS && markValues.map(renderMark),
|
|
561
|
+
isRange && renderThumb("MINIMUM"),
|
|
562
|
+
renderThumb("MAXIMUM")
|
|
563
|
+
] }),
|
|
564
|
+
renderLabels()
|
|
565
|
+
] });
|
|
566
|
+
}
|
|
567
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
568
|
+
0 && (module.exports = {
|
|
569
|
+
Slider
|
|
570
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mobilestock-native/slider",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@mobilestock-native/container": "^1.5.0",
|
|
7
|
+
"@mobilestock-native/tools": "^0.1.0",
|
|
8
|
+
"@mobilestock-native/typography": "^0.1.3"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"react": "18.2.0",
|
|
12
|
+
"react-native": "0.74.5",
|
|
13
|
+
"react-native-gesture-handler": "~2.16.1",
|
|
14
|
+
"react-native-reanimated": "^3.10.1",
|
|
15
|
+
"styled-components": "^6.1.8"
|
|
16
|
+
}
|
|
6
17
|
}
|
package/README.md
DELETED