From c25ec3be2c2bd4631d4d295b0e0b079ef09360bd Mon Sep 17 00:00:00 2001 From: sage Date: Wed, 9 Feb 2022 12:09:45 +0700 Subject: [PATCH] :ambulance: Resize iframe with ResizeObserver --- packages/@shared/util.ts | 13 ++++++++++-- packages/react/lib/Giscus.tsx | 17 ++++------------ packages/react/package.json | 4 +--- packages/svelte/lib/Giscus.svelte | 33 +++++++------------------------ packages/svelte/package.json | 2 -- packages/vue/lib/Giscus.tsx | 27 ++++++------------------- packages/vue/package.json | 2 -- yarn.lock | 10 ---------- 8 files changed, 29 insertions(+), 79 deletions(-) diff --git a/packages/@shared/util.ts b/packages/@shared/util.ts index cf6ce6d4..ce7f231f 100644 --- a/packages/@shared/util.ts +++ b/packages/@shared/util.ts @@ -97,12 +97,21 @@ export function addDefaultStyles() { document.head.prepend(style) } -export function createErrorMessageListener(resetSession: () => void) { +export function createErrorMessageListener( + resetSession: () => void, + iframe: HTMLIFrameElement | null = null +) { return function (event: MessageEvent) { if (event.origin !== GISCUS_ORIGIN) return const { data } = event - if (!(typeof data === 'object' && data?.giscus?.error)) return + if (!(typeof data === 'object' && data.giscus)) return + + if (iframe && data.giscus.resizeHeight) { + iframe.style.height = `${data.giscus.resizeHeight}px` + } + + if (!data.giscus.error) return const message: string = data.giscus.error diff --git a/packages/react/lib/Giscus.tsx b/packages/react/lib/Giscus.tsx index 6d008071..1bf7cd3c 100644 --- a/packages/react/lib/Giscus.tsx +++ b/packages/react/lib/Giscus.tsx @@ -5,10 +5,8 @@ import { createErrorMessageListener, formatError, getIframeSrc, - GISCUS_ORIGIN, GISCUS_SESSION_KEY } from '@shared/util' -import iFrameResizer from 'iframe-resizer' function GiscusClient(props: GiscusProps) { const [session, setSession] = useState('') @@ -43,21 +41,14 @@ function GiscusClient(props: GiscusProps) { useEffect(addDefaultStyles, []) useEffect(() => { - const listener = createErrorMessageListener(() => setSession('')) + const listener = createErrorMessageListener( + () => setSession(''), + iframe.current + ) window.addEventListener('message', listener) return () => window.removeEventListener('message', listener) }, []) - useEffect(() => { - if (!iframe.current) return - iframe.current.addEventListener('load', () => - iFrameResizer.iframeResizer( - { checkOrigin: [GISCUS_ORIGIN] }, - iframe.current as HTMLIFrameElement - ) - ) - }, [src]) - return (