Coder Social home page Coder Social logo

react-swiper's Introduction

Important steps:

  1. import Swiper component
  2. import useSwiper hook
  3. setup CSS files for the swiper component

NPM URL

How to use:

Install npm package

npm i @glitch-txs/react-swiper

import Swiper component and useSwiper hook:

import { Swiper, useSwiper } from '@glitch-txs/react-swiper'

Set up the swiper, you will need to pass CSS classes to the swiper to be able to have full control on styling (wrapperClass and containerClass).

Here I'm using CSS modules:

export default function YourComponent() {

  const { provider, handleNext, handlePrev } = useSwiper()
  
  return (
    <div className={styles.container}>

        <Swiper provider={provider} wrapperClass={styles.wrapperClass} containerClass={styles.containerClass}>

            Your slides here...

        </Swiper>

        <div className={styles.controlsContainer} >
            <button onClick={handlePrev}>
                Left
            </button>

            <button onClick={handleNext}>
                Right
            </button>
        </div>

    </div>    

Clickable

If you want to have clickable components inside the swiper use the prop clickable.

If you are using images remember to add the prop draggable={false}

        <Swiper provider={provider} clickable wrapperClass={styles.wrapperClass} containerClass={styles.containerClass}>

          <Image src='mySrc' draggable={false} alt='myImg' />

        </Swiper>

Adding auto-slide:

  const { provider, handleNext, handlePrev, handleGoTo, autoStart, autoStop, inView } = useSwiper()

  useEffect(()=>{

    //the auto-slide will only be active when the component is in view.
    if(inView){
      //the argument taken by autoStart is the time in miliseconds for each slide. 
      autoStart()
    } else {
      autoStop()
    }

    //autoStart uses eventListeners so we need to pass the same params used when returning.
    return ()=> autoStop()

  },[inView])

CSS files:

The Swiper component is built with two HTML Div's. One is the window (it will have overflow: hidden, so you will only see through it) that will wrap everything. And the child div that will be the container for all your slides, this one will be hidden by the most part.

.wrapperClass{
    /* the width and height must be equal to each slide individuly including their margins */
    width: 100%;
    max-width: 1000px;
    height: 250px;

    /* Needed overflow hidden - comment this line to see how the swiper works internaly */
    overflow: hidden;
}

.containerClass{
    /* Display flex will align the slides elements in one row */
    display: flex;

    /* Width is calculated multiplying the full width of the parent container by the number of slider elements inside the swiper,
    this way THIS container will take the width of the sum of all children elements. Width: calc(100% * numberOfCards) */

    /* Here there's number four because I'm using 4 cards */
    width: calc(100% * 4);
    height: 100%;

    z-index: 10;
}

Progress Points/bars

if you want to add a fixed component INSIDE the swiper you can pass a JSX variable to the component as progress

Hooks functions and variables

  • provider
  • handleNext
  • handlePrev
  • handleGoTo
  • autoStart
  • autoStop
  • inView
  • currentIndex

useSwiper arguments (Not Mandatory)

useSwiper hook takes an object as arguments with the following vaiables:

rerender: default = true, (This will render the whole component each time the currentIndex changes, you can turn this off if you're not using curentIndex variable)

widthOffset: default = 0, ( margin of the sliders if any. E.g: margin: 0 5px 0 5px ==> widthOffset = 10 )

transitionTime: default = 300 (The time in miliseconds it takes to slide)

animationTime: default = 4000 (The time in miliseconds it will automatically move to the next slide)

async: default = false (if contenct is async set this to true and add a conditional for the Swipper component to render, you won't need to add width to the containerClass in this case.)

example:

export default function YourComponent() {

  const { provider } = useSwiper({ async: true })

  const [isData, setIsData] = useState<boolean>(false)

  useEffect(()=>{
    setIsData(true)
  },[isData])

  
  return (

    {
      isData && <Swiper provider={provider} wrapperClass={s.container} containerClass={s.slideContainer}>

        { data?.map((item, index)=>(
            <div key={index} className={s.slide}>

                Content Here

            </div>
        ))}

      </Swiper>
    }

  )
}

All functions must be disabled until the content is loaded.

GitHub examples are currently outdated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.