From 62265e4a653d161483e3844b568ab150334e9238 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 11 May 2023 16:09:22 +0200
Subject: Add mobile table of contents component (#36)
---
.changeset/tasty-balloons-hang.md | 5 +
packages/starlight/components/Header.astro | 2 +-
packages/starlight/components/RightSidebar.astro | 29 +++++
.../TableOfContents/MobileTableOfContents.astro | 125 +++++++++++++++++++++
.../TableOfContents/TableOfContentsList.astro | 37 ++++--
packages/starlight/index.astro | 23 +---
packages/starlight/layout/PageFrame.astro | 3 +-
packages/starlight/layout/TwoColumnContent.astro | 2 +-
packages/starlight/style/props.css | 8 +-
packages/starlight/style/util.css | 11 ++
10 files changed, 214 insertions(+), 31 deletions(-)
create mode 100644 .changeset/tasty-balloons-hang.md
create mode 100644 packages/starlight/components/RightSidebar.astro
create mode 100644 packages/starlight/components/TableOfContents/MobileTableOfContents.astro
diff --git a/.changeset/tasty-balloons-hang.md b/.changeset/tasty-balloons-hang.md
new file mode 100644
index 00000000..212cc72f
--- /dev/null
+++ b/.changeset/tasty-balloons-hang.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/starlight": patch
+---
+
+Collapse table of contents in dropdown on narrower viewports
diff --git a/packages/starlight/components/Header.astro b/packages/starlight/components/Header.astro
index 7547c30e..235ade3a 100644
--- a/packages/starlight/components/Header.astro
+++ b/packages/starlight/components/Header.astro
@@ -30,7 +30,7 @@ interface Props {
}
.site-title {
- font-size: var(--sl-text-2xl);
+ font-size: var(--sl-text-h4);
font-weight: 600;
color: var(--sl-color-text-accent);
text-decoration: none;
diff --git a/packages/starlight/components/RightSidebar.astro b/packages/starlight/components/RightSidebar.astro
new file mode 100644
index 00000000..b7a30a6b
--- /dev/null
+++ b/packages/starlight/components/RightSidebar.astro
@@ -0,0 +1,29 @@
+---
+import type { MarkdownHeading } from 'astro';
+import type { CollectionEntry } from 'astro:content';
+import config from 'virtual:starlight/user-config';
+import EditLink from './EditLink.astro';
+import RightSidebarPanel from './RightSidebarPanel.astro';
+import TableOfContents from './TableOfContents.astro';
+
+interface Props {
+ entry: CollectionEntry<'docs'>;
+ headings: MarkdownHeading[];
+}
+
+const { entry, headings } = Astro.props;
+---
+
+
+
+
+
+ {
+ config.editLink.baseUrl && (
+ <>
+ Contribute
+
+ >
+ )
+ }
+
diff --git a/packages/starlight/components/TableOfContents/MobileTableOfContents.astro b/packages/starlight/components/TableOfContents/MobileTableOfContents.astro
new file mode 100644
index 00000000..494b1d00
--- /dev/null
+++ b/packages/starlight/components/TableOfContents/MobileTableOfContents.astro
@@ -0,0 +1,125 @@
+---
+import type { MarkdownHeading } from 'astro';
+import config from 'virtual:starlight/user-config';
+import { generateToC } from './generateToC';
+import TableOfContentsList from './TableOfContentsList.astro';
+import Icon from '../Icon.astro';
+
+interface Props {
+ headings: MarkdownHeading[];
+}
+
+const toc = generateToC(Astro.props.headings, config.tableOfContents);
+---
+
+
+
+
+
+
diff --git a/packages/starlight/components/TableOfContents/TableOfContentsList.astro b/packages/starlight/components/TableOfContents/TableOfContentsList.astro
index d05b60bd..f19c91bd 100644
--- a/packages/starlight/components/TableOfContents/TableOfContentsList.astro
+++ b/packages/starlight/components/TableOfContents/TableOfContentsList.astro
@@ -3,33 +3,54 @@ import type { generateToC } from './generateToC';
interface Props {
toc: ReturnType;
- nested?: boolean;
+ depth?: number;
+ isMobile?: boolean;
}
-const { toc, nested } = Astro.props;
+const { toc, isMobile = false, depth = 0 } = Astro.props;
---
-
+
{
toc.map((heading) => (
-
{heading.text}
{heading.children.length > 0 && (
-
+
)}
))
}
-
diff --git a/packages/starlight/index.astro b/packages/starlight/index.astro
index 238ce8e6..d0c79d34 100644
--- a/packages/starlight/index.astro
+++ b/packages/starlight/index.astro
@@ -1,6 +1,5 @@
---
import type { InferGetStaticPropsType } from 'astro';
-import config from 'virtual:starlight/user-config';
import { getPrevNextLinks, getSidebar } from './utils/navigation';
import { paths } from './utils/routing';
@@ -13,17 +12,16 @@ import './style/util.css';
// Components — can override built-in CSS, but not user CSS.
import ContentPanel from './components/ContentPanel.astro';
-import EditLink from './components/EditLink.astro';
import FallbackContentNotice from './components/FallbackContentNotice.astro';
import HeadSEO from './components/HeadSEO.astro';
import Header from './components/Header.astro';
import LastUpdated from './components/LastUpdated.astro';
import MarkdownContent from './components/MarkdownContent.astro';
import PrevNextLinks from './components/PrevNextLinks.astro';
-import RightSidebarPanel from './components/RightSidebarPanel.astro';
+import RightSidebar from './components/RightSidebar.astro';
import Sidebar from './components/Sidebar.astro';
import SkipLink from './components/SkipLink.astro';
-import TableOfContents from './components/TableOfContents.astro';
+import MobileTableOfContents from './components/TableOfContents/MobileTableOfContents.astro';
import ThemeProvider from './components/ThemeProvider.astro';
import PageFrame from './layout/PageFrame.astro';
import TwoColumnContent from './layout/TwoColumnContent.astro';
@@ -58,22 +56,9 @@ const prevNextLinks = getPrevNextLinks(sidebar);
+
-
-
-
-
-
- {
- config.editLink.baseUrl && (
- <>
- Contribute
-
- >
- )
- }
-
-
+
-