-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathsliderRendering.tsx
More file actions
201 lines (179 loc) · 6.18 KB
/
sliderRendering.tsx
File metadata and controls
201 lines (179 loc) · 6.18 KB
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
import React from 'react';
import {SliderMarks} from '../types';
// Replicate Radix UI's exact positioning logic for displaying marks
const convertValueToPercentage = (value: number, min: number, max: number) => {
const maxSteps = max - min;
const percentPerStep = 100 / maxSteps;
const percentage = percentPerStep * (value - min);
// Clamp to 0-100 range like Radix does
return Math.max(0, Math.min(100, percentage));
};
const linearScale = (
input: readonly [number, number],
output: readonly [number, number]
) => {
return (value: number) => {
if (input[0] === input[1] || output[0] === output[1]) {
return output[0];
}
const ratio = (output[1] - output[0]) / (input[1] - input[0]);
return output[0] + ratio * (value - input[0]);
};
};
const getThumbInBoundsOffset = (
width: number,
left: number,
direction: number
) => {
const halfWidth = width / 2;
const halfPercent = 50;
const offset = linearScale([0, halfPercent], [0, halfWidth]);
return (halfWidth - offset(left) * direction) * direction;
};
// Calculate the exact position including pixel offset as Radix does
const getRadixThumbPosition = (
value: number,
minMaxValues: {min_mark: number; max_mark: number},
thumbWidth = 16
) => {
const percentage = convertValueToPercentage(
value,
minMaxValues.min_mark,
minMaxValues.max_mark
);
const direction = 1; // LTR direction
const thumbInBoundsOffset = getThumbInBoundsOffset(
thumbWidth,
percentage,
direction
);
return {percentage, offset: thumbInBoundsOffset};
};
export const renderSliderMarks = (
renderedMarks: SliderMarks,
vertical: boolean,
minMaxValues: {min_mark: number; max_mark: number},
selectedValues: number[] = [],
dots: boolean,
reverse = false
) => {
return Object.entries(renderedMarks).map(([position, mark]) => {
const pos = parseFloat(position);
// When reversed, use the inverted value for positioning
const displayPos = reverse
? minMaxValues.max_mark - pos + minMaxValues.min_mark
: pos;
const thumbPosition = getRadixThumbPosition(displayPos, minMaxValues);
const style = vertical
? {
bottom: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px - 13px)`,
left: 'calc(100% + 8px)',
transform: 'translateY(-50%)',
}
: {
left: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
bottom: 0,
transform: 'translateX(-50%)',
};
// Determine if mark is outside the selected range
let isOutsideSelection = false;
if (selectedValues.length === 1) {
isOutsideSelection = pos > selectedValues[0];
} else if (selectedValues.length > 1) {
const [minValue, maxValue] = [
selectedValues[0],
selectedValues[selectedValues.length - 1],
];
isOutsideSelection = pos < minValue || pos > maxValue;
}
const outsideClassName = isOutsideSelection
? 'dash-slider-mark-outside-selection'
: '';
const className = `dash-slider-mark ${
dots ? 'with-dots' : ''
} ${outsideClassName}`.trim();
return (
<div
key={position}
className={className}
style={{
...style,
...(typeof mark === 'object' && mark.style
? mark.style
: {}),
}}
>
{typeof mark === 'string' ? mark : mark?.label || pos}
</div>
);
});
};
export const renderSliderDots = (
stepValue: number,
minMaxValues: {min_mark: number; max_mark: number},
selectedValues: number[] = [],
vertical: boolean,
reverse = false
) => {
if (stepValue <= 1) {
return null;
}
const dotCount =
Math.floor(
(minMaxValues.max_mark - minMaxValues.min_mark) / stepValue
) + 1;
// Cap at 100 dots - if more, don't render any dots
if (dotCount > 100) {
return null;
}
return Array.from(
{
length: dotCount,
},
(_, i) => {
const dotValue = minMaxValues.min_mark + i * stepValue;
// When reversed, use the inverted value for positioning
const displayValue = reverse
? minMaxValues.max_mark - dotValue + minMaxValues.min_mark
: dotValue;
const thumbPosition = getRadixThumbPosition(
displayValue,
minMaxValues
);
const dotStyle = vertical
? {
bottom: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
left: '2px',
transform: 'translate(50%, 50%)',
}
: {
left: `calc(${thumbPosition.percentage}% + ${thumbPosition.offset}px)`,
top: '0',
transform: 'translate(-50%, 50%)',
};
// Determine if dot is outside the selected range
let isOutsideSelection = false;
if (selectedValues.length === 1) {
isOutsideSelection = dotValue > selectedValues[0];
} else if (selectedValues.length > 1) {
const [minValue, maxValue] = [
selectedValues[0],
selectedValues[selectedValues.length - 1],
];
isOutsideSelection = dotValue < minValue || dotValue > maxValue;
}
const className = isOutsideSelection
? 'dash-slider-dot dash-slider-dot-outside-selection'
: 'dash-slider-dot';
return (
<div
key={i}
className={className}
style={{
...dotStyle,
}}
/>
);
}
);
};