soapbox/app/soapbox/components/ui/column/column.tsx

71 wiersze
1.9 KiB
TypeScript
Czysty Zwykły widok Historia

import classNames from 'classnames';
2022-03-21 18:09:01 +00:00
import React from 'react';
2022-03-22 12:42:26 +00:00
import { useHistory } from 'react-router-dom';
2022-03-21 18:09:01 +00:00
import Helmet from 'soapbox/components/helmet';
import { Card, CardBody, CardHeader, CardTitle } from '../card/card';
2022-03-22 12:42:26 +00:00
interface IColumn {
/** Route the back button goes to. */
2022-03-21 18:09:01 +00:00
backHref?: string,
/** Column title text. */
2022-03-21 18:09:01 +00:00
label?: string,
/** Whether this column should have a transparent background. */
2022-03-21 18:09:01 +00:00
transparent?: boolean,
/** Whether this column should have a title and back button. */
2022-03-21 18:09:01 +00:00
withHeader?: boolean,
/** Extra class name for top <div> element. */
className?: string,
2022-03-21 18:09:01 +00:00
}
/** A backdrop for the main section of the UI. */
2022-03-21 18:09:01 +00:00
const Column: React.FC<IColumn> = React.forwardRef((props, ref: React.ForwardedRef<HTMLDivElement>): JSX.Element => {
const { backHref, children, label, transparent = false, withHeader = true, className } = props;
2022-03-22 12:42:26 +00:00
const history = useHistory();
const handleBackClick = () => {
if (backHref) {
history.push(backHref);
return;
}
if (history.length === 1) {
history.push('/');
} else {
history.goBack();
}
};
2022-03-21 18:09:01 +00:00
const renderChildren = () => {
if (transparent) {
return <div className={classNames('bg-white dark:bg-slate-800 text-black dark:text-white sm:bg-transparent sm:dark:bg-transparent', className)}>{children}</div>;
2022-03-21 18:09:01 +00:00
}
return (
<Card variant='rounded' className={className}>
2022-03-21 18:09:01 +00:00
{withHeader ? (
<CardHeader onBackClick={handleBackClick}>
2022-03-21 18:09:01 +00:00
<CardTitle title={label} />
</CardHeader>
) : null}
<CardBody>
{children}
</CardBody>
</Card>
);
};
return (
2022-03-28 23:54:43 +00:00
<div role='region' className='relative' ref={ref} aria-label={label} column-type={transparent ? 'transparent' : 'filled'}>
2022-03-21 18:09:01 +00:00
<Helmet><title>{label}</title></Helmet>
{renderChildren()}
</div>
);
});
2022-03-22 12:42:26 +00:00
export default Column;