Skip to content

Commit

Permalink
Commenting the behavior of Product Component.
Browse files Browse the repository at this point in the history
  • Loading branch information
romualdo-ah committed Jan 22, 2022
1 parent 28f2ce4 commit 7958d9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
40 changes: 40 additions & 0 deletions components/GoToTopButton/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="fixed bottom-4 right-3">
<button className='rounded-full bg-gray-300 hover:scale-125 drop-shadow-sm transition-all p-2 text-gray-500' onClick={goToTop}>
<AiOutlineArrowUp className='text-1xl'/>
</button>
</div>
);
}
return null;
}
6 changes: 5 additions & 1 deletion components/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<Navbar/>
<main className="flex w-full min-h-screen px-5 md:px-20 font-montserrat py-20 pb-24 bg-gray-200">
{children}
</main>
<GoToTopButton/>
</>
)
}
7 changes: 7 additions & 0 deletions components/Product/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 7958d9f

Please sign in to comment.