Framer-motion element appears multiple times

What am I trying to do & context?

I am currently making a personal website, and I wanted center this container. You can an example on Aceternity.
With a modified version this container centered, the container would jump.

As a result I have created the code found on Replit (it is commented with further details on how the code works).

What is the problem I am facing?

At random intervals (around every 1-7 cycles) the animation seems to bug out and create two instances of itself, for a single frame. As show in the images below.

image of bug

I have attempted to localise the problem, with no success. The glitch seems to last for around a single frame. I truly
have no clue and have been stuck on this for a while.

Problem file location?

./src/app/ui/title-section/vanishing-words.tsx
Or press this link.

Theories

  1. It is possible that multiple nested motions, could be somehow effecting one another – I have tested to see if
    onExitComplete is being called multiple times, and it does not seem like it.
  2. Uncommenting b1 does seem to make it worse.
    image of second bug
  3. I have tested on Chrome and Safari browsers.

Code that is likely causing the problem

I have reduced the code down to this minimal state and have still been finding them problem.

<AnimatePresence
        onExitComplete={() => {
          // 5. The animation loops
          setIsAnimating(false);
        }}
      >
        <motion.div
          key={`${currentWord}-${xOffSet}`}
          style={{
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center'
          }}
          ref={divRef}
          transition={{ duration: 1 }}
          initial={{ x: xOffSet }}
          animate={{ x: 0 }}
        >
          <p>{relativeClause}</p>

          <motion.div
            key={currentWord}
          >
          {/* b1: Swapping out the div above with the one below seems to make it worse */}
          {/* <motion.div
            key={currentWord}
            initial={{
              opacity: 0,
              y: 10,
            }}
            animate={{
              opacity: 1,
              y: 0,
            }}
            transition={{
              duration: 0.4,
              ease: "easeInOut",
              type: "spring",
              stiffness: 100,
              damping: 10,
            }}
            exit={{
              opacity: 0,
              y: -40,
              x: 40,
              filter: "blur(8px)",
              scale: 2,
              position: "absolute",
            }}
          > */}
            
            {currentWord.split("").map((letter, index) => (
              <motion.span
                key={currentWord + index}
                initial={{
                  opacity: 0,
                  y: 10,
                  filter: "blur(8px)",
                }}
                animate={{
                  opacity: 1,
                  y: 0,
                  filter: "blur(0px)",
                }}
                transition={{
                  delay: index * 0.08,
                  duration: 0.3,
                }}
                className={clsx("inline-block", letter === " " && "w-1")}
              >
                {letter}
              </motion.span>
            ))}
          </motion.div>
        </motion.div>
      </AnimatePresence>

New contributor

Ben Lewis-Jones is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

  • Please update the key prop for the motion.div to ensure React properly handles component re-renders. Changing keys can force React to unmount and remount components, which might be causing the glitch.
  • Having multiple motion.div elements nested and animated might cause conflicts or unexpected behavior. Try simplifying the structure and animations.

This codebase is working on my end. Please let me know if you have any questions.

<AnimatePresence
  onExitComplete={() => {
    setIsAnimating(false);
  }}
>
  <motion.div
    key={`${currentWord}-${xOffSet}`}
    style={{
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
    }}
    ref={divRef}
    initial={{ x: xOffSet }}
    animate={{ x: 0 }}
    exit={{ x: -xOffSet }} // Add exit animation to ensure smooth removal
    transition={{ duration: 1 }}
  >
    <p>{relativeClause}</p>

    <motion.div key={currentWord}>
      {currentWord.split("").map((letter, index) => (
        <motion.span
          key={`${currentWord}-${index}`}
          initial={{
            opacity: 0,
            y: 10,
            filter: "blur(8px)",
          }}
          animate={{
            opacity: 1,
            y: 0,
            filter: "blur(0px)",
          }}
          transition={{
            delay: index * 0.08,
            duration: 0.3,
          }}
          className={clsx("inline-block", letter === " " && "w-1")}
        >
          {letter}
        </motion.span>
      ))}
    </motion.div>
  </motion.div>
</AnimatePresence>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật