0.0.12 • Published 6 months ago

@jpd.nz/react-card-carousel v0.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

React Card Carousel

@jpd.nz/react-card-carousel

A basic card carousel for react, usable with items of either fixed or variable widths.

To play around with the example code, head over to the github repo and download the source. Change (cd) into the example/ directory, run npm install then npm run start. Open /example/src/__example.js to modify the carousel settings.

Install

Instal the package via npm or yarn:

npm i -s '@jpd.nz/react-card-carousel'
yarn add '@jpd.nz/react-card-carousel'

Usage

import { useRef } from 'react'
import CardCarousel from '@jpd.nz/react-card-carousel'

const MyComponent = () => {

  const myCarouselRef = useRef()

  const settings = {
    pagination: true,
    arrows: true,
    touchControls: true,
    buffer: 100,
    beforeChange: (currentIndex, newIndex) => {
      console.log('Before Change', currentIndex, newIndex)
    },
    afterChange: (newIndex) => {
      console.log('After Change', newIndex)
    }
  }

  const carouselItems = [
    {
      title: 'Carousel Card 01',
      image: '/path/to/my-image-01.jpg',
      alt: 'my image alt text'
    },
    {
      title: 'Carousel Card 02',
      image: '/path/to/my-image-02.jpg',
      alt: 'my image alt text'
    },
    {
      title: 'Carousel Card 03',
      image: '/path/to/my-image-03.jpg',
      alt: 'my image alt text'
    }
  ]

  return (
    <div className='my-component'>

      {/* Optionally you can define your own completely custom buttons */}
      {/* <button onClick={ () => myCarouselRef?.current?.prevCard() }>Prev</button> */}
      {/* <button onClick={ () => myCarouselRef?.current?.nextCard() }>Next</button> */}

      <CardCarousel ref={myCarouselRef} settings={settings}>
        {
          carouselItems?.map((card, key) => {
            return (
              <div key={key} className='my-carousel-card'>
                <img src={card.image} alt={card.title || card.alt} />
                { card.title && <p>{card.title}</p> }
              </div>
            )
          })
        }
      </CardCarousel>
    </div>
  )
}

export default MyComponent

Optional settings

Presentation settings

PropertyTypeUnitDefaultDescription
gapintpx20Gap size between each card/silde
paddingintpx50Padding value either side of the main card slider
cardsToShowintn/a0Number of cards to display in one view. Set to 0 to inherit widths from card contents and support variable widths
transitionSpeedintms300Speed of the carousel move transition

Control settings

PropertyTypeUnitDefaultDescription
bufferintpx50A buffer for the edge of the carousel viewbox. If an item in the carousel overlaps the viewbox by this amount, treat it as visible and skip to show the next item on change
touchChangeThresholdintpx100How far someone has to swipe on a touch device to trigger a change
paginationbooln/afalseEnable or disable pagination
touchControlsbooln/atrueEnable or disable touch controls
arrowsbooln/atrueEnable or disable arrows
nextArrowhtmln/afalseProvide custom markup for the next button
prevArrowhtmln/afalseProvide custom markup for the prev button
yieldToImagesbooln/afalseWhether to defer calculating the overall container width till all images have loaded. Useful if images have intrinsic dimentions.

Event Hooks

NameDescription
beforeChangeFires just before the change takes place, returns the currentIndex and the newIndex
afterChangeFires immediately after the change takes place, returns the newIndex

Functions

NameDescription
nextCard()Change to the next card
prevCard()Change to the previous card
goToCard(index)Change to a specific card, ignored if card is currently in view (must provide an index)
getCurrentIndex()Retrieve the current index the carousel is at
0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago