10

How do I use a Framer Motion tag, and in the animate and initial props, Apply a class. Like so.

<motion.div
  initial={{ className: 'hidden' }}
  animate={{ className: 'visible' }}
>
  <div>yo</div>
</motion.div>

I HAVE to use classes as I am using tailwindcss.

I expect and hope there is a way to do this, 'cus the example I showed above, Just puts this in the rendered HTML tag style="class-name:{class_name}" which does absolutely nothing.

Thanks in advance.

2 Answers 2

-1

You can use vanilla js with event

onAnimationStart()

and

onAnimationComplate()

then do your logic. References https://www.framer.com/docs/component/###onanimationstart

-1

If you want to control only visibility, you can play with css attributes like

<motion.div
  initial={{ display: "none" }}
  animate={{ display: "block" }}
>
  <div>yo</div>
</motion.div>

However, there you can do more things with callback functions and javascript selectors as @Arfan mentioned:

  function onStart() {
    document.getElementById("target")?.classList.add("visible");
    document.getElementById("target")?.classList.remove("hidden");
  }
  function onEnd() {
    document.getElementById("target")?.classList.remove("visible");
    document.getElementById("target")?.classList.add("hidden");
  }
...
<motion.div
  initial="rest"
  animate="anim"
  onAnimationStart={onStart}
  onAnimationComplate={onEnd}
  id="target"
>
  <div>yo</div>
</motion.div>
1
  • 1
    This is not a viable approach. Manipulating DOM elements controlled by React means that React is likely to remove the altered values after the component is re-rendered.
    – coreyward
    Commented Apr 19 at 1:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.