Skip to content

Commit

Permalink
fix: multi sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhyll committed May 13, 2024
1 parent 5f06916 commit 541b5b2
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 42 deletions.
89 changes: 47 additions & 42 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ export default defineConfig({
rss: `${site}/rss`,
},
components: {
SiteTitle: 'src/components/overrides/SiteTitle.astro',
Sidebar: "./src/components/overrides/Sidebar.astro",
Footer: 'src/components/overrides/Footer.astro',
Header: 'src/components/overrides/Header.astro',
ThemeSelect: 'src/components/overrides/ThemeSelect.astro',
},
head: [
Expand All @@ -107,54 +106,60 @@ export default defineConfig({
customCss: ['./src/styles/custom.css'],
sidebar: [
{
label: 'Home',
link: '/'
},
{
label: 'Quick Start',
label: 'Guides',
collapsed: true,
autogenerate: { directory: 'start' },
},
{
label: 'Core Concepts',
collapsed: true,
autogenerate: { directory: 'concepts' },
},
{
label: 'Security',
collapsed: true,
autogenerate: { directory: 'security' },
},
{
label: 'Develop',
collapsed: true,
autogenerate: { directory: 'develop' },
},
{
label: 'Test',
collapsed: true,
autogenerate: { directory: 'test' },
},
{
label: 'Distribute',
collapsed: true,
autogenerate: { directory: 'distribute' },
},
{
label: 'Learn',
collapsed: true,
autogenerate: { directory: 'learn' },
},
{
label: 'Features & Recipes',
collapsed: true,
autogenerate: { directory: 'features' },
},
{
label: 'References',
collapsed: true,
autogenerate: { directory: 'references' },
},
// {
// label: 'Quick Start',
// collapsed: true,
// autogenerate: { directory: 'start' },
// },
// {
// label: 'Core Concepts',
// collapsed: true,
// autogenerate: { directory: 'concepts' },
// },
// {
// label: 'Security',
// collapsed: true,
// autogenerate: { directory: 'security' },
// },
// {
// label: 'Develop',
// collapsed: true,
// autogenerate: { directory: 'develop' },
// },
// {
// label: 'Test',
// collapsed: true,
// autogenerate: { directory: 'test' },
// },
// {
// label: 'Distribute',
// collapsed: true,
// autogenerate: { directory: 'distribute' },
// },
// {
// label: 'Learn',
// collapsed: true,
// autogenerate: { directory: 'learn' },
// },
// {
// label: 'Features & Recipes',
// collapsed: true,
// autogenerate: { directory: 'features' },
// },
// {
// label: 'References',
// collapsed: true,
// autogenerate: { directory: 'references' },
// },
],
locales,
lastUpdated: true,
Expand Down
114 changes: 114 additions & 0 deletions src/components/overrides/Sidebar.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
import type { Props } from "@astrojs/starlight/props";
import Default from "@astrojs/starlight/components/Sidebar.astro";
import { AstroError } from "astro/errors";
// Styles and CSS logic derived from https://daisyui.com/components/collapse/
// This Sidebar override uses the top-level items from the Starlight sidebar config to create sidebars.
// Go through each top-level sidebar item from the Astro config to...
// 1. Validate the config is set up correctly
// 2. Create a new derived set of `Astro.props` that only contains one set of sidebar entries
// 3. Check if the current page being rendered is current page to determine if this sidebar group should be selected
const multiSidebarConfig: [string, boolean, Props][] = Astro.props.sidebar.map(
(entry) => {
if (entry.type !== "group") {
throw new AstroError(
`\`${entry.label}\` cannot be used with multiple Starlight sidebars.
Each top-level \`sidebar\` item in the Starlight config must be either a group or autogenerated.
See https://starlight.astro.build/guides/sidebar/#groups and https://starlight.astro.build/guides/sidebar/#autogenerated-groups`
);
}
// Recursively check if a group of sidebar entries contains the current page
const findIfIsCurrent = (
entry: (typeof Astro.props.sidebar)[number]
): boolean => {
if (entry.type === "link") {
return entry.isCurrent;
}
return entry.entries.some((item) => findIfIsCurrent(item));
};
const isCurrentPage = findIfIsCurrent(entry);
return [
entry.label,
isCurrentPage,
{ ...Astro.props, sidebar: [...entry.entries] },
];
}
);
---

<div class="__collapse">
{
multiSidebarConfig.map(([label, isCurrentPage, config]) => (
<>
<input
type="radio"
name="sidebar"
role="tab"
aria-label={label}
checked={isCurrentPage}
/>
<div class="__collapse-content">
<Default {...config}>
<slot />
</Default>
</div>
</>
))
}
</div>
<style>
.__collapse {
display: grid;
}
.__collapse > input {
/* Layout */
position: relative;
display: inline-flex;
grid-row-start: 1;
appearance: none;

/* Styles */
border-radius: 0.25rem;
padding: 0.2em 0.5rem;
line-height: 1.4;
font-size: var(--sl-text-lg);
font-weight: 600;
cursor: pointer;
user-select: none;
margin-bottom: var(--sl-nav-pad-y);
}

.__collapse > input::after {
content: attr(aria-label);
}

.__collapse > input:checked {
color: var(--sl-color-text-invert);
background-color: var(--sl-color-text-accent);
}

.__collapse > .__collapse-content {
display: none;
grid-column-start: 1;
grid-column-end: span 999;
grid-row-start: 2;
border-top: 1px solid var(--sl-color-gray-6);
padding-top: 1rem;
}
.__collapse > input:checked + .__collapse-content {
display: block;
}
</style>
<style is:global>
.top-level {
margin-bottom: 1rem;
}
</style>

0 comments on commit 541b5b2

Please sign in to comment.