11'use client' ;
22
3- import { ChevronRightIcon , FlaskConicalIcon } from 'lucide-react' ;
3+ import { FlaskConicalIcon } from 'lucide-react' ;
44import Link from 'next/link' ;
55import { usePathname } from 'next/navigation' ;
66import * as React from 'react' ;
77
8- import { StatusDot } from '@/components/StatusDot' ;
9- import { Collapsible , CollapsibleContent , CollapsibleTrigger } from '@/components/ui/collapsible' ;
8+ import { type NavigationGroup , SidebarNavigation } from '@/components/sidebar-navigation' ;
109import {
1110 Sidebar ,
1211 SidebarContent ,
1312 SidebarFooter ,
1413 SidebarGroup ,
1514 SidebarGroupContent ,
16- SidebarGroupLabel ,
1715 SidebarHeader ,
1816 SidebarMenu ,
1917 SidebarMenuButton ,
2018 SidebarMenuItem ,
2119 SidebarRail ,
2220 SidebarSeparator ,
2321} from '@/components/ui/sidebar' ;
24- import { Tooltip , TooltipContent , TooltipTrigger } from '@/components/ui/tooltip' ;
2522import { getSidebarGroups } from '@/lib/registry' ;
26- import type { StoryStatus , WipSubstatus } from '@/lib/types ' ;
23+ import { getSidebarCategories } from '@/lib/sidebar-navigation ' ;
2724
2825const groups = getSidebarGroups ( ) ;
2926
30- const COLLAPSED_BY_DEFAULT = new Set ( [ 'Blocks' , 'Primitives' , 'Components' , 'Styles' , 'Hooks' , 'Localization' ] ) ;
31-
32- type SidebarEntry = ReturnType < typeof getSidebarGroups > [ number ] [ 'components' ] [ number ] ;
33-
34- // Partitions a group's entries by `meta.navigation.category` into subheaded runs. Category and
35- // entry order both follow first appearance in the registry; uncategorized entries get no subheading.
36- function byCategory ( components : SidebarEntry [ ] ) {
37- const categories : { category : string ; components : SidebarEntry [ ] } [ ] = [ ] ;
38- for ( const component of components ) {
39- const category = component . mod . meta . navigation ?. category ?? '' ;
40- const bucket = categories . find ( c => c . category === category ) ;
41- if ( bucket ) {
42- bucket . components . push ( component ) ;
43- } else {
44- categories . push ( { category, components : [ component ] } ) ;
45- }
46- }
47- return categories ;
48- }
49-
50- function SidebarUsageItem ( {
51- usage,
52- href,
53- isActive,
54- status,
55- substatus,
56- } : {
57- usage : string ;
58- href : string ;
59- isActive : boolean ;
60- status ?: StoryStatus ;
61- substatus ?: WipSubstatus ;
62- } ) {
63- const labelRef = React . useRef < HTMLSpanElement > ( null ) ;
64- const [ isTruncated , setIsTruncated ] = React . useState ( false ) ;
65-
66- React . useEffect ( ( ) => {
67- const label = labelRef . current ;
68- if ( ! label ) {
69- return ;
70- }
71- const check = ( ) => setIsTruncated ( label . scrollWidth > label . clientWidth ) ;
72- check ( ) ;
73- const observer = new ResizeObserver ( check ) ;
74- observer . observe ( label ) ;
75- return ( ) => observer . disconnect ( ) ;
76- } , [ ] ) ;
77-
78- return (
79- < SidebarMenuItem >
80- < Tooltip disabled = { ! isTruncated } >
81- < TooltipTrigger
82- delay = { 300 }
83- render = {
84- < SidebarMenuButton
85- className = 'h-auto py-1 text-xs'
86- isActive = { isActive }
87- render = { < Link href = { href } /> }
88- >
89- { status ? (
90- < StatusDot
91- status = { status }
92- substatus = { substatus }
93- />
94- ) : null }
95- < span
96- ref = { labelRef }
97- className = 'truncate font-mono text-[10px] leading-relaxed'
98- >
99- { usage }
100- </ span >
101- </ SidebarMenuButton >
102- }
103- />
104- < TooltipContent
105- side = 'right'
106- className = 'font-mono text-[10px]'
107- >
108- { usage }
109- </ TooltipContent >
110- </ Tooltip >
111- </ SidebarMenuItem >
112- ) ;
113- }
114-
115- function SidebarEntryMenu ( {
116- components,
117- groupSlug,
118- pathname,
119- } : {
120- components : SidebarEntry [ ] ;
121- groupSlug : string ;
122- pathname : string ;
123- } ) {
124- return (
125- < SidebarMenu >
126- { components . map ( ( { mod, componentSlug } ) => {
27+ export function AppSidebar ( { ...props } : React . ComponentProps < typeof Sidebar > ) {
28+ const pathname = usePathname ( ) ;
29+ const navigationGroups : NavigationGroup [ ] = groups . map ( ( { group, groupSlug, components } ) => ( {
30+ label : group ,
31+ separatorBefore : group === 'Blocks' ,
32+ categories : getSidebarCategories ( components ) . map ( ( { category, components } ) => ( {
33+ label : category ,
34+ items : components . map ( ( { mod, componentSlug } ) => {
12735 const href = `/${ groupSlug } /${ componentSlug } ` ;
12836 // How an entry is USED differs by layer, so the label follows the layer rather
12937 // than a guess at the title: hooks are called, atomic styles are a set of
@@ -135,23 +43,16 @@ function SidebarEntryMenu({
13543 : mod . meta . group === 'Styles' || mod . meta . group === 'Localization'
13644 ? mod . meta . title
13745 : `<${ mod . meta . title } />` ;
138- return (
139- < SidebarUsageItem
140- key = { mod . meta . title }
141- usage = { usage }
142- href = { href }
143- isActive = { pathname === href }
144- status = { mod . meta . status }
145- substatus = { mod . meta . substatus }
146- />
147- ) ;
148- } ) }
149- </ SidebarMenu >
150- ) ;
151- }
152-
153- export function AppSidebar ( { ...props } : React . ComponentProps < typeof Sidebar > ) {
154- const pathname = usePathname ( ) ;
46+ return {
47+ href,
48+ usage,
49+ isActive : pathname === href ,
50+ status : mod . meta . status ,
51+ substatus : mod . meta . substatus ,
52+ } ;
53+ } ) ,
54+ } ) ) ,
55+ } ) ) ;
15556
15657 return (
15758 < Sidebar { ...props } >
@@ -188,71 +89,7 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
18889 < span className = 'text-sidebar-foreground/70 text-[10px] font-medium' > Mosaic - Swingset</ span >
18990 </ SidebarHeader >
19091 < SidebarContent className = 'gap-0' >
191- { groups . map ( ( { group, groupSlug, components } ) => (
192- < React . Fragment key = { group } >
193- { group === 'Blocks' && < SidebarSeparator className = 'data-horizontal:w-auto my-1' /> }
194- < Collapsible
195- defaultOpen = { ! COLLAPSED_BY_DEFAULT . has ( group ) }
196- className = 'group/collapsible'
197- >
198- < SidebarGroup
199- className = 'py-1'
200- data-section = { group }
201- >
202- < SidebarGroupLabel
203- className = 'text-sidebar-foreground/50 hover:text-sidebar-foreground/80 h-auto w-full px-2 pb-1 pt-3 text-[10px] font-semibold uppercase tracking-wider'
204- render = { < CollapsibleTrigger /> }
205- >
206- { group }
207- < ChevronRightIcon className = 'size-3! ml-auto transition-transform group-data-[open]/collapsible:rotate-90' />
208- </ SidebarGroupLabel >
209- < CollapsibleContent >
210- < SidebarGroupContent >
211- { byCategory ( components ) . map ( ( { category, components } ) =>
212- category ? (
213- < Collapsible
214- key = { category }
215- // Collapsed by default, unless it holds the page being viewed.
216- defaultOpen = { components . some (
217- ( { componentSlug } ) => pathname === `/${ groupSlug } /${ componentSlug } ` ,
218- ) }
219- className = 'group/category'
220- >
221- < CollapsibleTrigger className = 'text-sidebar-foreground/40 hover:text-sidebar-foreground/70 flex w-full items-center gap-1 px-2 pb-0.5 pt-2 text-[9px] font-semibold uppercase tracking-wider' >
222- < span
223- aria-hidden = 'true'
224- className = 'font-mono text-[10px] leading-none'
225- >
226- └
227- </ span >
228- { category }
229- < ChevronRightIcon className = 'size-2.5! ml-auto transition-transform group-data-[open]/category:rotate-90' />
230- </ CollapsibleTrigger >
231- < CollapsibleContent >
232- < div className = 'border-sidebar-border ml-3 border-l pl-1' >
233- < SidebarEntryMenu
234- components = { components }
235- groupSlug = { groupSlug }
236- pathname = { pathname }
237- />
238- </ div >
239- </ CollapsibleContent >
240- </ Collapsible >
241- ) : (
242- < SidebarEntryMenu
243- key = { group }
244- components = { components }
245- groupSlug = { groupSlug }
246- pathname = { pathname }
247- />
248- ) ,
249- ) }
250- </ SidebarGroupContent >
251- </ CollapsibleContent >
252- </ SidebarGroup >
253- </ Collapsible >
254- </ React . Fragment >
255- ) ) }
92+ < SidebarNavigation groups = { navigationGroups } />
25693 </ SidebarContent >
25794 < SidebarFooter className = 'gap-0 p-0' >
25895 < SidebarSeparator className = 'data-horizontal:w-auto my-1' />
0 commit comments