自动在鼠标离开时折叠 Shadcn-UI 手风琴

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

Auto-Collapse Shadcn-UI Accordion on mouseLeave

问题

我一直在尝试构建一个带有手风琴效果的小侧边栏,用于自我教育项目。我对 Shadcn-ui、NextJS(13.4)、React 和 Node 都比较新手,总体来说,我遇到了一个小问题,无法解决。

侧边栏在鼠标悬停时扩展,至少对于我在这个项目中尝试学习的内容来说,这个功能都很好用。但是,在鼠标离开时,当元素扩展时,它们在侧边栏再次收缩时看起来很糟糕。

我想绑定一个 onMouseLeave 事件或类似的东西到侧边栏,以便在它收缩时也将手风琴元素折叠起来,但是我尝试过的一切似乎都不起作用。

有人有关于解决这个问题的指导吗?

我当前侧边栏的代码如下:

英文:

So I've been trying to build a little sidebar with an accordion for a self-education project, I'm pretty new to Shadcn-ui, NextJS (13.4), React and Node in general, and I've run into a small problem I can't figure out.

The sidebar extends on mouseover, and that and all the sidebar functionality works great, at least for what I'm trying to learn with this project, but on mouseLeave, when the elements are expanded, they look terrible when the sidebar shrinks again.

I'd like to bind an onMouseLeave event or something to the sidebar so that when it shrinks, it'll also collapse the accordion elements back in, but nothing I've tried seems to work.

Anyone got any pointers for figuring this one out?

Code for my sidebar currently:

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/components/ui/accordion";
import Link from "next/link";
import { MdLock, MdCorporateFare, MdDashboard, MdDevices } from "react-icons/md";

export default function Sidebar() {
  return (
    <div className="fixed left-0 md:w-[5%] w-[15%] bg-primary h-full hover:w-[50%] md:hover:w-[20%] easing shadow-lg w-fit">
      <Accordion type="single" collapsible className="w-full">
        <AccordionItem value="item-1" className="sidebarItem">
          <AccordionTrigger className="p-5">
            <MdDashboard ></MdDashboard>
          </AccordionTrigger>
          <AccordionContent>
          <ul>
              <li className="hover:bg-tertiary p-5">
                <Link href="/page" >an item</Link>
              </li>
              <li className="text-secondary p-5 hover:bg-tertiary">
                <Link href="/page">an item</Link>
              </li>
            </ul>
          </AccordionContent>
        </AccordionItem>
        <AccordionItem value="item-2" className="sidebarItem">
          <AccordionTrigger className="p-5">
            <MdCorporateFare ></MdCorporateFare>
          </AccordionTrigger>
          <AccordionContent>
          <ul>
              <li className="hover:bg-tertiary p-5">
                <Link href="/page" className="text-ellipsis">an item</Link>
              </li>
              <li className="text-secondary p-5 hover:bg-tertiary">
                <Link href="/page">an item</Link>
              </li>
            </ul>
          </AccordionContent>
        </AccordionItem>
        <AccordionItem value="item-3" className="sidebarItem">
          <AccordionTrigger className="p-5">
            <MdDevices ></MdDevices>
          </AccordionTrigger>
          <AccordionContent>
          <ul>
              <li className="hover:bg-tertiary p-5">
                <Link href="/page" >an item</Link>
              </li>
              <li className="text-secondary p-5 hover:bg-tertiary ">
                <Link href="/page" className="text-ellipsis">an item</Link>
              </li>
            </ul>
          </AccordionContent>
        </AccordionItem>
        <AccordionItem value="item-4" className="sidebarItem">
          <AccordionTrigger className="p-5">
            <MdLock className="text-secondary "/>
          </AccordionTrigger>
          <AccordionContent>
            <ul>
              <li className="text-secondary p-5 hover:bg-tertiary easing text-ellipsis w-fit">
                <Link href="/page" >an item</Link>
              </li>
              <li className="text-secondary p-5 hover:bg-tertiary easing text-ellipsis">
                <Link href="/page">an item</Link>
              </li>
            </ul>
          </AccordionContent>
        </AccordionItem>
      </Accordion>
    </div>
  );
}

答案1

得分: 0

你需要为侧边栏缩小时展开手风琴元素添加功能。为了实现这个目标,你可以使用React的useState钩子来管理手风琴项目的状态并处理onMouseLeave事件。以下是如何实现的:

首先从React中导入useState钩子:

import React, { useState } from "react";

其次,修改你的Sidebar组件以使用useState钩子并处理手风琴的状态:

export default function Sidebar() {
  const [accordionOpen, setAccordionOpen] = useState("");

  const handleAccordionCollapse = () => {
    setAccordionOpen("");
  };

  const handleAccordionExpand = (value) => {
    setAccordionOpen(value);
  };

  return (
    <div
      className="fixed left-0 md:w-[5%] w-[15%] bg-primary h-full hover:w-[50%] md:hover:w-[20%] easing shadow-lg w-fit"
      onMouseLeave={handleAccordionCollapse}
    >
      <Accordion type="single" collapsible className="w-full">
        <AccordionItem
          value="item-1"
          className={`sidebarItem ${accordionOpen === "item-1" ? "open" : ""}`}
        >
          <AccordionTrigger
            className="p-5"
            onClick={() =>
              handleAccordionExpand(accordionOpen === "item-1" ? "" : "item-1")
            }
          >
            <MdDashboard />
          </AccordionTrigger>
          <AccordionContent>
            <ul>
              <li className="hover:bg-tertiary p-5">
                <Link href="/page">an item</Link>
              </li>
              <li className="text-secondary p-5 hover:bg-tertiary">
                <Link href="/page">an item</Link>
              </li>
            </ul>
          </AccordionContent>
        </AccordionItem>
        {/* Add other AccordionItems here */}
      </Accordion>
    </div>
  );
}

通过使用useState钩子,我们监控accordionOpen状态变量,以了解当前展开的手风琴项目。当鼠标离开侧边栏时,会触发handleAccordionCollapse函数,并将accordionOpen状态重置为收起展开的项目。此外,我们使用handleAccordionExpand来在点击手风琴项目时切换展开状态。className根据accordionOpen状态动态调整,以应用于展开的手风琴项目的样式。

英文:

you need to feature functionality to fall apart the accordion elements whilst the sidebar shrinks. To acquire this, you may use React's useState hook to manage the nation of the accordion gadgets and handle the onMouseLeave event. right here's how you may do it:

First Import the useState hook from React:

import React, { useState } from &quot;react&quot;;

Eecond Modify your Sidebar component to use the useState hook and handle the accordion state:

export default function Sidebar() {
  const [accordionOpen, setAccordionOpen] = useState(&quot;&quot;);

  const handleAccordionCollapse = () =&gt; {
    setAccordionOpen(&quot;&quot;);
  };

  const handleAccordionExpand = (value) =&gt; {
    setAccordionOpen(value);
  };

  return (
    &lt;div
      className=&quot;fixed left-0 md:w-[5%] w-[15%] bg-primary h-full hover:w-[50%] md:hover:w-[20%] easing shadow-lg w-fit&quot;
      onMouseLeave={handleAccordionCollapse}
    &gt;
      &lt;Accordion type=&quot;single&quot; collapsible className=&quot;w-full&quot;&gt;
        &lt;AccordionItem
          value=&quot;item-1&quot;
          className={`sidebarItem ${accordionOpen === &quot;item-1&quot; ? &quot;open&quot; : &quot;&quot;}`}
        &gt;
          &lt;AccordionTrigger
            className=&quot;p-5&quot;
            onClick={() =&gt;
              handleAccordionExpand(accordionOpen === &quot;item-1&quot; ? &quot;&quot; : &quot;item-1&quot;)
            }
          &gt;
            &lt;MdDashboard /&gt;
          &lt;/AccordionTrigger&gt;
          &lt;AccordionContent&gt;
            &lt;ul&gt;
              &lt;li className=&quot;hover:bg-tertiary p-5&quot;&gt;
                &lt;Link href=&quot;/page&quot;&gt;an item&lt;/Link&gt;
              &lt;/li&gt;
              &lt;li className=&quot;text-secondary p-5 hover:bg-tertiary&quot;&gt;
                &lt;Link href=&quot;/page&quot;&gt;an item&lt;/Link&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/AccordionContent&gt;
        &lt;/AccordionItem&gt;
        {/* Add other AccordionItems here */}
      &lt;/Accordion&gt;
    &lt;/div&gt;
  );
}

By way of using the useState hook, we're monitoring the accordionOpen state variable to recognise which accordion item is nowadays extended. The handleAccordionCollapse characteristic may be prompted when the mouse leaves the sidebar, and it resets the accordionOpen state to fall apart the increased items. additionally, we're the use of handleAccordionExpand to toggle the expansion whilst an accordion object is clicked. The className is dynamically adjusted based on the accordionOpen country to use patterns to the multiplied accordion object.

huangapple
  • 本文由 发表于 2023年8月4日 03:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831122.html
匿名

发表评论

匿名网友

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

确定