Skip to content

Commit

Permalink
fix: indendation level of modals
Browse files Browse the repository at this point in the history
refactor: inside
  • Loading branch information
kark committed May 8, 2024
1 parent 49953c6 commit 1209eaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-readers-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-frontend/application-components': patch
---

Use correct indentation level for modals rendered in portals container
Original file line number Diff line number Diff line change
@@ -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), []);

Expand Down

0 comments on commit 1209eaa

Please sign in to comment.