Detalhes do pacote

react-id-swiper

kidjp85114.6kMIT4.0.0

ReactJs component for iDangerous Swiper

iDangerous, Swiper, Reactjs

readme (leia-me)

npm Version Coverage Status Weekly download Total Downloads Build Status

Package Quality

react-id-swiper ( Newest version 4.0.0 )

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. Swiper does work well with Gatsby.

Props

Name Type Default value Description
ContainerEl String 'div' Element type for container
containerClass String swiper-container Swiper container class name
WrapperEl String 'div' Element type for wrapper
wrapperClass String swiper-wrapper Swiper wrapper class name
slideClass String swiper-slide Swiper slide class name
shouldSwiperUpdate Boolean false Update swiper when component is updated
rebuildOnUpdate Boolean false Rebuild swiper when component is updated
noSwiping Boolean false Disable swiping by condition
activeSlideKey String null Initial slide index
renderPrevButton function Render props function for prev button
renderNextButton function Render props function for next button
renderScrollbar function Render props function for scrollbar
renderPagination function Render props function for pagination
renderParallax function Render props function for parallax

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

Custom build extra props

Name Type Default value Description
Swiper Class Swiper class
modules array Array 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
  • <= 3.x documentation

Documentation

Installation and setup

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

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@3.0.0/lib/react-id-swiper.js"></script>
<script src="https://unpkg.com/react-id-swiper@3.0.0/lib/react-id-swiper.min.js"></script>

Styling

Swiper stylesheet file is required

Use Swiper stylesheet file from CDN

<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.css">
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">

Or from Swiper package

You should import directly from Swiper package which supports css, scss and less

css

import 'swiper/css/swiper.css'

scss

import 'swiper/swiper.scss'

less

import 'swiper/swiper.less'

Examples

Numerous live examples:

Navigation, Pagination, Pagination / Dynamic Bullets, Progress Pagination, Fraction Pagination, Custom Pagination, Scrollbar, Vertical slider, Space Between Slides, Mutiple Slides Per View, Auto Slides Per View / Carousel Mode, Centered Slides, Centered Slides + Auto Slides Per View, Free Mode / No Fixed Positions, Scroll Container, Multiple Row Slides Layout, Nested Swipers, Grab Cursor, Loop Mode / Infinite Loop, Loop Mode With Multiple Slides Per Group, Fade Effect, 3D Cube Effect, 3D Coverflow Effect, 3D Flip Effect, Mousewheel-control, Auto Play, Thumbs Gallery With Two-way Control, RTL Layout, Parallax, Lazyload Image, Responsive Breakpoints, Manipulating component outside Swiper, Customized Component

Default

import React from 'react';
import Swiper from 'react-id-swiper';
import 'swiper/css/swiper.css';

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, useRef } from 'react';
import Swiper from 'react-id-swiper';

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

  const ref = useRef(null);

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

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

  return (
    <div>
      <Swiper ref={ref}>
        <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 ReactIdSwiperCustom from 'react-id-swiper/lib/ReactIdSwiper.custom';
import { Swiper, Navigation, Pagination } from 'swiper/js/swiper.esm';

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 :thumbsdown:

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

GOOD CODE :thumbsup:

<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.

Buy me a coffee

Buy Me A Coffee

License

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

changelog (log de mudanças)

Change Log for React-id-swiper

v4.0.0 - Released on Jun 22th, 2020

  • Upgrade to support Swiper newest version
  • Remove getSwiper props
  • Allow to access Swiper instance from ref
  • Upgrade npm packages

v3.0.0 - Released on Feb 29th, 2020

  • Upgrade to support Swiper version 5.3.0
  • Replace createRef with useRef for swiper node ref
  • Upgrade npm packages

v2.4.0 - Released on Nov 7th, 2019

  • Upgrade to support Swiper version 5.x
  • Drop support for Swiper's stylesheet
  • Upgrade npm packages

v2.3.2 - Released on July 24th, 2019

  • Upgrade npm packages
  • Replace tslint with eslint

v2.3.1 - Released on June 27th, 2019

  • Fix bug 276 for rebuildOnUpdate and shouldSwiperUpdate not working properly.

v2.3.0 - Released on June 27th, 2019

  • Use Swiper full build version as default instead of react-id-swiper/lib/react-id-swiper.full
  • Provide custom build version which allows to use Swiper class as props instead of importing directly from swiper/dist/js/swiper.esm
  • Stylesheet files are now can be loaded from react-id-swiper/lib/styles

v2.1.2 - Released on April 16th, 2019

  • Fix rendering issue for styled-component element

v2.1.1 - Released on Mar 26th, 2019

  • Add full version back to lib folder

v2.1.0 - Released on Mar 15th, 2019

  • :bomb: Great news - Add new props modules that allows to import only necessary swiper modules.
  • Fix activeSlideKey does not work correctly when loop: true

v2.0.0 - Released on Mar 14th, 2019

  • Add tests
  • Update README

v2.0.0-beta - Released on Mar 13th, 2019

  • Rewrite completely new package with Typescript + React Hooks Apis
  • Use swiper as peer-dependencies
  • Add new prop getSwiper function that returns Swiper instance.
  • Drop custom build for lightweight version
  • Drop swiper from standalone umd build
  • Add swiper@4.5.0 stylesheet files

v1.6.9 - Released on Feb 24th, 2019

v1.6.8 - Released on Sep 20th, 2018

  • Upgrade swiper@4.4.1
  • Put src/styles folder back which only support css, scss
  • Update README

v1.6.7 - Released on Sep 2nd, 2018

  • Upgrade swiper@4.3.5
  • Fix typo for README
  • Remove src/styles folder
  • Update test

v1.6.6 - Released on June 17th, 2018

  • Upgrade swiper@4.3.3 for custom build
  • Remove support for parallax in lightweight version
  • Update test

v1.6.5 - Released on June 14th, 2018

  • Upgrade swiper@4.3.3

v1.6.4 - Released on May 18th, 2018

  • Provide lightweight version which drops those features below
    • Virtual
    • Keyboard
    • Mouse wheel
    • Zoom
    • Lazy load image
    • A11y
    • History
    • Hash-navigation
    • Effect-cube
    • Effect-flip
    • Effect-coverflow

v1.6.3 - Released on May 3rd, 2018

  • Upgrade swiper@4.2.6
  • Deprecate these props:
    • renderCustomPrevButton
    • renderCustomNextButton
    • renderCustomScrollbar
    • renderCustomPagination
    • renderCustomParallax
    • prevButtonCustomizedClass,
    • nextButtonCustomizedClass,
    • paginationCustomizedClass,
    • scrollbarCustomizedClass
  • Add new render props:
    • renderPrevButton
    • renderNextButton
    • renderScrollbar
    • renderPagination
    • renderParallax
  • Clean code
  • Update unit test

v1.6.2 - Released on April 1st, 2018

  • Upgrade swiper@4.2.2
  • Update unit test

v1.6.1 - Released on February 17th, 2018

  • Upgrade swiper@4.1.6
  • Add unit test

v1.5.8 - Released on January 24th, 2018

  • Upgrade swiper@4.1.0
  • Fix bug for UMD build

v1.5.7 - Released on December 20th, 2017

  • Add umd version
  • Add new params
    • ContainerEl
    • WrapperEl
    • renderCustomPrevButton
    • renderCustomNextButton
    • renderCustomScrolbar
    • renderCustomPagination
    • renderCustomParallax

v1.5.6 - Released on December 1st, 2017

  • Upgrade Swiper@4.0.7
  • Add jest. Expose rebuildSwiper instance method PR 108 by timkindberg

v1.5.5 - Released on November 15, 2017

  • Upgrade Swiper@4.0.6
  • Add support for parallax