|
| 1 | +import React, { |
| 2 | + createRef, |
| 3 | + forwardRef, |
| 4 | + useEffect, |
| 5 | + useImperativeHandle, |
| 6 | + useRef, |
| 7 | + type CSSProperties, |
| 8 | + type Ref, |
| 9 | +} from "react"; |
| 10 | +import { createDecorators, type EventEmitter } from "@next-core/element"; |
| 11 | +import { ReactNextElement } from "@next-core/react-element"; |
| 12 | +import styleText from "./styles.shadow.css"; |
| 13 | + |
| 14 | +const { defineElement, property, event, method } = createDecorators(); |
| 15 | + |
| 16 | +export interface IframeProps { |
| 17 | + src?: string; |
| 18 | + iframeStyle?: CSSProperties; |
| 19 | +} |
| 20 | + |
| 21 | +type PostMessageParameters = |
| 22 | + | [message: unknown, targetOrigin: string, transfer?: Transferable[]] |
| 23 | + | [message: unknown, options?: WindowPostMessageOptions]; |
| 24 | + |
| 25 | +interface IframeRef { |
| 26 | + postMessage: (...args: PostMessageParameters) => void; |
| 27 | +} |
| 28 | + |
| 29 | +const IframeComponent = forwardRef<IframeRef, IframeComponentProps>( |
| 30 | + LegacyIframeComponent |
| 31 | +); |
| 32 | + |
| 33 | +/** |
| 34 | + * 构件 `eo-iframe` |
| 35 | + */ |
| 36 | +export |
| 37 | +@defineElement("eo-iframe", { |
| 38 | + styleTexts: [styleText], |
| 39 | +}) |
| 40 | +class Iframe extends ReactNextElement implements IframeProps { |
| 41 | + /** |
| 42 | + * @required |
| 43 | + */ |
| 44 | + @property() accessor src: string | undefined; |
| 45 | + |
| 46 | + /** |
| 47 | + * Default style: |
| 48 | + * |
| 49 | + * ```css |
| 50 | + * iframe { |
| 51 | + * margin: 0; |
| 52 | + * border: 0; |
| 53 | + * padding: 0; |
| 54 | + * width: 100%; |
| 55 | + * height: 100%; |
| 56 | + * vertical-align: top; |
| 57 | + * } |
| 58 | + * ``` |
| 59 | + */ |
| 60 | + @property({ attribute: false }) |
| 61 | + accessor iframeStyle: CSSProperties | undefined; |
| 62 | + |
| 63 | + @event({ type: "load" }) |
| 64 | + accessor #loadEvent!: EventEmitter<void>; |
| 65 | + |
| 66 | + #handleLoad = () => { |
| 67 | + this.#loadEvent.emit(); |
| 68 | + }; |
| 69 | + |
| 70 | + #iframeRef = createRef<IframeRef>(); |
| 71 | + |
| 72 | + @method() |
| 73 | + postMessage(...args: PostMessageParameters) { |
| 74 | + this.#iframeRef.current?.postMessage(...args); |
| 75 | + } |
| 76 | + |
| 77 | + render() { |
| 78 | + return ( |
| 79 | + <IframeComponent |
| 80 | + src={this.src} |
| 81 | + iframeStyle={this.iframeStyle} |
| 82 | + onLoad={this.#handleLoad} |
| 83 | + ref={this.#iframeRef} |
| 84 | + /> |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export interface IframeComponentProps extends IframeProps { |
| 90 | + onLoad: () => void; |
| 91 | +} |
| 92 | + |
| 93 | +export function LegacyIframeComponent( |
| 94 | + { src, iframeStyle, onLoad }: IframeComponentProps, |
| 95 | + ref: Ref<IframeRef> |
| 96 | +) { |
| 97 | + const iframeRef = useRef<HTMLIFrameElement>(null); |
| 98 | + |
| 99 | + useImperativeHandle( |
| 100 | + ref, |
| 101 | + () => ({ |
| 102 | + postMessage(...args: PostMessageParameters) { |
| 103 | + iframeRef.current?.contentWindow?.postMessage( |
| 104 | + ...(args as Parameters<Window["postMessage"]>) |
| 105 | + ); |
| 106 | + }, |
| 107 | + }), |
| 108 | + [] |
| 109 | + ); |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + const iframe = iframeRef.current; |
| 113 | + iframe?.addEventListener("load", onLoad); |
| 114 | + return () => { |
| 115 | + iframe?.removeEventListener("load", onLoad); |
| 116 | + }; |
| 117 | + }, [onLoad]); |
| 118 | + |
| 119 | + return <iframe src={src} style={iframeStyle} ref={iframeRef} />; |
| 120 | +} |
0 commit comments