@@ -295,11 +295,29 @@ function scrollToElement(selector) {
295295function initializeFloatingToc ( ) {
296296 const tocMenu = document . getElementById ( "toc-menu" ) ;
297297 const tocClose = document . getElementById ( "toc-close" ) ;
298+ const tocToggle = document . getElementById ( "toc-toggle" ) ;
298299
299- if ( ! tocMenu || ! tocClose ) {
300+ if ( ! tocMenu || ! tocClose || ! tocToggle ) {
300301 return ;
301302 }
302303
304+ // Check if we're on mobile
305+ const isMobile = window . innerWidth <= 768 ;
306+
307+ // Hide menu by default on mobile
308+ if ( isMobile ) {
309+ tocMenu . classList . add ( "hidden" ) ;
310+ }
311+
312+ // Set initial visibility state
313+ updateTocVisibility ( ) ;
314+
315+ // Toggle button click
316+ tocToggle . addEventListener ( "click" , function ( e ) {
317+ e . stopPropagation ( ) ;
318+ toggleTocMenu ( ) ;
319+ } ) ;
320+
303321 // Close TOC menu
304322 tocClose . addEventListener ( "click" , function ( e ) {
305323 e . stopPropagation ( ) ;
@@ -311,20 +329,67 @@ function initializeFloatingToc() {
311329 e . stopPropagation ( ) ;
312330 } ) ;
313331
332+ // Handle window resize to update mobile state
333+ window . addEventListener ( "resize" , function ( ) {
334+ const isMobileNow = window . innerWidth <= 768 ;
335+ if ( isMobileNow && ! tocMenu . classList . contains ( "hidden" ) ) {
336+ // On mobile, ensure proper visibility class
337+ updateTocVisibility ( ) ;
338+ }
339+ } ) ;
340+
314341 // TOC keyboard shortcuts are now handled in the main keyboard handler
315342}
316343
317344function toggleTocMenu ( ) {
318345 const tocMenu = document . getElementById ( "toc-menu" ) ;
319- if ( tocMenu ) {
320- tocMenu . classList . toggle ( "hidden" ) ;
346+ const tocToggle = document . getElementById ( "toc-toggle" ) ;
347+
348+ if ( tocMenu && tocToggle ) {
349+ const isMobile = window . innerWidth <= 768 ;
350+
351+ if ( isMobile ) {
352+ // On mobile, use 'visible' class to override default hidden state
353+ tocMenu . classList . toggle ( "visible" ) ;
354+ tocMenu . classList . toggle ( "hidden" ) ;
355+ } else {
356+ // On desktop, use 'hidden' class
357+ tocMenu . classList . toggle ( "hidden" ) ;
358+ }
359+
360+ updateTocVisibility ( ) ;
321361 }
322362}
323363
324364function closeTocMenu ( ) {
325365 const tocMenu = document . getElementById ( "toc-menu" ) ;
326- if ( tocMenu ) {
366+ const tocToggle = document . getElementById ( "toc-toggle" ) ;
367+
368+ if ( tocMenu && tocToggle ) {
369+ const isMobile = window . innerWidth <= 768 ;
370+
327371 tocMenu . classList . add ( "hidden" ) ;
372+ if ( isMobile ) {
373+ tocMenu . classList . remove ( "visible" ) ;
374+ }
375+
376+ updateTocVisibility ( ) ;
377+ }
378+ }
379+
380+ function updateTocVisibility ( ) {
381+ const tocMenu = document . getElementById ( "toc-menu" ) ;
382+ const tocToggle = document . getElementById ( "toc-toggle" ) ;
383+
384+ if ( ! tocMenu || ! tocToggle ) return ;
385+
386+ const isMenuHidden = tocMenu . classList . contains ( "hidden" ) ;
387+
388+ // Show toggle button when menu is hidden, hide it when menu is shown
389+ if ( isMenuHidden ) {
390+ tocToggle . classList . remove ( "hidden" ) ;
391+ } else {
392+ tocToggle . classList . add ( "hidden" ) ;
328393 }
329394}
330395
0 commit comments