To enhance your Certifications section with an awesome and mobile-friendly carousel effect, you can integrate the Swiper library. Swiper is a powerful and flexible touch slider widely used in modern web applications for creating responsive and interactive carousels.
Below, I'll guide you through the steps to implement the carousel effect using Swiper in your existing certifications.js component.
1. Install Swiper
First, you need to install Swiper and its React components. You can do this using npm or yarn:
# Using npm
npm install swiper
# Using yarn
yarn add swiper
2. Update certifications.js
We'll modify your certifications.js to integrate Swiper. This involves importing Swiper components and styles, replacing the grid layout with Swiper slides, and adjusting the styled-components for better compatibility.
Here's the updated code:
// src/components/sections/certifications.js
import React, { useEffect } from 'react';
import styled, { css, keyframes } from 'styled-components';
import { srConfig } from '@config';
import sr from '@utils/sr';
import { usePrefersReducedMotion } from '@hooks';
// Import Swiper React components and styles
import { Swiper, SwiperSlide } from 'swiper/react';
import { Navigation, Pagination, A11y, Autoplay } from 'swiper';
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
// Optional: Define FadeIn Animation for future use
const fadeIn = keyframes`
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
// Dummy Certification Data (10 Certifications)
const certifications = [
// ... (your certification data remains unchanged)
{
id: 1,
name: 'Certified React Developer',
issuer: 'React Training',
image: 'https://via.placeholder.com/150x100',
date: 'June 2023',
description:
'Completed advanced React development training covering hooks, state management, and performance optimization.',
url: 'https://example.com/certificates/react-developer',
},
// ... (other certifications)
// Ensure all certifications have unique ids
];
const StyledCertificationsSection = styled.section`
max-width: 1200px;
margin: 0 auto;
padding: 2rem 1rem;
h2.numbered-heading {
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(24px, 5vw, var(--fz-heading));
width: 100%;
margin: 40px 0;
&::before,
&::after {
content: '';
flex: 1;
border-bottom: 1px solid SlateGray;
margin: 0 15px;
}
}
.heading {
text-align: center;
margin-bottom: 2rem;
font-size: 2rem;
position: relative;
color: var(--green);
&::after {
content: '';
width: 60px;
height: 3px;
background: var(--green);
display: block;
margin: 0.5rem auto 0;
border-radius: 2px;
}
}
/* Swiper Navigation Styles Override (Optional) */
.swiper-button-prev,
.swiper-button-next {
color: var(--green);
}
.swiper-pagination-bullet {
background: var(--green);
}
.swiper-pagination-bullet-active {
background: var(--light-green);
}
`;
const CertificationCard = styled.a`
display: flex;
flex-direction: column;
background: var(--lightest-navy);
padding: 1rem;
border-radius: var(--border-radius);
transition: transform 0.3s ease, background 0.3s ease, box-shadow 0.3s ease;
text-decoration: none;
color: inherit;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
position: relative;
opacity: 0;
transform: translateY(20px);
${props =>
!props.prefersReducedMotion
? css`
animation: ${fadeIn} 0.5s ease-in-out forwards;
`
: css`
opacity: 1;
transform: translateY(0);
`}
&:hover,
&:focus {
background: var(--navy);
transform: translateY(-3px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
border-left: 3px solid var(--green);
}
&:hover .image-container img,
&:focus .image-container img {
transform: scale(1.05);
}
.image-container {
width: 100%;
height: 120px;
margin-bottom: 0.75rem;
overflow: hidden;
border-radius: var(--border-radius);
background: #fff;
img {
width: 100%;
height: 100%;
object-fit: contain;
transition: transform 0.3s ease;
}
}
.details {
flex: 1;
display: flex;
flex-direction: column;
.header {
display: flex;
flex-direction: column;
margin-bottom: 0.5rem;
.title {
font-size: 1.1rem;
font-weight: 700;
margin-bottom: 0.3rem;
color: var(--green);
}
.issuer {
font-size: 0.8rem;
color: var(--slate);
}
}
.date {
font-size: 0.75rem;
color: var(--light-slate);
margin-bottom: 0.5rem;
}
.description {
font-size: 0.8rem;
color: var(--light-slate);
line-height: 1.3;
/* Optional: Add more styling or limit lines */
}
}
`;
const CertificationsCarousel = styled(Swiper)`
width: 100%;
padding: 2rem 0;
.swiper-slide {
display: flex;
justify-content: center;
}
`;
const CertificationsListComponent = () => {
const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => {
if (prefersReducedMotion) {
return;
}
sr.reveal('.certification-card', srConfig());
}, [prefersReducedMotion]);
return (
<StyledCertificationsSection id="certifications">
<h2 className="numbered-heading">About Me</h2>
<div className="heading">Certifications</div>
<CertificationsCarousel
modules={[Navigation, Pagination, A11y, Autoplay]}
spaceBetween={30}
slidesPerView={3}
navigation
pagination={{ clickable: true }}
autoplay={{
delay: 3000,
disableOnInteraction: false,
}}
loop
breakpoints={{
// When window width is >= 320px
320: {
slidesPerView: 1,
spaceBetween: 20,
},
// When window width is >= 480px
480: {
slidesPerView: 2,
spaceBetween: 20,
},
// When window width is >= 768px
768: {
slidesPerView: 3,
spaceBetween: 30,
},
}}
>
{certifications.map(cert => (
<SwiperSlide key={cert.id}>
<CertificationCard
href={cert.url}
target="_blank"
rel="noopener noreferrer"
className="certification-card"
prefersReducedMotion={prefersReducedMotion}
>
<div className="image-container">
<img src={cert.image} alt={`${cert.name} logo`} />
</div>
<div className="details">
<div className="header">
<h3 className="title">{cert.name}</h3>
<span className="issuer">{cert.issuer}</span>
</div>
<div className="date">{cert.date}</div>
<div className="description">{cert.description}</div>
</div>
</CertificationCard>
</SwiperSlide>
))}
</CertificationsCarousel>
</StyledCertificationsSection>
);
};
export default CertificationsListComponent;
3. Explanation of Changes
-
Installing and Importing Swiper:
- Installation: Ensure Swiper is installed via npm or yarn.
- Imports: Import necessary Swiper React components (
Swiper, SwiperSlide) and modules (Navigation, Pagination, A11y, Autoplay). Also, import the Swiper CSS styles.
-
Styled Components Adjustments:
- StyledCertificationsSection: Added styles to override Swiper's default navigation and pagination colors to match your theme.
- CertificationsCarousel: Created a styled version of Swiper to manage the carousel's width and padding. Also styled the Swiper slides to center the certification cards.
-
Responsive Breakpoints:
- Defined breakpoints in the Swiper component to adjust the number of visible slides based on the viewport width:
- < 480px: 1 slide per view (ideal for mobile devices).
- 480px - 768px: 2 slides per view (tablets).
- >= 768px: 3 slides per view (desktops).
-
Swiper Configuration:
- Modules: Included
Navigation, Pagination, A11y, and Autoplay for enhanced user experience.
- spaceBetween: Set the space between slides to 30px for desktop and adjusted for smaller screens.
- navigation: Enabled next and previous arrows.
- pagination: Enabled clickable pagination bullets.
- autoplay: Enabled automatic slide transitions with a 3-second delay, which doesn't disable on user interaction.
- loop: Enabled continuous loop mode.
-
CertificationCard Integration:
- Wrapped each
CertificationCard within a SwiperSlide to make it a part of the carousel.
-
Accessibility and Animation:
- Maintained the existing scroll reveal (
sr.reveal) and conditional animations based on user preferences for reduced motion.
4. Additional Styling (Optional)
You can further customize the Swiper's navigation buttons and pagination bullets to better fit your design. For example, you might want to adjust the size, color, or position of these elements. Here's how you can do it in your styled-components:
const StyledCertificationsSection = styled.section`
/* ... existing styles ... */
/* Customize Swiper Navigation Buttons */
.swiper-button-prev,
.swiper-button-next {
color: var(--green);
width: 40px;
height: 40px;
background: rgba(255, 255, 255, 0.8);
border-radius: 50%;
&::after {
font-size: 20px;
}
&:hover {
background: var(--navy);
color: var(--light-green);
}
}
/* Customize Swiper Pagination Bullets */
.swiper-pagination-bullet {
background: var(--green);
opacity: 0.7;
}
.swiper-pagination-bullet-active {
background: var(--light-green);
opacity: 1;
}
`;
5. Final Touches
Ensure that your CSS variables (e.g., --green, --navy, --light-green) are correctly defined in your global styles to match the desired theme. Also, verify that the certification images are optimized for better performance and visual appeal.
6. Testing
After implementing the carousel, test it across various devices and screen sizes to ensure responsiveness and smooth interactions. Pay special attention to touch interactions on mobile devices and the accessibility of navigation controls.
7. Complete Example
For your convenience, here's the complete updated certifications.js file with all the changes incorporated:
// src/components/sections/certifications.js
import React, { useEffect } from 'react';
import styled, { css, keyframes } from 'styled-components';
import { srConfig } from '@config';
import sr from '@utils/sr';
import { usePrefersReducedMotion } from '@hooks';
// Swiper imports
import { Swiper, SwiperSlide } from 'swiper/react';
import { Navigation, Pagination, A11y, Autoplay } from 'swiper';
// Swiper styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
// Optional: Define FadeIn Animation for future use
const fadeIn = keyframes`
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
// Dummy Certification Data (10 Certifications)
const certifications = [
// ... your certification objects ...
// Ensure unique IDs
{
id: 1,
name: 'Certified React Developer',
issuer: 'React Training',
image: 'https://via.placeholder.com/150x100',
date: 'June 2023',
description:
'Completed advanced React development training covering hooks, state management, and performance optimization.',
url: 'https://example.com/certificates/react-developer',
},
// Add the rest of your certifications here...
];
const StyledCertificationsSection = styled.section`
max-width: 1200px;
margin: 0 auto;
padding: 2rem 1rem;
h2.numbered-heading {
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(24px, 5vw, var(--fz-heading));
width: 100%;
margin: 40px 0;
&::before,
&::after {
content: '';
flex: 1;
border-bottom: 1px solid SlateGray;
margin: 0 15px;
}
}
.heading {
text-align: center;
margin-bottom: 2rem;
font-size: 2rem;
position: relative;
color: var(--green);
&::after {
content: '';
width: 60px;
height: 3px;
background: var(--green);
display: block;
margin: 0.5rem auto 0;
border-radius: 2px;
}
}
/* Swiper Navigation and Pagination Styles */
.swiper-button-prev,
.swiper-button-next {
color: var(--green);
width: 40px;
height: 40px;
background: rgba(255, 255, 255, 0.8);
border-radius: 50%;
&::after {
font-size: 20px;
}
&:hover {
background: var(--navy);
color: var(--light-green);
}
}
.swiper-pagination-bullet {
background: var(--green);
opacity: 0.7;
}
.swiper-pagination-bullet-active {
background: var(--light-green);
opacity: 1;
}
`;
const CertificationCard = styled.a`
display: flex;
flex-direction: column;
background: var(--lightest-navy);
padding: 1rem;
border-radius: var(--border-radius);
transition: transform 0.3s ease, background 0.3s ease, box-shadow 0.3s ease;
text-decoration: none;
color: inherit;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
position: relative;
opacity: 0;
transform: translateY(20px);
${props =>
!props.prefersReducedMotion
? css`
animation: ${fadeIn} 0.5s ease-in-out forwards;
`
: css`
opacity: 1;
transform: translateY(0);
`}
&:hover,
&:focus {
background: var(--navy);
transform: translateY(-3px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
border-left: 3px solid var(--green);
}
&:hover .image-container img,
&:focus .image-container img {
transform: scale(1.05);
}
.image-container {
width: 100%;
height: 120px;
margin-bottom: 0.75rem;
overflow: hidden;
border-radius: var(--border-radius);
background: #fff;
img {
width: 100%;
height: 100%;
object-fit: contain;
transition: transform 0.3s ease;
}
}
.details {
flex: 1;
display: flex;
flex-direction: column;
.header {
display: flex;
flex-direction: column;
margin-bottom: 0.5rem;
.title {
font-size: 1.1rem;
font-weight: 700;
margin-bottom: 0.3rem;
color: var(--green);
}
.issuer {
font-size: 0.8rem;
color: var(--slate);
}
}
.date {
font-size: 0.75rem;
color: var(--light-slate);
margin-bottom: 0.5rem;
}
.description {
font-size: 0.8rem;
color: var(--light-slate);
line-height: 1.3;
/* Optional: Add more styling or limit lines */
}
}
`;
const CertificationsCarousel = styled(Swiper)`
width: 100%;
padding: 2rem 0;
.swiper-slide {
display: flex;
justify-content: center;
}
`;
const CertificationsListComponent = () => {
const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => {
if (prefersReducedMotion) {
return;
}
sr.reveal('.certification-card', srConfig());
}, [prefersReducedMotion]);
return (
<StyledCertificationsSection id="certifications">
<h2 className="numbered-heading">About Me</h2>
<div className="heading">Certifications</div>
<CertificationsCarousel
modules={[Navigation, Pagination, A11y, Autoplay]}
spaceBetween={30}
slidesPerView={3}
navigation
pagination={{ clickable: true }}
autoplay={{
delay: 3000,
disableOnInteraction: false,
}}
loop
breakpoints={{
320: {
slidesPerView: 1,
spaceBetween: 20,
},
480: {
slidesPerView: 2,
spaceBetween: 20,
},
768: {
slidesPerView: 3,
spaceBetween: 30,
},
}}
>
{certifications.map(cert => (
<SwiperSlide key={cert.id}>
<CertificationCard
href={cert.url}
target="_blank"
rel="noopener noreferrer"
className="certification-card"
prefersReducedMotion={prefersReducedMotion}
>
<div className="image-container">
<img src={cert.image} alt={`${cert.name} logo`} />
</div>
<div className="details">
<div className="header">
<h3 className="title">{cert.name}</h3>
<span className="issuer">{cert.issuer}</span>
</div>
<div className="date">{cert.date}</div>
<div className="description">{cert.description}</div>
</div>
</CertificationCard>
</SwiperSlide>
))}
</CertificationsCarousel>
</StyledCertificationsSection>
);
};
export default CertificationsListComponent;
8. Conclusion
By integrating Swiper into your Certifications section, you've transformed a static grid layout into an interactive and visually appealing carousel. This not only enhances the user experience but also ensures that your certifications are showcased effectively across all device sizes. Feel free to further customize the Swiper settings and styles to perfectly match your website's design and functionality requirements.