-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTabPanelCarousel.tsx
More file actions
54 lines (48 loc) · 1.64 KB
/
TabPanelCarousel.tsx
File metadata and controls
54 lines (48 loc) · 1.64 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
import {ReactNode, useCallback, useContext, useEffect, useLayoutEffect, useRef} from "react";
import {TabListStateContext} from "react-aria-components";
export function TabPanelCarousel({children}: {children: ReactNode}) {
let state = useContext(TabListStateContext)!;
let ref = useRef<HTMLDivElement | null>(null);
// Update the selected tab on scroll end.
let onScrollEnd = useCallback(() => {
let el = ref.current;
if (!el) return;
let index = Math.round(el.scrollLeft / el.offsetWidth);
state.setSelectedKey([...state.collection][index].key);
}, [state]);
// Polyfill for onScrollEnd in Safari.
let timeout = useRef<ReturnType<typeof setTimeout>>(undefined);
useEffect(() => {
let onScroll = () => {
clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
onScrollEnd();
}, 300);
};
if (!('onscrollend' in window)) {
ref.current?.addEventListener('scroll', onScroll);
return () => ref.current?.removeEventListener('scroll', onScroll);
}
}, [onScrollEnd]);
// Scroll the selected tab panel into view when tapping on a tab.
useLayoutEffect(() => {
if (!state?.selectedItem || !ref.current) return;
let panel = ref.current.children[state.selectedItem!.index] as HTMLElement;
ref.current.scrollTo({
left: panel.offsetLeft,
behavior: 'smooth'
});
}, [state?.selectedKey]);
return (
<div
ref={ref}
className="overflow-auto snap-x snap-mandatory flex relative"
style={{
scrollTimeline: '--scroll x',
scrollbarWidth: 'none'
} as any}
onScrollEnd={onScrollEnd}>
{children}
</div>
);
}