Ir al contenido

TagSidebar

Esta página aún no está disponible en tu idioma.

The TagSidebar component displays popular tags in a compact sidebar format. It’s designed to be added to Starlight’s sidebar for quick tag navigation.

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
titlestring'Popular Tags'Section heading
collapsiblebooleantrueEnable collapsible behavior
collapsedbooleanfalseStart with sidebar collapsed

To add the TagSidebar to your Starlight site, 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" title="Popular Tags" />

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',
},
}),
],
});

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.