stacker.news/components/accordian-item.js

47 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-07-24 18:35:05 +00:00
import Accordion from 'react-bootstrap/Accordion'
import AccordionContext from 'react-bootstrap/AccordionContext'
import { useAccordionButton } from 'react-bootstrap/AccordionButton'
import ArrowRight from '@/svgs/arrow-right-s-fill.svg'
import ArrowDown from '@/svgs/arrow-down-s-fill.svg'
2023-07-24 18:35:05 +00:00
import { useContext } from 'react'
2021-09-11 21:52:19 +00:00
2023-07-24 18:35:05 +00:00
function ContextAwareToggle ({ children, headerColor = 'var(--theme-grey)', eventKey }) {
const { activeEventKey } = useContext(AccordionContext)
const decoratedOnClick = useAccordionButton(eventKey)
const isCurrentEventKey = activeEventKey === eventKey
2021-09-11 21:52:19 +00:00
2023-07-24 18:35:05 +00:00
return (
<div style={{ cursor: 'pointer', display: 'flex', alignItems: 'center' }} onClick={decoratedOnClick}>
{isCurrentEventKey
? <ArrowDown style={{ fill: headerColor }} height={20} width={20} />
: <ArrowRight style={{ fill: headerColor }} height={20} width={20} />}
{children}
</div>
)
}
2021-09-11 21:52:19 +00:00
2023-07-24 18:35:05 +00:00
export default function AccordianItem ({ header, body, headerColor = 'var(--theme-grey)', show }) {
2021-09-11 21:52:19 +00:00
return (
2023-07-24 18:35:05 +00:00
<Accordion defaultActiveKey={show ? '0' : undefined}>
<ContextAwareToggle eventKey='0'><div style={{ color: headerColor }}>{header}</div></ContextAwareToggle>
2021-09-11 21:52:19 +00:00
<Accordion.Collapse eventKey='0' className='mt-2'>
<div>{body}</div>
</Accordion.Collapse>
</Accordion>
)
}
2023-11-21 23:32:22 +00:00
export function AccordianCard ({ header, children, show }) {
return (
<Accordion defaultActiveKey={show ? '0' : undefined}>
<Accordion.Item eventKey='0'>
<Accordion.Header>{header}</Accordion.Header>
<Accordion.Body>
{children}
</Accordion.Body>
</Accordion.Item>
</Accordion>
)
}