|
| 1 | +import 'swiper/css'; |
| 2 | +import 'swiper/css/navigation'; |
| 3 | +import 'swiper/css/pagination'; |
| 4 | +import 'swiper/css/effect-fade'; |
| 5 | +import { Swiper, SwiperSlide } from 'swiper/react'; |
| 6 | +import { Autoplay, Pagination } from 'swiper/modules'; |
| 7 | +import { useState, useEffect, useRef } from 'react'; |
| 8 | +import styles from './styles.module.css'; |
| 9 | +import { SlideButton } from './SlideButton'; |
| 10 | +import { SlideContent } from './SlideContent'; |
| 11 | +import slides from '@site/static/data/blogPosts.json'; |
| 12 | + |
| 13 | +function getWindowDimensions() { |
| 14 | + const { innerWidth: width, innerHeight: height } = window; |
| 15 | + return { |
| 16 | + width, |
| 17 | + height, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +export default function useWindowDimensions() { |
| 22 | + const [windowDimensions, setWindowDimensions] = useState( |
| 23 | + getWindowDimensions() |
| 24 | + ); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + function handleResize() { |
| 28 | + setWindowDimensions(getWindowDimensions()); |
| 29 | + } |
| 30 | + |
| 31 | + window.addEventListener('resize', handleResize); |
| 32 | + return () => window.removeEventListener('resize', handleResize); |
| 33 | + }, []); |
| 34 | + |
| 35 | + return windowDimensions; |
| 36 | +} |
| 37 | + |
| 38 | +export const Carousel = ({ ...props }) => { |
| 39 | + const swiperRef = useRef(null); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <div className={styles.titleContainer}> |
| 44 | + <h2 className={styles.title}> |
| 45 | + Learn more about React Native ExecuTorch |
| 46 | + </h2> |
| 47 | + </div> |
| 48 | + <div className={styles.container}> |
| 49 | + <SlideButton |
| 50 | + forward={false} |
| 51 | + onClick={() => swiperRef.current?.swiper?.slidePrev()} |
| 52 | + /> |
| 53 | + <Swiper |
| 54 | + modules={[Pagination, Autoplay]} |
| 55 | + ref={swiperRef} |
| 56 | + spaceBetween={20} |
| 57 | + slidesPerView={1} |
| 58 | + pagination={{ |
| 59 | + clickable: true, |
| 60 | + dynamicBullets: true, |
| 61 | + }} |
| 62 | + breakpoints={{ |
| 63 | + 768: { slidesPerView: 2 }, |
| 64 | + 1280: { slidesPerView: 3 }, |
| 65 | + }} |
| 66 | + autoplay={{ |
| 67 | + delay: 4000, |
| 68 | + disableOnInteraction: false, |
| 69 | + }} |
| 70 | + loop={true} |
| 71 | + className={styles.swiper} |
| 72 | + {...props} |
| 73 | + > |
| 74 | + {slides.map((slide) => ( |
| 75 | + <SwiperSlide key={slide.id}> |
| 76 | + <SlideContent slide={slide} /> |
| 77 | + </SwiperSlide> |
| 78 | + ))} |
| 79 | + </Swiper> |
| 80 | + <SlideButton |
| 81 | + forward={true} |
| 82 | + onClick={() => swiperRef.current?.swiper?.slideNext()} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +}; |
0 commit comments