Skip to content

Commit 61509da

Browse files
committed
refactor: ♻️ handle script loading
1 parent d4f6b79 commit 61509da

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

src/lib/components/toot.svelte

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
<script lang="ts">
2+
import { onDestroy, onMount } from 'svelte'
23
export let instance: string = ''
34
export let username: string = ''
45
export let tootId: string = ''
56
7+
let mastodon_embed_script: HTMLScriptElement | null = null
8+
9+
const load_mastodon_embed_script = () => {
10+
if (mastodon_embed_script) return
11+
mastodon_embed_script = document.createElement('script')
12+
mastodon_embed_script.src = `https://${instance}/embed.js`
13+
mastodon_embed_script.async = true
14+
document.head.appendChild(mastodon_embed_script)
15+
}
16+
17+
const remove_mastodon_embed_script = () => {
18+
if (mastodon_embed_script) {
19+
document.head.removeChild(mastodon_embed_script)
20+
mastodon_embed_script = null
21+
}
22+
}
23+
24+
onMount(load_mastodon_embed_script)
25+
onDestroy(remove_mastodon_embed_script)
26+
627
$: trimmedUsername = username.trim()
728
$: atUsername = trimmedUsername.startsWith('@')
829
? trimmedUsername
930
: `@${trimmedUsername}`
1031
</script>
1132

12-
<svelte:head>
13-
<script
14-
async
15-
src="https://{instance}/embed.js"
16-
charset="utf-8"
17-
></script>
18-
</svelte:head>
19-
20-
<div class="flex justify-center mb-12">
33+
<div>
2134
<iframe
2235
title=""
23-
src="https://{instance}/{atUsername}/{tootId}/embed"
36+
src={`https://${instance}/${atUsername}/${tootId}/embed`}
2437
class="mastodon-embed"
25-
style="max-width: 100%; border: 0"
2638
width="400"
2739
allowfullscreen
2840
/>
2941
</div>
42+
43+
<style>
44+
div {
45+
display: flex;
46+
justify-content: center;
47+
margin-bottom: 12px;
48+
}
49+
50+
iframe {
51+
max-width: 100%;
52+
border: 0;
53+
}
54+
</style>

src/lib/components/tweet.svelte

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
<script lang="ts">
2+
import { onDestroy, onMount } from 'svelte'
23
export let tweetLink: string = ''
3-
</script>
44
5-
<svelte:head>
6-
<script
7-
async
8-
src="https://platform.twitter.com/widgets.js"
9-
charset="utf-8"
10-
></script>
11-
</svelte:head>
5+
let twitter_widgets_script: HTMLScriptElement | null = null
6+
7+
const load_twitter_widgets_script = () => {
8+
if (twitter_widgets_script) return
9+
twitter_widgets_script = document.createElement('script')
10+
twitter_widgets_script.src =
11+
'https://platform.twitter.com/widgets.js'
12+
twitter_widgets_script.async = true
13+
document.head.appendChild(twitter_widgets_script)
14+
}
15+
16+
const remove_twitter_widget_script = () => {
17+
if (twitter_widgets_script) {
18+
document.head.removeChild(twitter_widgets_script)
19+
twitter_widgets_script = null
20+
}
21+
}
22+
23+
onMount(load_twitter_widgets_script)
24+
onDestroy(remove_twitter_widget_script)
25+
</script>
1226

13-
<div class="flex justify-center mb-12">
27+
<div class="tweet-wrapper">
1428
<blockquote class="twitter-tweet">
15-
<a class="twitter-tweet" href={`https://twitter.com/${tweetLink}`}
16-
>Loading Tweet...</a
17-
>
29+
<a href={`https://twitter.com/${tweetLink}`}>Loading Tweet...</a>
1830
</blockquote>
1931
</div>
32+
33+
<style>
34+
.tweet-wrapper {
35+
display: flex;
36+
justify-content: center;
37+
margin-bottom: 12px;
38+
}
39+
40+
.twitter-tweet {
41+
display: block;
42+
margin: 0;
43+
padding: 0;
44+
}
45+
46+
.twitter-tweet a {
47+
color: #1da1f2;
48+
font-weight: bold;
49+
text-decoration: none;
50+
}
51+
</style>

0 commit comments

Comments
 (0)