npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-sequencer

v3.0.1

Published

Step based sequencer to give your components reliable states for transitions and animations.

Downloads

494

Readme

React Sequencer

A better way to do animations in React.

Links

Overview

Introduction

React sequencer lets you perform complex animations easily by tying them to a time-sequenced state machine. The simplest usage is to implement the useSequencer hook inside your function components, which will give you the sequencer state and an api to control it.

You first define a set of steps for your sequence as tuples of names and durations:

const steps = [
  ['initial', 100],
  ['middle', 100],
  ['final', 0],
]

Then pass this as configuration to useSequencer:

import { useSequencer } from 'react-sequencer'

const steps = [
  ['initial', 100],
  ['middle', 100],
  ['final', 0],
]

const MyComponent = (props) => {
  let [state, api] = useSequencer({ steps })
  return (
    <div>
      <div>The current step: {state.current}</div>
      <button
        onClick={() => {
          api.play()
        }}
      >
        Start
      </button>
    </div>
  )
}

Here state contains the sequencer state and api provides some methods to control the sequencer. When your sequencer starts playing, it runs through the steps and updates the state on every step, passing the current name into state.current.

Allowing you to control your time sequenced events in this way has many benefits:

  • Easily add / remove or edit your steps from one place in your code
  • Have your sequencer trigger css animations, text changes, or whatever you like
  • Every step runs in its own animation frame so no repaint or timeout hacks are needed to get your animations behaving exactly as you like
  • Add sequencers to multiple components and guarantee that they update in sync

Getting started

Install from NPM:

npm install react-sequencer

Documentation

useSequencer()

(options: Options) => [SequencerState, SequencerApi]

The useSequencer hook is the recommended way to create a sequencer and inject its state into your component. It takes an options object as an argument.

options

options: Options

A configuration object to initialize the sequencer.

Returns

The hook returns a tuple of a SequencerState and SequencerApi.

Example

const [state, api] = useSequencer({
  steps: [
    ['initial', 100],
    ['middle', 100],
    ['final', 0],
  ],
  endMode: 'end',
  complete: true,
})

Options

Options is an object passed to useSequencer. It contains the following properties:

steps

steps: Array<[any, number]>

Pass an array of tuples that defines the steps of the sequence. The first value should be the name of the step, the second the duration in milliseconds.

useSequencer({
  steps: [
    ['initial', 100],
    ['middle', 100],
    ['final', 0],
  ],
})

If you specify a duration of 0 for a step, it means that the following step will fire on the next animation frame. This guarantees that every state must be visited and rendered before transitioning to the next state.

This is useful for creating an animation 'set up' state where you may want to prepare some css before an animation begins. You can simply do this without needing to change anything else in your sequence:

const steps = [
  ['pre', 0],
  ['initial', 100],
  ['middle', 100],
  ['final', 0],
]

pre becomes the default state when your component mounts, until the sequencer is started, which moves on to initial on the next frame. By defining all the states explicitly in this fashion, it becomes easy to insert steps, change durations, swap steps and understand how your animation behaves.

endMode

endMode: 'end' | 'start' | 'loop'

The end mode determines the behavior of the sequencer once it reaches the end of the last step.

  • 'end' (default): The sequencer remains in the last step.
  • 'start': The sequencer resets to the first step and becomes idle.
  • 'loop': The sequencer resets to the first step and continues looping until stop() or pause() is called.

autoplay

autoPlay: boolean

Set to true to start playing the sequencer when your component mounts.

complete

complete: boolean

If set to true, the sequencer is initialized in the 'completed' state, meaning it is in the final step and idle. It will remain in this state until either play() or stop() is called.

Sequencer State

The sequencer state is an object representing the current state of the sequencer. It includes these properties.

current

current: any

The current step of the sequencer, as specified by the step names provided in the config. While these examples use a string, you can actually use any type for your names.

index

index: number

The index of the current step.

isPlaying

isPlaying: boolean

true if the sequencer is playing.

isComplete

isComplete: boolean

true if the sequencer has finished sequencing through the steps and is idle. endMode must be set to end in order to reach this state.

isStopped

isStopped: boolean

true if the sequencer is in its first step and not playing.

Sequencer Api

isBefore()

isBefore(name: string): boolean

true if the sequencer has not yet reached the step with the provided name.

isAfter()

isAfter(name: string): boolean

true if the sequencer has passed the step with the provided name.

play()

play(): void

Starts the sequencer, or continues playing if the sequencer was paused.

pause()

pause(): void

Pauses the sequencer. The sequencer tracks how far it is through the current step by the millisecond so that playback continues from the same moment.

stop()

stop(): void

Stops playback and resets the sequencer back to the first step.

complete()

complete(): void

Stops playback and puts the sequencer to the end of the final step.

Sequencer component

If you prefer you may also use a wrapper component <Sequencer> to create a sequencer. Here you pass all the options above as props, and you should pass a function as the child component with state and api as arguments:

import { Sequencer } from 'react-sequencer'

const MyComponent = (props) => {
  return (
    <Sequencer steps={mySteps}>
      {(state, api) => <div>The current state is {state.current}</div>}
    </Sequencer>
  )
}

LICENSE

MIT