diff --git a/src/bundle/Resources/public/js/scripts/admin.content.tree.js b/src/bundle/Resources/public/js/scripts/admin.content.tree.js index de6da074d2..518817022a 100644 --- a/src/bundle/Resources/public/js/scripts/admin.content.tree.js +++ b/src/bundle/Resources/public/js/scripts/admin.content.tree.js @@ -2,14 +2,13 @@ const KEY_CONTENT_TREE_EXPANDED = 'ez-content-tree-expanded'; const CLASS_CONTENT_TREE_EXPANDED = 'ez-content-tree-container--expanded'; const CLASS_BTN_CONTENT_TREE_EXPANDED = 'ez-btn--content-tree-expanded'; - const VIEWPORT_CHANGE_TIMEOUT = 50; const token = doc.querySelector('meta[name="CSRF-Token"]').content; const siteaccess = doc.querySelector('meta[name="SiteAccess"]').content; const contentTreeContainer = doc.querySelector('.ez-content-tree-container'); const contentTreeWrapper = doc.querySelector('.ez-content-tree-container__wrapper'); const btn = doc.querySelector('.ez-btn--toggle-content-tree'); const { currentLocationPath, userId, treeRootLocationId } = contentTreeContainer.dataset; - let onViewportChangeTimeout = null; + let frame = null; const toggleContentTreePanel = () => { contentTreeContainer.classList.toggle(CLASS_CONTENT_TREE_EXPANDED); btn.classList.toggle(CLASS_BTN_CONTENT_TREE_EXPANDED); @@ -23,15 +22,13 @@ const height = Math.min(window.innerHeight - contentTreeContainer.getBoundingClientRect().top, window.innerHeight); contentTreeWrapper.style.height = `${height}px`; - onViewportChangeTimeout = null; }; const handleViewportChange = () => { - if (onViewportChangeTimeout) { - return; + if (frame) { + cancelAnimationFrame(frame); } - window.clearTimeout(onViewportChangeTimeout); - onViewportChangeTimeout = window.setTimeout(updateContentTreeWrapperHeight, VIEWPORT_CHANGE_TIMEOUT); + frame = requestAnimationFrame(updateContentTreeWrapperHeight); }; const saveContentTreeExpandedState = (userId, isExpanded) => { let expandedState = JSON.parse(localStorage.getItem(KEY_CONTENT_TREE_EXPANDED)); @@ -69,6 +66,6 @@ updateContentTreeWrapperHeight(); - doc.addEventListener('scroll', handleViewportChange, false); - window.addEventListener('resize', handleViewportChange, false); + doc.addEventListener('scroll', handleViewportChange, { capture: false, passive: true }); + window.addEventListener('resize', handleViewportChange, { capture: false, passive: true }); })(window.document, window.React, window.ReactDOM, window.eZ, window.localStorage);