Skip to content

Commit

Permalink
change scroll/collapsed card behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiano cardelli committed Aug 30, 2024
1 parent 7d64cdf commit a4ebc07
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/grants/components/grant-card/client-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
'use client';

import { useCallback, useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { cn } from '@/shared/utils/cn';
import { useDebounceFn } from '@/shared/hooks/use-debounce-fn';

interface Props {
backButton: React.ReactNode;
Expand All @@ -13,41 +12,49 @@ interface Props {
}

export const ClientWrapper = ({ backButton, collapsed, full }: Props) => {
const [prevScrollPos, setPrevScrollPos] = useState(0);

const [isCollapsed, setIsCollapsed] = useState(false);
const sentinelRef = useRef<HTMLDivElement>(null);

const handleScroll = useCallback(() => {
const currentScrollPos = window.scrollY;

if (currentScrollPos >= 50) {
setIsCollapsed(true);
} else if (currentScrollPos < prevScrollPos) {
setIsCollapsed(false);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
setIsCollapsed(!entry.isIntersecting);
},
{
rootMargin: '50px 0px 0px 0px', // Adjust as needed
threshold: 0,
},
);

if (sentinelRef.current) {
observer.observe(sentinelRef.current);
}
setPrevScrollPos(currentScrollPos);
}, [prevScrollPos]);

const debouncedHandleScroll = useDebounceFn(handleScroll, 50);

useEffect(() => {
window.addEventListener('scroll', debouncedHandleScroll);
return () => {
window.removeEventListener('scroll', debouncedHandleScroll);
if (sentinelRef.current) {
observer.unobserve(sentinelRef.current);

Check warning on line 35 in src/grants/components/grant-card/client-wrapper.tsx

View workflow job for this annotation

GitHub Actions / Run Lint/Build/Test

The ref value 'sentinelRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'sentinelRef.current' to a variable inside the effect, and use that variable in the cleanup function
}
};
}, [debouncedHandleScroll]);
}, []);

const content = isCollapsed ? collapsed : full;

return (
<div className="fixed inset-x-0 top-0 z-50 mt-[56px] bg-app-bg md:mt-20 lg:ml-[264px] lg:mr-8 lg:mt-0 lg:rounded-b-20">
<div className="flex items-center px-5 lg:h-[115px] lg:px-0">{backButton}</div>
<div
className={cn('w-screen transition-all duration-700 lg:w-full', {
pinned: isCollapsed,
})}
>
{content}
<div>
<div ref={sentinelRef} className="absolute left-0 top-0 h-1 w-full"></div>
<div className="fixed inset-x-0 top-0 z-50 mt-[56px] bg-app-bg md:mt-20 lg:ml-[264px] lg:mr-8 lg:mt-0 lg:rounded-b-20">
{/* Sentinel element for IntersectionObserver */}

<div className="flex items-center px-5 lg:h-[115px] lg:px-0">
{backButton}
</div>
<div
className={cn('w-screen transition-all duration-700 lg:w-full', {
pinned: isCollapsed,
})}
>
{content}
</div>
</div>
</div>
);
Expand Down

0 comments on commit a4ebc07

Please sign in to comment.