User avatar

Jeroen Reumkens PRO

Motion animate opacity with strict mode

Unlock Playground Access

Playgrounds let you build, test, and share frontend ideas instantly — right in your browser. Access is included in PRO. Just want Playgrounds? Pick a plan below.

Maker

€5,- / month

Unlimited Playgrounds access to build, experiment, and share without limits.

Join for €5 per month

Champion

€10,- / month

All the benefits of Maker, plus you help Frontend.fyi grow and get a Champion badge on your profile.

Join for €10 per month

Cancel anytime — no commitment.
Upgrade to PRO anytime and get a partial refund.We'll credit 100% of your first 3 months and 50% of the rest toward the PRO lifetime price.

Already a member? Login

Playground settings

Title

Description

Public

Editor settings


Packages

These packages can be imported in your JavaScript files.

Package name

Version

@

@

@

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
import { useAnimate, useInView } from "motion/react";
import { useEffect, useRef } from "react";

export const App = () => {  
  return (
    <div className="px-8">
      <p className="text-white text-center pt-8 pb-[100vh]">Scroll down and see the logo animate in</p>
      <Card title="Hello" />
    </div>
  );
};

const Card = ({ title }) => {
  const [scope, animate] = useAnimate();
  const inViewRef = useRef(null);
  const isInView = useInView(inViewRef);
  const controlsRef = useRef(null);
  
  useEffect(() => {
    controlsRef.current = animate([
      [scope.current, { opacity: 1 }],
    ], {autoplay: false});    
  }, []);
  
  useEffect(() => {
    if (!controlsRef.current) return;
    
    controlsRef.current.speed = isInView ? 1 : -1;
    controlsRef.current.play();
  }, [isInView])
  
  return (
    <>
      <section className="h-[20vh] bg-gray-100 mb-8 p-6" ref={inViewRef}>
        When this block is in view, the aside should fade in
        
        <aside
          ref={scope}
          className="fixed top-0 left-0 bar text-black bg-white rounded-2xl px-6 py-12 opacity-0">
          <p>{title}</p>
        </aside>
      </section>
    </>
  );
}
One sec — editor's thinking…