sticky tabs
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { FloatingIndicator, UnstyledButton, Box, Text, ScrollArea } from "@mantine/core";
|
import { FloatingIndicator, UnstyledButton, Box, Text, ScrollArea } from "@mantine/core";
|
||||||
import { Carousel } from "@mantine/carousel";
|
import { Carousel } from "@mantine/carousel";
|
||||||
import { useState, useEffect, ReactNode } from "react";
|
import { useState, useEffect, ReactNode, useRef } from "react";
|
||||||
|
|
||||||
interface TabItem {
|
interface TabItem {
|
||||||
label: string;
|
label: string;
|
||||||
@@ -11,13 +11,18 @@ interface SwipeableTabsProps {
|
|||||||
tabs: TabItem[];
|
tabs: TabItem[];
|
||||||
defaultTab?: number;
|
defaultTab?: number;
|
||||||
onTabChange?: (index: number, tab: TabItem) => void;
|
onTabChange?: (index: number, tab: TabItem) => void;
|
||||||
|
scrollPosition?: { x: number; y: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps) {
|
function SwipeableTabs({ tabs, defaultTab = 0, onTabChange, scrollPosition }: SwipeableTabsProps) {
|
||||||
const [activeTab, setActiveTab] = useState(defaultTab);
|
const [activeTab, setActiveTab] = useState(defaultTab);
|
||||||
const [embla, setEmbla] = useState<any>(null);
|
const [embla, setEmbla] = useState<any>(null);
|
||||||
const [rootRef, setRootRef] = useState<HTMLDivElement | null>(null);
|
const [rootRef, setRootRef] = useState<HTMLDivElement | null>(null);
|
||||||
const [controlsRefs, setControlsRefs] = useState<Record<number, HTMLSpanElement | null>>({});
|
const [controlsRefs, setControlsRefs] = useState<Record<number, HTMLSpanElement | null>>({});
|
||||||
|
const [isSticky, setIsSticky] = useState(false);
|
||||||
|
const tabsRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const originalPositionRef = useRef<number | null>(null);
|
||||||
|
const stickyThreshold = 0;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!embla) return;
|
if (!embla) return;
|
||||||
@@ -34,6 +39,40 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
};
|
};
|
||||||
}, [embla]);
|
}, [embla]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (scrollPosition) {
|
||||||
|
setIsSticky(scrollPosition.y > stickyThreshold);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scrollWrapper = document.getElementById('scroll-wrapper');
|
||||||
|
let viewport = scrollWrapper!.querySelector('.mantine-ScrollArea-viewport') as HTMLElement;
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
if (!tabsRef.current) return;
|
||||||
|
|
||||||
|
const scrollTop = viewport.scrollTop;
|
||||||
|
const viewportRect = viewport.getBoundingClientRect();
|
||||||
|
|
||||||
|
if (originalPositionRef.current === null && !isSticky) {
|
||||||
|
const tabsRect = tabsRef.current.getBoundingClientRect();
|
||||||
|
originalPositionRef.current = tabsRect.top - viewportRect.top + scrollTop;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsSticky(
|
||||||
|
originalPositionRef.current !== null
|
||||||
|
&& scrollTop >= (originalPositionRef.current - viewportRect.top - stickyThreshold)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
handleScroll();
|
||||||
|
viewport.addEventListener('scroll', handleScroll, { passive: true });
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
viewport.removeEventListener('scroll', handleScroll);
|
||||||
|
};
|
||||||
|
}, [stickyThreshold, isSticky, scrollPosition]);
|
||||||
|
|
||||||
const handleTabChange = (index: number) => {
|
const handleTabChange = (index: number) => {
|
||||||
if (index !== activeTab && index >= 0 && index < tabs.length) {
|
if (index !== activeTab && index >= 0 && index < tabs.length) {
|
||||||
setActiveTab(index);
|
setActiveTab(index);
|
||||||
@@ -49,12 +88,24 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
|
{isSticky && (
|
||||||
|
<Box style={{ height: '60px' }} />
|
||||||
|
)}
|
||||||
<Box
|
<Box
|
||||||
ref={setRootRef}
|
ref={(node) => {
|
||||||
pos="relative"
|
setRootRef(node);
|
||||||
|
tabsRef.current = node;
|
||||||
|
}}
|
||||||
|
pos={isSticky ? "fixed" : "relative"}
|
||||||
style={{
|
style={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
marginBottom: 'var(--mantine-spacing-md)',
|
marginBottom: 'var(--mantine-spacing-md)',
|
||||||
|
top: isSticky ? 0 : 'auto',
|
||||||
|
left: isSticky ? 0 : 'auto',
|
||||||
|
right: isSticky ? 0 : 'auto',
|
||||||
|
zIndex: 100,
|
||||||
|
backgroundColor: 'var(--mantine-color-body)',
|
||||||
|
transition: 'all 200ms ease',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<FloatingIndicator
|
<FloatingIndicator
|
||||||
|
|||||||
Reference in New Issue
Block a user