|
1 | 1 | <script lang="ts"> |
2 | 2 | interface Props { |
3 | 3 | tweetLink?: string; |
| 4 | + theme?: 'light' | 'dark'; |
4 | 5 | } |
5 | 6 |
|
6 | | - let { tweetLink = '' }: Props = $props(); |
| 7 | + let { tweetLink = '', theme = 'light' }: Props = $props(); |
7 | 8 |
|
8 | | - let twitter_widgets_script: HTMLScriptElement | null = null; |
| 9 | + let twitter_widgets_script = $state.raw<HTMLScriptElement | null>( |
| 10 | + null, |
| 11 | + ); |
9 | 12 |
|
10 | 13 | const load_twitter_widgets_script = () => { |
| 14 | + if ( |
| 15 | + document.querySelector( |
| 16 | + 'script[src*="platform.twitter.com/widgets.js"]', |
| 17 | + ) |
| 18 | + ) { |
| 19 | + return; |
| 20 | + } |
| 21 | +
|
11 | 22 | if (twitter_widgets_script) return; |
| 23 | +
|
12 | 24 | twitter_widgets_script = document.createElement('script'); |
13 | 25 | twitter_widgets_script.src = |
14 | 26 | 'https://platform.twitter.com/widgets.js'; |
|
17 | 29 | }; |
18 | 30 |
|
19 | 31 | const remove_twitter_widget_script = () => { |
20 | | - if (twitter_widgets_script) { |
21 | | - document.head.removeChild(twitter_widgets_script); |
22 | | - twitter_widgets_script = null; |
23 | | - } |
| 32 | + if (!twitter_widgets_script) return; |
| 33 | + document.head.removeChild(twitter_widgets_script); |
| 34 | + twitter_widgets_script = null; |
24 | 35 | }; |
25 | 36 |
|
26 | | - $effect(() => { |
| 37 | + $effect.root(() => { |
27 | 38 | load_twitter_widgets_script(); |
28 | 39 | return () => { |
29 | 40 | remove_twitter_widget_script(); |
30 | 41 | }; |
31 | 42 | }); |
32 | 43 | </script> |
33 | 44 |
|
34 | | -<div class="tweet-wrapper"> |
35 | | - <blockquote class="twitter-tweet"> |
| 45 | +<div |
| 46 | + class="tweet-wrapper" |
| 47 | + data-theme={theme} |
| 48 | + data-loaded={twitter_widgets_script != null} |
| 49 | +> |
| 50 | + <blockquote class="twitter-tweet" data-theme={theme}> |
36 | 51 | <a href={`https://twitter.com/${tweetLink}`}>Loading Tweet...</a> |
37 | 52 | </blockquote> |
38 | 53 | </div> |
|
42 | 57 | display: flex; |
43 | 58 | justify-content: center; |
44 | 59 | margin-bottom: 12px; |
| 60 | + border-radius: 13px; |
| 61 | + overflow: hidden; |
| 62 | + } |
| 63 | +
|
| 64 | + .tweet-wrapper :global(iframe) { |
| 65 | + border-radius: 13px !important; |
45 | 66 | } |
46 | 67 |
|
47 | 68 | .twitter-tweet { |
|
0 commit comments