Compare commits
2 Commits
faa2f2c16d
...
a413d4421b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a413d4421b | ||
|
|
e8bb6a5a36 |
@@ -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,20 @@ 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 slideRefs = useRef<Record<number, HTMLDivElement | null>>({});
|
||||||
|
const [activeSlideHeight, setActiveSlideHeight] = useState<number | 'auto'>('auto');
|
||||||
|
const stickyThreshold = 0;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!embla) return;
|
if (!embla) return;
|
||||||
@@ -25,6 +32,13 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
const onSelect = () => {
|
const onSelect = () => {
|
||||||
const newIndex = embla.selectedScrollSnap();
|
const newIndex = embla.selectedScrollSnap();
|
||||||
setActiveTab(newIndex);
|
setActiveTab(newIndex);
|
||||||
|
|
||||||
|
// Update height based on active slide content
|
||||||
|
const activeSlideRef = slideRefs.current[newIndex];
|
||||||
|
if (activeSlideRef) {
|
||||||
|
const height = activeSlideRef.scrollHeight;
|
||||||
|
setActiveSlideHeight(height);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
embla.on("select", onSelect);
|
embla.on("select", onSelect);
|
||||||
@@ -34,6 +48,49 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
};
|
};
|
||||||
}, [embla]);
|
}, [embla]);
|
||||||
|
|
||||||
|
// Update height when activeTab changes
|
||||||
|
useEffect(() => {
|
||||||
|
const activeSlideRef = slideRefs.current[activeTab];
|
||||||
|
if (activeSlideRef) {
|
||||||
|
const height = activeSlideRef.scrollHeight;
|
||||||
|
setActiveSlideHeight(height);
|
||||||
|
}
|
||||||
|
}, [activeTab]);
|
||||||
|
|
||||||
|
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);
|
||||||
@@ -47,14 +104,30 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
setControlsRefs(controlsRefs);
|
setControlsRefs(controlsRefs);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setSlideRef = (index: number) => (node: HTMLDivElement | null) => {
|
||||||
|
slideRefs.current[index] = node;
|
||||||
|
};
|
||||||
|
|
||||||
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
|
||||||
@@ -79,7 +152,7 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
? 'var(--mantine-color-blue-6)'
|
? 'var(--mantine-color-blue-6)'
|
||||||
: 'var(--mantine-color-text)',
|
: 'var(--mantine-color-text)',
|
||||||
fontWeight: activeTab === index ? 600 : 400,
|
fontWeight: activeTab === index ? 600 : 400,
|
||||||
transition: 'color 200ms ease, font-weight 200ms ease',
|
transition: 'color 200ms ease',
|
||||||
backgroundColor: 'transparent',
|
backgroundColor: 'transparent',
|
||||||
border: 'none',
|
border: 'none',
|
||||||
borderRadius: 0,
|
borderRadius: 0,
|
||||||
@@ -109,12 +182,21 @@ function SwipeableTabs({ tabs, defaultTab = 0, onTabChange }: SwipeableTabsProps
|
|||||||
slideSize="100%"
|
slideSize="100%"
|
||||||
initialSlide={activeTab}
|
initialSlide={activeTab}
|
||||||
style={{
|
style={{
|
||||||
overflow: 'hidden'
|
overflow: 'hidden',
|
||||||
|
height: activeSlideHeight === 'auto' ? 'auto' : `${activeSlideHeight}px`,
|
||||||
|
transition: 'height 300ms ease'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{tabs.map((tab, index) => (
|
{tabs.map((tab, index) => (
|
||||||
<Carousel.Slide key={`${tab.label}-content-${index}`}>
|
<Carousel.Slide key={`${tab.label}-content-${index}`} style={{ height: 'auto' }}>
|
||||||
<Box>
|
<Box
|
||||||
|
ref={setSlideRef(index)}
|
||||||
|
style={{
|
||||||
|
minHeight: 'fit-content',
|
||||||
|
height: 'auto',
|
||||||
|
visibility: index === activeTab ? 'visible' : 'hidden'
|
||||||
|
}}
|
||||||
|
>
|
||||||
{tab.content}
|
{tab.content}
|
||||||
</Box>
|
</Box>
|
||||||
</Carousel.Slide>
|
</Carousel.Slide>
|
||||||
|
|||||||
Reference in New Issue
Block a user