social-embeds 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -18
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ Turn social media embed code or post URLs into working React components.
|
|
|
4
4
|
|
|
5
5
|
Pasting a platform's embed code into `dangerouslySetInnerHTML` does not work in React: the `<script>` tag never runs, every embed adds the same script again, and nothing initialises after client-side navigation. `social-embeds` recognises the platform, reads the post and its options from the embed code, drops the `<script>` tag, loads each platform script once per page and asks the platform to render the post. Iframe-based platforms get a responsive `<iframe>` instead.
|
|
6
6
|
|
|
7
|
+
**[Live demo](https://amitrev.github.io/social-embeds/)**: every platform from embed code and post URLs, and a box for your own, running the released version.
|
|
8
|
+
|
|
7
9
|
- X, Instagram, Threads, Facebook, TikTok, YouTube, Telegram, Snapchat, LinkedIn, Reddit, Pinterest, Spotify, Vimeo, Twitch, Dailymotion, Bluesky, SoundCloud, Apple Music, Apple Podcasts, Tumblr, Kick and Giphy
|
|
8
10
|
- React 19, Next.js App Router (Server Components, client navigation), or plain React
|
|
9
11
|
- Zero runtime dependencies, ESM only, typed
|
|
@@ -109,47 +111,47 @@ import {
|
|
|
109
111
|
|
|
110
112
|
<XEmbed url="https://x.com/jack/status/20" theme="dark" cards="hidden" />
|
|
111
113
|
|
|
112
|
-
<InstagramEmbed url="https://www.instagram.com/p/
|
|
114
|
+
<InstagramEmbed url="https://www.instagram.com/p/Df75lxnmIrF/" captioned />
|
|
113
115
|
|
|
114
|
-
<ThreadsEmbed url="https://www.threads.com/@instagram/post/
|
|
116
|
+
<ThreadsEmbed url="https://www.threads.com/@instagram/post/Df5MmLBm0O7" theme="auto" />
|
|
115
117
|
|
|
116
118
|
<FacebookEmbed
|
|
117
119
|
url="https://www.facebook.com/facebook/videos/10153231379946729/"
|
|
118
|
-
locale="
|
|
120
|
+
locale="en_GB"
|
|
119
121
|
showCaptions
|
|
120
122
|
/>
|
|
121
123
|
|
|
122
124
|
<TikTokEmbed
|
|
123
|
-
url="https://www.tiktok.com/@
|
|
125
|
+
url="https://www.tiktok.com/@tiktok/video/7704834703446117662"
|
|
124
126
|
musicInfo={false}
|
|
125
127
|
description={false}
|
|
126
128
|
/>
|
|
127
129
|
|
|
128
|
-
<YouTubeEmbed url="https://youtu.be/jNQXAC9IVRw?t=
|
|
130
|
+
<YouTubeEmbed url="https://youtu.be/jNQXAC9IVRw?t=11" nocookie controls={false} />
|
|
129
131
|
|
|
130
132
|
<TelegramEmbed url="https://t.me/telegram/83" dark userpic={false} />
|
|
131
133
|
|
|
132
134
|
<SnapchatEmbed url="https://www.snapchat.com/@teamsnapchat" />
|
|
133
135
|
|
|
134
136
|
<LinkedInEmbed
|
|
135
|
-
url="https://www.linkedin.com/feed/update/urn:li:
|
|
137
|
+
url="https://www.linkedin.com/feed/update/urn:li:ugcPost:7528828146767409153/"
|
|
136
138
|
collapsed
|
|
137
139
|
height={560}
|
|
138
140
|
/>
|
|
139
141
|
|
|
140
142
|
<RedditEmbed
|
|
141
|
-
url="https://www.reddit.com/r/
|
|
143
|
+
url="https://www.reddit.com/r/reddit/comments/11zso11/"
|
|
142
144
|
theme="dark"
|
|
143
|
-
locale="
|
|
145
|
+
locale="en-GB"
|
|
144
146
|
/>
|
|
145
147
|
|
|
146
|
-
<PinterestEmbed url="https://www.pinterest.com/pin/
|
|
148
|
+
<PinterestEmbed url="https://www.pinterest.com/pin/424605071145752542/" pinWidth="large" />
|
|
147
149
|
|
|
148
|
-
<SpotifyEmbed url="https://open.spotify.com/track/
|
|
150
|
+
<SpotifyEmbed url="https://open.spotify.com/track/0LrHzQHk5gYGbMsoVbKDuS" theme={0} />
|
|
149
151
|
|
|
150
152
|
<VimeoEmbed url="https://vimeo.com/76979871" muted title={false} t="1m2s" />
|
|
151
153
|
|
|
152
|
-
<TwitchEmbed url="https://www.twitch.tv/videos/
|
|
154
|
+
<TwitchEmbed url="https://www.twitch.tv/videos/1816688726" autoplay={false} time="30s" />
|
|
153
155
|
|
|
154
156
|
<DailymotionEmbed
|
|
155
157
|
url="https://www.dailymotion.com/video/x84sh87"
|
|
@@ -158,21 +160,21 @@ import {
|
|
|
158
160
|
/>
|
|
159
161
|
|
|
160
162
|
<BlueskyEmbed
|
|
161
|
-
url="https://bsky.app/profile/did:plc:z72i7hdynmk6r22z27h6tvur/post/
|
|
163
|
+
url="https://bsky.app/profile/did:plc:z72i7hdynmk6r22z27h6tvur/post/3mxu3hsnebc2v"
|
|
162
164
|
colorMode="dark"
|
|
163
165
|
/>
|
|
164
166
|
|
|
165
167
|
<SoundCloudEmbed url="https://soundcloud.com/forss/flickermood" visual={false} height={166} />
|
|
166
168
|
|
|
167
|
-
<AppleMusicEmbed url="https://music.apple.com/us/album/
|
|
169
|
+
<AppleMusicEmbed url="https://music.apple.com/us/album/birds-of-oaks-bottom-piano-solos/1357820591?i=1357820597" />
|
|
168
170
|
|
|
169
|
-
<ApplePodcastsEmbed url="https://podcasts.apple.com/us/podcast/the-
|
|
171
|
+
<ApplePodcastsEmbed url="https://podcasts.apple.com/us/podcast/monument-valley-meet-the-developer/id947965068" />
|
|
170
172
|
|
|
171
|
-
<TumblrEmbed url="https://staff.tumblr.com/post/
|
|
173
|
+
<TumblrEmbed url="https://staff.tumblr.com/post/828009069026721792" />
|
|
172
174
|
|
|
173
|
-
<KickEmbed url="https://kick.com/
|
|
175
|
+
<KickEmbed url="https://kick.com/kick" muted />
|
|
174
176
|
|
|
175
|
-
<GiphyEmbed url="https://giphy.com/gifs/
|
|
177
|
+
<GiphyEmbed url="https://giphy.com/gifs/originals-animation-kitten-logo-l41YlHs99JsSptZf2" maxWidth={480} />
|
|
176
178
|
```
|
|
177
179
|
|
|
178
180
|
| Component | Option props |
|
|
@@ -271,7 +273,7 @@ A component checks an `embed` before using it: its `url` must be the exact URL `
|
|
|
271
273
|
| `onError` | `(error: Error) => void` | | Called on invalid input, a script that fails to load, or a post that does not render. Components never throw. |
|
|
272
274
|
| `...rest` | `ComponentProps<'div'>` | | Passed to the wrapper `div` (`className`, `style`, `data-*`, …). |
|
|
273
275
|
|
|
274
|
-
Script-based embeds try to render three times, waiting 15 s each time, 1 s and then 2 s apart, before `onError` reports the failure. Telegram, Reddit, Bluesky and Tumblr frames that report no height within 15 s show at their starting height, and `onError` reports it.
|
|
276
|
+
Script-based embeds try to render three times, waiting 15 s each time, 1 s and then 2 s apart, before `onError` reports the failure. Telegram, Reddit, Bluesky and Tumblr frames that report no height within 15 s show at their starting height, and `onError` reports it. Frames report their height only once painted, so the 15 s start when the embed comes near the viewport, also with `lazy={false}`.
|
|
275
277
|
|
|
276
278
|
## Platforms
|
|
277
279
|
|
|
@@ -416,6 +418,7 @@ URL shapes, each tested against Facebook's plugins on `www.facebook.com`, `faceb
|
|
|
416
418
|
- Embed code from Dailymotion Studio carries the Player ID (`geo.dailymotion.com/player/{playerId}.html?video={id}`) and renders as given, also inside Dailymotion's responsive wrapper `div`.
|
|
417
419
|
- URLs, and code without a Player ID, render only through `DailymotionEmbed` with your `playerId`. A `playerId` prop also replaces the one in the embed code. `<SocialEmbed>` takes none: for that input it renders nothing and calls `onError`.
|
|
418
420
|
- The package never uses a Player ID you did not give it.
|
|
421
|
+
- Playback with a publisher's own Player ID is not yet tested. The sample Player ID from Dailymotion's docs loads the player and plays, but its stream is refused after a few plays (`docs/decisions.md` D69), so the demo's Dailymotion page may show "Playback error".
|
|
419
422
|
- Playlists are not supported.
|
|
420
423
|
|
|
421
424
|
### Bluesky
|
package/dist/index.d.ts
CHANGED
|
@@ -258,7 +258,7 @@ type SocialEmbedProps = SocialEmbedSource & Omit<FrameProps, PlatformOption> & {
|
|
|
258
258
|
fallback?: (html: string) => ReactNode;
|
|
259
259
|
};
|
|
260
260
|
/** `height` and `maxHeight` reach only the iframe-based embeds: the others size themselves. */
|
|
261
|
-
export declare function SocialEmbed(props: SocialEmbedProps): string | number | bigint | boolean |
|
|
261
|
+
export declare function SocialEmbed(props: SocialEmbedProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react").JSX.Element | null | undefined;
|
|
262
262
|
type SnapchatEmbedProps = EmbedSource & FrameProps;
|
|
263
263
|
/** At most 416px wide by default, the size of Snapchat's own embed code (docs/decisions.md D50). */
|
|
264
264
|
export declare function SnapchatEmbed(props: SnapchatEmbedProps): import("react").JSX.Element | null;
|
package/dist/index.js
CHANGED
|
@@ -61,6 +61,8 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, sandbox, ra
|
|
|
61
61
|
const frame = useRef(null);
|
|
62
62
|
const iframe = useRef(null);
|
|
63
63
|
const near = useNearViewport(frame, lazy);
|
|
64
|
+
const inView = useNearViewport(frame, !lazy && messageHeight !== void 0);
|
|
65
|
+
const heard = useRef(false);
|
|
64
66
|
const [loaded, setLoaded] = useState(false);
|
|
65
67
|
const [reportedHeight, setReportedHeight] = useState();
|
|
66
68
|
const reported = useRef(false);
|
|
@@ -83,7 +85,7 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, sandbox, ra
|
|
|
83
85
|
useEffect(() => {
|
|
84
86
|
if (!near || !messageHeight) return;
|
|
85
87
|
const { origin, read } = messageHeight;
|
|
86
|
-
|
|
88
|
+
heard.current = false;
|
|
87
89
|
const listen = (event) => {
|
|
88
90
|
if (event.origin !== origin) return;
|
|
89
91
|
if (!iframe.current || event.source !== iframe.current.contentWindow) return;
|
|
@@ -94,25 +96,34 @@ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, sandbox, ra
|
|
|
94
96
|
return;
|
|
95
97
|
}
|
|
96
98
|
if (typeof value !== "number" || !(value > 0 && value <= MAX_REPORTED_HEIGHT)) return;
|
|
97
|
-
heard = true;
|
|
99
|
+
heard.current = true;
|
|
98
100
|
setReportedHeight(Math.ceil(value));
|
|
99
101
|
report();
|
|
100
102
|
};
|
|
101
103
|
window.addEventListener("message", listen);
|
|
104
|
+
return () => {
|
|
105
|
+
window.removeEventListener("message", listen);
|
|
106
|
+
};
|
|
107
|
+
}, [
|
|
108
|
+
near,
|
|
109
|
+
messageHeight,
|
|
110
|
+
report
|
|
111
|
+
]);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (!near || !inView || !messageHeight) return;
|
|
102
114
|
const timer = setTimeout(() => {
|
|
103
|
-
if (heard) return;
|
|
115
|
+
if (heard.current) return;
|
|
104
116
|
setLoaded(true);
|
|
105
117
|
callbacks.current.onError?.(/* @__PURE__ */ new Error(`social-embeds: ${title} did not report its height`));
|
|
106
118
|
}, HEIGHT_TIMEOUT_MS);
|
|
107
119
|
return () => {
|
|
108
|
-
window.removeEventListener("message", listen);
|
|
109
120
|
clearTimeout(timer);
|
|
110
121
|
};
|
|
111
122
|
}, [
|
|
112
123
|
near,
|
|
124
|
+
inView,
|
|
113
125
|
messageHeight,
|
|
114
|
-
title
|
|
115
|
-
report
|
|
126
|
+
title
|
|
116
127
|
]);
|
|
117
128
|
const aspectWidth = size?.width ?? ratio.width;
|
|
118
129
|
const aspectHeight = size?.height ?? ratio.height;
|