lbry-desktop/ui/component/expandable/view.jsx

48 lines
1.1 KiB
React
Raw Normal View History

2018-11-07 23:44:38 +01:00
// @flow
import React, { useRef, useState } from 'react';
2018-11-07 23:44:38 +01:00
import classnames from 'classnames';
import Button from 'component/button';
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
};
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
function handleClick() {
setExpanded(!expanded);
2018-11-07 23:44:38 +01: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}>
<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>
) : (
<div>{children}</div>
)}
</div>
);
2018-11-07 23:44:38 +01:00
}