spaCy/website/src/components/accordion.js

70 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-03-13 00:57:15 +03:00
import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import Link from './link'
import classes from '../styles/accordion.module.sass'
const Accordion = ({ title, id, expanded, children }) => {
2019-03-13 00:57:15 +03:00
const [isExpanded, setIsExpanded] = useState(true)
const contentClassNames = classNames(classes.content, {
[classes.hidden]: !isExpanded,
})
const iconClassNames = classNames({
[classes.hidden]: isExpanded,
})
2019-03-13 00:57:15 +03:00
// Make sure accordion is expanded if JS is disabled
useEffect(() => setIsExpanded(expanded), [])
return (
2019-03-13 00:57:15 +03:00
<section className="accordion" id={id}>
<div className={classes.root}>
2019-03-13 00:57:15 +03:00
<h4>
<button
className={classes.button}
aria-expanded={String(isExpanded)}
onClick={() => setIsExpanded(!isExpanded)}
>
<span>
2019-03-13 00:57:15 +03:00
<span className="heading-text">{title}</span>
{isExpanded && !!id && (
2019-03-13 00:57:15 +03:00
<Link
to={`#${id}`}
className={classes.anchor}
hidden
onClick={event => event.stopPropagation()}
>
&para;
</Link>
)}
</span>
<svg
className={classes.icon}
width={20}
height={20}
viewBox="0 0 10 10"
aria-hidden="true"
focusable="false"
>
<rect className={iconClassNames} height={8} width={2} x={4} y={1} />
<rect height={2} width={8} x={1} y={4} />
</svg>
</button>
2019-03-13 00:57:15 +03:00
</h4>
<div className={contentClassNames}>{children}</div>
</div>
</section>
)
}
Accordion.defaultProps = {
expanded: false,
}
Accordion.propTypes = {
title: PropTypes.string,
id: PropTypes.string,
children: PropTypes.node.isRequired,
}
export default Accordion