mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
refactor(composed-modal): use onMount, afterUpdate methods
This commit is contained in:
parent
52dd244516
commit
3f03a1c259
1 changed files with 16 additions and 11 deletions
|
@ -8,7 +8,7 @@
|
||||||
export let size = undefined;
|
export let size = undefined;
|
||||||
export let style = undefined;
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher, setContext, onDestroy } from 'svelte';
|
import { createEventDispatcher, tick, setContext, onMount, afterUpdate } from 'svelte';
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
|
@ -30,10 +30,6 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
|
||||||
document.body.classList.remove(cx('--body--with-modal-open'));
|
|
||||||
});
|
|
||||||
|
|
||||||
function focus(element) {
|
function focus(element) {
|
||||||
if (element.querySelector(selectorPrimaryFocus)) {
|
if (element.querySelector(selectorPrimaryFocus)) {
|
||||||
return focusElement.focus();
|
return focusElement.focus();
|
||||||
|
@ -44,10 +40,18 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$: didOpen = open;
|
onMount(async () => {
|
||||||
$: {
|
await tick();
|
||||||
if (innerModal) {
|
focus(innerModal);
|
||||||
focus(innerModal);
|
|
||||||
|
return () => {
|
||||||
|
document.body.classList.remove(cx('--body--with-modal-open'));
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterUpdate(() => {
|
||||||
|
if (!didOpen) {
|
||||||
|
focus(outerModal);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (open) {
|
if (open) {
|
||||||
|
@ -56,7 +60,9 @@
|
||||||
dispatch('close');
|
dispatch('close');
|
||||||
document.body.classList.remove(cx('--body--with-modal-open'));
|
document.body.classList.remove(cx('--body--with-modal-open'));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
$: didOpen = open;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
@ -76,7 +82,6 @@
|
||||||
on:transitionend
|
on:transitionend
|
||||||
on:transitionend={() => {
|
on:transitionend={() => {
|
||||||
if (didOpen) {
|
if (didOpen) {
|
||||||
focus(outerModal);
|
|
||||||
didOpen = false;
|
didOpen = false;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue