@@ -8,7 +8,6 @@ import { useActiveMatcher } from '@rspress/core/runtime';
88import ArrowRight from '@theme-assets/arrow-right' ;
99import clsx from 'clsx' ;
1010import type React from 'react' ;
11- import { useCallback , useRef } from 'react' ;
1211import { useNavigate } from '../Link/useNavigate' ;
1312import { SvgWrapper } from '../SvgWrapper' ;
1413import { SidebarDivider } from './SidebarDivider' ;
@@ -54,58 +53,10 @@ export function SidebarGroup(props: SidebarGroupProps) {
5453 const activeMatcher = useActiveMatcher ( ) ;
5554 const { item, depth, id, setSidebarData, className } = props ;
5655 const navigate = useNavigate ( ) ;
57- const initialState = useRef ( 'collapsed' in item && item . collapsed ) ;
58- const containerRef = useRef < HTMLDivElement > ( null ) ;
59- const containerHeightRef = useRef < number > ( item . items . length * 40 ) ;
60-
61- const transitionRef = useRef < number > ( null ) ;
6256 const active = item . link && activeMatcher ( item . link ) ;
6357 const { collapsed = false , collapsible = true } =
6458 item as NormalizedSidebarGroup ;
6559
66- const handleClickCollapsedTransition = useCallback (
67- ( collapsed : boolean /*target value */ ) => {
68- if ( transitionRef . current ) {
69- clearTimeout ( transitionRef . current ) ;
70- }
71-
72- const container = containerRef . current ;
73-
74- if ( ! container ) {
75- return ;
76- }
77-
78- if ( collapsed ) {
79- // fold
80- const contentHeight = container . clientHeight ;
81- containerHeightRef . current = contentHeight ;
82-
83- container . style . maxHeight = `${ contentHeight } px` ;
84- container . style . opacity = '0' ;
85- container . style . transition = 'all .2s ease-out' ;
86-
87- transitionRef . current = window . setTimeout ( ( ) => {
88- if ( containerRef . current ) {
89- containerRef . current . style . maxHeight = '0px' ;
90- }
91- } , 200 ) ;
92- } else {
93- // unfold
94- const contentHeight = containerHeightRef . current ;
95- container . style . transition = 'all .2s ease-in' ;
96- container . style . maxHeight = `${ contentHeight } px` ;
97- container . style . opacity = '1' ;
98-
99- transitionRef . current = window . setTimeout ( ( ) => {
100- if ( containerRef . current ) {
101- containerRef . current . style . removeProperty ( 'max-height' ) ;
102- }
103- } , 200 ) ;
104- }
105- } ,
106- [ ] ,
107- ) ;
108-
10960 const toggleCollapse = ( ) : void => {
11061 // update collapsed state
11162 setSidebarData ( sidebarData => {
@@ -132,13 +83,7 @@ export function SidebarGroup(props: SidebarGroupProps) {
13283 tag = { item . tag }
13384 text = { item . text }
13485 context = { item . context }
135- className = { clsx (
136- 'rp-sidebar-group' ,
137- {
138- 'rp-sidebar-group--top-level' : depth === 0 ,
139- } ,
140- className ,
141- ) }
86+ className = { clsx ( 'rp-sidebar-group' , className ) }
14287 depth = { depth }
14388 onClick = { e => {
14489 if ( ! active && item . link && ! collapsed ) {
@@ -149,55 +94,55 @@ export function SidebarGroup(props: SidebarGroupProps) {
14994 e . stopPropagation ( ) ;
15095 navigate ( item . link ) . then ( ( ) => {
15196 collapsible && toggleCollapse ( ) ;
152- collapsible && handleClickCollapsedTransition ( ! collapsed ) ;
15397 } ) ;
15498 return ;
15599 }
156100 e . stopPropagation ( ) ;
157101 collapsible && toggleCollapse ( ) ;
158- collapsible && handleClickCollapsedTransition ( ! collapsed ) ;
159102 } }
160103 right = { collapsible && < CollapsibleIcon collapsed = { collapsed } /> }
161104 />
162105
163106 < div
164- ref = { containerRef }
165107 style = { {
166- overflow : 'hidden' ,
167- maxHeight : initialState . current ? '0px' : undefined ,
168- opacity : initialState . current ? 0 : 1 ,
108+ // Expand/collapse by animating grid-template-rows from 0fr to 1fr.
109+ display : 'grid' ,
110+ gridTemplateRows : collapsed ? '0fr' : '1fr' ,
111+ transition : 'grid-template-rows 0.2s ease-out' ,
169112 } }
170113 >
171- { item . items ?. map ( ( item , index ) =>
172- isSidebarGroup ( item ) ? (
173- < SidebarGroup
174- id = { `${ id } -${ index } ` }
175- depth = { depth + 1 }
176- key = { `${ id } -${ index } ` }
177- item = { item }
178- setSidebarData = { setSidebarData }
179- className = "rp-sidebar-item--group-item"
180- />
181- ) : isSidebarDivider ( item ) ? (
182- < SidebarDivider
183- key = { index }
184- depth = { depth + 1 }
185- dividerType = { item . dividerType }
186- />
187- ) : isSidebarSectionHeader ( item ) ? (
188- < SidebarSectionHeader
189- sectionHeaderText = { item . sectionHeaderText }
190- key = { index }
191- />
192- ) : (
193- < SidebarItemComp
194- key = { index }
195- item = { item }
196- depth = { depth + 1 }
197- className = "rp-sidebar-item--group-item"
198- />
199- ) ,
200- ) }
114+ < div style = { { overflow : 'hidden' } } >
115+ { item . items ?. map ( ( item , index ) =>
116+ isSidebarGroup ( item ) ? (
117+ < SidebarGroup
118+ id = { `${ id } -${ index } ` }
119+ depth = { depth + 1 }
120+ key = { `${ id } -${ index } ` }
121+ item = { item }
122+ setSidebarData = { setSidebarData }
123+ className = "rp-sidebar-item--group-item"
124+ />
125+ ) : isSidebarDivider ( item ) ? (
126+ < SidebarDivider
127+ key = { index }
128+ depth = { depth + 1 }
129+ dividerType = { item . dividerType }
130+ />
131+ ) : isSidebarSectionHeader ( item ) ? (
132+ < SidebarSectionHeader
133+ sectionHeaderText = { item . sectionHeaderText }
134+ key = { index }
135+ />
136+ ) : (
137+ < SidebarItemComp
138+ key = { index }
139+ item = { item }
140+ depth = { depth + 1 }
141+ className = "rp-sidebar-item--group-item"
142+ />
143+ ) ,
144+ ) }
145+ </ div >
201146 </ div >
202147 </ >
203148 ) ;
0 commit comments