How to add a fade transition while updating the content react - css

I'm trying to create a slider with react. I've an array of slides. I'm using useState to store the active slide and then updating it every 3 seconds with useEffect.
const [activeSlide, setActiveSlide] = useState(slides[2]);
useEffect(() => {
setTimeout(() => {
setActiveSlide((prevState) => slides[prevState.next]);
}, 5000);
}, [activeSlide]);
using active slide in jsx like so:
<h4 className='font-medium text-gray-600 dark:text-gray-200'>{activeSlide.desc}</h4>
I'm not sure how can I add a fade effect when slide is updating. any suggestions ?

If you are improving your CSS skills, please don't regard my suggestion. Otherwise, you can use Crousel from react-slick.
There is an example, code took from material-kit-react:
Import the react-slick:
import Carousel from "react-slick";
Define settings with autoplay:true and fade:true:
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
fade: true
};
The return function in material-kit-react:
return (
<div className={classes.section}>
<div className={classes.container}>
<GridContainer>
<GridItem xs={12} sm={12} md={8} className={classes.marginAuto}>
<Card carousel>
<Carousel {...settings}>
<div>
<img src={image1} alt="First slide" className="slick-image" />
<div className="slick-caption">
<h4>
<LocationOn className="slick-icons" />
Yellowstone National Park, United States
</h4>
</div>
</div>
<div>
<img
src={image2}
alt="Second slide"
className="slick-image"
/>
<div className="slick-caption">
<h4>
<LocationOn className="slick-icons" />
Somewhere Beyond, United States
</h4>
</div>
</div>
<div>
<img src={image3} alt="Third slide" className="slick-image" />
<div className="slick-caption">
<h4>
<LocationOn className="slick-icons" />
Yellowstone National Park, United States
</h4>
</div>
</div>
</Carousel>
</Card>
</GridItem>
</GridContainer>
</div>
</div>
);
Just change it and ready to go. I wish that help you and save your time.

Related

Sitecore NextJs Carousel

I want to build a Carousel in Sitecore NextJs. I found a carousel package to do so, but it hardcodes the number of slides. Since the author will create the slides dynamically in Sitecore, my NextJs component should be smart enough to detect the number of "child slides" added to the Carousel and render them accordingly.
This is the example I found online:
import React, { Component } from 'react';
import "react-responsive-carousel/lib/styles/carousel.min.css";
import { Carousel } from 'react-responsive-carousel';
export default class NextJsCarousel extends Component {
render() {
return (
<div>
<h2>NextJs Carousel - GeeksforGeeks</h2>
<Carousel>
<div>
<img src="/1.png" alt="image1"/>
<p className="legend">Image 1</p>
</div>
<div>
<img src="/2.png" alt="image2" />
<p className="legend">Image 2</p>
</div>
<div>
<img src="/3.png" alt="image3"/>
<p className="legend">Image 3</p>
</div>
<div>
<img src="/4.png" alt="image4"/>
<p className="legend">Image 4</p>
</div>
<div>
<img src="/5.png" alt="image5"/>
<p className="legend">Image 5</p>
</div>
</Carousel>
</div>
);
}
};
Instead, I want the Slides within the Carousel to be dynamic and use a loop based on how many slides are added under my Carousel component in Sitecore.
I am using react-slick for my carousels you may follow something similar approach as i did for your react-responsive-carousel.
My Slider Component:
export const MainSlider: FC<SliderProps> = ({
settings,
data,
type,
className,
}) => {
if (!(data.length > 0)) return null;
return (
<Fragment>
<div className="mian-slider">
<div className="container cus-container">
<div className="main-sec-slider">
<div className="img-box-back" />
<SlickSlider {...settings} className={className}>
{data.map((value, index) => {
return (
<div key={index}>
{type === "main-carousel"
? InnerSlider.Main(value)
: InnerSlider.Article(value)}
</div>
);
})}
</SlickSlider>
</div>
</div>
</div>
</Fragment>
);
};
Inside map function you can pass another component as i did with name InnerSlider which contains html code for your carousel images.
In your case you need to pass this:
<div>
<img src="/1.png" alt="image1"/>
<p className="legend">Image 1</p>
</div>
at last pass props data for MainSlider component :
<MainSlider
settings={settings}
data={[YOUR_SLIDER_DATA_ARRAY]}
type="main-carousel"
className="slider-main-text"
/>

Why am I getting Next Js hydration error?

I am trying to make an web app and using react swiper fot it, but it is causing hydration. When I comment out the react swiper then I don't see the error.
I am getting "Warning: Text content did not match. Server: "Zach N." Client: "Lisa A." in the console.
Can someone help me to fix it????
Why am I getting this error and how to solve it?
thanks in advance.
// Slider
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import AOS from "aos";
import "aos/dist/aos.css"; // You can also use <link> for styles
function Testimonials() {
const sliderRef = useRef(null);
useEffect(() => {
AOS.init({
duration: 1500,
});
});
const handlePrev = useCallback(() => {
if (!sliderRef.current) return;
sliderRef.current.swiper.slidePrev();
}, []);
const handleNext = useCallback(() => {
if (!sliderRef.current) return;
sliderRef.current.swiper.slideNext();
}, []);
return (
<div>
<div className="lg:h-[80vh] lg:pt-16 ">
<div data-aos="fade-up">
<h2 className="text-[40px] xl:text-[70px] leading-[44px] lx:leading-[70px] mt-[70px] mb-[15px] md:mb-[19px] font-bold font-FreightSemibold ">
Today&apos;s news.Edited to be{" "}
<span className="italic block xl:mt-7">
unbiased as humanly possible.
</span>
</h2>
</div>
<div data-aos="fade-up" className="flex lg:justify-between">
<div className="w-[600px]">
<p className="xl:text-[20px]">
Every morning, we triple check headlines, stories and sources for
bias. All by hand, no algorithms.{" "}
<span className="font-bold">See what readers are saying:</span>
</p>
</div>
<div className="hidden md:flex ">
<div
onClick={handlePrev}
className="w-[55.84px] h-[55.84px] group rounded-full flex cursor-pointer hover:bg-black md:mx-[18px] justify-center items-center ring-1 ring-black "
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-6 w-6 group-hover:text-white"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M7 16l-4-4m0 0l4-4m-4 4h18"
/>
</svg>
</div>
<div
onClick={handleNext}
className="w-[55.84px] h-[55.84px] group rounded-full flex cursor-pointer justify-center items-center hover:bg-black ring-1 ring-black "
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-6 w-6 group-hover:text-white"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M17 8l4 4m0 0l-4 4m4-4H3"
/>
</svg>
</div>
</div>
</div>
{/* Slider Here */}
<div className="md:flex">
<Swiper
ref={sliderRef}
loop={true}
pagination={{ clickable: true }}
slidesPerView={1}
spaceBetween={10}
breakpoints={{
"#0.00": {
slidesPerView: 1,
spaceBetween: 10,
},
"#0.75": {
slidesPerView: 2,
spaceBetween: 20,
},
"#1.00": {
slidesPerView: 3,
spaceBetween: 40,
},
"#1.50": {
slidesPerView: 4,
spaceBetween: 50,
},
}}
>
<SwiperSlide>
<div data-aos="fade-left" className="mt-[80px] md:mx-5 mb-[45px]">
<div className="border-black mb-[40px] border-b-[1px] ">
<h3 className="font-bold py-1">Lisa A.</h3>
</div>
<div className="mb-[40px]">
<p>
“I consider myself a centrist - all the other news sources
lean so far to the left or right, it makes my head spin.
Thank you for{" "}
<span className={`bg-[#6fef8d]`}>
providing strictly the facts
</span>{" "}
- you re the only news source that can actually pull off
neutrality these days.”
</p>
</div>
</div>
</SwiperSlide>
<SwiperSlide>
<div data-aos="fade-left" className="mt-[80px] md:mx-2 mb-[45px]">
<div className="border-black mb-[40px] border-b-[1px] ">
<h3 className="font-bold py-1">Emma S.</h3>
</div>
<div className="mb-[40px]">
<p>
“I consider myself a centrist - all the other news sources
lean so far to the left or right, it makes my head spin.
Thank you for{" "}
<span className={`bg-[#EFEF6F]`}>
providing strictly the facts
</span>{" "}
- you re the only news source that can actually pull off
neutrality these days.”
</p>
</div>
</div>
</SwiperSlide>
<SwiperSlide>
<div data-aos="fade-left" className="mt-[80px] md:mx-5 mb-[45px]">
<div className="border-black mb-[40px] border-b-[1px] ">
<h3 className="font-bold py-1">Jim M.</h3>
</div>
<div className="mb-[40px]">
<p>
“I stopped watching the news, so sick of the bias. Was
searching for an alternative that would just tell me WHAT
happened, with NO editorializing. I found it. It s called
1440. It assumes you are smart enough to{" "}
<span className={`bg-[#7EDCF2]`}>
form your own opinions.
</span>
</p>
</div>
</div>
</SwiperSlide>
<SwiperSlide>
<div data-aos="fade-left" className="mt-[80px] md:mx-5 mb-[45px]">
<div className="border-black mb-[40px] border-b-[1px] ">
<h3 className="font-bold py-1">Zach N.</h3>
</div>
<div className="mb-[40px]">
<p className="">
“I stopped my habit of spending my day doomscrolling. I
signed up for 1440 &{" "}
<span className={`bg-[#F48CD9]`}>
{" "}
feel better informed in less time.
</span>{" "}
I m also less stressed out. Thank you.
</p>
</div>
</div>
</SwiperSlide>
</Swiper>
</div>
</div>
</div>
);
}
export default Testimonials;
Replace slidesPerView={1} by slidesPerView="auto"
Also update your AOS init like this to avoid infinite loop
useEffect(() => {
AOS.init({
duration: 1500,
});
},[]);

I am importing Bootstrap in my project but it isn't working

I want to show cards in the slider. When clicking on the next carousel it will show the next card.
I am importing Bootstrap into my project but it isn't working.
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
I am using this tutorial for creating cards and sliders.
You can check the full code here
Sdata.jsx
const Sdata =[
{
imgsrc: work2,
title: "Android Application",
href: "https://mybestdjangoblog.herokuapp.com/",
},
{
imgsrc: work3,
title: "Android Application",
href: "https://mybestdjangoblog.herokuapp.com/",
}
];
Card.jsx
function Card(props){
return(
<>
<img className="card-img-top"
src={props.imgsrc} alt="" />
<div className="card-body">
<h4 className="card-title">{props.title}</h4>
<p className="card-text">Some quick example text to build on the card title and make up the bulk of the
card's content.</p>
View Project
</div>
</>
);
};
Work.jsx
const Work =() =>{
return(
<>
<div id="multi-item-example" className="carousel slide carousel-multi-item" data-ride="carousel">
<div className="controls-top">
<a className="btn-floating" href="#multi-item-example" data-slide="prev"><i className="fas fa-chevron-left"></i></a>
<a className="btn-floating" href="#multi-item-example" data-slide="next"><i
className="fas fa-chevron-right"></i></a>
</div>
<ol className="carousel-indicators">
<li data-target="#multi-item-example" data-slide-to="0" className="active"></li>
<li data-target="#multi-item-example" data-slide-to="1"></li>
</ol>
<div className="carousel-inner" role="listbox">
<div className="carousel-item active">
<div className="col-md-3" style={{ float: "left"}}>
<div className="card mb-2">
<img className="card-img-top"
src={work1} alt="" />
<div className="card-body">
<h4 className="card-title">Android Application</h4>
<p className="card-text">Some quick example text to build on the card title and make up the bulk of the
card's content.</p>
View Project
</div>
<div className="carousel-item">
<div className="col-md-3" style={{float:"left"}}>
<div className="card mb-2">
{Sdata.map((val, ind) => {
return(
<Card key={ind} imgsrc={val.imgsrc} title={val.title} href={val.href}/>
)})}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</>
npm install
will install all modules listed as dependencies in package.json.
You should install bootstrap as:
npm i react-bootstrap
npm i bootstrap
Then import styles as:
import "bootstrap/dist/css/bootstrap.min.css";

Create a dropdown animation in react

How can I create dropdown animation when I click to 3 dots and if I click one more time it will toggle the dropdown.
Here is my following code:
const [modalStatus, setModalStatus] = useState(false);
const openModal = () => {
setModalStatus(true);
};
<div className="member-edit" onClick={openModal}>
<Symlink />
</div>
{modalStatus && <TeamStatusModal />}
<div
className="member-avatar"
style={{
backgroundImage: `url(${member.User.picture})`,
}}
/>
<div className="member-description">
<p className="member-name">{member.User.fullname}</p>
<p className="member-date">
Joined on {moment(member.date_joined).format("MMMM Do, YYYY")}
</p>
</div>
Updated add TeamStatusModal.js:
const TeamStatusModal = () => {
return (
<div className="team-status-modal-container">
<div className="status">
<ProfileIcon /> <span>View Profile</span>
<hr />
</div>
<div className="status">
<MessageIcon /> <span>Message Me</span>
<hr />
</div>
<div className="status">
<ReviewIcon /> <span>Leave Review</span>
</div>
</div>
);
};
You should reorganize the logic a bit. Intead of: {modalStatus && <TeamStatusModal />} you can pass the state as a prop like this:
<TeamStatusModal active={modalStatus} />
and then you will use it in the TeamStatusModal component:
const TeamStatusModal = ({ active }) => {
return (
<div className={`team-status-modal-container ${active ? 'ACTIVE_CLASS' : ''}`}>
<div className="status">
<ProfileIcon /> <span>View Profile</span>
<hr />
</div>
<div className="status">
<MessageIcon /> <span>Message Me</span>
<hr />
</div>
<div className="status">
<ReviewIcon /> <span>Leave Review</span>
</div>
</div>
);
};
Another approach is using e.g. Framer Motion library, which allows animate removing elements from DOM.
https://www.framer.com/api/motion/animate-shared-layout/#syncing-layout-animations

Swiper No Swiping

SwiperNoSwiping.mov
https://swiperjs.com/
I am having some trouble using Swiper. When I load in raw data to Swipe, the component swipes. However, if I were to pull data from an API, the component does not swipe until there is a change in the view: such as if I were to change the size of the window or click Inspect element. I cannot figure out has to do with the event loop and the view or if it is something to do with CSS.
I have provided a link to the video in order for you to see what I am saying.
My code:
event.component.html
<swiper fxFlex="auto" [config]="config">
<div *ngFor="let event of totalEvents" class="swiper-slide cursor-pointer">
<div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
<!-- Your content goes here -->
<mat-card class="example-card">
<mat-card-header>
<img src="{{event.image}}" style="max-width:30%;max-height:35%;">
<mat-card-title>{{event.eventName}}</mat-card-title>
<mat-card-subtitle>{{event.startDate}}</mat-card-subtitle>
<mat-card-subtitle>{{event.venue}} <br>
{{event.location}}</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<p>{{event.promoter}}</p>
<p>{{event.info}}</p>
<hr>
<p>$ {{event.price}}</p>
<button *ngIf="event.url"mat-raised-button color="basic">
Buy Ticket
</button>
</mat-card-content>
</mat-card>
</div>
</div>
</swiper>
<swiper fxFlex="auto" [config]="config">
<div *ngFor="let step of something" class="swiper-slide cursor-pointer">
<div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
<!-- Your content goes here -->
<mat-card class="example-card">
<mat-card-header>
<div mat-card-avatar class="example-header-image"></div>
<mat-card-title>{{step.name}}</mat-card-title>
</mat-card-header>
<mat-card-content>
<p>
The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from Japan.
A small, agile dog that copes very well with mountainous terrain, the Shiba Inu was originally
bred for hunting.
</p>
</mat-card-content>
</mat-card>
</div>
</div>
</swiper>
event.component.ts
totalEvents: any = [];
something = [{
name: 'Test1'
},
{
name: 'Test2'
}
];
/* cardSwipe */
index;
config: SwiperConfigInterface = {
effect: 'slide',
a11y: true,
direction: 'horizontal',
slidesPerView: 3,
slideToClickedSlide: true,
mousewheel: true,
scrollbar: true,
watchSlidesProgress: true,
navigation: true,
keyboard: true,
pagination: false,
centeredSlides: true,
loop: true,
roundLengths: true,
slidesOffsetBefore: 100,
slidesOffsetAfter: 100,
spaceBetween: 50,
breakpoints: {
// when window width is >= 320px
320: {
slidesPerView: 1
}
}
};
ngOnInit() {
this.fetchEvents();
}

Resources