Giter VIP home page Giter VIP logo

slider's Introduction

VTEX Slider

Description

The VTEX Slider is a slider that aims a good support for SSR and can display one or more slides per page.

๐Ÿ“ข Disclaimer: Don't fork this project; use, contribute, or open issue with your feature request

Table of Contents

Usage

To import it you can add to you manifest.json the following:

{
  "dependencies": {
    "vtex.slider": "0.x"
  }
}

And then in your component you can import the components exported from the slider:

import { Slider, Slide } from 'vtex.slider'

You can use it in your code like a React component with the jsx tags:

handleChangeSlide = i => {
  this.setState({ currentSlide: i })
}

render() {
  const { currentSlide } = this.state
  // ...

  const { myProducts } = this.props

  return (
    <Slider currentSlide={currentSlide} onChangeSlide={this.handleChangeSlide}>
      {myProducts.map(product => (
        <Slide key={product.id}>
          <DisplayProductComponent product={product} />
        </Slide>
      ))}
    </Slider>
  )
}

If you want to show multiple items in the same page at the same time you can use the prop perPage:

render() {
  const { currentSlide } = this.state
  const { myProducts } = this.props

  // The keys of the object represent the size of the window in px.
  // In this case if the window is 1300px large or bigger it will show 5 items,
  // If it has 900px or any size until 1299px it will show 4 items
  const perPage = {
    1300: 5,
    900: 4,
    700: 2,
    600: 1,
  }

  return (
    <Slider
      currentSlide={currentSlide}
      onChangeSlide={this.handleChangeSlide}
      perPage={perPage}
    >
      {myProducts.map(product => (
        <Slide key={product.id}>
          <DisplayProductComponent product={product} />
        </Slide>
      ))}
    </Slider>
  )
}

Bellow is an example with all the components together:

arrowRender = ({ orientation, onClick }) => {
  return (
    <div className="arrow-container-class" onClick={onClick}>
      <MyArrowComponent orientation={orientation} />
    </div>
  )
}

arrowContainerRender = ({ children }) => {
  return (
    <MyContainerComponent>
      {children}
    </MyContainerComponent>
  )
}

render() {
  const { currentSlide } = this.state
  const { myProducts } = this.props

  const perPage = {
    1300: 5,
    1100: 4,
    900: 3,
    700: 2,
    300: 1
  }

  return (
    <SliderContainer className="mw9">
      <Slider
        loop
        easing="ease"
        duration={500}
        perPage={perPage}
        currentSlide={currentSlide}
        arrowRender={this.arrowRender}
        onChangeSlide={this.handleChangeSlide}
        arrowsContainerComponent={this.arrowContainerRender}
      >
        {myProducts.map(product => (
          <Slide
            className="slide-css-class"
            sliderTransitionDuration={500}
            key={product.id}
            defaultWidth={280}
          >
            <ProductComponent product={product} />
          </Slide>
        ))}
      </Slider>
      <Dots
        loop
        showDotsPerPage
        perPage={this.perPage}
        currentSlide={currentSlide}
        totalSlides={myProducts.length}
        onChangeSlide={this.handleChangeSlide}
        classes={{
          root: 'pv4',
          notActiveDot: 'bg-muted-3',
          dot: 'dot pointer br-100',
          activeDot: 'bg-emphasis'
        }}
      />
    </SliderContainer>
  )
}

Configuration

Slider

Prop name Type isRequired defaultValue Description
arrowRender func ๐Ÿšซ ๐Ÿšซ A render function that will receive as props an orientation prop and an onClick callback
arrowsContainerComponent func/string ๐Ÿšซ ๐Ÿšซ The component used to contain both arrows. Either a string to use a DOM element or a component
children element/array โœ”๏ธ ๐Ÿšซ The slides to render
classes object ๐Ÿšซ No extra classes applied to any element Classes to apply to the Slider elements
currentSlide number ๐Ÿšซ 0 Current slide on the screen, if you have perPage > 1, then the current slide is the most left slide on the screen (You should not use this variable to display the index of the slide on the screen if you're using loop={true}).
cursor string ๐Ÿšซ '-webkit-grab' Css value of cursor when mouse is hovering the slider frame
cursorOnMouseDown string ๐Ÿšซ '-webkit-grabbing' Css value of cursor when mouse is down
duration number ๐Ÿšซ 250 Duration of transitions
easing string ๐Ÿšซ 'ease-out' Transition function
loop bool ๐Ÿšซ false If the slides should be looping
onChangeSlide func โœ”๏ธ ๐Ÿšซ Function to change the value of currentSlide. The function should expect a number as it's only parameter
perPage number/object ๐Ÿšซ 1 Amount of slides to be on the screen, if a number is passed, then that's the number of slides that will be shown, if an object with breakpoints is passed, then the component will check the size of the screen to see how many slides will be on the screen at the same time
minPerPage number ๐Ÿšซ 1 Minimum amount of slides to be on the screen, can be used to control how many itens will be displayed in the smallest screen size
resizeDebounce number ๐Ÿšซ 250 Resize debounce timer in milliseconds
rootTag string ๐Ÿšซ 'div' Tag to be rendered in the root of the slider
sliderFrameTag string ๐Ÿšซ 'ul' Tag to be rendered in the slider frame element
threshold number ๐Ÿšซ 50 Minimum of pixels to drag until the slider change the currentSlide

Slide

Prop name Type isRequired defaultValue Description
children node โœ”๏ธ ๐Ÿšซ Node to render
className string ๐Ÿšซ ๐Ÿšซ Classes to pass to the root element of the Slide
defaultWidth number ๐Ÿšซ ๐Ÿšซ Default width of the slide (only applied in the first render)
tag string ๐Ÿšซ li Tag to be rendered in the root element
fitImg bool ๐Ÿšซ true If the slide component should try to fit the img (only works if children is an img element)
resizeDebounce number ๐Ÿšซ 250 Time of debounce of resize event listener
sliderTransitionDuration number ๐Ÿšซ 250 Duration of transition passed to Slider (must be the same), if nothing is passed to any of the components it will apply the same default value

SliderContainer

Prop name Type isRequired defaultValue Description
autoplay bool ๐Ÿšซ false If the slider should be passing automatically
autoplayInterval number ๐Ÿšซ 5000 Time in milliseconds of the interval to change the currentSlider
children node โœ”๏ธ ๐Ÿšซ Children of the component to render
className string ๐Ÿšซ ๐Ÿšซ Classes to be applied to the root element
onNextSlide func ๐Ÿšซ ๐Ÿšซ Function to be called if autoplay={true}
pauseOnHover bool ๐Ÿšซ true If the interval should not be executed when the mouse is hovering the component
tag string ๐Ÿšซ 'div' Tag to render the component

Dots

Prop name Type isRequired defaultValue Description
classes object ๐Ÿšซ No extra classes applied to any element Classes to style the elements of the component
dotProps object ๐Ÿšซ ๐Ÿšซ Extra props to be applied to the dot element
dotSize number/string ๐Ÿšซ ๐Ÿšซ The size of the dots, can be a number (in this case it will use px unit), or a string (you have to pass the number with the unit e.g '3rem')
dotTag string ๐Ÿšซ 'li' Tag to be rendered in the dot element
loop bool ๐Ÿšซ false If the slides should be looping
onChangeSlide func โœ”๏ธ ๐Ÿšซ Function to change the currentSlide
perPage number/object ๐Ÿšซ 1 This prop works the same way the perPage of Slider and this component should receive the same value of Slider
resizeDebounce number ๐Ÿšซ 250 Debounce time in milliseconds
rootTag string ๐Ÿšซ 'ul' Tag to be rendered as the root element of the component
totalSlides number โœ”๏ธ ๐Ÿšซ Total value of sliders that will be rendered
minPerPage number ๐Ÿšซ 1 This prop works the same way the minPerPage of Slider and this component should receive the same value of Slider
showDotsPerPage bool ๐Ÿšซ false If this frag is true, then every dot represent a page of slides (e.g. if perPage = 2 and you have 4 elements, then you have 2 dots), if false, then it will render one dot to each slide

Styles API

You can style this app by using the props classeName and classes of the components. But if you want to style every slider of your app you need to use the CSS namespaces to do it.

CSS namespaces

๐Ÿšง ๐Ÿšง ๐Ÿšง

Tests

Coverage Status

Travis CI

Build Status

slider's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slider's Issues

autoplay prop not working properly

If autoplay={false} it still keeps passing slides automatically.

Expected Behavior

If autoplay={false} it should not be playing automatically.

Steps to Reproduce (for bugs)

  1. Use the Slider with the SliderContainer and pass autoplay={false}
  2. Hover the slider
  3. Take the mouse off it. It's gonna pass automatically.

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.