1.1.0 β€’ Published 4 months ago

react-stagger-text v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

React Stagger Text

StaggerText is React component for creating staggered text animations, by word or letter, when text enters viewport.

Docs / Demo

Contents

  1. πŸ“Œ Features
  2. 🎯 Quickstart
  3. πŸ€– Commands
  4. 🧬 Options
  5. πŸ•ΉοΈ Usage
  6. πŸ““ Notes
  7. πŸ“… To Dos

πŸ“Œ Features

  • Built in Typescript
  • Wraps words or letters inside spans so we can sequenly fade in each span.
  • Animations use css transitions.
  • Animations are triggered when text is in viewport, via Intersection Observer
  • Options exist for animation start, start delay, and stagger duration, delay, and easing.
  • Callback for when animation is complete

Live Demo→

🎯 Quickstart

Install package from npm

npm i react-stagger-text

Use that thing

Import component and provide some text. Below instance will use all default values and stagger word-by-word.

import StaggerText from "react-stagger-text"

function SomeComponent() {
  return (
    <h1>
      <StaggerText>
        This text will be staggered by word
      </StaggerText>
    </h1>
  )
}

πŸ€– Commands

Install npm i react-stagger-text Build: npm run build Dev: npm run dev Demo Run: npm run demo:start Demo Build: npm run demo:build Demo Clean: npm run demo:clean

Dev

Runing dev fires up the docs/demo and begins watching src.

The docs/demo app is bundled with Parcel.js and served at http://localhost:1234/.

Dist

On build, src populates dist with commonjs, es, umd versions of the component.

🧬 Options

OptionTypeDescriptionDefault
shouldStartbooleanFlag that stars stagger transition/animationtrue
startTresholdnumberIntersection Observer value between 0 and 1 representing the percentage component must be visible before stagger animation starts.0.1
startDelaynumberDelay before stagger animation starts0
staggerType'word' \| 'letter'Defines if stagger animation is by word or letterword
staggerDurationnumberDuration of animation0.5
staggerDelaynumberDelay between staggers0.05
staggerEasingstringCustom easing to stagger transition-based animationease-in
hasInlineBlockWrapperbooleanAdds inline-block display to wrapping elementfalse
onTransitionComplete() => voidCallback when stagger animation fully completesvoid
childrensringReact children for providing text as stringnull

πŸ•ΉοΈ Usage

Stagger by letter

<StaggerText
  staggerType='letter'
  staggerDuration={0.4}
  startDelay={0.04}
>
 Let's go ahead and stagger this by letter.
</StaggerText>

Stagger with extended start delay

<StaggerText
  staggerType='letter'
  staggerDuration={0.4}
  startDelay={0.04}
  startDelay={500}
>
 Let's go ahead and stagger this by letter with a start delay.
</StaggerText>

Stagger with custom easing

<StaggerText
  staggerType='letter'
  staggerEasing='cubic-bezier(0.4, 0, 0.2, 1)'
>
 Stagger this text with custom easing
</StaggerText>

Stagger with callback

const handleStaggerEnd = () => {
  console.log('sup ya'll, i'm dun')
}

<StaggerText
  onStaggerComplete={handleStaggerEnd}
>
  Stagger this text, then let em know
</StaggerText>

Sequentially stagger multiple instances with callback and shouldStart

// Some data with titles and config
const lines = [
  {
    title: "Stagger this first line by word",
    staggerType: "word",
    staggerDelay: 0.09,
    staggerDuration: 0.7
  },
  {
    title: "And, stagger this line by letter after the first.",
    staggerType: "letter",
    staggerDelay: 0.04,
    staggerDuration: 0.4,
    startDelay: 300
  }
  // etc
]


// Component
import { useState } from 'react'

const StaggeredTextLines: React.FC<Props> = (lines) => {
  const [currentIndex, setCurrentIndex] = useState(0)

  // Callback handler
  const handleStaggerComplete = () => {
    setCurrentIndex((prevIndex) => prevIndex + 1)
  }

  return (
    {lines.map((line, index) => (
      <StaggerText
        key={index}
        onStaggerComplete={
          index === currentIndex ? handleStaggerComplete : null
        }
        shouldStart={index === currentIndex}
        startDelay={line.startDelay}
        staggerType={line.staggerType}
        staggerDuration={line.staggerDuration}
        staggerDelay={line.staggerDelay}
      >
        {line.title}
      </StaggerText>
    }
  )
}

πŸ““ Notes

Smooth transitions

For smoother, less chopy transitions, favor longer staggerDuration and shorter staggerDelay. For example, StaggerDuration={0.7} StaggerDelay={0.09} provides a nice smooth effect by word. For letters, StaggerDuration={0.5} StaggerDelay={0.04} returns a smooth transition.

Staggering a series of lines, sequentially

The stagger a series of lines, wrap each line in a component instances and leverage the onStaggerComplete callback and shouldStart prop. Ideally, you can do this dynamically with some data defining your text and prop config and useState. A complete example of this can be found above in Useage

πŸ“… To Dos

  • Add callback for when stagger completes.
  • Add option for controling start of stagger
  • Maybe remove span wrappers from dom once stagger completes?
  • Maybe provide a method for restarting or even rewinding transitions?
  • Provide addition animationType that slices text into view via translateY
  • Add some proper tests

Have fun ya'll.

1.1.0

4 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

9 months ago