-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement page scroller for topic pages
- Loading branch information
1 parent
5531163
commit beb18c6
Showing
7 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client"; | ||
|
||
import React, { ReactNode, useEffect, useRef, useState } from "react"; | ||
import { ArrowDownIcon } from "lucide-react"; | ||
import { Button } from "@/components/ui/button"; | ||
import { cn } from "@/lib/utils"; | ||
import { trackEvent } from "@/lib/google-analytics"; | ||
import { GoogleAnalyticsEvent } from "@/lib/enums"; | ||
|
||
type Props = { | ||
className?: string; | ||
children: ReactNode; | ||
}; | ||
|
||
export default function PageScroller({ className, children }: Props) { | ||
const btnText = "Subscribe Now"; | ||
const subscribeElRef = useRef<HTMLDivElement>(null); | ||
const [shouldDisplayBtn, setShouldDisplayBtn] = useState(true); | ||
const btnOnClick = () => { | ||
const subscribeEl = subscribeElRef.current; | ||
|
||
if (subscribeEl) { | ||
subscribeEl.scrollIntoView({ behavior: "smooth" }); | ||
} | ||
|
||
trackEvent({ | ||
event: GoogleAnalyticsEvent.SCROLL_CLICK, | ||
buttonText: btnText, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
([entry]) => { | ||
setShouldDisplayBtn(!entry.isIntersecting); | ||
}, | ||
{ threshold: 0.15 }, | ||
); | ||
const subscribeEl = subscribeElRef.current; | ||
|
||
if (subscribeEl) { | ||
observer.observe(subscribeEl); | ||
} | ||
|
||
return () => { | ||
if (subscribeEl) { | ||
observer.unobserve(subscribeEl); | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div ref={subscribeElRef} className={className}> | ||
{children} | ||
</div> | ||
<div | ||
className={cn( | ||
"pointer-events-none fixed bottom-0 left-0 z-50 w-full translate-y-full text-center", | ||
"transition-all duration-300 ease-in-out", | ||
{ | ||
"bottom-6 translate-y-0": shouldDisplayBtn, | ||
}, | ||
)} | ||
> | ||
<Button onClick={btnOnClick} className="pointer-events-auto"> | ||
<span className="align-middle">{btnText}</span> | ||
<ArrowDownIcon className="ml-2 inline-block h-4 w-4 align-middle" /> | ||
</Button> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters