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

93 wiersze
2.5 KiB
TypeScript
Czysty Zwykły widok Historia

2023-02-06 18:01:03 +00:00
import clsx from 'clsx';
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';
2022-05-03 14:43:17 +00:00
import { useSoapboxConfig } from 'soapbox/hooks';
2022-03-21 18:09:01 +00:00
import { Card, CardBody, CardHeader, CardTitle } from '../card/card';
type IColumnHeader = Pick<IColumn, 'label' | 'backHref' |'className'>;
2022-11-30 17:19:16 +00:00
/** Contains the column title with optional back button. */
const ColumnHeader: React.FC<IColumnHeader> = ({ label, backHref, className }) => {
2022-11-30 17:19:16 +00:00
const history = useHistory();
const handleBackClick = () => {
if (backHref) {
history.push(backHref);
return;
}
if (history.length === 1) {
history.push('/');
} else {
history.goBack();
}
};
return (
<CardHeader className={className} onBackClick={handleBackClick}>
2022-11-30 17:19:16 +00:00
<CardTitle title={label} />
</CardHeader>
);
};
export interface IColumn {
/** Route the back button goes to. */
backHref?: string
/** Column title text. */
label?: string
/** Whether this column should have a transparent background. */
transparent?: boolean
/** Whether this column should have a title and back button. */
withHeader?: boolean
/** Extra class name for top <div> element. */
className?: string
/** Ref forwarded to column. */
ref?: React.Ref<HTMLDivElement>
2023-01-10 23:03:15 +00:00
/** Children to display in the column. */
children?: React.ReactNode
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-05-03 14:43:17 +00:00
const soapboxConfig = useSoapboxConfig();
2022-03-21 18:09:01 +00:00
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-05-03 14:43:17 +00:00
<Helmet>
<title>{label}</title>
{soapboxConfig.appleAppId && (
<meta
data-react-helmet='true'
name='apple-itunes-app'
content={`app-id=${soapboxConfig.appleAppId}, app-argument=${location.href}`}
/>
)}
</Helmet>
2022-03-21 18:09:01 +00:00
2022-11-30 17:19:16 +00:00
<Card variant={transparent ? undefined : 'rounded'} className={className}>
{withHeader && (
<ColumnHeader
label={label}
backHref={backHref}
2023-02-06 18:01:03 +00:00
className={clsx({ 'px-4 pt-4 sm:p-0': transparent })}
/>
2022-11-30 17:19:16 +00:00
)}
<CardBody>
{children}
</CardBody>
</Card>
2022-03-21 18:09:01 +00:00
</div>
);
});
2022-11-30 17:19:16 +00:00
export {
Column,
ColumnHeader,
};