mams_frontend/src/components/PageHeader.tsx

74 lines
2.1 KiB
TypeScript

import type { ReactNode } from 'react';
import { useNavigate } from 'react-router-dom';
import { Box, IconButton, Stack, Tooltip, Typography } from '@mui/material';
import ArrowBack from '@mui/icons-material/ArrowBack';
import { useT } from '../i18n';
/** Back one step in the browser history, or to `to` when this page was opened directly. */
export function BackButton({ to }: { to: string }) {
const nav = useNavigate();
const t = useT();
const goBack = () => {
const idx = (window.history.state as { idx?: number } | null)?.idx ?? 0;
if (idx > 0) nav(-1);
else nav(to);
};
return (
<Tooltip title={t('BACK')}>
<IconButton onClick={goBack} size="small" sx={{ border: 1, borderColor: 'divider' }} aria-label={t('BACK')}>
<ArrowBack fontSize="small" className="flip-rtl" />
</IconButton>
</Tooltip>
);
}
export function PageHeader({
title, subtitle, actions, lead, backTo,
}: {
title: ReactNode;
subtitle?: ReactNode;
actions?: ReactNode;
lead?: ReactNode;
/** show a back button; the value is where to go when there is no history */
backTo?: string;
}) {
return (
<Stack
direction={{ xs: 'column', sm: 'row' }}
spacing={1.5}
sx={{
alignItems: { sm: 'center' },
justifyContent: "space-between",
mb: 2
}}>
<Stack
direction="row"
spacing={1.5}
sx={{
alignItems: "center",
minWidth: 0
}}>
{backTo && <BackButton to={backTo} />}
{lead}
<Box sx={{ minWidth: 0 }}>
{/* the page's one h1: a screen reader's outline starts from it */}
<Typography
variant="h5"
component="h1"
noWrap
sx={{
fontWeight: 800,
color: '#d1a233'
}}>{title}</Typography>
{subtitle && <Typography variant="body2" component="div" sx={{
color: "text.secondary"
}}>{subtitle}</Typography>}
</Box>
</Stack>
{actions && <Stack direction="row" spacing={1} sx={{
flexShrink: 0
}}>{actions}</Stack>}
</Stack>
);
}