Files
outline/app/menus/TableOfContentsMenu.tsx
T
Tom MoorandClaude Opus 4.8 e7613baccf fix: Retain location state when clicking on hash links in doc and TOC (#12805)
* fix: Retain location state when clicking on hash links in doc and TOC

* fix: Pass explicit location fields when navigating to hash links

Avoid spreading history.location (which carries internal fields like key)
and preserve the href's query string for absolute same-path links.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Refactor to patchLocation helper

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 21:00:41 -04:00

99 lines
2.9 KiB
TypeScript

import { observer } from "mobx-react";
import { TableOfContentsIcon } from "outline-icons";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { EmojiText } from "@shared/components/EmojiText";
import { createAction, createActionGroup } from "~/actions";
import { ActiveDocumentSection } from "~/actions/sections";
import Button from "~/components/Button";
import { useDocumentContext } from "~/components/DocumentContext";
import { DropdownMenu } from "~/components/Menu/DropdownMenu";
import { useMenuAction } from "~/hooks/useMenuAction";
import history, { patchLocation } from "~/utils/history";
function TableOfContentsMenu() {
const { headings } = useDocumentContext();
const { t } = useTranslation();
const minHeading = headings.reduce(
(memo, heading) => (heading.level < memo ? heading.level : memo),
Infinity
);
const headingActions = useMemo(
() =>
headings
.filter((heading) => heading.level < 4)
.map((heading) =>
createAction({
name: (
<HeadingWrapper $level={heading.level - minHeading}>
<EmojiText>{heading.title}</EmojiText>
</HeadingWrapper>
),
section: ActiveDocumentSection,
perform: () =>
requestAnimationFrame(() =>
requestAnimationFrame(() =>
// Navigate via history so the location state (active sidebar
// context) is retained when scrolling to the heading.
history.push(
patchLocation(history.location, { hash: `#${heading.id}` })
)
)
),
})
),
[headings, minHeading]
);
const actions = useMemo(() => {
let childActions = headingActions;
if (!childActions.length) {
childActions = [
createAction({
name: (
<HeadingWrapper>
{t("Headings you add to the document will appear here")}
</HeadingWrapper>
),
section: ActiveDocumentSection,
disabled: true,
perform: () => {},
}),
];
}
return [
createActionGroup({
name: t("Contents"),
actions: childActions,
}),
];
}, [t, headingActions]);
const rootAction = useMenuAction(actions);
return (
<DropdownMenu action={rootAction} ariaLabel={t("Table of contents")}>
<Button
icon={<TableOfContentsIcon />}
aria-label={t("Table of contents")}
borderOnHover
neutral
/>
</DropdownMenu>
);
}
const HeadingWrapper = styled.div<{ $level?: number }>`
max-width: 100%;
white-space: normal;
overflow-wrap: anywhere;
margin-left: ${({ $level }) => `${12 * ($level ?? 0)}px`};
`;
export default observer(TableOfContentsMenu);