lbry-desktop/ui/component/common/card.jsx

88 lines
2.4 KiB
React
Raw Normal View History

2019-09-26 18:07:11 +02:00
// @flow
import type { Node } from 'react';
import React, { useState } from 'react';
2019-09-26 18:07:11 +02:00
import classnames from 'classnames';
import Icon from 'component/common/icon';
import Button from 'component/button';
import { toCapitalCase } from 'util/string';
import * as ICONS from 'constants/icons';
2019-09-26 18:07:11 +02:00
type Props = {
2019-09-27 20:56:15 +02:00
title?: string | Node,
2019-09-26 18:07:11 +02:00
subtitle?: string | Node,
titleActions?: string | Node,
2019-09-26 18:07:11 +02:00
body?: string | Node,
actions?: string | Node,
icon?: string,
2019-11-22 22:13:00 +01:00
className?: string,
isPageTitle?: boolean,
isBodyTable?: boolean,
defaultExpand?: boolean,
nag?: Node,
2019-09-26 18:07:11 +02:00
};
export default function Card(props: Props) {
const {
title,
subtitle,
titleActions,
body,
actions,
icon,
className,
isPageTitle = false,
isBodyTable = false,
defaultExpand,
nag,
} = props;
const [expanded, setExpanded] = useState(defaultExpand);
const expandable = defaultExpand !== undefined;
2019-09-26 18:07:11 +02:00
return (
2019-11-22 22:13:00 +01:00
<section className={classnames(className, 'card')}>
{(title || subtitle) && (
<div className="card__header--between">
<div className="card__section--flex">
{icon && <Icon sectionIcon icon={icon} />}
<div>
{isPageTitle && <h1 className="card__title">{title}</h1>}
{!isPageTitle && <h2 className="card__title">{title}</h2>}
{subtitle && <div className="card__subtitle">{subtitle}</div>}
</div>
2019-09-26 18:07:11 +02:00
</div>
<div>
{titleActions && <div className="card__title-actions">{titleActions}</div>}
{expandable && (
<div className="card__title-actions">
<Button
button={'alt'}
aria-label={__('More')}
icon={expanded ? toCapitalCase(ICONS.SUBTRACT) : toCapitalCase(ICONS.ADD)}
onClick={() => setExpanded(!expanded)}
/>
</div>
)}
</div>
2019-09-26 18:07:11 +02:00
</div>
2019-09-27 20:56:15 +02:00
)}
{(!expandable || (expandable && expanded)) && (
<>
{body && (
<div
className={classnames('card__body', {
'card__body--no-title': !title && !subtitle,
'card__body--table': isBodyTable,
})}
>
{body}
</div>
)}
{actions && <div className="card__main-actions">{actions}</div>}
</>
2019-09-26 18:07:11 +02:00
)}
{nag}
2019-09-26 18:07:11 +02:00
</section>
);
}