Skip to content

Commit 6ccf03f

Browse files
committed
Use previous sibling as target, no more initialization loop
1 parent b404424 commit 6ccf03f

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

sophora-components/datawrapper-switcher.html

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@
2424
</head>
2525
<body>
2626
<div data-lab-components-embed="datawrapper-switcher"></div>
27+
<script>
28+
const target = document.currentScript.previousElementSibling;
29+
if (window.__mountDatawrapperSwitcher) {
30+
window.__mountDatawrapperSwitcher(target);
31+
} else {
32+
document.addEventListener('DOMContentLoaded', () => {
33+
window.__mountDatawrapperSwitcher(target);
34+
});
35+
}
36+
</script>
2737
<script type="module">
28-
import { mountEmbed } from '/src/lib/utils/mountEmbed';
38+
import mountEmbed from '/src/lib/utils/mountEmbed';
2939
import Embed from '/src/components/datawrapper-switcher/embed.svelte';
30-
window.__mountDatawrapperSwitcher = () => mountEmbed('datawrapper-switcher', Embed);
31-
window.__mountDatawrapperSwitcher();
32-
</script>
33-
<script>
34-
window.__mountDatawrapperSwitcher && window.__mountDatawrapperSwitcher();
40+
41+
window.__mountDatawrapperSwitcher = (target) => {
42+
console.log('Mounting Datawrapper Switcher component on', target);
43+
mountEmbed(target, Embed);
44+
};
3545
</script>
3646
</body>
3747
</html>

sophora-components/highlight-cards.html

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@
2424
</head>
2525
<body>
2626
<div data-lab-components-embed="highlight-cards"></div>
27+
<script>
28+
const target = document.currentScript.previousElementSibling;
29+
if (window.__mountHighlightCards) {
30+
window.__mountHighlightCards(target);
31+
} else {
32+
document.addEventListener('DOMContentLoaded', () => {
33+
window.__mountHighlightCards(target);
34+
});
35+
}
36+
</script>
2737
<script type="module">
28-
import { mountEmbed } from '/src/lib/utils/mountEmbed';
38+
import mountEmbed from '/src/lib/utils/mountEmbed';
2939
import Embed from '/src/components/highlight-cards/embed.svelte';
3040

31-
window.__mountHighlightCards = () => mountEmbed('highlight-cards', Embed);
32-
window.__mountHighlightCards();
33-
</script>
34-
<script>
35-
window.__mountHighlightCards && window.__mountHighlightCards();
41+
window.__mountHighlightCards = (target) => {
42+
console.log('Mounting Highlight Cards component on', target);
43+
mountEmbed(target, Embed);
44+
};
3645
</script>
3746
</body>
3847
</html>

sophora-components/src/lib/utils/mountEmbed.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ import { mount } from 'svelte';
22

33
type ComponentConstructor = new (...args: any[]) => any;
44

5-
export async function mountEmbed(
6-
componentName: string,
5+
export default async function mountEmbed(
6+
target: Element,
77
Component: ComponentConstructor
88
): Promise<void> {
9-
const selector = `[data-lab-components-embed="${componentName}"]`;
10-
document.querySelectorAll(selector).forEach((target) => {
11-
mount(Component, { target });
12-
});
9+
mount(Component, { target });
1310
}

0 commit comments

Comments
 (0)