Typewriter Loader

PreviousNext

A customizable typewriter loader component for indicating loading states in your application. Features smooth typing animations and customizable text content.

Docs
moleculeuiui

Preview

Loading preview…
registry/molecule-ui/typewriter-loader.tsx
"use client"

import React from "react"
import { HTMLMotionProps, motion } from "motion/react"

import { cn } from "@/lib/utils"

export interface TypewriterLoaderProps {
  /**
   * The text to display with the typewriter effect.
   * @default Loading...
   */
  typingText?: string
  /**
   * The speed of the typewriter effect in milliseconds between each character.
   * @default 200
   */
  typingSpeed?: number
}

const DIVIDER = 400

export function TypewriterLoader({
  className,
  typingText = "Loading...",
  typingSpeed = 200,
  ...props
}: HTMLMotionProps<"div"> & TypewriterLoaderProps) {
  const [displayText, setDisplayText] = React.useState("")

  React.useEffect(() => {
    let index = 0
    const interval = setInterval(() => {
      if (index <= typingText.length) {
        setDisplayText(typingText.slice(0, index))
        index++
      } else {
        index = 0
      }
    }, typingSpeed)
    return () => clearInterval(interval)
  }, [typingSpeed, typingText])

  return (
    <div className="flex items-center justify-center font-mono text-lg">
      {displayText}
      <motion.span
        animate={{ opacity: [1, 0] }}
        transition={{
          duration: typingSpeed / DIVIDER,
          repeat: Number.POSITIVE_INFINITY,
        }}
        className={cn("ml-1", className)}
        {...props}
      >
        |
      </motion.span>
    </div>
  )
}

Installation

npx shadcn@latest add @moleculeui/typewriter-loader

Usage

import { TypewriterLoader } from "@/components/ui/typewriter-loader"
<TypewriterLoader />