mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
fix(modal): nested modals should correctly toggle the body class (#1331)
Fixes #1324
This commit is contained in:
parent
0063c01beb
commit
22f93ee675
3 changed files with 34 additions and 2 deletions
|
@ -38,6 +38,7 @@
|
||||||
afterUpdate,
|
afterUpdate,
|
||||||
} from "svelte";
|
} from "svelte";
|
||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
|
import { modalsOpen, addModalId, removeModalId } from "../Modal/modalStore";
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
const label = writable(undefined);
|
const label = writable(undefined);
|
||||||
|
@ -45,6 +46,7 @@
|
||||||
let buttonRef = null;
|
let buttonRef = null;
|
||||||
let innerModal = null;
|
let innerModal = null;
|
||||||
let didClickInnerModal = false;
|
let didClickInnerModal = false;
|
||||||
|
let id = "ccs-" + Math.random().toString(36);
|
||||||
|
|
||||||
setContext("ComposedModal", {
|
setContext("ComposedModal", {
|
||||||
closeModal: () => {
|
closeModal: () => {
|
||||||
|
@ -78,6 +80,7 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
removeModalId(id);
|
||||||
document.body.classList.remove("bx--body--with-modal-open");
|
document.body.classList.remove("bx--body--with-modal-open");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -87,14 +90,24 @@
|
||||||
if (!open) {
|
if (!open) {
|
||||||
opened = false;
|
opened = false;
|
||||||
dispatch("close");
|
dispatch("close");
|
||||||
document.body.classList.remove("bx--body--with-modal-open");
|
|
||||||
}
|
}
|
||||||
} else if (open) {
|
} else if (open) {
|
||||||
opened = true;
|
opened = true;
|
||||||
dispatch("open");
|
dispatch("open");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($modalsOpen.length > 0) {
|
||||||
document.body.classList.add("bx--body--with-modal-open");
|
document.body.classList.add("bx--body--with-modal-open");
|
||||||
|
} else {
|
||||||
|
document.body.classList.remove("bx--body--with-modal-open");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$: if (open) {
|
||||||
|
addModalId(id);
|
||||||
|
} else {
|
||||||
|
removeModalId(id);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
||||||
|
|
|
@ -92,6 +92,7 @@
|
||||||
import { createEventDispatcher, onMount, afterUpdate } from "svelte";
|
import { createEventDispatcher, onMount, afterUpdate } from "svelte";
|
||||||
import Close from "../icons/Close.svelte";
|
import Close from "../icons/Close.svelte";
|
||||||
import Button from "../Button/Button.svelte";
|
import Button from "../Button/Button.svelte";
|
||||||
|
import { modalsOpen, addModalId, removeModalId } from "./modalStore";
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
|
@ -108,6 +109,7 @@
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
return () => {
|
return () => {
|
||||||
|
removeModalId(id);
|
||||||
document.body.classList.remove("bx--body--with-modal-open");
|
document.body.classList.remove("bx--body--with-modal-open");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -117,13 +119,17 @@
|
||||||
if (!open) {
|
if (!open) {
|
||||||
opened = false;
|
opened = false;
|
||||||
dispatch("close");
|
dispatch("close");
|
||||||
document.body.classList.remove("bx--body--with-modal-open");
|
|
||||||
}
|
}
|
||||||
} else if (open) {
|
} else if (open) {
|
||||||
opened = true;
|
opened = true;
|
||||||
focus();
|
focus();
|
||||||
dispatch("open");
|
dispatch("open");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($modalsOpen.length > 0) {
|
||||||
document.body.classList.add("bx--body--with-modal-open");
|
document.body.classList.add("bx--body--with-modal-open");
|
||||||
|
} else {
|
||||||
|
document.body.classList.remove("bx--body--with-modal-open");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -143,6 +149,11 @@
|
||||||
alertDialogProps["aria-describedby"] = modalBodyId;
|
alertDialogProps["aria-describedby"] = modalBodyId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$: if (open) {
|
||||||
|
addModalId(id);
|
||||||
|
} else {
|
||||||
|
removeModalId(id);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
||||||
|
|
8
src/Modal/modalStore.js
Normal file
8
src/Modal/modalStore.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { writable } from "svelte/store";
|
||||||
|
|
||||||
|
export const modalsOpen = writable([]);
|
||||||
|
|
||||||
|
export const addModalId = (id) => modalsOpen.update((ids) => [...ids, id]);
|
||||||
|
|
||||||
|
export const removeModalId = (id) =>
|
||||||
|
modalsOpen.update((ids) => ids.filter((_id) => _id !== id));
|
Loading…
Add table
Add a link
Reference in a new issue