diff --git a/.changeset/tasty-readers-mate.md b/.changeset/tasty-readers-mate.md new file mode 100644 index 0000000000..e0be17df6a --- /dev/null +++ b/.changeset/tasty-readers-mate.md @@ -0,0 +1,5 @@ +--- +'@commercetools-frontend/application-components': patch +--- + +Use correct indentation level for modals rendered in portals container diff --git a/packages/application-components/src/hooks/use-modal-state/use-modal-state.ts b/packages/application-components/src/hooks/use-modal-state/use-modal-state.ts index ea8710c91a..8a5b0bee7b 100644 --- a/packages/application-components/src/hooks/use-modal-state/use-modal-state.ts +++ b/packages/application-components/src/hooks/use-modal-state/use-modal-state.ts @@ -1,8 +1,29 @@ -import { useState, useCallback } from 'react'; +import { useState, useCallback, useEffect } from 'react'; +import { PORTALS_CONTAINER_ID } from '@commercetools-frontend/constants'; const useModalState = (isInitiallyOpen = false) => { const [isModalOpen, setIsModalOpen] = useState(isInitiallyOpen); + // This is a workaround to make sure the portals container's Mutation Observer picks up a DOM mutation when the modal is opened + // This is neccessary to make sure the modal stacking layer's indentation level is correctly calculated + useEffect(() => { + const PORTALS_CONTAINER_OBSERVER_TRIGGER_ID = + 'stacking-layer-observer-trigger'; + if (isModalOpen) { + const newDiv = document.createElement('div'); + newDiv.setAttribute('id', PORTALS_CONTAINER_OBSERVER_TRIGGER_ID); + const portalsContainer = document.querySelector( + `div#${PORTALS_CONTAINER_ID}` + ); + portalsContainer?.appendChild(newDiv); + } else { + const trigger = document.querySelector( + `div#${PORTALS_CONTAINER_OBSERVER_TRIGGER_ID}` + ); + trigger?.remove(); + } + }, [isModalOpen]); + const openModal = useCallback(() => setIsModalOpen(true), []); const closeModal = useCallback(() => setIsModalOpen(false), []);