fix: 🐛
This commit is contained in:
@@ -25,6 +25,7 @@ export default function Tabs({
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const { deepBrowsing, lastScrollTop } = useDeepBrowsing()
|
const { deepBrowsing, lastScrollTop } = useDeepBrowsing()
|
||||||
const tabRefs = useRef<(HTMLDivElement | null)[]>([])
|
const tabRefs = useRef<(HTMLDivElement | null)[]>([])
|
||||||
|
const containerRef = useRef<HTMLDivElement | null>(null)
|
||||||
const [indicatorStyle, setIndicatorStyle] = useState({ width: 0, left: 0 })
|
const [indicatorStyle, setIndicatorStyle] = useState({ width: 0, left: 0 })
|
||||||
|
|
||||||
const updateIndicatorPosition = () => {
|
const updateIndicatorPosition = () => {
|
||||||
@@ -51,21 +52,40 @@ export default function Tabs({
|
|||||||
}, [tabs, value])
|
}, [tabs, value])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!containerRef.current) return
|
||||||
|
|
||||||
const resizeObserver = new ResizeObserver(() => {
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
updateIndicatorPosition()
|
updateIndicatorPosition()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const intersectionObserver = new IntersectionObserver(
|
||||||
|
(entries) => {
|
||||||
|
entries.forEach((entry) => {
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
updateIndicatorPosition()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
{ threshold: 0 }
|
||||||
|
)
|
||||||
|
|
||||||
|
intersectionObserver.observe(containerRef.current)
|
||||||
|
|
||||||
tabRefs.current.forEach((tab) => {
|
tabRefs.current.forEach((tab) => {
|
||||||
if (tab) resizeObserver.observe(tab)
|
if (tab) resizeObserver.observe(tab)
|
||||||
})
|
})
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
resizeObserver.disconnect()
|
resizeObserver.disconnect()
|
||||||
|
intersectionObserver.disconnect()
|
||||||
}
|
}
|
||||||
}, [tabs])
|
}, [tabs, value])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
ref={containerRef}
|
||||||
className={cn(
|
className={cn(
|
||||||
'sticky flex justify-between top-12 bg-background z-30 px-1 w-full transition-transform',
|
'sticky flex justify-between top-12 bg-background z-30 px-1 w-full transition-transform',
|
||||||
deepBrowsing && lastScrollTop > threshold ? '-translate-y-[calc(100%+12rem)]' : ''
|
deepBrowsing && lastScrollTop > threshold ? '-translate-y-[calc(100%+12rem)]' : ''
|
||||||
|
|||||||
Reference in New Issue
Block a user