Swiper styles being applied globally to other carousels - Swiper Element (WebComponent) - React #6745
Unanswered
marcosriani
asked this question in
Q&A
Replies: 2 comments
-
managed to solve? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Just at a quick glance, your swiperContainer and params are named the same thing for both instances. Try changing renaming the second set |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I'm currently working on a real estate agency website and using Swiper JS as the main carousel. Since the website will have multiple carousels with different styles, I faced an issue when building the second carousel. I used the Swiper Element (WebComponent) in React and injected custom styles using injectStyles to change the styles of the swiper-pagination class. I also created divs outside the to display the navigation and pagination outside of the carousel based on the page design.
The problem arose when the styles from the first carousel were being applied to the second carousel, preventing me from overriding them. I'm seeking ideas on how to prevent this issue and ensure that each carousel has its own distinct styles. Additionally, I'm curious about the reasons why sometimes the styles applied through injectStyles are not being properly displayed.
Any suggestions or insights would be greatly appreciated. Thank you!
This is the code for the first carousel
`import React, { useRef, useEffect } from 'react';
import { register } from 'swiper/element/bundle';
import ArticleCardContainer from './ArticleCard';
import { ArrowRight, ArrowLeft } from './Icons';
register();
export const PropertiesNewsCarousel = () => {
const { articles } = Homeflow.get('latestPropertyNews');
const isBackgroundWhite = Homeflow.get('isBackgroundWhite').is_background_white;
const swiperElRef = useRef(null);
// Swiper Element web components - css to overide shadown down styles
const navigationStyles = {
position: 'static',
height: '14px',
marginTop: '0 !important',
width: 'calc(var(--swiper-navigation-size) / 44 * 27)',
zIndex: 10,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
};
useEffect(() => {
const swiperContainer = swiperElRef.current;
}, []);
return (
{articles?.map(article => (
<ArticleCardContainer imageUrl={article?.image} title={article?.title} articleUrl={
/articles/${article?.slug}
} />))}
);
};
export default PropertiesNewsCarousel;
`
and this is the code for the second
`import React, { useRef, useEffect } from 'react';
import { uniqueKey } from 'homeflowjs';
import { register } from 'swiper/element/bundle';
register();
const PromoBoxesCarousel = () => {
const promoCardsData = Homeflow.get('promoCards').promo_cards_data;
const swiperElRef = useRef(null);
// Swiper Element web components - css to overide shadown down styles
const paginationStyles = {
backgroundColor: '#fff',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '15px',
transform: 'translateX(0)',
};
useEffect(() => {
const swiperContainer = swiperElRef.current;
const params = {
grabCursor: true,
// Default parameters
slidesPerView: 1,
spaceBetween: 10,
// Responsive breakpoints
breakpoints: {
// small screens
640: {
slidesPerView: 1,
},
// medium screens
768: {
slidesPerView: 3,
},
// small laptop screens
1024: {
slidesPerView: 4,
},
// normal laptop screens
1400: {
slidesPerView: 5,
},
},
mousewheel: {
forceToAxis: true,
},
paginationClickable: true,
pagination: {
el: '.promo-swiper-paginations',
dynamicBullets: true,
clickable: true,
dynamicMainBullets: 3,
},
injectStyles: [
`
.swiper-pagination-bullet,
.swiper-pagination-bullet-active-prev,
.swiper-pagination-bullet-active-next,
.swiper-pagination-bullet-active-prev-prev,
.swiper-pagination-bullet-active-next-next {
background-color: transparent !important;
}
}, []);
return (
{promoCardsData?.map(card => (
<section className="promo-boxes__item" style={{ backgroundImage:
url(${card.image_url})
}}>{ card.title }
{ card.button_title }
);
};
export default PromoBoxesCarousel;
`
Beta Was this translation helpful? Give feedback.
All reactions