social-embeds 0.5.2 → 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 CHANGED
@@ -273,7 +273,7 @@ A component checks an `embed` before using it: its `url` must be the exact URL `
273
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. |
274
274
  | `...rest` | `ComponentProps<'div'>` | | Passed to the wrapper `div` (`className`, `style`, `data-*`, …). |
275
275
 
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. Bluesky's and Tumblr's frames report only once they are painted, so with `lazy={false}` an embed far below the fold can report this before it is scrolled to, and then still take its height.
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}`.
277
277
 
278
278
  ## Platforms
279
279
 
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 | 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;
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
- let heard = false;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "social-embeds",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Turn social media embed code or post URLs into working React components.",
5
5
  "keywords": [
6
6
  "react",