ChakraUI 自定义菜单按钮的问题

发布于 2025-01-11 19:07:43 字数 2453 浏览 1 评论 0原文

我使用 ChakraUI 为我的网站创建菜单。我想将我的自定义图像组件设置为 MenuButton,这是 ChakraUI 的菜单组件正常工作所必需的。我尝试了在网上找到的两种方法,这些是我遇到的问题:

方法 1: forwardRefs

正如网站所说,我编写了代码,以便我的自定义组件可以接受 MenuButton 传递的引用。遗憾的是按钮不起作用。

const Menu = () => {
  const { user, signOut } = useAuth();

  return (
    <motion.div
      initial={{
        opacity: 0,
      }}
      animate={{ opacity: 1 }}
      transition={{ duration: 0.4, delay: 0.2 }}
    >
      <Menu>
        <MenuButton as={CustomButton}></MenuButton>
        <MenuList>
          <MenuItem>Test</MenuItem>
        </MenuList>
      </Menu>
    </motion.div>
  );
};
import { forwardRef } from "@chakra-ui/react";

const CustomButton= forwardRef((props, ref) => {
  const { user, signOut } = useAuth();

  return (
    <motion.img
      ref={ref}
      whileHover={{ scale: 1.1, rotate: "-360deg" }}
      whileTap={{
        scale: 0.95,
        borderRadius: "10rem",
      }}
      src={
        "https://source.boringavatars.com/beam/240/" + user.email + "?square"
      }
      height="50px"
      width="50px"
    />
  );
});

方法 2: Chakra Framer Motion

作为网站说。该按钮现在可以正常工作,但是一旦我单击该按钮,动画就会无限期地停止工作。

import { motion } from 'framer-motion'

const ProfileButton = () => {
  const { user, signOut } = useAuth();
  const MotionMenuButton= motion(MenuButton)

  return (
    <motion.div
      initial={{
        opacity: 0,
      }}
      animate={{ opacity: 1 }}
      transition={{ duration: 0.4, delay: 0.2 }}
    >
      <Menu>
        <MotionMenuButton
           whileHover={{ scale: 1.1, rotate: "-360deg" }}
           whileTap={{
             scale: 0.95
      }}>
           <Image
             src={"https://source.boringavatars.com/beam/240/" + user.email + "?square"}/>
        </MotionMenuButton>
        <MenuList>
          <MenuItem>Bonk</MenuItem>
        </MenuList>
      </Menu>
    </motion.div>
  );
};

我哪里错了?

Im using ChakraUI to create a menu for my website. I want to set my custom Image Component as the MenuButton which is required for the Menu Component from ChakraUI to work properly. I tried two methods which I found on the web and these were the problems I faced:

Method 1: forwardRefs

As the website said I wrote code so that my custom component could accept the ref passed by MenuButton. Sadly the button doesnt work.

const Menu = () => {
  const { user, signOut } = useAuth();

  return (
    <motion.div
      initial={{
        opacity: 0,
      }}
      animate={{ opacity: 1 }}
      transition={{ duration: 0.4, delay: 0.2 }}
    >
      <Menu>
        <MenuButton as={CustomButton}></MenuButton>
        <MenuList>
          <MenuItem>Test</MenuItem>
        </MenuList>
      </Menu>
    </motion.div>
  );
};
import { forwardRef } from "@chakra-ui/react";

const CustomButton= forwardRef((props, ref) => {
  const { user, signOut } = useAuth();

  return (
    <motion.img
      ref={ref}
      whileHover={{ scale: 1.1, rotate: "-360deg" }}
      whileTap={{
        scale: 0.95,
        borderRadius: "10rem",
      }}
      src={
        "https://source.boringavatars.com/beam/240/" + user.email + "?square"
      }
      height="50px"
      width="50px"
    />
  );
});

Method 2: Chakra Framer Motion

Did as the website said. The Button works properly now, but once I click the button, the animation stops working indefinitely.

import { motion } from 'framer-motion'

const ProfileButton = () => {
  const { user, signOut } = useAuth();
  const MotionMenuButton= motion(MenuButton)

  return (
    <motion.div
      initial={{
        opacity: 0,
      }}
      animate={{ opacity: 1 }}
      transition={{ duration: 0.4, delay: 0.2 }}
    >
      <Menu>
        <MotionMenuButton
           whileHover={{ scale: 1.1, rotate: "-360deg" }}
           whileTap={{
             scale: 0.95
      }}>
           <Image
             src={"https://source.boringavatars.com/beam/240/" + user.email + "?square"}/>
        </MotionMenuButton>
        <MenuList>
          <MenuItem>Bonk</MenuItem>
        </MenuList>
      </Menu>
    </motion.div>
  );
};

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-01-18 19:07:44

我知道这是一个老问题,但可能会帮助遇到同样问题的人。

在方法 1 中,确保也传播 props 对象:

import { forwardRef } from "@chakra-ui/react";

const CustomButton= forwardRef((props, ref) => {
  const { user, signOut } = useAuth();

  return (
    <motion.img
      ref={ref}
      whileHover={{ scale: 1.1, rotate: "-360deg" }}
      whileTap={{
        scale: 0.95,
        borderRadius: "10rem",
      }}
      src={
        "https://source.boringavatars.com/beam/240/" + user.email + "?square"
      }
      height="50px"
      width="50px"
      props={...props}
    />
  );
});

I know it is an old question, but might help someone having the same issue.

In the method 1, make sure to spread the props object as well:

import { forwardRef } from "@chakra-ui/react";

const CustomButton= forwardRef((props, ref) => {
  const { user, signOut } = useAuth();

  return (
    <motion.img
      ref={ref}
      whileHover={{ scale: 1.1, rotate: "-360deg" }}
      whileTap={{
        scale: 0.95,
        borderRadius: "10rem",
      }}
      src={
        "https://source.boringavatars.com/beam/240/" + user.email + "?square"
      }
      height="50px"
      width="50px"
      props={...props}
    />
  );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文