From 17b80c6c9e73081e56653b4d7e2d65a44f4db172 Mon Sep 17 00:00:00 2001 From: Charles Teague Date: Thu, 2 Nov 2023 09:41:41 -0400 Subject: [PATCH] Only apply sidebar behaviors upon width changes --- .../formats/dashboard/quarto-dashboard.js | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/resources/formats/dashboard/quarto-dashboard.js b/src/resources/formats/dashboard/quarto-dashboard.js index 183b67a2d3..a94c7f3381 100644 --- a/src/resources/formats/dashboard/quarto-dashboard.js +++ b/src/resources/formats/dashboard/quarto-dashboard.js @@ -69,20 +69,25 @@ window.document.addEventListener("DOMContentLoaded", function (_event) { const sidebar = window.document.querySelector( ".quarto-dashboard-content .bslib-sidebar-layout" ); + let prevWidth = window.document.body.clientWidth; const sidebarCollapseClass = "sidebar-collapsed"; if (sidebar) { const resizeObserver = new ResizeObserver( - throttle(function () { - if (window.document.body.clientWidth <= 576) { - // Hide the sidebar - if (!sidebar.classList.contains(sidebarCollapseClass)) { - sidebar.classList.add(sidebarCollapseClass); - } - } else { - // Show the sidebar - if (sidebar.classList.contains(sidebarCollapseClass)) { - sidebar.classList.remove(sidebarCollapseClass); + throttle(function (e) { + const clientWidth = window.document.body.clientWidth; + if (prevWidth !== clientWidth) { + if (clientWidth <= 576) { + // Hide the sidebar + if (!sidebar.classList.contains(sidebarCollapseClass)) { + sidebar.classList.add(sidebarCollapseClass); + } + } else { + // Show the sidebar + if (sidebar.classList.contains(sidebarCollapseClass)) { + sidebar.classList.remove(sidebarCollapseClass); + } } + prevWidth = clientWidth; } }, 5) );