0.0.1 • Published 6 years ago

carousel-virtualized v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Carousel-virtualized (in active development)

Highly performant react carousel built with virtualization (thanks to react-window)

Motivation

After trying most of carousels out there, we couldn't find one that would allow us to load large number of slide (e.g. >10k) and had all the features that one carousel needs.

Features

...

Todo

  • add documentation
  • make demos on codesandbox
  • tests
  • Improve a11y
  • autoplay - should we support this
  • dots - should we support this
  • Finish modular architecture
  • add variable width items in carousel

Done

  • auto container sizer
  • basic mouse, touch and keyboard navigation (improvements needed)
  • auto resizes
  • convert to typescript
  • build/bundling
  • basic example
  • mouse, touch and keyboard navigation
  • add animation

Table of contents

Basic usage

class Basic extends React.Component {
  this.state = { currentIndex: 0 };

  handleOnEvent = ({ newIndex }) => {
    this.setState({ currentIndex: newIndex });
  }

  leftArrow = ({ onClick }) => <button onClick={onClick}>Left</button>
  rightArrow = ({ onClick }) => <button onClick={onClick}>Right</button>

  render() {
    return (
    <div style={containerStyles}>
      <CarouselVirtualized
        itemCount={500}
        onEvent={this.handleOnEvent}
        currentIndex={this.state.currentIndex}
        leftArrow={this.leftArrow}
        rightArrow={this.rightArrow}
      >
        {slide}
      </CarouselVirtualized>
    </div>
    );
  }
}

const slide = ({ style }) => (
  <div style={style}>
    <img src='https://picsum.photos/800/400/?random' />
  </div>
);

Advance usage

Examples/demos

API

Props

propertytypedefaultrequiredpurpose
alignmentstring'start'NoIt tells carousel where to position current slide relative to other visible slides. Should be used when there are multiple slides on the screen in one carousel. Possible values: 'start' (selected slide will be first in carousel), 'center' (selected slide will be in the middle of carousel) and 'end' (selected slide will be last in carousel).
animationDurationnumber500NoAnimation duration in ms.
arrowStepstring|number1Nostring|nullstring|nullstring|null How many slides to move on arrow button click. Possible string values: 'all' slides to move is equal to number of visible slides on the screen.
arrowStepOffsetnumber0NoIntended to use with arrowStep prop. To move equal to number of all visible slide on the screen minus onem arrowStep should be set to 'all' and arrowStepOffset to -1.
carouselNamestring''NoWhen there are multiple carousel on the screen this prop is used for easier detecion in which carousel change has happend.
childrenReact component-YesSlide component to render.
currentIndexnumber0NoSlide number to show.
disableAnimationbooleanfalseNoDisabling animation.
disableMouseDragbooleanfalseNoDisabling mouse drag.
disableTouchDragbooleanfalseNoDisabling touch drag.
easingfunctionEaseInOutQuintNoEasing function for animation.
enableKeyboardbooleanfalseNoUsed for moving through carousel with keyboard. If enabled carousel will set event listener to document and will react on left and right arrow keydown events.
heightnumber|stringfalseNoSet height of carousel in pixels number or as percentage (string, eg. 50%). If this value is not set, carousel will have height of it's parent element.
initialScrollOffsetnumber0NoStart position of carousel in px.
innerClassNamestringundefinedNoOptional CSS class that will be applied on slide outermost element.
innerStyleobjectempty objectNoInline style that will be applied on slide outermost element.
itemCountnumber-YesNumber of slides in carousel.
itemDataanyundefinedNoData that will be passed to children component as data prop.
itemSizenumberundefinedNoWidth of slide in px. If itemSize is not set and and slideCount is itemSize will equal to width of carousel divide by slideCount. If itemSize and slideCount are not set, itemSize will be equal to carousel, meaning that only one slide will be visible in carousel.
leftArrowfunctionundefinedNoIt takes function that should return left arrrow button component. Function will receive onClick argument that should be attached to button component.
onAnimationCompletefunction() => {}NoFunction that will be envoked when animation is done.
onEventfunction() => {}NoFunction that will be called when event is triggers. Function will recieve object with eventName property and newIndex that should be used as next currentIndex.
onItemsRenderedfunction() => {}NoFunction that will be envoked when new slides are rendered on the screen. Function will recieve single argument object with following properties: carouselName, eventName (that causes new render), visibleStartIndex (first visible slide), visibleStopIndex (last visible slide), overscanStartIndex and overscanStopIndex.
outerClassNamestringundefinedNoOptional CSS class that will be applied to carousel's container.
outerRefobject|function undefinedNoRef to attach to carousel's container.
outerStyleobjectempty objectNoInline style that will be applied to carousel's container.
overscanCountnumber1NoThe number of slides to render outside of the visible area. This property can be important for two reasons:
  • Overscanning by one slide allows the tab key to focus on the next (not yet visible) slide.
  • Overscanning slightly can reduce or prevent a flash of empty space when a user first starts scrolling. | rightArrow | function | undefined | No | It takes function that should return left arrrow button component. Function will receive onClick argument that should be attached to button component. | slideCount | number | undefined | No | How many slides to show on the screen at once. | width | number | undefined | No | Set width of carousel in pixels. If this value is not set, carousel will have width of it's parent element. | wrapAround | boolean | false | No | Should carousel go in circle or should it stop at first or last slide.

Methods

Creators

Nino Majder

Ivica Batinić

License

This is open source software licensed as MIT.