-
-
Notifications
You must be signed in to change notification settings - Fork 430
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
Showing
8 changed files
with
209 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
import { Children, cloneElement, FC, ReactElement, useMemo } from 'react'; | ||
import { AccordionPanel, AccordionPanelProps } from './AccordionPanel'; | ||
import { AccordionTitle } from './AccordionTitle'; | ||
import { AccordionContent } from './AccordionContent'; | ||
import classNames from 'classnames'; | ||
|
||
export type AccordionProps = { | ||
flush?: boolean; | ||
}; | ||
|
||
const AccordionComponent: FC<AccordionProps> = ({ children, flush }) => { | ||
const panels = useMemo( | ||
() => Children.map(children as ReactElement<AccordionPanelProps>[], (child) => cloneElement(child, { flush })), | ||
[children, flush], | ||
); | ||
|
||
return ( | ||
<div | ||
className={classNames('divide-y divide-gray-200 border-gray-200 dark:divide-gray-700 dark:border-gray-700', { | ||
'rounded-lg border': !flush, | ||
'border-b': flush, | ||
})} | ||
> | ||
{panels} | ||
</div> | ||
); | ||
}; | ||
|
||
AccordionComponent.displayName = 'Accordion'; | ||
|
||
export const Accordion = Object.assign(AccordionComponent, { | ||
Panel: AccordionPanel, | ||
Title: AccordionTitle, | ||
Content: AccordionContent, | ||
}); |
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,16 @@ | ||
import { ComponentProps, FC } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { useAccordionContext } from './AccordionPanelContext'; | ||
|
||
export const AccordionContent: FC<ComponentProps<'div'>> = ({ children, className, ...props }) => { | ||
const { isOpen } = useAccordionContext(); | ||
|
||
return isOpen ? ( | ||
<div {...props} className={classNames('py-5 dark:bg-gray-900', className)}> | ||
{children} | ||
</div> | ||
) : null; | ||
}; | ||
|
||
AccordionContent.displayName = 'Accordion.Content'; |
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,26 @@ | ||
import { Children, cloneElement, ComponentProps, FC, ReactElement, useMemo, useState } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { AccordionPanelContext } from './AccordionPanelContext'; | ||
|
||
export type AccordionPanelProps = { | ||
open?: boolean; | ||
flush?: boolean; | ||
}; | ||
|
||
export const AccordionPanel: FC<AccordionPanelProps> = ({ children, open, flush }) => { | ||
const [isOpen, setIsOpen] = useState(open); | ||
const items = useMemo( | ||
() => | ||
Children.map(children as ReactElement<ComponentProps<'div' | 'button'>>[], (child) => | ||
cloneElement(child, { | ||
className: classNames(child.props.className, { 'px-5 first:rounded-t-lg last:rounded-b-lg': !flush }), | ||
}), | ||
), | ||
[children, flush], | ||
); | ||
|
||
return <AccordionPanelContext.Provider value={{ flush, isOpen, setIsOpen }}>{items}</AccordionPanelContext.Provider>; | ||
}; | ||
|
||
AccordionPanel.displayName = 'Accordion.Panel'; |
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,19 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
type AccordionPanelContext = { | ||
flush?: boolean; | ||
isOpen?: boolean; | ||
setIsOpen: (isOpen: boolean) => void; | ||
}; | ||
|
||
export const AccordionPanelContext = createContext<AccordionPanelContext | undefined>(undefined); | ||
|
||
export function useAccordionContext(): AccordionPanelContext { | ||
const context = useContext(AccordionPanelContext); | ||
|
||
if (!context) { | ||
throw new Error('useAccordionContext should be used within the AccordionPanelContext provider!'); | ||
} | ||
|
||
return context; | ||
} |
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,42 @@ | ||
import { ComponentProps, FC } from 'react'; | ||
import classNames from 'classnames'; | ||
import { HiChevronDown } from 'react-icons/hi'; | ||
|
||
import { useAccordionContext } from './AccordionPanelContext'; | ||
|
||
export type AccordionTitleProps = ComponentProps<'button'> & { | ||
arrowIcon?: FC<ComponentProps<'svg'>>; | ||
}; | ||
|
||
export const AccordionTitle: FC<AccordionTitleProps> = ({ | ||
children, | ||
className, | ||
arrowIcon: ArrowIcon = HiChevronDown, | ||
...props | ||
}) => { | ||
const { flush, isOpen, setIsOpen } = useAccordionContext(); | ||
|
||
const onClick = () => setIsOpen(!isOpen); | ||
|
||
return ( | ||
<button | ||
{...props} | ||
type="button" | ||
className={classNames( | ||
'flex w-full items-center justify-between py-5 text-left font-medium text-gray-500 dark:text-gray-400', | ||
{ | ||
'hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 dark:hover:bg-gray-800 dark:focus:ring-gray-800': !flush, | ||
'text-gray-900 dark:text-white': isOpen, | ||
'bg-gray-100 dark:bg-gray-800': isOpen && !flush, | ||
}, | ||
className, | ||
)} | ||
onClick={onClick} | ||
> | ||
<h2>{children}</h2> | ||
<ArrowIcon className={classNames('h-6 w-6 shrink-0', { 'rotate-180': isOpen })} /> | ||
</button> | ||
); | ||
}; | ||
|
||
AccordionTitle.displayName = 'Accordion.Title'; |
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