@@ -1337,6 +1337,13 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
13371337 // it gets the sidebar to restore its size.
13381338 let desiredSidebarSize = null ;
13391339
1340+ // Sidebar resize debouncer.
1341+ //
1342+ // The sidebar itself is resized instantly, but the body HTML can be too
1343+ // big for that, causing reflow jank. To reduce this, we queue up a separate
1344+ // animation frame and throttle it.
1345+ let pendingSidebarResizingFrame = false ;
1346+
13401347 // If this page has no sidebar at all, bail out.
13411348 const resizer = document . querySelector ( ".sidebar-resizer" ) ;
13421349 const sidebar = document . querySelector ( ".sidebar" ) ;
@@ -1360,12 +1367,26 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
13601367 if ( isSrcPage ) {
13611368 window . rustdocCloseSourceSidebar ( ) ;
13621369 updateLocalStorage ( "src-sidebar-width" , null ) ;
1370+ // [RUSTDOCIMPL] CSS variable fast path
1371+ //
1372+ // The sidebar width variable is attached to the <html> element by
1373+ // storage.js, because the sidebar and resizer don't exist yet.
1374+ // But the resize code, in `resize()`, sets the property on the
1375+ // sidebar and resizer elements (which are the only elements that
1376+ // use the variable) to avoid recalculating CSS on the entire
1377+ // document on every frame.
1378+ //
1379+ // So, to clear it, we need to clear all three.
13631380 document . documentElement . style . removeProperty ( "--src-sidebar-width" ) ;
1381+ sidebar . style . removeProperty ( "--src-sidebar-width" ) ;
1382+ resizer . style . removeProperty ( "--src-sidebar-width" ) ;
13641383 } else {
13651384 addClass ( document . documentElement , "hide-sidebar" ) ;
13661385 updateLocalStorage ( "hide-sidebar" , "true" ) ;
13671386 updateLocalStorage ( "desktop-sidebar-width" , null ) ;
13681387 document . documentElement . style . removeProperty ( "--desktop-sidebar-width" ) ;
1388+ sidebar . style . removeProperty ( "--desktop-sidebar-width" ) ;
1389+ resizer . style . removeProperty ( "--desktop-sidebar-width" ) ;
13691390 }
13701391 }
13711392
@@ -1384,15 +1405,27 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
13841405 }
13851406 }
13861407
1387- // Call this to set the correct CSS variable and setting.
1388- // This function doesn't enforce size constraints. Do that before calling it!
1408+ /**
1409+ * Call this to set the correct CSS variable and setting.
1410+ * This function doesn't enforce size constraints. Do that before calling it!
1411+ *
1412+ * @param {number } size - CSS px width of the sidebar.
1413+ */
13891414 function changeSidebarSize ( size ) {
13901415 if ( isSrcPage ) {
13911416 updateLocalStorage ( "src-sidebar-width" , size ) ;
1392- document . documentElement . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
1417+ // [RUSTDOCIMPL] CSS variable fast path
1418+ //
1419+ // While this property is set on the HTML element at load time,
1420+ // because the sidebar isn't actually loaded yet,
1421+ // we scope this update to the sidebar to avoid hitting a slow
1422+ // path in WebKit.
1423+ sidebar . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
1424+ resizer . style . setProperty ( "--src-sidebar-width" , size + "px" ) ;
13931425 } else {
13941426 updateLocalStorage ( "desktop-sidebar-width" , size ) ;
1395- document . documentElement . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
1427+ sidebar . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
1428+ resizer . style . setProperty ( "--desktop-sidebar-width" , size + "px" ) ;
13961429 }
13971430 }
13981431
@@ -1424,6 +1457,19 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
14241457 const constrainedPos = Math . min ( pos , window . innerWidth - BODY_MIN , SIDEBAR_MAX ) ;
14251458 changeSidebarSize ( constrainedPos ) ;
14261459 desiredSidebarSize = constrainedPos ;
1460+ if ( pendingSidebarResizingFrame !== false ) {
1461+ clearTimeout ( pendingSidebarResizingFrame ) ;
1462+ }
1463+ pendingSidebarResizingFrame = setTimeout ( ( ) => {
1464+ if ( currentPointerId === null || pendingSidebarResizingFrame === false ) {
1465+ return ;
1466+ }
1467+ pendingSidebarResizingFrame = false ;
1468+ document . documentElement . style . setProperty (
1469+ "--resizing-sidebar-width" ,
1470+ desiredSidebarSize + "px"
1471+ ) ;
1472+ } , 100 ) ;
14271473 }
14281474 }
14291475 // Respond to the window resize event.
@@ -1447,6 +1493,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
14471493 window . removeEventListener ( "pointermove" , resize , false ) ;
14481494 window . removeEventListener ( "pointerup" , stopResize , false ) ;
14491495 removeClass ( document . documentElement , "sidebar-resizing" ) ;
1496+ document . documentElement . style . removeProperty ( "--resizing-sidebar-width" ) ;
14501497 if ( resizer . releasePointerCapture ) {
14511498 resizer . releasePointerCapture ( currentPointerId ) ;
14521499 currentPointerId = null ;
@@ -1472,6 +1519,8 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
14721519 window . addEventListener ( "pointerup" , stopResize , false ) ;
14731520 addClass ( resizer , "active" ) ;
14741521 addClass ( document . documentElement , "sidebar-resizing" ) ;
1522+ const pos = e . clientX - sidebar . offsetLeft - 3 ;
1523+ document . documentElement . style . setProperty ( "--resizing-sidebar-width" , pos + "px" ) ;
14751524 desiredSidebarSize = null ;
14761525 }
14771526 resizer . addEventListener ( "pointerdown" , initResize , false ) ;
0 commit comments