diff --git a/src/components/atoms/Plugin/IFrame/hooks.ts b/src/components/atoms/Plugin/IFrame/hooks.ts index f4072f0ea..901af2acb 100644 --- a/src/components/atoms/Plugin/IFrame/hooks.ts +++ b/src/components/atoms/Plugin/IFrame/hooks.ts @@ -1,9 +1,11 @@ import { + IframeHTMLAttributes, Ref, RefObject, useCallback, useEffect, useImperativeHandle, + useMemo, useRef, useState, } from "react"; @@ -17,6 +19,8 @@ export default function useHook({ html, ref, autoResize, + visible, + iFrameProps, onLoad, onMessage, }: { @@ -24,13 +28,14 @@ export default function useHook({ html?: string; ref?: Ref; autoResize?: boolean; + visible?: boolean; + iFrameProps?: IframeHTMLAttributes; onLoad?: () => void; onMessage?: (message: any) => void; } = {}): { ref: RefObject; + props: IframeHTMLAttributes; onLoad?: () => void; - width?: string; - height?: string; } { const loaded = useRef(false); const iFrameRef = useRef(null); @@ -86,7 +91,7 @@ export default function useHook({ horizontalMargin = parseInt(st.getPropertyValue("margin-left")) + parseInt(st.getPropertyValue("margin-right")); verticalMargin = parseInt(st.getPropertyValue("margin-top")) + parseInt(st.getPropertyValue("margin-bottom")); const resize = { - width: el.offsetWidth + horizontalMargin, + width: el.offsetWidth + horizontalMargin, height: el.offsetHeight + verticalMargin, }; parent.postMessage({ @@ -126,10 +131,23 @@ export default function useHook({ onLoad?.(); }, [autoResizeMessageKey, html, onLoad]); + const props = useMemo>( + () => ({ + style: { + display: visible ? undefined : "none", + width: visible ? (autoResize ? iFrameSize?.[0] : "100%") : "0px", + height: visible ? (autoResize ? iFrameSize?.[1] : "100%") : "0px", + minWidth: "100%", + ...iFrameProps?.style, + }, + ...iFrameProps, + }), + [autoResize, iFrameProps, iFrameSize, visible], + ); + return { ref: iFrameRef, - width: iFrameSize?.[0], - height: iFrameSize?.[1], + props, onLoad: onIframeLoad, }; } diff --git a/src/components/atoms/Plugin/IFrame/index.stories.tsx b/src/components/atoms/Plugin/IFrame/index.stories.tsx index 687a05d91..5744fae78 100644 --- a/src/components/atoms/Plugin/IFrame/index.stories.tsx +++ b/src/components/atoms/Plugin/IFrame/index.stories.tsx @@ -31,9 +31,11 @@ export const Default: Story = args => { Default.args = { autoResize: false, visible: true, - style: { - width: "400px", - height: "300px", + iFrameProps: { + style: { + width: "400px", + height: "300px", + }, }, html: `

iframe