Hide document TOC when empty

This commit is contained in:
Tom Moor
2024-07-07 20:15:26 -04:00
parent ddd103542a
commit 49198aafe9
3 changed files with 25 additions and 23 deletions

View File

@@ -5,7 +5,6 @@ import styled from "styled-components";
import breakpoint from "styled-components-breakpoint";
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
import { depths, s } from "@shared/styles";
import Text from "~/components/Text";
import useWindowScrollPosition from "~/hooks/useWindowScrollPosition";
const HEADING_OFFSET = 20;
@@ -54,26 +53,26 @@ export default function Contents({ headings }: Props) {
const headingAdjustment = minHeading - 1;
const { t } = useTranslation();
if (headings.length === 0) {
return null;
}
return (
<StickyWrapper>
<Heading>{t("Contents")}</Heading>
{headings.length ? (
<List>
{headings
.filter((heading) => heading.level < 4)
.map((heading) => (
<ListItem
key={heading.id}
level={heading.level - headingAdjustment}
active={activeSlug === heading.id}
>
<Link href={`#${heading.id}`}>{heading.title}</Link>
</ListItem>
))}
</List>
) : (
<Empty>{t("Headings you add to the document will appear here")}</Empty>
)}
<List>
{headings
.filter((heading) => heading.level < 4)
.map((heading) => (
<ListItem
key={heading.id}
level={heading.level - headingAdjustment}
active={activeSlug === heading.id}
>
<Link href={`#${heading.id}`}>{heading.title}</Link>
</ListItem>
))}
</List>
</StickyWrapper>
);
}
@@ -112,10 +111,6 @@ const Heading = styled.h3`
margin-top: 10px;
`;
const Empty = styled(Text)`
font-size: 14px;
`;
const ListItem = styled.li<{ level: number; active?: boolean }>`
margin-left: ${(props) => (props.level - 1) * 10}px;
margin-bottom: 8px;

View File

@@ -121,7 +121,13 @@ function DocumentHeader({
const canToggleEmbeds = team?.documentEmbeds;
const toc = (
<Tooltip
content={ui.tocVisible ? t("Hide contents") : t("Show contents")}
content={
ui.tocVisible
? t("Hide contents")
: documentHasHeadings
? t("Show contents")
: `${t("Show contents")} (${t("available when headings are added")})`
}
shortcut="ctrl+alt+h"
delay={250}
placement="bottom"

View File

@@ -570,6 +570,7 @@
"Type '/' to insert, or start writing…": "Type '/' to insert, or start writing…",
"Hide contents": "Hide contents",
"Show contents": "Show contents",
"available when headings are added": "available when headings are added",
"Edit {{noun}}": "Edit {{noun}}",
"Switch to dark": "Switch to dark",
"Switch to light": "Switch to light",