-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslider.tsx
162 lines (143 loc) · 4.62 KB
/
slider.tsx
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
import { component$, useStyles$, $, useComputed$, PropsOf, useContext, createContextId, useContextProvider, Slot } from "@builder.io/qwik";
import { WithControl, extractControls, useControlProvider } from "../control";
import { mergeProps } from "../../utils/attributes";
import style from './slider.scss?inline';
interface BaseProps {
/** Default to 0 */
min: number;
/** Default to 100 */
max: number;
/** Default to 1 */
step: number
/** Default to false */
vertical: boolean;
/** Disable both thumbs */
disabled: boolean;
}
interface Context extends BaseProps {}
const SliderContext = createContextId<Context>('SliderContext');
type Props = Partial<BaseProps> & PropsOf<'div'>;
export const Slider = component$<WithControl<number, Props>>((props) => {
useStyles$(style);
const { attr, controls } = extractControls(props);
const { control, change } = useControlProvider(controls);
const {
min = 0,
max = 100,
step = 1,
vertical = false,
disabled = false,
...rest
} = attr;
useContextProvider(SliderContext, {
min,
max,
step,
vertical,
disabled
});
// TODO: remove after useContext is fixed in v2.0
useContext(SliderContext);
const thumb = useComputed$(() => ((control.value ?? min) - min) / (max - min));
const onKeydown$ = $((e: KeyboardEvent) => {
const current = control.value ?? min;
if (e.key === 'Home') change(min);
if (vertical) {
if (e.key === 'ArrowUp') change(Math.max(current - step, min));
if (e.key === 'ArrowDown') change(Math.min(current + step, max));
} else {
if (e.key === 'ArrowLeft') change(Math.max(current - step, min));
if (e.key === 'ArrowRight') change(Math.min(current + step, max));
}
if (e.key === 'End') change(max);
});
const start$ = $((e: TouchEvent | MouseEvent, el: HTMLElement) => {
const clamp = (v: number) => Math.max(0, Math.min(v, 1));
const round = (v: number) => Math.round(v / step) * step;
const getPercent = (event: TouchEvent | MouseEvent) => {
const { width, height, left, top } = el.getBoundingClientRect();
if (vertical) {
const y = 'touches' in event
? event.touches.item(0)!.clientY
: event.clientY;
return clamp(((y - top) / height));
} else {
const x = 'touches' in event
? event.touches.item(0)!.clientX
: event.clientX;
return clamp(((x - left) / width));
}
}
const setValue = (percent: number) => {
change(round(min + percent * (max - min)));
}
const move = (event: TouchEvent | MouseEvent) => {
const percent = getPercent(event);
setValue(percent)
};
const leave = () => {
if ('touches' in e) {
document.removeEventListener('touchmove', move);
document.removeEventListener('touchend', leave);
} else {
document.removeEventListener('mousemove', move);
document.removeEventListener('mouseup', leave);
}
};
const percent = getPercent(e);
setValue(percent);
if ('touches' in e) {
document.addEventListener('touchmove', move);
document.addEventListener('touchend', leave);
} else {
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', leave);
}
});
const wheel$ = $((e: WheelEvent) => {
const current = control.value ?? min;
if (e.deltaY < 0) change(Math.max(current - step, min));
else change(Math.min(current + step, max));
})
const containerProps = mergeProps<'div'>(rest, {
role: 'group',
class: ['he-slider-container', disabled ? 'disable' : ''],
style: {
'--he-thumb': thumb.value,
},
onMouseDown$: start$,
onTouchStart$: start$,
onWheel$: wheel$,
'preventdefault:wheel': true,
'aria-orientation': vertical ? 'vertical' : 'horizontal',
});
const btnProps: PropsOf<'button'> = {
type: 'button',
disabled: disabled,
class: 'he-slider-thumb',
onKeyDown$: onKeydown$,
'aria-valuemin': min,
'aria-valuemax': max,
'aria-valuenow': control.value ?? min,
};
return (
<div {...containerProps}>
<div class="he-slider-track"></div>
<button {...btnProps}>
<div class="he-slider-thumb-shadow"></div>
<div class="he-slider-thumb-indicator"></div>
</button>
<Slot />
</div>
)
});
export const SliderTickList = component$(() => {
const { min, max, step } = useContext(SliderContext);
const ticks = [];
for (let i = min; i <= max; i += step) ticks.push(i);
return (
<ul class="he-slider-tick-list">
{ticks.map(i => <li key={i} class="he-slider-tick"></li>)}
</ul>
)
});