Skip to content

Commit

Permalink
refactor: remove all
Browse files Browse the repository at this point in the history
  • Loading branch information
kark committed May 9, 2024
1 parent 1209eaa commit f289bba
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { useState, useCallback, useEffect } from 'react';
import { PORTALS_CONTAINER_ID } from '@commercetools-frontend/constants';

const PORTALS_CONTAINER_MUTATION_TRIGGER = 'portals-container-mutation-trigger';

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);
newDiv.dataset.role = PORTALS_CONTAINER_MUTATION_TRIGGER;
const portalsContainer = document.querySelector(
`div#${PORTALS_CONTAINER_ID}`
);
portalsContainer?.appendChild(newDiv);
} else {
const trigger = document.querySelector(
`div#${PORTALS_CONTAINER_OBSERVER_TRIGGER_ID}`
const triggers = document.querySelectorAll(
`div[data-role="${PORTALS_CONTAINER_MUTATION_TRIGGER}"]`
);
trigger?.remove();
triggers.forEach((trigger) => trigger.remove());
}
}, [isModalOpen]);

Expand Down

0 comments on commit f289bba

Please sign in to comment.