Logo

dev-resources.site

for different kinds of informations.

Creating a text writing animation in React using Framer-Motion

Published at
3/18/2024
Categories
react
framermotion
framer
nextjs
Author
samyarkd
Categories
4 categories in total
react
open
framermotion
open
framer
open
nextjs
open
Author
8 person written this
samyarkd
open
Creating a text writing animation in React using Framer-Motion

Placeholder edit animation preview

Creating a cool placeholder text writing effect animation in React using Framer-Motion was one the recent challenges that overcome and today i want to share it with you guys.

Framer Motion Solution

This solution is pretty easy to understand and implement in framer motion.

This animation is achievable via animate function provided by Framer Motion, you can learn about how it works here.



import { animate } from "framer-motion";
import { useEffect, useRef, useState } from "react";

const phrases = [
  "Enter a phrase to learn it!",
  "こんにちは、リンゴザンです。",
  "Silav navê min Lingozan e.",
  "Hallo, mein Name ist Lingozan.",
];

export function PlaceHolderAnimation() {
  const [placeholder, setPlaceholder] = useState("");
  const inputRef = useRef<HTMLTextAreaElement>(null);

  useEffect(() => {
    async function runAnimations() {
      while (true) {
        for (const phrase of phrases) {
          await animate(0, phrase.length, {
            onUpdate: (latest) => {
              setPlaceholder(phrase.slice(0, latest));
            },
            delay: 0.5,
            duration: 0.5,
          });

          await animate(phrase.length, 0, {
            onUpdate: (latest) => {
              setPlaceholder(phrase.slice(0, latest));
            },
            delay: 2.5,
            duration: 0.5,
          });
        }
      }
    }

    runAnimations();
  }, []);

  return <textarea
     ref={inputRef}
     placeholder={placeholder}
    />
}


Enter fullscreen mode Exit fullscreen mode

In this example as you can see, I have several phrases that i want them to be animated in a sequential order.

How it works?

I think it’s obvious enough that we iterate through the phrases and for each one we have a twostep animation.

![[Creating a Placeholder animation in React using Framer-Motion-20240313025309264.webp]]

The first one is for writing animation and second one for erasing animation. at the start of the animation the placeholder state is empty and we ask the animate function to start animating number 0 to phrase.length in the duration of 0.5seconds.

When the value increases from 0 we want to slowly set the placeholder value to the phrase from index 0 to how far we animated.

The erasing animation is the same but reverse, it’s from the phrase.length to 0.

framermotion Article's
30 articles in total
Favicon
Building a food simulation game with Next.js and generative design
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
State-of-the-Art Web Design with Remix, Tailwind, and Framer Motion
Favicon
How I Created a Stunning Portfolio with Next.js, Tailwind CSS, and Framer Motion
Favicon
Creating the iMessage Card Stack Animation: The Interactive Layer
Favicon
Creating the iMessage Card Stack Animation: The Timeline Design
Favicon
How to build awesome website with stunning animation?
Favicon
Adding motion to 3D models with Framer Motion and Three.js
Favicon
Creating a Smooth Animated Menu with React and Framer Motion
Favicon
My-Portfolio
Favicon
Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs
Favicon
Could not find a declaration file for module framer-motion Error in Next.js 14
Favicon
How to add Animations and Transitions in React
Favicon
Creating a text writing animation in React using Framer-Motion
Favicon
Unveiling My Portfolio Website: A Fusion of Tech and Creativity 🚀
Favicon
React developer portfolio template use framer motion to animate components
Favicon
Building a mini casual game with Next.js
Favicon
Creating a Multi-Level Sidebar Animation with Tailwind and Framer Motion in React
Favicon
Easy Animated Tabs in ReactJS with Framer Motion
Favicon
A Tinder-like card game with Framer-Motion
Favicon
Elevating Web Design with Framer Motion: Bringing Your Website to Life
Favicon
Animating the Web: Route Transitions in Next.js with Framer Motion
Favicon
Enhancing Form Usability with Framer Motion: A Guide to Animated, Chunked Form Transitions
Favicon
How to Build a fully accessible Accordion with HeadlessUI, Framer Motion, and TailwindCSS
Favicon
Build Portfolio Theme in Tailwind CSS & Next.js
Favicon
How to create Scroll-Linked Animations with React and Framer Motion 💃🏻🕺🏻
Favicon
How to build 3 simple animation with framer motion
Favicon
How to create an awesome navigation menu using chakra-UI and framer-motion.
Favicon
Make a slide open envelope
Favicon
Animate everything with Framer Motion

Featured ones: