Socket
Socket
Sign inDemoInstall

dub-step

Package Overview
Dependencies
6
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dub-step

A set of primitives for building index based UI widgets controlled by swipe, timers, and/or buttons.


Version published
Maintainers
1
Created

Readme

Source

dub-step 🕺🏽
dub-step logo

Primitives for building index based UI widgets controlled by swipe, timers, and/or buttons.


Many existing carousel/swipe solutions in one way or another end up dictating your markup of your UI. They expose many options to allow for extensibility, but this results in a convoluted API that is not flexible. In these cases, your often very specific design must be fit into an existing rigid solution.

dub-step simply manages the state needed to power a carousel, slideshow, photo gallery, or even multi-step forms, allowing you to build the UI how you want. It uses the function as child and "prop getter" patterns, which gives you maximum flexibility with a minimal API.

Table of Contents

Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your project's dependencies:

npm install --save dub-step

This package also depends on react and prop-types. Please make sure you have those installed as well.

Usage

NOTE: Glamorous is used for styles in this example, but is not required.

import DubStep from 'dub-step';
import glamorous, { Div, Img } from 'glamorous';

function BasicSlideshow({slides, onChange}) {
  return (
    <DubStep
      cycle
      pauseOnHover
      duration={1500}
      total={slides.length}
    >
      {({
        getNextControlProps,
        getPreviousControlProps,
        getControlContainerProps,
        getPauseControlProps,
        getPlayControlProps,
        getIndexControlProps,
        index
      }) => (
        <section>
          <Div width="350px" overflow="hidden" margin="0 auto">
            <Div
              display="flex"
              transform={`translate3d(${-index * 350}px, 0, 0)`}
              transition="all .3s ease-in-out"
            >
              {slides.map((url, i) => <Img src={url} alt="doge pic" width="100%" height="100%" />)}
            </Div>
          </Div>
          <Div display="flex" justifyContent="center">
            {slides.map((url, i) => (
              <Img
                {...getIndexControlProps({ index: i })}
                src={url}
                width="30px"
                height="30px"
                margin="5px"
                padding="2px"
                border={i === index ? '1px solid darkgray' : 'none'}
                transform={`scale(${i === index ? 1.2 : 1})`}
              />
            ))}
          </Div>
          <Div display="flex" justifyContent="center">
            <button {...getPreviousControlProps()}>Previous</button>
            <button {...getNextControlProps()}>Next</button>
            <button {...getPlayControlProps()}>Play</button>
            <button {...getPauseControlProps()}>Pause</button>
          </Div>
        </section>
      )}
    </DubStep>
  );
}

const DOGE_PICS = [
  'http://doge2048.com/meta/doge-600.png',
  'http://doge2048.com/meta/doge-600.png',
  'http://doge2048.com/meta/doge-600.png',
  'http://doge2048.com/meta/doge-600.png',
  'http://doge2048.com/meta/doge-600.png'
];

function App() {
  return (
    <BasicSlideshow
      slides={DOGE_PICS}
      onChange={currentIdex => console.log(currentIdex)}
    />
  )
}

Builds... simpleslideshow

In the example of above, the props returned by the get*ControlProps parameters empower any element in your UI to control the state of the slideshow. The index is used in coordination with a css transform/transition to animate the changing slides. dub-step is the only component. It doesn't render anything itself, it just calls the child function and renders that. Wrap everything in <DubStep>{/* your function here! */}</DubStep>.

Props

See the API Docs for information on the props exposed by this package.

Control Props

dub-step manages its own state internally and calls your onChange/OnPlay/OnPause etc. handlers with any relevant changes. The controllable state that dub-step manages includes: index and paused. Your child callback function (read more below) can be used to manipulate this state from within the render function and can likely support many of your use cases.

However, if more control is needed, you can pass any of these pieces of state as a prop (as indicated above) and that state becomes controlled. As soon as this.props[controllableStatePropKey] !== undefined, internally, dub-step will determine its state based on your prop's value rather than its own internal state. You will be required to keep the state up to date, but you can also control the state from anywhere, be that state from other components, redux, react-router, or anywhere else.

Child Callback Function

This is where you render whatever you want to based on the state of downshift. The function is passed as the child prop:

<DubStep>
  {({/* parameters here */}) => (/* your render code here*/)}
</DubStep>

The paramters of this function can be split into three categories: State, Prop getters, and Actions.

See the API Docs for a list of these properties.

Examples

These are not yet available on github. But check out the codesandbox until they are! Fork it and build your own examples then tweet me about it!

Edit dub-step Examples 🕺🏽

Credits

Kent Dodds' work on downshift 🏎 heavily inspired this package. You may even notice some copy pasta in the README 😏.

Much of the swipe code was lifted from react-slick by akiran a very solid solution for swipe friently carousels.

There is no lack of carousel libraries out there. Many of which I looked at while writting this package. I hope dub-step represents a move towards an unopinionated solution that enables design and development to work together not against each other.

Check out other solutions on npm.

Some of the time spent writting this package was sponsored by Four Kitchens. I ❤️ 4K. Come work with us!

Four Kitchens logo

License

MIT

Keywords

FAQs

Last updated on 18 Sep 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc