diff --git a/components/GoToTopButton/index.tsx b/components/GoToTopButton/index.tsx new file mode 100644 index 0000000..8c3b2f8 --- /dev/null +++ b/components/GoToTopButton/index.tsx @@ -0,0 +1,40 @@ +import React, { useEffect, useState } from 'react'; +import {AiOutlineArrowUp} from 'react-icons/ai'; +export function GoToTopButton() { + const [ isVisible, setIsVisible ] = useState(false); + //create a go to top button + + const goToTop = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth' + }); + }; + + const toggleVisibility = () => { + + window.pageYOffset > 300? + setIsVisible(true): + setIsVisible(false) + + }; + + useEffect(() => { + window.addEventListener('scroll', toggleVisibility); + return () => { + window.removeEventListener('scroll', toggleVisibility); + }; + }, []); + + //show the button only when the user scrolls down + if (isVisible) { + return ( +
+ +
+ ); + } + return null; +} diff --git a/components/Layout/index.tsx b/components/Layout/index.tsx index ddd056e..bea84c0 100644 --- a/components/Layout/index.tsx +++ b/components/Layout/index.tsx @@ -1,17 +1,21 @@ import React ,{ReactNode}from 'react' import Navbar from '../Navbar' - +import {GoToTopButton} from '../GoToTopButton'; interface LayoutProps { children: ReactNode } export default function Layout({children}:LayoutProps) { + + + return ( <>
{children}
+ ) } diff --git a/components/Product/index.tsx b/components/Product/index.tsx index 9927719..51262c4 100644 --- a/components/Product/index.tsx +++ b/components/Product/index.tsx @@ -3,6 +3,13 @@ import Link from 'next/link'; import { useRouter } from 'next/router'; import { productProp } from '../../interfaces'; +/* + This component is used to display the product common details + like name and price + if the current url is not the product page + When the user clicks on the product name it should redirects to the product page with the product id, + else stays on the current page +*/ export function Product({ product }: productProp) { const router = useRouter();