Skip to content

Commit

Permalink
fix(modal): refactor locationchangetrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
manoncarbonnel committed Oct 5, 2023
1 parent 50d5c86 commit 81f1d07
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,24 @@ const TemplateDefault = (args:any) => {
}
}

const locationChangeTrigger = () => {
setTimeout(() => {
let prevUrl = window.location.href;
const interval = setInterval(() => {
const currUrl = window.location.href;
if (currUrl !== prevUrl) {
prevUrl = currUrl;
const modals = document.getElementsByTagName('osds-modal');
for (let i = 0; i < modals.length; i++) {
modals[i].close();
}
clearInterval(interval);
const locationChangeTrigger = () => {
let prevUrl = document.location.href;
const body = document.querySelector("body");

const observer = new MutationObserver(() => {
if (prevUrl !== document.location.href) {
prevUrl = document.location.href;

const modals = document.getElementsByTagName('osds-modal');
for (let i = 0; i < modals.length; i++) {
modals[i].close();
}
}, 100);
}, 0);
};
}
});
observer.observe(body, { childList: true, subtree: true });
};

window.onload = locationChangeTrigger;

return html`
<button @click=${handleOpenModal}>Trigger "open()"</button>
Expand All @@ -96,7 +98,7 @@ const TemplateDefault = (args:any) => {
${unsafeHTML(args.content)}
${unsafeHTML(args.actions)}
</osds-modal>
`;
`
}

export const Default = TemplateDefault.bind({});
Expand Down

0 comments on commit 81f1d07

Please sign in to comment.