-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c415bd0
commit ba59ce0
Showing
3 changed files
with
172 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"use client" | ||
|
||
import {HTMLAttributes, JSX, useId} from "react" | ||
import {ChevronDownIcon, ChevronUpIcon} from "@heroicons/react/20/solid" | ||
import {twMerge} from "tailwind-merge" | ||
import useAccordion from "@/lib/hooks/useAccordion" | ||
|
||
type Props = HTMLAttributes<HTMLElement> & { | ||
/** | ||
* Button clickable element or string. | ||
*/ | ||
button: JSX.Element | string | ||
/** | ||
* Heading level element. | ||
*/ | ||
headingLevel?: "h2" | "h3" | "h4" | ||
/** | ||
* If the accordion should be visible on first render. | ||
*/ | ||
initiallyVisible?: boolean | ||
/** | ||
* Button click event if the component is controlled. | ||
*/ | ||
onClick?: () => void | ||
/** | ||
* Panel visibility state if the component is controlled. | ||
*/ | ||
isVisible?: boolean | ||
/** | ||
* Extra attributes on the button element. | ||
*/ | ||
buttonProps?: HTMLAttributes<HTMLButtonElement> | ||
/** | ||
* Extra attributes on the panel element. | ||
*/ | ||
panelProps?: HTMLAttributes<HTMLDivElement> | ||
} | ||
|
||
const Accordion = ({button, children, headingLevel = "h2", ...props}: Props) => { | ||
const id = useId() | ||
const {buttonProps, panelProps, expanded, ref} = useAccordion({buttonId: `${id}-button`}) | ||
|
||
const Heading = headingLevel === "h2" ? "h2" : headingLevel === "h3" ? "h3" : "h3" | ||
return ( | ||
<section | ||
{...props} | ||
ref={ref} | ||
aria-labelledby={`${id}-button`} | ||
className={twMerge("relative w-full", props.className)} | ||
> | ||
<Heading className="type-0 mb-0 font-sans font-semibold"> | ||
<button | ||
{...buttonProps} | ||
className={twMerge( | ||
"relative flex w-full items-center justify-between rounded-full border border-black-40 px-5 py-9 pl-15 text-left", | ||
buttonProps?.className | ||
)} | ||
> | ||
{button} | ||
<span className="grow-1 m-4 font-normal text-black transition"> | ||
{expanded && <ChevronUpIcon height={20} className="ml-auto shrink-0" />} | ||
|
||
{!expanded && <ChevronDownIcon height={20} className="ml-auto shrink-0" />} | ||
</span> | ||
</button> | ||
</Heading> | ||
|
||
<div | ||
{...panelProps} | ||
className={twMerge( | ||
"max-h-[300px] w-full overflow-y-scroll border border-black-20 bg-white shadow-lg", | ||
panelProps.className | ||
)} | ||
> | ||
<div>{children}</div> | ||
</div> | ||
</section> | ||
) | ||
} | ||
export default Accordion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use client" | ||
|
||
import {HTMLAttributes, useId, useRef} from "react" | ||
import {useBoolean, useOnClickOutside} from "usehooks-ts" | ||
|
||
type Props = { | ||
initiallyVisible?: boolean | ||
panelId?: string | ||
buttonId?: string | ||
} | ||
|
||
const useAccordion = ({initiallyVisible, panelId, buttonId}: Props = {}) => { | ||
const { | ||
value: expanded, | ||
setTrue: expandAccordion, | ||
setFalse: collapseAccordion, | ||
toggle: toggleExpanded, | ||
} = useBoolean(initiallyVisible) | ||
const id = useId() | ||
const ref = useRef<HTMLDivElement>(null) | ||
|
||
useOnClickOutside(ref, collapseAccordion) | ||
|
||
const buttonProps: HTMLAttributes<HTMLButtonElement> = { | ||
"aria-controls": panelId || `${id}--panel`, | ||
"aria-expanded": expanded, | ||
id: buttonId || `${id}--button`, | ||
onClick: toggleExpanded, | ||
} | ||
|
||
const panelProps: HTMLAttributes<HTMLElement> = { | ||
"aria-labelledby": buttonId || `${id}--button`, | ||
id: panelId || `${id}--panel`, | ||
role: "region", | ||
className: expanded ? "block absolute left-0 top-full z-[10]" : "hidden", | ||
} | ||
|
||
return { | ||
buttonProps, | ||
panelProps, | ||
expanded, | ||
toggleExpanded, | ||
expandAccordion, | ||
collapseAccordion, | ||
ref, | ||
} | ||
} | ||
|
||
export default useAccordion |