Skip to content

Commit 244c9fd

Browse files
VEH9Самсонов Иван Александрович
authored andcommitted
fix: modal closing with a custom animation now always works, even if there are animated elements inside the modal
1 parent f258ff2 commit 244c9fd

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

.changeset/itchy-bobcats-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik-ui/headless': patch
3+
---
4+
5+
fix: modal closing with a custom animation now always works, even if there are animated elements inside the modal

packages/kit-headless/src/components/modal/use-modal.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,29 @@ export function useModal() {
1919
const { animationDuration, transitionDuration } = getComputedStyle(modal);
2020

2121
if (animationDuration !== '0s') {
22-
modal.addEventListener(
23-
'animationend',
24-
(e) => {
25-
if (e.target === modal) {
26-
delete modal.dataset.closing;
27-
modal.classList.remove('modal-closing');
28-
enableBodyScroll(modal);
29-
modal.close();
30-
}
31-
},
32-
{ once: true },
33-
);
22+
const handler = (e: AnimationEvent) => {
23+
if (e.target === modal) {
24+
delete modal.dataset.closing;
25+
modal.classList.remove('modal-closing');
26+
enableBodyScroll(modal);
27+
modal.close();
28+
modal.removeEventListener('animationend', handler);
29+
}
30+
};
31+
32+
modal.addEventListener('animationend', handler);
3433
} else if (transitionDuration !== '0s') {
35-
modal.addEventListener(
36-
'transitionend',
37-
(e) => {
38-
if (e.target === modal) {
39-
delete modal.dataset.closing;
40-
modal.classList.remove('modal-closing');
41-
enableBodyScroll(modal);
42-
modal.close();
43-
}
44-
},
45-
{ once: true },
46-
);
34+
const handler = (e: TransitionEvent) => {
35+
if (e.target === modal) {
36+
delete modal.dataset.closing;
37+
modal.classList.remove('modal-closing');
38+
enableBodyScroll(modal);
39+
modal.close();
40+
modal.removeEventListener('transitionend', handler);
41+
}
42+
};
43+
44+
modal.addEventListener('transitionend', handler);
4745
} else if (animationDuration === '0s' && transitionDuration === '0s') {
4846
delete modal.dataset.closing;
4947
modal.classList.remove('modal-closing');

0 commit comments

Comments
 (0)