Skip to content

Commit f3b0665

Browse files
committed
fix: handle multiple iframes on same page correctly
Update version to 0.0.1 and refactor `AsyncIframe` component for improved styling and functionality * optimized performance * refactored naming
1 parent 3811a9b commit f3b0665

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "astro-async-loader",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"description": "Astro components for elements, that load asynchronously, and should have loading indicators.",
55
"repository": "https://github.com/jonasfroeller/astro.async.loader",
66
"license": "MIT",

src/AsyncIframe.astro

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,96 @@
1-
<div class="async-iframe-wrapper">
2-
<div class="loader-container">
3-
<div class="loader"></div>
1+
<div class="async-iframe__wrapper">
2+
<div class="async-iframe__loader-container">
3+
<div class="async-iframe__loader"></div>
44
</div>
55

6-
<div class="iframe-container">
6+
<div class="async-iframe__container">
77
<slot />
88
</div>
99
</div>
1010

1111
<script>
12-
function setupIframe() {
13-
const wrapper = document.getElementsByClassName('async-iframe-wrapper')[0];
14-
const loaderContainer = wrapper.getElementsByClassName('loader-container')[0];
15-
const iframe = wrapper.querySelector('iframe');
16-
17-
if (iframe && loaderContainer) {
18-
const handleLoad = () => {
19-
loaderContainer.classList.add('hidden');
20-
iframe.removeEventListener('load', handleLoad);
21-
};
22-
23-
iframe.addEventListener('load', handleLoad);
12+
function setupIframes() {
13+
const wrappers = document.getElementsByClassName('async-iframe__wrapper');
14+
15+
for (let i = 0; i < wrappers.length; i++) {
16+
const wrapper = wrappers[i];
17+
const loaderContainer = wrapper.getElementsByClassName('async-iframe__loader-container')[0];
18+
const iframe = wrapper.getElementsByTagName('iframe')[0];
19+
20+
if (iframe && loaderContainer) {
21+
const handleLoad = () => {
22+
loaderContainer.classList.add('async-iframe__hidden');
23+
iframe.removeEventListener('load', handleLoad);
24+
};
25+
26+
iframe.addEventListener('load', handleLoad);
27+
}
2428
}
2529
}
2630

2731
if (document.readyState === 'loading') {
2832
const handleDOMContentLoaded = () => {
29-
setupIframe();
33+
setupIframes();
3034
document.removeEventListener('DOMContentLoaded', handleDOMContentLoaded);
3135
};
3236

3337
document.addEventListener('DOMContentLoaded', handleDOMContentLoaded);
3438
} else {
35-
setupIframe();
39+
setupIframes();
3640
}
3741
</script>
3842

3943
<style>
40-
.async-iframe-wrapper {
44+
.async-iframe__wrapper {
4145
position: relative;
4246
padding-top: 85%;
4347
height: 0;
4448
overflow: hidden;
4549
}
4650

47-
.loader-container {
51+
.async-iframe__loader-container {
4852
position: absolute;
49-
top: 0;
50-
left: 0;
51-
right: 0;
52-
bottom: 0;
53-
display: flex;
54-
align-items: center;
55-
justify-content: center;
56-
z-index: 10;
53+
inset: 0;
54+
display: grid;
55+
place-items: center;
56+
z-index: 5;
57+
background: rgba(255, 255, 255, 0.9);
5758
}
5859

59-
.loader {
60+
.async-iframe__loader {
6061
width: 2rem;
6162
height: 2rem;
6263
border-radius: 50%;
6364
border: 0.125rem solid transparent;
6465
border-top-color: #3b82f6;
65-
animation: spin 1s linear infinite;
66+
animation: async-iframe__spin 1s linear infinite;
6667
}
6768

68-
@keyframes spin {
69+
@keyframes async-iframe__spin {
6970
0% { transform: rotate(0deg); }
7071
100% { transform: rotate(360deg); }
7172
}
7273

73-
.iframe-container {
74+
.async-iframe__container {
7475
position: absolute;
75-
top: 0px;
76-
left: 0px;
77-
right: 0px;
78-
bottom: 0px;
76+
inset: 0;
7977
}
8078

8179
:global(iframe) {
8280
position: absolute;
83-
top: 0px;
84-
left: 0px;
81+
inset: 0;
8582
width: 100%;
8683
height: 100%;
87-
border: 0px;
84+
border: 0;
85+
opacity: 0;
86+
transition: opacity 0.3s ease-in-out;
8887
}
8988

90-
.hidden {
89+
.async-iframe__hidden {
9190
display: none !important;
9291
}
92+
93+
.async-iframe__hidden + .async-iframe__container :global(iframe) {
94+
opacity: 1;
95+
}
9396
</style>

0 commit comments

Comments
 (0)