Directory
- Welcome
Directory Component
The Directory component creates structured, hierarchical navigation with collapsible sections. Perfect for documentation sites, file explorers, and nested navigation menus. Inspired by the Astro Starlight navigation component.
Key Features
-
Hierarchical Display: Visualize complex content structures
-
Collapsible Sections: Toggle visibility of nested items
-
Active Item Tracking: Automatically highlights current selection
-
Smooth Scrolling: Auto-scrolls to active items
-
Badge Support: Add visual indicators to items
Basic Usage
<Directory directory={[{ label: 'Getting Started', items: [ { label: 'Installation', href: '/docs/installation' }, { label: 'Configuration', href: '/docs/configuration' }, ], }]} currentDocSlug="installation" collapsable={true}/>
Depth Control
Limit the nesting depth of your directory structure:
<Directory directory={directoryData} maxDepth={3} Only show 3 levels of nesting />
Utilizing Content Collections
Below is an example of how to utilize the Directory component with the Astro content collections. You can see the example on the left side of the screen now, we use this component for our documentation navigation.
Layout File
---import { type CollectionEntry } from 'astro:content';import Directory from '@/components/NST-components/UI/directory/Directory.astro';import { getCollection } from 'astro:content';
export interface Props { post?: CollectionEntry<'docs'>;}
const { post } = Astro.props;const { slug } = post || {};const directory = await getCollection('docs');---
<Directory collapsable currentDocSlug={slug || ''} baseUrl="/docs/" {directory}/>
Content Collections Config
import { badgeStyleKeys } from '@/components/NST-components/UI/badge/styling';import { defineCollection, z } from 'astro:content';
const docs = defineCollection({ type: 'content', schema: () => z.object({ sidebar: z .object({ label: z.string(), dropDownLabel: z.string().optional(), order: z.number().optional(), badge: z .object({ text: z.string(), variant: z.enum(badgeStyleKeys), }) .optional(), }) .optional(), }),});
export const collections = { docs };
Base URL
Set the base URL to the root of your content collection. This is used to generate the correct URLs for the directory items. If all your content collection starts at the path /docs/
, you can set the base URL to /docs/
.
Directory Component Props API
Prop | Type | Default | Description |
---|---|---|---|
baseUrl | string | Base URL for the directory | |
collapsable | boolean | false | Enable collapsible sections |
currentDocSlug | string | Slug of the current active document | |
defaultCollapsed | boolean | true | Initial collapsed state of sections |
directory | DirectoryItem[] | Array of directory items and their structure | |
maxDepth | number | 9 | Maximum nesting depth to display |
DirectoryItem Type
interface DirectoryItem { label: string; href?: string; items?: any[]; badge?: { variant: BadgeStyleKeys; text: string; }; dropDownLabel?: string; dropDownBadge?: { variant: BadgeStyleKeys; text: string; }; collapsed?: boolean;}
Best Practices
-
Structure Organization
- Keep nesting depth reasonable (3-4 levels max)
- Use clear, descriptive labels
- Group related items logically
- Consider alphabetical ordering
-
Visual Clarity
- Use badges sparingly
- Maintain consistent spacing
- Ensure sufficient contrast
- Consider mobile viewports
Common Use Cases
Limited Time Launch Sale
Build intuitive navigation structures with our Directory component. Get the AstroJS starter template now and create organized, accessible content hierarchies.
GET 60% OFF!