-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathSmartScrollbar.tsx
More file actions
292 lines (262 loc) · 9.79 KB
/
SmartScrollbar.tsx
File metadata and controls
292 lines (262 loc) · 9.79 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
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
import React, {
createContext,
useContext,
useState,
useEffect,
useRef,
useCallback,
useMemo,
Children,
isValidElement,
} from 'react';
import { getIndicatorLayout } from './utils';
import { SmartScrollbarIndicator } from './SmartScrollbarIndicator';
// ── Child validation ────────────────────────────────────────────
function validateChildren(children: React.ReactNode): void {
let hasIndicator = false;
Children.forEach(children, child => {
if (!isValidElement(child)) return;
if (child.type === SmartScrollbarIndicator) hasIndicator = true;
});
if (!hasIndicator) {
throw new Error(
'SmartScrollbar: <SmartScrollbarIndicator> is a required child. ' +
'Users will not see their current scroll position without it.'
);
}
}
// ── Layout and timing constants ─────────────────────────────────
const TRACK_WIDTH = 8;
const RESTING_WIDTH = 4;
const FILL_PADDING = 3;
const INDICATOR_SIZE = 8;
const INDICATOR_BORDER_WIDTH = 1;
const SETTLE_DELAY = 600;
// ── Contexts ───────────────────────────────────────────────────
export interface SmartScrollbarLayoutContextValue {
total: number;
trackHeight: number;
isLoading: boolean;
effectiveWidth: number;
trackWidth: number;
fillPadding: number;
stableLayerEl: HTMLDivElement | null;
}
const SmartScrollbarLayoutContext = createContext<SmartScrollbarLayoutContextValue | null>(null);
const SmartScrollbarScrollContext = createContext<number | null>(null);
export function useSmartScrollbarLayoutContext(): SmartScrollbarLayoutContextValue {
const ctx = useContext(SmartScrollbarLayoutContext);
if (!ctx)
throw new Error('SmartScrollbar compound components must be used inside <SmartScrollbar>');
return ctx;
}
export function useSmartScrollbarScrollContext(): number {
const value = useContext(SmartScrollbarScrollContext);
if (value === null)
throw new Error('SmartScrollbar compound components must be used inside <SmartScrollbar>');
return value;
}
// ── Props ──────────────────────────────────────────────────────
interface SmartScrollbarProps {
value: number;
total: number;
onValueChange: (index: number) => void;
isLoading?: boolean;
enableKeyboardNavigation?: boolean;
'aria-label'?: string;
className?: string;
children: React.ReactNode;
}
// ── Component ──────────────────────────────────────────────────
export function SmartScrollbar({
value,
total,
onValueChange,
isLoading = false,
enableKeyboardNavigation = false,
'aria-label': ariaLabel = 'Scroll position',
className,
children,
}: SmartScrollbarProps) {
validateChildren(children);
// ── ResizeObserver for trackHeight ───────────────────────────
const containerRef = useRef<HTMLDivElement>(null);
const [trackHeight, setTrackHeight] = useState(0);
useEffect(() => {
const el = containerRef.current;
if (!el) return;
const ro = new ResizeObserver(([entry]) => {
setTrackHeight(entry.contentRect.height);
});
ro.observe(el);
return () => ro.disconnect();
}, []);
// ── Contraction state ────────────────────────────────────────
const [isHovered, setIsHovered] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const isDraggingRef = useRef(false);
const trackTopRef = useRef(0);
// Settle delay — only contract after a real loading→done transition
const [hasSettled, setHasSettled] = useState(false);
const wasEverLoading = useRef(false);
useEffect(() => {
if (isLoading) {
wasEverLoading.current = true;
setHasSettled(false);
} else if (wasEverLoading.current) {
const timer = setTimeout(() => setHasSettled(true), SETTLE_DELAY);
return () => clearTimeout(timer);
}
}, [isLoading]);
const isExpanded = !hasSettled || isHovered || isDragging;
const effectiveWidth = isExpanded ? TRACK_WIDTH : RESTING_WIDTH;
// ── Hit zone extension ───────────────────────────────────────
const { leftPos } = getIndicatorLayout(TRACK_WIDTH, INDICATOR_SIZE, INDICATOR_BORDER_WIDTH);
const hitZoneLeftExtension = Math.max(0, -leftPos);
// ── Stable layer (for elements that shouldn't move during contraction) ──
// Uses useState + callback ref so React triggers a re-render when the
// DOM node mounts — ensuring endpoints render on the first valid pass.
const [stableLayerEl, setStableLayerEl] = useState<HTMLDivElement | null>(null);
// ── Pointer helpers ──────────────────────────────────────────
const clamp = useCallback(
(val: number) => Math.max(0, Math.min(total - 1, val)),
[total]
);
const indexFromPointerY = useCallback(
(clientY: number) => {
const ratio = Math.max(0, Math.min(1, (clientY - trackTopRef.current) / trackHeight));
return Math.round(ratio * (total - 1));
},
[trackHeight, total]
);
const handlePointerDown = useCallback(
(e: React.PointerEvent) => {
trackTopRef.current = e.currentTarget.getBoundingClientRect().top;
isDraggingRef.current = true;
setIsDragging(true);
e.currentTarget.setPointerCapture(e.pointerId);
onValueChange(clamp(indexFromPointerY(e.clientY)));
},
[clamp, indexFromPointerY, onValueChange]
);
const handlePointerMove = useCallback(
(e: React.PointerEvent) => {
if (!isDraggingRef.current) return;
onValueChange(clamp(indexFromPointerY(e.clientY)));
},
[clamp, indexFromPointerY, onValueChange]
);
const handlePointerUp = useCallback((e: React.PointerEvent) => {
isDraggingRef.current = false;
setIsDragging(false);
e.currentTarget.releasePointerCapture(e.pointerId);
}, []);
// ── Keyboard interaction (WAI-ARIA slider spec) ────────────
const PAGE_STEP = 10;
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
let next: number | null = null;
switch (e.key) {
case 'ArrowUp':
case 'ArrowLeft':
next = value - 1;
break;
case 'ArrowDown':
case 'ArrowRight':
next = value + 1;
break;
case 'PageUp':
next = value - PAGE_STEP;
break;
case 'PageDown':
next = value + PAGE_STEP;
break;
case 'Home':
next = 0;
break;
case 'End':
next = total - 1;
break;
default:
return;
}
e.preventDefault();
onValueChange(clamp(next));
},
[value, total, clamp, onValueChange]
);
// ── Context values ───────────────────────────────────────────
const layoutCtx = useMemo<SmartScrollbarLayoutContextValue>(() => ({
total,
trackHeight,
isLoading,
effectiveWidth,
trackWidth: TRACK_WIDTH,
fillPadding: FILL_PADDING,
stableLayerEl,
}), [total, trackHeight, isLoading, effectiveWidth, stableLayerEl]);
return (
<SmartScrollbarLayoutContext.Provider value={layoutCtx}>
<SmartScrollbarScrollContext.Provider value={value}>
<div
ref={containerRef}
role="slider"
aria-valuenow={value}
aria-valuemin={0}
aria-valuemax={total - 1}
aria-orientation="vertical"
aria-label={ariaLabel}
tabIndex={0}
className={className}
style={{
width: TRACK_WIDTH + hitZoneLeftExtension,
height: '100%',
position: 'relative',
marginLeft: -hitZoneLeftExtension,
cursor: isDragging ? 'grabbing' : 'grab',
touchAction: 'none',
}}
onPointerEnter={() => setIsHovered(true)}
onPointerLeave={() => setIsHovered(false)}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onKeyDown={enableKeyboardNavigation ? handleKeyDown : undefined}
>
{trackHeight > 0 && (
<div
style={{
position: 'absolute',
right: 0,
top: 0,
width: TRACK_WIDTH,
height: trackHeight,
display: 'flex',
justifyContent: 'center',
}}
>
<div
className="relative"
style={{
width: effectiveWidth,
height: trackHeight,
transition: 'width 300ms ease',
}}
>
{children}
</div>
{/* Stable layer — always TRACK_WIDTH, never contracts. For elements like
endpoints that must not jitter during width transitions. Children
render here via createPortal using stableLayerRef from context. */}
<div
ref={setStableLayerEl}
style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}
/>
</div>
)}
</div>
</SmartScrollbarScrollContext.Provider>
</SmartScrollbarLayoutContext.Provider>
);
}