forked from mantinedev/next-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a reusable Collapse component
- Loading branch information
Showing
5 changed files
with
78 additions
and
110 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,61 @@ | ||
'use client'; | ||
|
||
import { IconChevronDown, IconChevronUp } from '@tabler/icons-react'; | ||
import { useState } from 'react'; | ||
import { CollapseProps } from '~/shared/types'; | ||
|
||
const Collapse = ({ items, classCollapseItem }: CollapseProps) => { | ||
const [toggle, setToggle] = useState<boolean>(true); | ||
const [activeIndex, setActiveIndex] = useState<undefined | number>(undefined); | ||
|
||
const handleSetIndex = (index: number) => { | ||
if (activeIndex !== index) { | ||
setActiveIndex(index); | ||
setToggle(!toggle); | ||
} else { | ||
setActiveIndex(undefined); | ||
setToggle(!toggle); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{items.map(({ title, description }, index) => ( | ||
<div | ||
key={`accordion-${index}`} | ||
onClick={() => handleSetIndex(index)} | ||
className="mx-auto max-w-3xl select-none bg-transparent text-base text-gray-700" | ||
> | ||
<div className={classCollapseItem}> | ||
<div | ||
className="align-center flex justify-between" | ||
id={`accordion__heading-${index}`} | ||
aria-disabled="false" | ||
aria-expanded="false" | ||
aria-controls={`accordion__panel-${index}`} | ||
role="button" | ||
> | ||
<h2 className="w-full pr-2 text-lg font-medium leading-6 text-gray-900 dark:text-slate-300">{title}</h2> | ||
{activeIndex === index ? ( | ||
<IconChevronUp className="h-6 w-6 text-primary-600 dark:text-slate-200" /> | ||
) : ( | ||
<IconChevronDown className="h-6 w-6 text-primary-600 dark:text-slate-200" /> | ||
)} | ||
</div> | ||
{activeIndex === index && ( | ||
<div | ||
className="mt-3 select-none" | ||
aria-labelledby={`accordion__heading-${index}`} | ||
id={`accordion__panel-${index}`} | ||
> | ||
<p className="mt-2 text-gray-600 dark:text-slate-400">{description}</p> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default Collapse; |
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
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