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

50 lines
1.2 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
// Note:
// When we use this in other parts of the app, we will probably need to
// add props for collapsed height
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}>
{rect && rect.height > 120 ? (
<div ref={ref} className="expandable">
<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
}