Aller au contenu

TagSidebar

Ce contenu n’est pas encore disponible dans votre langue.

The TagSidebar component displays popular tags in a compact format for quick tag navigation. Despite its name, this component can be placed anywhere in your site, not just the sidebar.

import { TagSidebar } from 'starlight-tags/components';
PropTypeDefaultDescription
limitnumber10Maximum number of tags to display
sortBy'count' | 'priority' | 'alphabetical''count'Sort order for tags
showCountbooleantrueShow page count badge next to each tag
titlestringLocalized 'Popular Tags'Section heading (uses starlightTags.popularTags translation)
collapsiblebooleantrueEnable collapsible behavior
collapsedbooleanfalseStart with sidebar collapsed

Create a sidebar component override:

src/components/SidebarOverride.astro
---
import Default from '@astrojs/starlight/components/Sidebar.astro';
import { TagSidebar } from 'starlight-tags/components';
---
<Default {...Astro.props} />
<TagSidebar limit={8} sortBy="count" />

Then register the override in your astro.config.mjs:

astro.config.mjs
import starlight from '@astrojs/starlight';
export default defineConfig({
integrations: [
starlight({
title: 'My Docs',
components: {
Sidebar: './src/components/SidebarOverride.astro',
},
}),
],
});

Create a TOC component override:

src/components/TableOfContentsOverride.astro
---
import Default from '@astrojs/starlight/components/TableOfContents.astro';
import { TagSidebar } from 'starlight-tags/components';
---
<Default {...Astro.props} />
<TagSidebar title="Browse Tags" collapsible={false} />

Register the override:

astro.config.mjs
components: {
TableOfContents: './src/components/TableOfContentsOverride.astro',
}

Use the component directly in any MDX page:

---
title: My Page
---
import { TagSidebar } from 'starlight-tags/components';
Some content here...
<TagSidebar limit={5} showCount={false} />

Show only the top 5 tags:

<TagSidebar limit={5} />
<TagSidebar sortBy="alphabetical" title="Tags A-Z" />

Uses the priority field from your tag definitions:

<TagSidebar sortBy="priority" title="Featured Tags" />
<TagSidebar showCount={false} />
<TagSidebar title="Browse by Topic" />
<TagSidebar
limit={8}
sortBy="count"
showCount={true}
title="Top Topics"
/>

The component uses Starlight’s CSS variables for consistent theming:

  • --sl-color-bg-nav - Background color
  • --sl-color-gray-5 - Border color
  • --sl-color-gray-2 - Link text color
  • --sl-color-accent - Hover and link accent color
  • --sl-color-white - Title and count text color

The component automatically adapts to light and dark themes.

The TagSidebar component:

  1. Fetches all tags from middleware (Astro.locals.starlightTags)
  2. Sorts tags based on the sortBy prop (default: by page count, descending)
  3. Limits to the specified number of tags
  4. Renders a vertical list with optional count badges
  5. Includes a “View all tags” link to the full tag index

The component renders nothing if no tags are defined in your site.