@@ -90,7 +90,7 @@ export function CommandPalette(props: CommandPaletteProps) {
9090
9191 const actions = useMemo < CMDKFlatItem [ ] > ( ( ) => {
9292 if ( ! state . query ) {
93- return flattenActions ( currentNodes , null ) ;
93+ return flattenActions ( currentNodes , null , ! state . action ) ;
9494 }
9595
9696 const scores = new Map <
@@ -99,7 +99,7 @@ export function CommandPalette(props: CommandPaletteProps) {
9999 > ( ) ;
100100 scoreTree ( currentNodes , scores , state . query . toLowerCase ( ) ) ;
101101 return flattenActions ( currentNodes , scores ) ;
102- } , [ currentNodes , state . query ] ) ;
102+ } , [ currentNodes , state . query , state . action ] ) ;
103103
104104 const analytics = useCommandPaletteAnalytics ( actions . length ) ;
105105
@@ -459,12 +459,21 @@ function scoreTree(
459459 }
460460}
461461
462+ // Maximum number of children to show per group, in both browse and search mode.
463+ // Prevents groups with many items (e.g. per-project settings on large orgs)
464+ // from flooding the results list.
465+ const MAX_GROUP_CHILDREN = 4 ;
466+
462467function flattenActions (
463468 nodes : Array < CollectionTreeNode < CMDKActionData > > ,
464469 scores : Map <
465470 string ,
466471 { node : CollectionTreeNode < CMDKActionData > ; score : { matched : boolean ; score : number } }
467- > | null
472+ > | null ,
473+ // Only expand groups inline at the true root level. When the user has
474+ // navigated into a group, nested groups should appear as navigable actions
475+ // rather than being pre-expanded as section headers with their children.
476+ expandGroups = true
468477) : CMDKFlatItem [ ] {
469478 // Browse mode: show each top-level node and its direct children.
470479 if ( ! scores ) {
@@ -476,11 +485,19 @@ function flattenActions(
476485 if ( ! isGroup && ! ( 'to' in node ) && ! ( 'onAction' in node ) ) {
477486 continue ;
478487 }
479- results . push ( { ...node , listItemType : isGroup ? 'section' : 'action' } ) ;
480- if ( isGroup ) {
481- for ( const child of node . children ) {
488+ if ( isGroup && expandGroups ) {
489+ // Expand the group inline: render it as a section header followed by
490+ // its direct children. This only happens at the true root level so
491+ // that nested groups (e.g. "Set Priority" inside "Select All") are
492+ // shown as navigable actions rather than pre-expanded sections.
493+ results . push ( { ...node , listItemType : 'section' } ) ;
494+ for ( const child of node . children . slice ( 0 , MAX_GROUP_CHILDREN ) ) {
482495 results . push ( { ...child , listItemType : 'action' } ) ;
483496 }
497+ } else {
498+ // Leaf action or a group that should not be expanded inline — treat
499+ // as a single navigable item regardless.
500+ results . push ( { ...node , listItemType : 'action' } ) ;
484501 }
485502 }
486503 return results ;
@@ -526,6 +543,7 @@ function flattenActions(
526543 ( scores . get ( b . key ) ?. score . score ?? 0 ) -
527544 ( scores . get ( a . key ) ?. score . score ?? 0 )
528545 )
546+ . slice ( 0 , MAX_GROUP_CHILDREN )
529547 . map ( c => ( { ...c , listItemType : 'action' as const } ) ) ,
530548 ] ;
531549 }
0 commit comments