如何检查元素是否被点击以使用Tailwind进行动画。

huangapple go评论55阅读模式
英文:

How to check if element was clicked to make animation using tailwind

问题

I'm trying to make a user menu appear using Tailwind, and I'm not sure how to check if the peer is clicked or not to display the animation.

I could use a hook which makes it appear and disappear, etc. but if I use a hook, I won't be able to let the user menu slide in from the right like I'm trying.

Here's what I have:

<div className="relative">
  <Image
    className="rounded-full cursor-pointer peer"
    src={currentUser?.user.image}
    alt="user avatar"
    width="35"
    height="35"
    onClick={() => menu.onToggle()}
  />
  <UserMenu />
</div>

And the user menu:

<div
  className="absolute bg-black top-[59px] left-[100px] rounded-bottom-md transform duration-200 peer-hover:-translate-x-[200px]"
>
  {userMenuItems.map((item) => (
    <div className="py-4 px-8 flex flex-row items-center justify-start text-zinc-400">
      {item.label}
    </div>
  ))}
</div>

Using peer hover is also interesting, does anyone also know how to make the user menu appear on hover but then stay?

英文:

I'm trying to make a user menu appear using Tailwind and I'm not sure how to check if the peer is clicked or not to display the animation.

I could use a hook which makes it appear and disappear, etc. but if I use a hook, I won't be able to let the user menu slide in from the right like I'm trying.

Here's what I have:

&lt;div className=&quot;relative&quot;&gt;
  &lt;Image
    className=&quot;rounded-full cursor-pointer peer&quot;
    src={currentUser?.user.image}
    alt=&quot;user avatar&quot;
    width=&quot;35&quot;
    height=&quot;35&quot;
    onClick={() =&gt; menu.onToggle()}
  /&gt;
  =&lt;UserMenu /&gt;
&lt;/div&gt;

And the user menu:

&lt;div
  className=&quot;absolute bg-black top-[59px] left-[100px] rounded-bottom-md transform    duration-200 peer-hover:-translate-x-[200px]&quot;
&gt;
  {userMenuItems.map((item) =&gt; (
    &lt;div className=&quot;py-4 px-8 flex flex-row items-center justify-start text-zinc-400&quot;&gt;
      {item.label}
    &lt;/div&gt;
  ))}
&lt;/div&gt;

Using peer hover is also interesting, does anyone also know how to make the user menu appear on hover but then stay?

答案1

得分: 1

我找到了对我有效的问题的解决方案,但有点巧妙。请注意,我的解决方案是用于悬停的,但对于点击来说是类似的,唯一的区别是使用 onClick 属性而不是 onMouseEnter

悬停时进行动画,而不消失。

您可以使用状态 hovered 并使用 onMouseEnter 属性指示对等体是否被悬停,在 UserMenu 组件中,使用该状态有条件地应用 tailwind 类,使用模板文字。

这保留了动画,并应用了悬停功能。

const [hovered, setHovered] = useState(false);

//...

<div className="relative">
  <Image
    className="rounded-full cursor-pointer peer"
    src={currentUser?.user.image}
    alt="user avatar"
    width="35"
    height="35"
    onClick={() => menu.onToggle()}
    onMouseEnter={() => setHovered(true)}   // 悬停?将状态设置为 true。
  />
  <UserMenu hovered={hovered} />
</div>
<div
  className={`absolute top-[59px] left-[100px] rounded-bottom-md transform duration-200 ${hovered ? "bg-black -translate-x-[200px]" : "bg-transparent"}`}
>
  {userMenuItems.map((item) => (
    <div className={`py-4 px-8 flex flex-row items-center justify-start ${hovered ? "text-zinc-400" : "text-transparent"}`}>
      {item.label}
    </div>
  ))}
</div>

悬停时包含取消悬停时的消失。

通常,当菜单显示并且用户悬停在菜单上时,当您不再悬停在实际菜单上时,它将消失(对等体不相关)。为此,如果您愿意,您可以使用 onMouseLeave 属性使菜单消失。

此代码如下:

const [hovered, setHovered] = useState(false);

//...

<div className="relative">
  <Image
    className="rounded-full cursor-pointer peer"
    src={currentUser?.user.image}
    alt="user avatar"
    width="35"
    height="35"
    onClick={() => menu.onToggle()}
    onMouseEnter={() => setHovered(true)}   // 悬停?将状态设置为 true。
  />
  <UserMenu hovered={hovered} onLeave={() => setHovered(false)} />
</div>
<div
  className={`absolute top-[59px] left-[100px] rounded-bottom-md transform duration-200 ${hovered ? "bg-black -translate-x-[200px]" : "bg-transparent"}`}
>
  {userMenuItems.map((item) => (
    <div className={`py-4 px-8 flex flex-row items-center justify-start ${hovered ? "text-zinc-400" : "text-transparent"}`}
      onMouseLeave={onLeave}
    >
      {item.label}
    </div>
  ))}
</div>
英文:

I found a solution to that problem that worked for me, but is a little hacky. Notice that my solution is for hovering, yet for clicking it is similar, and the only difference is using the onClick prop instead of the onMouseEnter.

Animate on hover, without vanishing.

What you can do is have a state hovered and use the onMouseEnter prop to indicate whether the peer is hovered, and in the UserMenu component, use that state to apply tailwind classes conditionally, using template literals.

This preserves the animation, and applies the hover functionality.

const [hovered, setHovered] = useState(false);

//...

&lt;div className=&quot;relative&quot;&gt;
  &lt;Image
    className=&quot;rounded-full cursor-pointer peer&quot;
    src={currentUser?.user.image}
    alt=&quot;user avatar&quot;
    width=&quot;35&quot;
    height=&quot;35&quot;
    onClick={() =&gt; menu.onToggle()}
    onMouseEnter={() =&gt; setHovered(true)}   // hovered? set the state to true.
  /&gt;
  &lt;UserMenu hovered={hovered} /&gt;
&lt;/div&gt;
&lt;div
  className={`absolute top-[59px] left-[100px] rounded-bottom-md transform    duration-200 ${hovered ? &quot;bg-black -translate-x-[200px]&quot; : &quot;bg-transparent&quot;}`}
&gt;
  {userMenuItems.map((item) =&gt; (
    &lt;div className={`py-4 px-8 flex flex-row items-center justify-start ${hovered ? &quot;text-zinc-400&quot; : &quot;text-transparent&quot;}`}&gt;
      {item.label}
    &lt;/div&gt;
  ))}
&lt;/div&gt;

Include vanish when un-hover.

It is also common that when the menu is displayed and the user hovers the menu, it will vanish when you no longer hover the actually menu (the peer is irrelevant). For this, if you wish, you can use the onMouseLeave prop to make the menu vanish.

The code for this is given below:

const [hovered, setHovered] = useState(false);

//...

&lt;div className=&quot;relative&quot;&gt;
  &lt;Image
    className=&quot;rounded-full cursor-pointer peer&quot;
    src={currentUser?.user.image}
    alt=&quot;user avatar&quot;
    width=&quot;35&quot;
    height=&quot;35&quot;
    onClick={() =&gt; menu.onToggle()}
    onMouseEnter={() =&gt; setHovered(true)}   // hovered? set the state to true.
  /&gt;
  &lt;UserMenu hovered={hovered} onLeave={() =&gt; setHovered(false)} /&gt;
&lt;/div&gt;
&lt;div
  className={`absolute top-[59px] left-[100px] rounded-bottom-md transform    duration-200 ${hovered ? &quot;bg-black -translate-x-[200px]&quot; : &quot;bg-transparent&quot;}`}
&gt;
  {userMenuItems.map((item) =&gt; (
    &lt;div className={`py-4 px-8 flex flex-row items-center justify-start ${hovered ? &quot;text-zinc-400&quot; : &quot;text-transparent&quot;}`}
      onMouseLeave={onLeave}
    &gt;
      {item.label}
    &lt;/div&gt;
  ))}
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月25日 02:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定