2018-11-07 23:44:38 +01:00
|
|
|
// @flow
|
2019-07-24 23:02:35 +02:00
|
|
|
import React, { useRef, useState } from 'react';
|
2018-11-07 23:44:38 +01:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import Button from 'component/button';
|
2019-07-24 23:02:35 +02:00
|
|
|
import { useRect } from '@reach/rect';
|
2018-11-07 23:44:38 +01:00
|
|
|
|
2019-07-29 16:49:43 +02:00
|
|
|
const COLLAPSED_HEIGHT = 120;
|
2018-11-07 23:44:38 +01:00
|
|
|
|
|
|
|
type Props = {
|
2019-04-24 16:02:08 +02:00
|
|
|
children: React$Node | Array<React$Node>,
|
2018-11-07 23:44:38 +01:00
|
|
|
};
|
|
|
|
|
2019-07-24 23:02:35 +02:00
|
|
|
export default function Expandable(props: Props) {
|
|
|
|
const [expanded, setExpanded] = useState(false);
|
|
|
|
const { children } = props;
|
|
|
|
const ref = useRef();
|
|
|
|
const rect = useRect(ref);
|
2018-11-07 23:44:38 +01:00
|
|
|
|
2019-07-24 23:02:35 +02:00
|
|
|
function handleClick() {
|
|
|
|
setExpanded(!expanded);
|
2018-11-07 23:44:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 23:02:35 +02:00
|
|
|
return (
|
|
|
|
<div ref={ref}>
|
2019-07-29 16:49:43 +02:00
|
|
|
{rect && rect.height > COLLAPSED_HEIGHT ? (
|
2019-11-22 22:13:00 +01:00
|
|
|
<div ref={ref}>
|
2019-07-24 23:02:35 +02:00
|
|
|
<div
|
|
|
|
className={classnames({
|
|
|
|
'expandable--open': expanded,
|
|
|
|
'expandable--closed': !expanded,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
className="expandable__button"
|
|
|
|
label={expanded ? __('Less') : __('More')}
|
|
|
|
onClick={handleClick}
|
|
|
|
/>
|
2018-11-07 23:44:38 +01:00
|
|
|
</div>
|
2019-07-24 23:02:35 +02:00
|
|
|
) : (
|
|
|
|
<div>{children}</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2018-11-07 23:44:38 +01:00
|
|
|
}
|