GithubHelp home page GithubHelp logo

stepable's Introduction

stepable

Created with CodeSandbox

Lightweight library to implement transitions between elements of a list.

Install

yarn install

Run

yarn build
yarn start

Usage

Create a container element with steps in it. Each step must have a class stepable and a unique step index in the data-step attribute.

<div id="app">
  <div class="stepable stepable-active--true" data-step="0">
    <p>Step 1</p>
    <button class="stepable-trigger">Next</button>
    <button class="stepable-trigger" data-step="2">Skip to 3</button>
  </div>
  <div class="stepable" data-step="1">
    <p>Step 2</p>
    <button class="stepable-trigger">Next</button>
    <button class="stepable-trigger--prev">Prev</button>
  </div>
  <div class="stepable" data-step="2">
    <p>Step 3</p>
    <button class="stepable-trigger--prev">Prev</button>
  </div>
</div>

Then add the following js to your page

import Stepable from "./stepable";

new Stepable(document.getElementById("app"));

Any click on a .stepable-trigger element within your container will move the class stepable-active--true to the next step (or a specific step using data-step attribute). It will also apply the class stepable-active--false on the previously active step so you can apply css transitions.

.stepable-trigger--prev will move to the previous step.

Options

You can customize the behavior of the stepable by applying options

new Stepable(document.getElementById("app"), {
  initialStep: 4,
  shouldTransition: (transitionEvent, event) => {
    if (event.target === "an invalid button") {
      // no transition will happen
      return false;
    }

    return true;
  },
  beforeTransition: (
    // TransitionEvent contains the original index and the target index plus the associated HTMLElements
    transitionEvent,
    event,
    cb
  ) => {
    // delay the transition by 1 second
    setTimeout(cb(transitionEvent), 1000);
  }
});

Full list of options

const defaultOptions: Options = {
  activeClass: "stepable-active",
  initialStep: 0,
  stepKey: "data-step",
  stepSelector: ".stepable",
  triggerSelector: ".stepable-trigger",
  prevTriggerSelector: ".stepable-trigger--prev",
  shouldTransition: (transitionEvent, event) => true,
  beforeTransition: (transitionEvent, event, cb) => cb(transitionEvent),
  afterTransition: (transitionEvent, event) => true
};

stepable's People

Contributors

srascar avatar

Watchers

 avatar  avatar

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.