Motion Framer animating between components with different contents

Viewed 32

I am using React + Motion Framer and I've encountered several places where I need to animate the layout between 2 components that don't have exactly the same contents. For example SignUp and Login form don't have the same amount of fields, or in the following case, a card can be expanded for more contents.

Now I've approached this as such

import { motion } from "framer-motion";
import { useState } from "react";
import AnimatedImage from "./animatedImage";

export default function ExpendableCard(props) {
  const [expanded, setExpanded] = useState(false);

  return expanded ? (
    <motion.div
      onClick={(ev) => setExpanded(!expanded)}
      className={
        "flex-fix-item w-full shadow-md rounded-md " +
        "items-center max-w-sm lg:shrink-0 snap-center "
      }
    >
      <div className="text-lg font-semibold text-white bg-red-500 text-center w-full flex items-center justify-center py-3 px-2 rounded-t-lg">
        PENDING
      </div>
      <div className="flex flex-col gap-2 px-3 pb-0">
        <div
          className={
            "flex-row-centered gap-3 my-3 py-3 " +
            " border-b border-gray-600 border-opacity-50 px-2 snap-x "
          }
        >
          <AnimatedImage
            classes="flex-fix-item rounded-sm shrink-0 snap-center "
            size= " w-52 h-52 sm:w-64 sm:h-64"
            alt="asdad"
            isVideo={false}
            src="https://picsum.photos/700/500"
          ></AnimatedImage>

<AnimatedImage
            classes="flex-fix-item rounded-sm shrink-0 snap-center "
            alt="asdad"
            size= " w-52 h-52 sm:w-64 sm:h-64"
            isVideo={true}
            src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"
          ></AnimatedImage>
        </div>

        <div className="flex flex-col w-full px-2">
          <p className="font-medium text-gray-600 text-opacity-70">
            {props.data.name}
          </p>
          <p className="text-md text-gray-600 w-full text-start max-h-60 overflow-y-auto whitespace-pre-wrap">
            asdadasd asd asd asd asd asd asdadasd asd asd asd asd asd asdadasd
            asd asd asd asd asd asdadasd asd asd asd asd asd asdadasd asd asd
            asd asd asd asdadasd asd asd asd asd asd asdadasd asd asd asd asd
            asd asdadasd asd asd asd asd asd asdadasd asd asd asd asd asd
            asdadasd asd asd asd asd asd asdadasd asd asd asd asd asd asdadasd
            asd asd asd asd asd asdadasd asd asd asd asd asd asdadasd asd asd
            asd asd asd
          </p>

          <p className="font-medium text-gray-600 text-opacity-70 border-gray-600 border-opacity-50 pt-4  border-t">
            {props.data.name}
          </p>
          <p className="text-md text-gray-600 w-full text-start max-h-60 overflow-y-auto  whitespace-pre-wrap">
            Contrary to popular belief, Lorem Ipsum is not simply random text.
            It has roots in a piece of classical Latin literature from 45 BC,
            making it over 2000 years old. Richard McClintock, a Latin professor
            at Hampden-Sydney College in Virginia, looked up one of the more
            obscure Latin words, consectetur, from a Lorem Ipsum passage, and
            going through the cites of the word in classical literature,
            discovered the undoubtable source. Lorem Ipsum comes from sections
            1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes
            of Good and Evil) by Cicero, written in 45 BC. This book is a
            treatise on the theory of ethics, very popular during the
            Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
            amet..", comes from a line in section 1.10.32.
          </p>
        </div>
      </div>
    </motion.div>
  ) : (
    <motion.div
      onClick={(ev) => setExpanded(!expanded)}
      className={
        "flex-fix-item w-full shadow-md rounded-md " +
        "items-center max-w-sm lg:shrink-0 snap-center "
      }
    >
      <div className="text-lg font-semibold text-white bg-red-500 text-center w-full flex items-center justify-center py-3 px-2 rounded-t-lg">
        PENDING
      </div>
      <div className="flex flex-col w-full px-2 border-gray-600 border-opacity-50 pt-4 ">
        <div className="flex flex-col gap-2 px-3">
          <p className="font-medium text-gray-600 text-opacity-70">
            {props.data.name}
          </p>
          <p className="text-md text-gray-600 text-start truncate">
            SDasda sd asd asd asd sad asd asd asd asd asd sad as as asdasdasd
            sdd asdasdsadsa
          </p>
        </div>
      </div>
    </motion.div>
  );
}

Tried having the same layoutId on both the expanded and the collapsed version. Tried having separate animation for each one but it all resulted in clunky and jumpy animations.

For some reason, I am unable to properly animate between these components that lose a component (like in the expanding card, the images are not present in the collapsed card). Any advice? Or guides on how to go about this?

0 Answers
Related