-
-
Notifications
You must be signed in to change notification settings - Fork 578
/
Copy pathScrollbarInput.svelte
295 lines (233 loc) · 8.29 KB
/
ScrollbarInput.svelte
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<script lang="ts" context="module">
export type ScrollbarDirection = "Horizontal" | "Vertical";
</script>
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { PRESS_REPEAT_DELAY_MS, PRESS_REPEAT_INTERVAL_MS, PRESS_REPEAT_INTERVAL_RAPID_MS } from "@graphite/io-managers/input";
const ARROW_CLICK_DISTANCE = 0.05;
const ARROW_REPEAT_DISTANCE = 0.01;
// Convert the position of the thumb (0-1) to the position on the track (0-1).
// This includes the 1/2 thumb length gap of the possible thumb position each side so the end of the thumb doesn't go off the track.
const lerp = (a: number, b: number, t: number): number => a * (1 - t) + b * t;
const thumbToTrack = (thumbLength: number, thumbPosition: number): number => lerp(thumbLength / 2, 1 - thumbLength / 2, thumbPosition);
const pointerPosition = (e: PointerEvent): number => (direction === "Vertical" ? e.clientY : e.clientX);
const clamp01 = (value: number): number => Math.min(Math.max(value, 0), 1);
const dispatch = createEventDispatcher<{ trackShift: number; thumbPosition: number; thumbDragStart: undefined; thumbDragEnd: undefined; thumbDragAbort: undefined }>();
export let direction: ScrollbarDirection = "Vertical";
export let thumbPosition = 0.5;
export let thumbLength = 0.5;
let scrollTrack: HTMLDivElement | undefined;
let dragging = false;
let pressingTrack = false;
let pressingArrow = false;
let repeatTimeout: ReturnType<typeof setTimeout> | undefined = undefined;
let pointerPositionLastFrame = 0;
let thumbTop: string | undefined = undefined;
let thumbBottom: string | undefined = undefined;
let thumbLeft: string | undefined = undefined;
let thumbRight: string | undefined = undefined;
$: start = thumbToTrack(thumbLength, thumbPosition) - thumbLength / 2;
$: end = 1 - thumbToTrack(thumbLength, thumbPosition) - thumbLength / 2;
$: [thumbTop, thumbBottom, thumbLeft, thumbRight] = direction === "Vertical" ? [`${start * 100}%`, `${end * 100}%`, "0%", "0%"] : ["0%", "0%", `${start * 100}%`, `${end * 100}%`];
function trackLength(): number | undefined {
if (scrollTrack === undefined) return undefined;
return direction === "Vertical" ? scrollTrack.clientHeight - thumbLength : scrollTrack.clientWidth;
}
function trackOffset(): number | undefined {
if (scrollTrack === undefined) return undefined;
return direction === "Vertical" ? scrollTrack.getBoundingClientRect().top : scrollTrack.getBoundingClientRect().left;
}
function dragThumb(e: PointerEvent) {
if (dragging) return;
dragging = true;
dispatch("thumbDragStart");
pointerPositionLastFrame = pointerPosition(e);
addEvents();
}
function pressArrow(direction: number) {
const sendMove = () => {
if (!pressingArrow) return;
const distance = afterInitialDelay ? ARROW_REPEAT_DISTANCE : ARROW_CLICK_DISTANCE;
dispatch("trackShift", -direction * distance);
if (afterInitialDelay) repeatTimeout = setTimeout(sendMove, PRESS_REPEAT_INTERVAL_RAPID_MS);
afterInitialDelay = true;
};
pressingArrow = true;
dispatch("thumbDragStart");
let afterInitialDelay = false;
sendMove();
repeatTimeout = setTimeout(sendMove, PRESS_REPEAT_DELAY_MS);
addEvents();
}
function pressTrack(e: PointerEvent) {
if (dragging) return;
const length = trackLength();
const offset = trackOffset();
if (length === undefined || offset === undefined) return;
const sendMove = () => {
if (!pressingTrack) return;
const oldPointer = thumbToTrack(thumbLength, thumbPosition) * length + offset;
const newPointer = pointerPosition(e);
// Check if the thumb has reached the cursor position
const proposedThumbPosition = (newPointer - offset) / length;
if (proposedThumbPosition >= start && proposedThumbPosition <= 1 - end) {
// End pressing the track
pressingTrack = false;
clearTimeout(repeatTimeout);
// Begin dragging the thumb
dragging = true;
pointerPositionLastFrame = newPointer;
return;
}
const move = newPointer - oldPointer < 0 ? 1 : -1;
dispatch("trackShift", move);
if (afterInitialDelay) repeatTimeout = setTimeout(sendMove, PRESS_REPEAT_INTERVAL_MS);
afterInitialDelay = true;
};
dispatch("thumbDragStart");
pressingTrack = true;
let afterInitialDelay = false;
sendMove();
repeatTimeout = setTimeout(sendMove, PRESS_REPEAT_DELAY_MS);
addEvents();
}
function abortInteraction() {
if (pressingTrack || pressingArrow) {
pressingTrack = false;
pressingArrow = false;
clearTimeout(repeatTimeout);
dispatch("thumbDragAbort");
}
if (dragging) {
dragging = false;
dispatch("thumbDragAbort");
}
}
function onPointerUp() {
if (dragging) dispatch("thumbDragEnd");
dragging = false;
pressingTrack = false;
pressingArrow = false;
clearTimeout(repeatTimeout);
removeEvents();
}
function onPointerMove(e: PointerEvent) {
if (pressingTrack) {
return;
}
if (pressingArrow) {
const target = e.target || undefined;
if (!target || !(target instanceof Element)) return;
if (!target?.closest?.("[data-scrollbar-arrow]")) {
pressingArrow = false;
clearTimeout(repeatTimeout);
removeEvents();
}
return;
}
if (dragging) {
const length = trackLength();
if (length === undefined) return;
const positionPositionThisFrame = pointerPosition(e);
const dragDelta = positionPositionThisFrame - pointerPositionLastFrame;
const movement = dragDelta / (length * (1 - thumbLength));
const newThumbPosition = clamp01(thumbPosition + movement);
dispatch("thumbPosition", newThumbPosition);
pointerPositionLastFrame = positionPositionThisFrame;
return;
}
removeEvents();
}
function onMouseDown(e: MouseEvent) {
const BUTTONS_RIGHT = 0b0000_0010;
if (e.buttons & BUTTONS_RIGHT) abortInteraction();
}
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") abortInteraction();
}
function addEvents() {
window.addEventListener("pointerup", onPointerUp);
window.addEventListener("pointermove", onPointerMove);
window.addEventListener("mousedown", onMouseDown);
window.addEventListener("keydown", onKeyDown);
}
function removeEvents() {
window.removeEventListener("pointerup", onPointerUp);
window.removeEventListener("pointermove", onPointerMove);
window.removeEventListener("mousedown", onMouseDown);
window.removeEventListener("keydown", onKeyDown);
}
</script>
<div class={`scrollbar-input ${direction.toLowerCase()}`}>
<button class="arrow decrease" on:pointerdown={() => pressArrow(-1)} tabindex="-1" data-scrollbar-arrow></button>
<div class="scroll-track" on:pointerdown={pressTrack} bind:this={scrollTrack}>
<div class="scroll-thumb" on:pointerdown={dragThumb} class:dragging style:top={thumbTop} style:bottom={thumbBottom} style:left={thumbLeft} style:right={thumbRight} />
</div>
<button class="arrow increase" on:pointerdown={() => pressArrow(1)} tabindex="-1" data-scrollbar-arrow></button>
</div>
<style lang="scss" global>
.scrollbar-input {
display: flex;
flex: 1 1 100%;
&.vertical {
flex-direction: column;
}
&.horizontal {
flex-direction: row;
}
.arrow {
--arrow-color: var(--color-5-dullgray);
flex: 0 0 auto;
background: none;
border: none;
margin: 0;
padding: 0;
width: 16px;
height: 16px;
&:hover {
--arrow-color: var(--color-6-lowergray);
}
&:hover:active {
--arrow-color: var(--color-c-brightgray);
}
&::after {
content: "";
display: block;
border-style: solid;
}
}
&.vertical .arrow.decrease::after {
margin: 4px 3px;
border-width: 0 5px 8px 5px;
border-color: transparent transparent var(--arrow-color) transparent;
}
&.vertical .arrow.increase::after {
margin: 4px 3px;
border-width: 8px 5px 0 5px;
border-color: var(--arrow-color) transparent transparent transparent;
}
&.horizontal .arrow.decrease::after {
margin: 3px 4px;
border-width: 5px 8px 5px 0;
border-color: transparent var(--arrow-color) transparent transparent;
}
&.horizontal .arrow.increase::after {
margin: 3px 4px;
border-width: 5px 0 5px 8px;
border-color: transparent transparent transparent var(--arrow-color);
}
.scroll-track {
position: relative;
flex: 1 1 100%;
.scroll-thumb {
position: absolute;
border-radius: 4px;
background: var(--color-5-dullgray);
&:hover,
&.dragging {
background: var(--color-6-lowergray);
}
}
}
}
</style>