Skip to content

Commit

Permalink
feat: use headlessui's disclosure to improve keyboard navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-salzmann committed Oct 13, 2023
1 parent 95b9cb4 commit fe0271c
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/library/accordeon/accordeon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { Disclosure } from "@headlessui/react";
import { ReactElement } from "../types";

interface AccordeonProps {
Expand All @@ -12,28 +12,21 @@ export const Accordeon = ({
className = "",
children,
}: AccordeonProps) => {
const [isOpen, setIsOpen] = useState<boolean>(false);

const onTitleClick = () => {
setIsOpen((isOpen) => !isOpen);
};

return (
<div className={className}>
<div
className="flex w-max cursor-pointer select-none items-center gap-2"
onClick={onTitleClick}
>
<span className="material-symbols-rounded">
{isOpen ? "expand_less" : "expand_more"}
</span>
{title}
</div>
{isOpen && (
<div className="mt-3 rounded-md bg-slate-200 p-3 text-slate-800">
{children}
</div>
<Disclosure as="div" className={className}>
{({ open }) => (
<>
<Disclosure.Button className="flex w-max cursor-pointer select-none items-center gap-2">
<span className="material-symbols-rounded">
{open ? "expand_less" : "expand_more"}
</span>
{title}
</Disclosure.Button>
<Disclosure.Panel className="mt-3 rounded-md bg-slate-200 p-3 text-slate-800">
{children}
</Disclosure.Panel>
</>
)}
</div>
</Disclosure>
);
};

0 comments on commit fe0271c

Please sign in to comment.