Files
Tom Moor 3d0d24c43d fix: Mobile header actions overlap the sidebar menu button while editing (#14629)
* fix: Mobile header actions overlap the sidebar menu button while editing

* fix: Keep mobile breadcrumbs shrinkable
2026-11-18 02:05:54 +00:00

344 lines
10 KiB
TypeScript

import { observer } from "mobx-react";
import { DoneIcon, TableOfContentsIcon, EditIcon } from "outline-icons";
import { useState, useCallback, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import useMeasure from "react-use-measure";
import styled, { useTheme } from "styled-components";
import Icon from "@shared/components/Icon";
import { HEADER_HEIGHT } from "@shared/constants";
import { s } from "@shared/styles";
import { altDisplay, metaDisplay } from "@shared/utils/keyboard";
import { publishDocument } from "~/actions/definitions/documents";
import { restoreRevision } from "~/actions/definitions/revisions";
import { Action } from "~/components/Actions";
import Badge from "~/components/Badge";
import Button from "~/components/Button";
import Collaborators from "~/components/Collaborators";
import DocumentBreadcrumb from "~/components/DocumentBreadcrumb";
import { useDocumentContext } from "~/components/DocumentContext";
import Flex from "~/components/Flex";
import Header from "~/components/Header";
import Star from "~/components/Star";
import Tooltip from "~/components/Tooltip";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import useCurrentUser from "~/hooks/useCurrentUser";
import useEditingFocus from "~/hooks/useEditingFocus";
import useKeyDown from "~/hooks/useKeyDown";
import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext";
import useMobile from "~/hooks/useMobile";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import DocumentMenu from "~/menus/DocumentMenu";
import NewChildDocumentMenu from "~/menus/NewChildDocumentMenu";
import TableOfContentsMenu from "~/menus/TableOfContentsMenu";
import TemplatesMenu from "~/menus/TemplatesMenu";
import type Document from "~/models/Document";
import type Revision from "~/models/Revision";
import type Template from "~/models/Template";
import { documentEditPath } from "~/utils/routeHelpers";
import { ChangesNavigation } from "./ChangesNavigation";
import ObservingBanner from "./ObservingBanner";
import { SearchHighlightChip } from "./SearchHighlightChip";
import ShareButton from "./ShareButton";
type Props = {
document: Document;
revision: Revision | undefined;
isDraft: boolean;
isEditing: boolean;
isSaving: boolean;
isPublishing: boolean;
publishingIsDisabled: boolean;
savingIsDisabled: boolean;
onSelectTemplate: (template: Template) => void;
onSave: (options: {
done?: boolean;
publish?: boolean;
autosave?: boolean;
}) => void;
};
function DocumentHeader({
document,
revision,
isEditing,
isDraft,
savingIsDisabled,
publishingIsDisabled,
onSelectTemplate,
onSave,
}: Props) {
const { t } = useTranslation();
const { ui } = useStores();
const theme = useTheme();
const team = useCurrentTeam({ rejectOnEmpty: false });
const user = useCurrentUser({ rejectOnEmpty: false });
const isMobileMedia = useMobile();
const isRevision = !!revision;
const isEditingFocus = useEditingFocus();
// Set CSS variable for header offset (used by sticky table headers)
useEffect(() => {
window.document.documentElement.style.setProperty(
"--header-offset",
isEditingFocus ? "0px" : `${HEADER_HEIGHT}px`
);
}, [isEditingFocus]);
const { hasHeadings, editor } = useDocumentContext();
const sidebarContext = useLocationSidebarContext();
const [measureRef, size] = useMeasure();
const isMobile = isMobileMedia || (size.width > 0 && size.width < 700);
// We cache this value for as long as the component is mounted so that if you
// apply a template there is still the option to replace it until the user
// navigates away from the doc
const [wasNew] = useState(document.isPersistedOnce);
const saveLabel = isDraft ? t("Save draft") : t("Done editing");
const handleSave = useCallback(() => {
onSave({
done: true,
});
}, [onSave]);
const handleToggle = useCallback(() => {
ui.set({ tocVisible: !ui.tocVisible });
}, [ui]);
const can = usePolicy(document);
const { isDeleted } = document;
const canToggleEmbeds = team?.documentEmbeds;
const showContents = ui.tocVisible === true;
const toc = (
<Tooltip
content={
showContents
? t("Hide contents")
: hasHeadings
? t("Show contents")
: `${t("Show contents")} (${t("available when headings are added")})`
}
shortcut={`Ctrl+${altDisplay}+h`}
side="bottom"
>
<TocButton
aria-label={t("Show contents")}
onClick={handleToggle}
icon={<TableOfContentsIcon />}
borderOnHover
neutral
/>
</Tooltip>
);
const editAction = (
<Action>
<Tooltip
content={t("Edit {{noun}}", {
noun: document.noun,
})}
shortcut="e"
side="bottom"
>
<Button
as={Link}
icon={<EditIcon />}
to={{
pathname: documentEditPath(document),
state: { sidebarContext },
}}
neutral
>
{isMobile ? null : t("Edit")}
</Button>
</Tooltip>
</Action>
);
useKeyDown(
(event) => event.ctrlKey && event.altKey && event.code === "KeyH",
handleToggle,
{
allowInInput: true,
}
);
return (
<StyledHeader
ref={measureRef}
$hidden={isEditingFocus}
hasSidebar
left={
isMobile ? null : (
<DocumentBreadcrumb document={document}>
{toc}{" "}
<StarAction>
<Star document={document} color={theme.textSecondary} />
</StarAction>
</DocumentBreadcrumb>
)
}
title={
<Flex gap={4} align="center">
{document.icon && (
<Icon
value={document.icon}
initial={document.initial}
color={document.color ?? undefined}
/>
)}
{document.title}
{document.isArchived && <Badge>{t("Archived")}</Badge>}
{document.isDraft && <Badge>{t("Draft")}</Badge>}
</Flex>
}
actions={({ isCompact }) => (
<>
<ObservingBanner />
<SearchHighlightChip />
{!isDeleted && !isRevision && can.listViews && (
<Collaborators
document={document}
limit={isCompact ? 3 : undefined}
/>
)}
{(isEditing || !user?.separateEditMode) && wasNew && can.update && (
<Action>
<TemplatesMenu
isCompact={isCompact}
document={document}
onSelectTemplate={onSelectTemplate}
/>
</Action>
)}
{!isEditing && !isRevision && can.update && (
<Action>
<ShareButton document={document} />
</Action>
)}
{isEditing && (
<Action>
<Tooltip
content={saveLabel}
shortcut={`${metaDisplay}+enter`}
side="bottom"
>
<Button
onClick={handleSave}
disabled={savingIsDisabled}
neutral={isDraft || isMobile}
icon={isMobile ? <DoneIcon /> : undefined}
aria-label={isMobile ? saveLabel : undefined}
>
{isMobile ? null : saveLabel}
</Button>
</Tooltip>
</Action>
)}
{can.update &&
!isEditing &&
user?.separateEditMode &&
!isRevision &&
editAction}
{can.update &&
can.createChildDocument &&
!isRevision &&
!isCompact &&
!isMobile && (
<Action>
<NewChildDocumentMenu document={document} />
</Action>
)}
{revision && (
<>
<Action>
<ChangesNavigation />
</Action>
<Action>
<Tooltip content={t("Restore version")} side="bottom">
<Button
action={restoreRevision}
disabled={revision.createdAt === document.updatedAt}
neutral
hideOnActionDisabled
>
{t("Restore")}
</Button>
</Tooltip>
</Action>
</>
)}
{can.publish && (
<Action>
<Button
action={publishDocument}
disabled={publishingIsDisabled}
hideOnActionDisabled
hideIcon
>
{document.collectionId ? t("Publish") : `${t("Publish")}…`}
</Button>
</Action>
)}
{isMobile && (
<Action>
<TableOfContentsMenu />
</Action>
)}
<Action>
<DocumentMenu
document={document}
align="end"
neutral
onSelectTemplate={onSelectTemplate}
onFindAndReplace={editor?.commands.openFindAndReplace}
showToggleEmbeds={canToggleEmbeds}
showDisplayOptions
/>
</Action>
</>
)}
/>
);
}
const StyledHeader = styled(Header)<{ $hidden: boolean }>`
transition: opacity 500ms ease-in-out;
${(props) => props.$hidden && "opacity: 0;"}
`;
const TocButton = styled(Button)`
border-radius: 4px;
&&:hover:not(:disabled),
&&[aria-expanded="true"] {
background: ${s("buttonNeutralHoverBackground")};
box-shadow: none;
transition: none;
}
`;
const StarAction = styled.span`
display: inline-flex;
button {
width: 32px;
height: 32px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 4px;
transition: background 100ms ease-in-out;
&:hover {
background: ${s("buttonNeutralHoverBackground")};
transition: none;
}
}
`;
export default observer(DocumentHeader);