2.3.2 • Published 5 years ago

@class101/swiper v2.3.2

Weekly downloads
16
License
MIT
Repository
github
Last release
5 years ago

npm Version Coverage Status npm Downloads Build Status Greenkeeper badge

Package Quality

react-id-swiper ( Newest version 2.3.2 )

A library to use Swiper as a ReactJs component

Demo

What is Swiper?

Swiper - is the free and most modern mobile touch slider with hardware accelerated transitions and amazing native behavior. It is intended to be used in mobile websites, mobile web apps, and mobile native/hybrid apps. Designed mostly for iOS, but also works great on latest Android, Windows Phone 8 and modern Desktop browsers Swiper is not compatible with all platforms, it is a modern touch slider which is focused only on modern apps/platforms to bring the best experience and simplicity.

Props

NameTypeDefault valueDescription
ContainerElString'div'Element type for container
containerClassStringswiper-containerSwiper container class name
WrapperElString'div'Element type for wrapper
wrapperClassStringswiper-wrapperSwiper wrapper class name
slideClassStringswiper-slideSwiper slide class name
shouldSwiperUpdateBooleanfalseUpdate swiper when component is updated
rebuildOnUpdateBooleanfalseRebuild swiper when component is updated
noSwipingBooleanfalseDisable swiping by condition
activeSlideKeyStringnullInitial slide index
renderPrevButtonfunctionRender props function for prev button
renderNextButtonfunctionRender props function for next button
renderScrollbarfunctionRender props function for scrollbar
renderPaginationfunctionRender props function for pagination
renderParallaxfunctionRender props function for parallax
getSwiperfunctionCallback function that returns Swiper instance

If you want to use Swiper custom build to reduce bundle size, you need to use extra props below.

Custom build extra props

NameTypeDefault valueDescription
SwiperClassSwiper class
modulesarrayArray of Swiper necessary modules

NOTE:

  • You can also use Swiper's original params too. Swiper API documentation HERE
  • Find more info about Swiper custom build HERE

Installation and setup

From version 2.0.0, it requires React & ReactDOM ver >=16.8.0 to use Hooks

Npm package

By npm

npm install --save react-id-swiper@latest swiper@latest

By Yarn

yarn add react-id-swiper@latest swiper@latest

CDN

<script src="https://unpkg.com/react-id-swiper@2.3.2/lib/react-id-swiper.js"></script>
<script src="https://unpkg.com/react-id-swiper@2.3.2/lib/react-id-swiper.min.js"></script>

Styling

Swiper stylesheet file is required

Use Swiper stylesheet file from CDN or react-id-swiper/lib/styles/ (supporting css, scss)

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.min.css">

Examples here

Default

import React from 'react';
import Swiper from 'react-id-swiper';

const SimpleSwiper = () => (
  <Swiper>
    <div>Slide 1</div>
    <div>Slide 2</div>
    <div>Slide 3</div>
    <div>Slide 4</div>
    <div>Slide 5</div>
  </Swiper>
)

export default SimpleSwiper;

Using params

import React from 'react';
import Swiper from 'react-id-swiper';

const SimpleSwiperWithParams = () => {
  const params = {
    pagination: {
      el: '.swiper-pagination',
      type: 'bullets',
      clickable: true
    },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    spaceBetween: 30
  }

  return(
    <Swiper {...params}>
      <div>Slide 1</div>
      <div>Slide 2</div>
      <div>Slide 3</div>
      <div>Slide 4</div>
      <div>Slide 5</div>
    </Swiper>
  )
}

export default SimpleSwiperWithParams;

Manipulating swiper from outside swiper component

import React, { useState } from 'react';
import Swiper from 'react-id-swiper';

const ManipulatingSwiper = () => {
  const [swiper, updateSwiper] = useState(null);

  const goNext = () => {
    if (swiper !== null) {
      swiper.slideNext();
    }
  };

  const goPrev = () => {
    if (swiper !== null) {
      swiper.slidePrev();
    }
  };

  return (
    <div>
      <Swiper getSwiper={updateSwiper}>
        <div>Slide 1</div>
        <div>Slide 2</div>
        <div>Slide 3</div>
        <div>Slide 4</div>
        <div>Slide 5</div>
      </Swiper>
      <button onClick={goPrev}>Prev</button>
      <button onClick={goNext}>Next</button>
    </div>
  );
};

export default ManipulatingSwiper;

Custom build Swiper

You can find the WORKING DEMO REPO HERE

import React from 'react';
import { Swiper, Navigation, Pagination } from 'swiper/dist/js/swiper.esm.js';
import ReactIdSwiperCustom from 'react-id-swiper/lib/ReactIdSwiper.custom';

const CustomBuildSwiper = () => {
  const params = {
    // Provide Swiper class as props
    Swiper,
    // Add modules you need
    modules: [Navigation, Pagination],
    pagination: {
      el: '.swiper-pagination',
      type: 'bullets',
      clickable: true
    },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    spaceBetween: 30
  }

  return(
    <ReactIdSwiperCustom {...params}>
      <div>Slide 1</div>
      <div>Slide 2</div>
      <div>Slide 3</div>
      <div>Slide 4</div>
      <div>Slide 5</div>
    </ReactIdSwiperCustom>
  )
}

export default CustomBuildSwiper;

NOTE:

  • If you use Webpack & Babel you need to setup Babel loader config in webpack.config.js like below:
module: {
  rules: [
    {
      exclude: [/node_modules\/(?!(swiper|dom7)\/).*/, /\.test\.js(x)?$/],
      test: /\.js(x)?$/,
      use: [{ loader: 'babel-loader' }],
    }
  ],
}

Adding customized css classes

const params = {
  pagination: {
    el: '.swiper-pagination.customized-swiper-pagination',
  }, // Add your class name for pagination container
  navigation: {
    nextEl: '.swiper-button-next.customized-swiper-button-next', // Add your class name for next button
    prevEl: '.swiper-button-prev.customized-swiper-button-prev' // Add your class name for prev button
  },
  containerClass: 'customized-swiper-container' // Replace swiper-container with customized-swiper-container
}

Adding customized components

For customized rendering to work, you have to use same classname with params el.

const params = {
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev'
  },
  renderPrevButton: () => <button className="swiper-button-prev">Prev</button>,
  renderNextButton: () => <button className="swiper-button-next">Next</button>,
}

Workable slides

Each slide should be wrapped by HTML element

BAD CODE

<Swiper {...params}>
  Slide content
</Swiper>

GOOD CODE

<Swiper {...params}>
  <span>Slide content</span>
</Swiper>

Bug report

Please use the prepared Codesanbox below to reproduce your issue. Thank you!!

Edit ReactIdSwiper - DEMO

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

2.3.2-test15

5 years ago

2.3.2-test13

5 years ago

2.3.2-test12

5 years ago

2.3.2-test11

5 years ago

2.3.2-test10

5 years ago

2.3.2-test9

5 years ago

2.3.2-test8

5 years ago

2.3.2-test7

5 years ago

2.3.2-test6

5 years ago

2.3.2-test5

5 years ago

2.3.2-test4

5 years ago

2.3.2-test3

5 years ago

2.3.2-test2

5 years ago

2.3.2-test

5 years ago

2.3.2

5 years ago