Skip to content

Commit 23ee8cd

Browse files
fix: add error handling for container removal in lightbox
1 parent b2e793b commit 23ee8cd

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/components/ui/WorkExperience.astro

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,34 @@ const images = entry.data.images
5858
import { createRoot } from 'react-dom/client';
5959
import { createElement } from 'react';
6060

61-
document.addEventListener('DOMContentLoaded', () => {
6261
const thumbs = document.querySelectorAll('.work-image-thumb');
63-
64-
thumbs.forEach((thumb) => {
65-
thumb.addEventListener('click', () => {
66-
const images = JSON.parse(thumb.getAttribute('data-images') || '[]');
67-
const index = parseInt(thumb.getAttribute('data-index') || '0');
68-
69-
const container = document.createElement('div');
70-
document.body.appendChild(container);
71-
const root = createRoot(container);
72-
73-
const closeHandler = () => {
74-
root.unmount();
62+
63+
thumbs.forEach((thumb) => {
64+
thumb.addEventListener('click', () => {
65+
const images = JSON.parse(thumb.getAttribute('data-images') || '[]');
66+
const index = parseInt(thumb.getAttribute('data-index') || '0');
67+
68+
const container = document.createElement('div');
69+
document.body.appendChild(container);
70+
const root = createRoot(container);
71+
72+
const closeHandler = () => {
73+
root.unmount();
74+
try {
7575
document.body.removeChild(container);
76-
};
77-
78-
root.render(
79-
createElement(ImageLightbox, {
80-
images,
81-
isOpen: true,
82-
initialIndex: index,
83-
onClose: closeHandler,
84-
})
85-
);
86-
});
76+
} catch (e) {
77+
// Container may have already been removed; ignore error
78+
}
79+
};
80+
81+
root.render(
82+
createElement(ImageLightbox, {
83+
images,
84+
isOpen: true,
85+
initialIndex: index,
86+
onClose: closeHandler,
87+
})
88+
);
8789
});
8890
});
8991
</script>

0 commit comments

Comments
 (0)