social-embeds 0.1.0 → 0.2.0

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 CHANGED
@@ -169,19 +169,20 @@ A component checks an `embed` before using it: its `url` must be one `matchUrl`
169
169
 
170
170
  ## Shared props
171
171
 
172
- | Prop | Type | Default | Notes |
173
- | ------------- | ------------------------ | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
174
- | `url` | `string` | | Post URL. Platform components take `url` or `embed`; `<SocialEmbed>` takes `url` or `html`. |
175
- | `embed` | `ParsedEmbed` | | Platform components only. |
176
- | `html` | `string` | | `<SocialEmbed>` only. |
177
- | `width` | `number \| string` | `'100%'` | Wrapper width. |
178
- | `maxWidth` | `number` | per platform | The embed fills its container up to this width, in pixels. See [Platforms](#platforms). |
179
- | `height` | `number` | | Iframe-based embeds only (YouTube, TikTok, Facebook video embed code): a fixed height instead of the aspect ratio. The others size themselves. |
180
- | `placeholder` | `ReactNode` | a skeleton | Shown until the post has loaded. The default skeleton has a per-platform minimum height to limit layout shift. |
181
- | `lazy` | `boolean` | `true` | Load the embed only when it comes within 200 px of the viewport. |
182
- | `onLoad` | `() => void` | | Called once the platform has rendered the post (iframe embeds: when the iframe has loaded). |
183
- | `onError` | `(error: Error) => void` | | Called on invalid input, a script that fails to load, or a post that does not render. Components never throw. |
184
- | `...rest` | `ComponentProps<'div'>` | | Passed to the wrapper `div` (`className`, `style`, `data-*`, …). |
172
+ | Prop | Type | Default | Notes |
173
+ | ------------- | ------------------------ | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
174
+ | `url` | `string` | | Post URL. Platform components take `url` or `embed`; `<SocialEmbed>` takes `url` or `html`. |
175
+ | `embed` | `ParsedEmbed` | | Platform components only. |
176
+ | `html` | `string` | | `<SocialEmbed>` only. |
177
+ | `width` | `number \| string` | `'100%'` | Wrapper width. |
178
+ | `maxWidth` | `number` | per platform | The embed fills its container up to this width, in pixels. See [Platforms](#platforms). |
179
+ | `height` | `number` | | Iframe-based embeds only (YouTube, TikTok, Facebook video embed code): a fixed height instead of the aspect ratio. The others size themselves. |
180
+ | `maxHeight` | `number` | | Iframe-based embeds only: the embed is at most this tall, in pixels. It narrows to keep its aspect ratio, so a vertical video does not fill the column. |
181
+ | `placeholder` | `ReactNode` | a skeleton | Shown until the post has loaded. The default skeleton has a per-platform minimum height to limit layout shift. |
182
+ | `lazy` | `boolean` | `true` | Load the embed only when it comes within 200 px of the viewport. |
183
+ | `onLoad` | `() => void` | | Called once the platform has rendered the post (iframe embeds: when the iframe has loaded). |
184
+ | `onError` | `(error: Error) => void` | | Called on invalid input, a script that fails to load, or a post that does not render. Components never throw. |
185
+ | `...rest` | `ComponentProps<'div'>` | | Passed to the wrapper `div` (`className`, `style`, `data-*`, …). |
185
186
 
186
187
  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.
187
188
 
@@ -223,6 +224,15 @@ The rules:
223
224
 
224
225
  - **Posts** always render through the SDK, including when the embed code is the `plugins/post.php` iframe. That iframe's height only fits the `width` it was made for, so it cannot be responsive; the SDK resizes the post itself. `show_text` and `width` from the iframe carry over; `height` is dropped.
225
226
  - **Video embed code** (the `plugins/video.php` iframe) renders as that iframe, in a box with its aspect ratio. No script.
227
+ - **Video embed code that shows the post text** (`show_text=true`, the Embed dialog's "Include full post") renders through the SDK, like a post. The text below the player has its own height at every width, so no aspect ratio fits the iframe, and its own height cuts the text off. `show_text`, `allowfullscreen`, `autoplay` and `show_captions` carry over, `width` and `height` are kept as the player's size, and `t` is dropped.
228
+ - **That code without its text**: pass `showText={false}` to `FacebookEmbed`. The player's size is known from the embed code, so it renders as the plain iframe again, in a box of the player's aspect ratio. No script, and `maxHeight` applies:
229
+
230
+ ```tsx
231
+ <FacebookEmbed embed={embed} showText={false} maxHeight={640} />
232
+ ```
233
+
234
+ A 9:16 reel is then at most 358×638 px, where the same code with its text is 550×1233 px in a 550 px column. Facebook has no option for a shorter text: it is shown in full or not at all.
235
+
226
236
  - **Video URLs** and `fb-video` SDK code render through the SDK, which knows the video's real size (videos are both 16:9 and vertical).
227
237
 
228
238
  URL shapes, each tested against Facebook's plugins on `www.facebook.com`, `facebook.com` and `m.facebook.com`:
@@ -302,7 +312,7 @@ The platform's class (`twitter-tweet`, `instagram-media`, `text-post-media`) is
302
312
  The host does nothing to make embeds responsive:
303
313
 
304
314
  - **Script-based embeds** (X, Instagram, Threads, Facebook SDK) get a wrapper of `width: 100%` (or `width`) and `max-width: {maxWidth}px`, and the platform widget fills it. Their height is the platform's own.
305
- - **Iframe embeds** get a wrapper with `aspect-ratio` taken from the embed code's `width` and `height` (16:9 without them; 9:16 for TikTok and YouTube Shorts URLs), and the iframe fills it. `height` replaces the aspect ratio with a fixed height.
315
+ - **Iframe embeds** get a wrapper with `aspect-ratio` taken from the embed code's `width` and `height` (16:9 without them; 9:16 for TikTok and YouTube Shorts URLs), and the iframe fills it. `height` replaces the aspect ratio with a fixed height. `maxHeight` keeps the aspect ratio and lowers the wrapper's `max-width` to the widest box that is at most that tall.
306
316
 
307
317
  ## License
308
318
 
package/dist/index.d.ts CHANGED
@@ -23,10 +23,12 @@ type SharedProps = Omit<ComponentProps<'div'>, 'children' | 'onLoad' | 'onError'
23
23
  /** Fires on invalid input, script load failure or init timeout. */
24
24
  onError?: (error: Error) => void;
25
25
  };
26
- /** Iframe-based embeds (Group B) may also be given a fixed height. */
26
+ /** Iframe-based embeds (Group B) may also be given a fixed or a maximum height. */
27
27
  type FrameProps = SharedProps & {
28
28
  /** A fixed height in pixels, instead of the embed's aspect ratio. */
29
29
  height?: number;
30
+ /** The embed is at most this tall, in pixels: it narrows to keep its aspect ratio. */
31
+ maxHeight?: number;
30
32
  };
31
33
  /**
32
34
  * The plugin attributes Facebook documents, plus the SDK locale. `data-width` and
@@ -44,11 +46,13 @@ type FacebookOptions = {
44
46
  /** Video only. */
45
47
  showCaptions?: boolean;
46
48
  };
47
- /** `height` applies only to video embed code, which renders as an iframe. */
49
+ /** `height` and `maxHeight` apply only to a video that renders as an iframe. */
48
50
  type FacebookEmbedProps = EmbedSource & Omit<FrameProps, keyof FacebookOptions> & FacebookOptions;
49
51
  /**
50
52
  * Facebook rendering rules (brief §5.4): posts always through the SDK; video embed code as
51
53
  * the iframe that came in; video URLs through the SDK. The parser has set `variant`.
54
+ * Video embed code that shows the post text also goes through the SDK (D44), unless
55
+ * `showText` turns the text off: then it is the plain iframe (D45).
52
56
  */
53
57
  export declare function FacebookEmbed(props: FacebookEmbedProps): import("react").JSX.Element | null;
54
58
  type InstagramOptions = {
@@ -148,8 +152,8 @@ type SocialEmbedProps = SocialEmbedSource & Omit<FrameProps, PlatformOption> & {
148
152
  fallback?: (html: string) => ReactNode;
149
153
  };
150
154
  /**
151
- * Recognises embed code or a post URL and renders the platform's component. `height`
152
- * reaches only the iframe-based embeds; the others size themselves (brief §6.1).
155
+ * Recognises embed code or a post URL and renders the platform's component. `height` and
156
+ * `maxHeight` reach only the iframe-based embeds; the others size themselves (brief §6.1).
153
157
  */
154
- 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;
158
+ export declare function SocialEmbed(props: SocialEmbedProps): string | number | bigint | boolean | import("react").JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null | undefined;
155
159
  export type { EmbedSource, FacebookEmbedProps, FacebookOptions, FrameProps, InstagramEmbedProps, InstagramOptions, SharedProps, SocialEmbedProps, SocialEmbedSource, ThreadsEmbedProps, ThreadsOptions, TikTokEmbedProps, TikTokOptions, XEmbedProps, XOptions, YouTubeEmbedProps, YouTubeOptions };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { a as escapeAttribute, i as x$1, n as matchUrl, o as parseHtml, r as platforms, s as rebuild, t as parseEmbed } from "./parser-DVync2rz.js";
2
+ import { a as escapeAttribute, i as x$1, n as matchUrl, o as parseHtml, r as platforms, s as rebuild, t as parseEmbed } from "./parser-B-_erpaf.js";
3
3
  import { useEffect, useMemo, useRef, useState } from "react";
4
4
  import { jsx, jsxs } from "react/jsx-runtime";
5
5
  // src/loader/load-script.ts
@@ -331,18 +331,22 @@ const FILL = {
331
331
  * iframe filling it. The iframe is created on the client only, so its load event is
332
332
  * never missed before hydration; the placeholder covers the box until then.
333
333
  */
334
- function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size, width = "100%", maxWidth, height, placeholder, lazy = true, onLoad, style, ...rest }) {
334
+ function IframeEmbed({ src, title, allow, referrerPolicy, scrolling, ratio, size, width = "100%", maxWidth, height, maxHeight, placeholder, lazy = true, onLoad, style, ...rest }) {
335
335
  const frame = useRef(null);
336
336
  const near = useNearViewport(frame, lazy);
337
337
  const [loaded, setLoaded] = useState(false);
338
338
  const reported = useRef(false);
339
339
  const aspectWidth = size?.width ?? ratio.width;
340
340
  const aspectHeight = size?.height ?? ratio.height;
341
+ const widest = maxHeight === void 0 || height !== void 0 ? void 0 : Math.floor(maxHeight * aspectWidth / aspectHeight);
341
342
  const wrapper = {
342
343
  position: "relative",
343
344
  width,
344
- maxWidth,
345
- ...height === void 0 ? { aspectRatio: `${String(aspectWidth)} / ${String(aspectHeight)}` } : { height },
345
+ maxWidth: widest === void 0 || maxWidth !== void 0 && maxWidth < widest ? maxWidth : widest,
346
+ ...height === void 0 ? { aspectRatio: `${String(aspectWidth)} / ${String(aspectHeight)}` } : {
347
+ height,
348
+ maxHeight
349
+ },
346
350
  ...style
347
351
  };
348
352
  return /* @__PURE__ */ jsx("div", {
@@ -628,6 +632,13 @@ const ATTRIBUTES$1 = {
628
632
  autoplay: "data-autoplay",
629
633
  showCaptions: "data-show-captions"
630
634
  };
635
+ /** The video options under the names `plugins/video.php` reads them by. */
636
+ const PLAYER_PARAMETERS = {
637
+ showText: "show_text",
638
+ allowfullscreen: "allowfullscreen",
639
+ autoplay: "autoplay",
640
+ showCaptions: "show_captions"
641
+ };
631
642
  /** Embedded Posts: "Max. 750 pixel"; videos have no upper limit (docs/decisions.md D33). */
632
643
  const POST_MAX_WIDTH = 750;
633
644
  /** docs/decisions.md D34. */
@@ -638,15 +649,21 @@ const MIN_HEIGHT$3 = {
638
649
  /**
639
650
  * Facebook rendering rules (brief §5.4): posts always through the SDK; video embed code as
640
651
  * the iframe that came in; video URLs through the SDK. The parser has set `variant`.
652
+ * Video embed code that shows the post text also goes through the SDK (D44), unless
653
+ * `showText` turns the text off: then it is the plain iframe (D45).
641
654
  */
642
655
  function FacebookEmbed(props) {
643
- const { url, embed: given, onError, locale, showText, allowfullscreen, autoplay, showCaptions, height, ...shared } = props;
656
+ const { url, embed: given, onError, locale, showText, allowfullscreen, autoplay, showCaptions, height, maxHeight, ...shared } = props;
644
657
  const { embed, error } = resolveEmbed(url, given, "facebook");
645
658
  useReportError(error, onError);
646
659
  if (!embed) return null;
660
+ const frame = {
661
+ height,
662
+ maxHeight
663
+ };
647
664
  if (embed.variant === "iframe") return /* @__PURE__ */ jsx(FacebookVideoIframe, {
648
665
  ...shared,
649
- height,
666
+ ...frame,
650
667
  embed
651
668
  });
652
669
  const options = withProps(embed.options, {
@@ -655,6 +672,24 @@ function FacebookEmbed(props) {
655
672
  autoplay,
656
673
  showCaptions
657
674
  });
675
+ const { size } = embed;
676
+ if (embed.kind === "video" && options.showText === false && size?.width !== void 0 && size.height !== void 0) {
677
+ const parameters = {};
678
+ for (const [option, name] of Object.entries(PLAYER_PARAMETERS)) {
679
+ const value = options[option];
680
+ if (value !== void 0) parameters[name] = value;
681
+ }
682
+ parameters.width = size.width;
683
+ parameters.height = size.height;
684
+ return /* @__PURE__ */ jsx(FacebookVideoIframe, {
685
+ ...shared,
686
+ ...frame,
687
+ embed: {
688
+ ...embed,
689
+ options: parameters
690
+ }
691
+ });
692
+ }
658
693
  const sdkLocale = locale ?? (typeof options.locale === "string" ? options.locale : "en_US");
659
694
  const markup = `<div class="${embed.kind === "video" ? "fb-video" : "fb-post"}" data-href="${escapeAttribute(embed.url)}"${dataAttributes(ATTRIBUTES$1, options)}>${embed.fallbackHtml ?? ""}</div>`;
660
695
  return /* @__PURE__ */ jsx(ScriptEmbed, {
@@ -891,11 +926,15 @@ function YouTubeEmbed(props) {
891
926
  }
892
927
  // src/components/social-embed.tsx
893
928
  /**
894
- * Recognises embed code or a post URL and renders the platform's component. `height`
895
- * reaches only the iframe-based embeds; the others size themselves (brief §6.1).
929
+ * Recognises embed code or a post URL and renders the platform's component. `height` and
930
+ * `maxHeight` reach only the iframe-based embeds; the others size themselves (brief §6.1).
896
931
  */
897
932
  function SocialEmbed(props) {
898
- const { html, url, fallback, height, onError, ...shared } = props;
933
+ const { html, url, fallback, height, maxHeight, onError, ...shared } = props;
934
+ const frame = {
935
+ height,
936
+ maxHeight
937
+ };
899
938
  const embed = useMemo(() => html !== void 0 ? parseEmbed(html) : matchUrl(url), [html, url]);
900
939
  useReportError(embed === null && !(html !== void 0 && fallback) ? html !== void 0 ? "social-embeds: unrecognised embed code" : `social-embeds: not a supported post URL: ${url}` : void 0, onError);
901
940
  if (!embed) return html !== void 0 && fallback ? fallback(html) : null;
@@ -917,19 +956,19 @@ function SocialEmbed(props) {
917
956
  });
918
957
  case "facebook": return /* @__PURE__ */ jsx(FacebookEmbed, {
919
958
  ...shared,
920
- height,
959
+ ...frame,
921
960
  onError,
922
961
  embed
923
962
  });
924
963
  case "tiktok": return /* @__PURE__ */ jsx(TikTokEmbed, {
925
964
  ...shared,
926
- height,
965
+ ...frame,
927
966
  onError,
928
967
  embed
929
968
  });
930
969
  case "youtube": return /* @__PURE__ */ jsx(YouTubeEmbed, {
931
970
  ...shared,
932
- height,
971
+ ...frame,
933
972
  onError,
934
973
  embed
935
974
  });
@@ -264,6 +264,12 @@ const OPTION_ATTRIBUTES$2 = [
264
264
  ["data-autoplay", "autoplay"],
265
265
  ["data-show-captions", "showCaptions"]
266
266
  ];
267
+ const VIDEO_SDK_PARAMETERS = [
268
+ ["show_text", "showText"],
269
+ ["allowfullscreen", "allowfullscreen"],
270
+ ["autoplay", "autoplay"],
271
+ ["show_captions", "showCaptions"]
272
+ ];
267
273
  const PARSE_IGNORE = {
268
274
  name: "class",
269
275
  value: /^fb-xfbml-parse-ignore$/
@@ -368,7 +374,26 @@ const facebook = {
368
374
  if (width !== void 0) options.width = coerce(width);
369
375
  return embed(found, "post", "sdk", options);
370
376
  }
371
- if (src.path === "/plugins/video.php") return withSize(embed(found, "video", "iframe", Object.fromEntries([...src.query].filter(([key]) => key !== "href").map(([key, value]) => [key, coerce(value)]))), sizeOf(root));
377
+ if (src.path === "/plugins/video.php") {
378
+ const showText = coerce(src.query.get("show_text") ?? "");
379
+ if (showText === true || showText === 1) {
380
+ const options = {};
381
+ for (const [parameter, key] of VIDEO_SDK_PARAMETERS) {
382
+ const value = src.query.get(parameter);
383
+ if (value !== void 0) options[key] = coerce(value);
384
+ }
385
+ const width = pixels(src.query.get("width"));
386
+ const height = pixels(src.query.get("height"));
387
+ return withSize(embed(found, "video", "sdk", {
388
+ ...options,
389
+ showText: true
390
+ }), width === void 0 || height === void 0 ? void 0 : {
391
+ width,
392
+ height
393
+ });
394
+ }
395
+ return withSize(embed(found, "video", "iframe", Object.fromEntries([...src.query].filter(([key]) => key !== "href").map(([key, value]) => [key, coerce(value)]))), sizeOf(root));
396
+ }
372
397
  return null;
373
398
  },
374
399
  isOwnScript: (src) => SCRIPT$4.test(src),
package/dist/parser.js CHANGED
@@ -1 +1 @@
1
- export { n as matchUrl, t as parseEmbed } from "./parser-DVync2rz.js";
1
+ export { n as matchUrl, t as parseEmbed } from "./parser-B-_erpaf.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "social-embeds",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Turn social media embed code or post URLs into working React components.",
5
5
  "license": "MIT",
6
6
  "author": "Atanas Mitrev",