From ab9eb6a6dc7d1a0fcd280aa75eaf4e230fe3f154 Mon Sep 17 00:00:00 2001 From: melloware Date: Mon, 19 Feb 2024 09:15:06 -0500 Subject: [PATCH] Fix #6000: Improved closeOnEscape handling --- components/lib/hooks/useDisplayOrder.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/components/lib/hooks/useDisplayOrder.js b/components/lib/hooks/useDisplayOrder.js index 71f8863377..fea25bf418 100644 --- a/components/lib/hooks/useDisplayOrder.js +++ b/components/lib/hooks/useDisplayOrder.js @@ -9,21 +9,22 @@ export const useDisplayOrder = (group, isVisible = true) => { React.useEffect(() => { if (isVisible) { - if (!(group in groupToDisplayedElements)) { + if (!groupToDisplayedElements[group]) { groupToDisplayedElements[group] = []; } - const newDisplayOrder = groupToDisplayedElements[group].length + 1; + const newDisplayOrder = groupToDisplayedElements[group].push(uid); - groupToDisplayedElements[group].push(uid); setDisplayOrder(newDisplayOrder); return () => { - delete groupToDisplayedElements[group][newDisplayOrder]; - const lastOrder = groupToDisplayedElements[group].findLastIndex((el) => el !== undefined); + delete groupToDisplayedElements[group][newDisplayOrder - 1]; // Reduce array length, by removing undefined elements at the end of array: - groupToDisplayedElements[group].splice(lastOrder + 1); + const lastIndex = groupToDisplayedElements[group].length - 1; + const lastOrder = groupToDisplayedElements[group].findLastIndex((el) => el !== undefined); + + if (lastOrder !== lastIndex) groupToDisplayedElements[group].splice(lastOrder + 1); setDisplayOrder(undefined); };