This tutorial explains how to customize the ImageGrid component. You’ll learn how to:
Update images (paths, alt text, and thumbnails).
Customize modal navigation (next, previous, and close buttons).
Adjust animations and transitions.
Ensure responsiveness and accessibility.
Project Resources
Tutorial
Step 1: Updating Images
Locate the Images Array
The images are stored in the images
array:
Update Image Paths (fullSize
)
Replace
/images/Image-X.webp
with the file path to your images.If using local files, save them in the
public/images/
folder.
Example Update:
Update Alt Text (alt
)
Provide meaningful descriptions to improve accessibility and SEO.
Avoid generic descriptions like
"Image"
; instead, describe the image content (e.g.,"Golden Gate Bridge at sunset"
).
Step 2: Customize Modal Navigation
Next and Previous Buttons
Navigation is handled using showNext
and showPrev
functions. Buttons are defined in this block:
Customize Styles:
Update
bg-white
andtext-black
for different colors.Modify
p-3
andw-8 h-8
to adjust padding and icon size.
Example Update:
Close Button
The close button is defined here:
Customize Appearance:
Modify
bg-white
tobg-red-500
or other colors.Adjust
p-2
for padding andw-6 h-6
for icon size.
Step 3: Adjust Animations
Modal Background Animation
The modal background uses AnimatePresence
and motion.div
:
Adjust Fade Timing (
duration
): Change the fade-in/out speed by updating thetransition
property.
Example Update:
Image Scale Animation
The image scaling effect is defined here:
Adjust Scale Timing and Amount:
Modify
scale: 0.8
to control the starting size.Adjust
transition
duration to make the animation slower or faster.
Example Update:
Step 4: Ensure Responsiveness
Thumbnail Grid
The grid of thumbnails is defined here:
Customize Columns:
Adjust
grid-cols-2
for mobile andsm:grid-cols-4
for larger screens.
Modify Spacing:
Change
gap-2
to increase or decrease space between images.
Example Update:
Modal Image Display
The modal image display ensures the image scales to fit the viewport:
Contain or Cover: Change
object-contain
toobject-cover
for different scaling behaviors.
Step 5: Accessibility Improvements
Keyboard Navigation:
Add
tabIndex
to buttons for keyboard accessibility.Use
onKeyDown
to handle arrow key navigation.
Example Update:
Alt Text:
Ensure every image has unique, descriptive alt text.
Focus Management:
Automatically focus on the modal when opened for screen reader users.
Step 6: Testing Your Component
Run Locally: Use
npm run dev
oryarn dev
to view the component.Responsive Testing:
Test grid and modal layout on different screen sizes.
Keyboard Navigation:
Verify that arrow keys and
Tab
work for navigation.
Best Practices
Image Optimization:
Use compressed
.webp
images for faster loading.
Consistent Styling:
Align colors, spacing, and typography with your project’s design system.
Smooth Animations:
Test easing and duration for a polished user experience.