This code creates a page that displays image thumbnails and allows the user to zoom in on the images in a lightbox when the thumbnails are clicked. The user can navigate between the images in the lightbox and close it when desired.
Deploy Vercel - LightBox Component
Photos Unsplash - Nubelson Fernandes
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
Open http://localhost:3000 with your browser to see the result.
page.tsx
Module and component imports
ThumbImage
component used to display thumbnails of the images on the main page.Image
module from Next.js for displaying the images.Component declaration
Home
function as a React component. It serves as the main page of the application.State
useState
hook to declare two states:indexCurrent
: Stores the index of the current image being displayed in the lightbox.dataImgCurrent
: Stores the data of the current image being displayed. It can be one of the imported images.isOpen
isOpen
variable to check if the lightbox is open. It is true when dataImgCurrent is not null.List of images
listImages
, containing the imported images.handleClickOpenImage
indexCurrent
and dataImgCurrent
states based on the clicked index.handleCloseLightBox
dataImgCurrent
to null, effectively closing the lightbox.handleNavigate
indexCurrent
and dataImgCurrent
states with the new index.Component rendering
ThumbImage
component.isOpen
is true.thumbImage.tsx
This file contains the ThumbImage component, which is used to render image thumbnails on the main page.
Imports
ThumbImage component:
srcImage
(the image to be displayed) and onClick (the function to be executed when the user clicks on the thumbnail) as properties.Image
component from Next.js, with defined dimensions, quality, and hover effects.In summary, this code creates a page that displays image thumbnails and allows the user to zoom in on the images in a lightbox when the thumbnails are clicked. The user can navigate between the images in the lightbox and close it when desired.