英文:
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 "react";
Eecond Modify your Sidebar
component to use the useState
hook and handle the accordion state:
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>
);
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论