-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8cb188
commit 5c0f3c4
Showing
5 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, test, expect, jest } from "@jest/globals"; | ||
import { act } from "react-dom/test-utils"; | ||
import { fireEvent } from "@testing-library/dom"; | ||
import "./"; | ||
import type { Iframe } from "./index.js"; | ||
|
||
jest.mock("@next-core/theme", () => ({})); | ||
|
||
describe("eo-iframe", () => { | ||
test("basic usage", async () => { | ||
const element = document.createElement("eo-iframe") as Iframe; | ||
element.src = "http://localhost/iframe"; | ||
|
||
const onLoad = jest.fn(); | ||
element.addEventListener("load", onLoad); | ||
|
||
act(() => { | ||
document.body.appendChild(element); | ||
}); | ||
expect(element.shadowRoot?.childNodes.length).toBeGreaterThan(1); | ||
|
||
const iframe = element.shadowRoot?.querySelector( | ||
"iframe" | ||
) as HTMLIFrameElement; | ||
fireEvent.load(iframe); | ||
expect(onLoad).toBeCalledTimes(1); | ||
|
||
const mockPostMessage = jest.fn(); | ||
Object.defineProperty(iframe, "contentWindow", { | ||
get() { | ||
return { | ||
postMessage: mockPostMessage, | ||
} as any; | ||
}, | ||
}); | ||
element.postMessage("hello", location.origin); | ||
expect(mockPostMessage).toBeCalledWith("hello", location.origin); | ||
|
||
act(() => { | ||
document.body.removeChild(element); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React, { | ||
createRef, | ||
forwardRef, | ||
useEffect, | ||
useImperativeHandle, | ||
useRef, | ||
type CSSProperties, | ||
type Ref, | ||
} from "react"; | ||
import { createDecorators, type EventEmitter } from "@next-core/element"; | ||
import { ReactNextElement } from "@next-core/react-element"; | ||
import styleText from "./styles.shadow.css"; | ||
|
||
const { defineElement, property, event, method } = createDecorators(); | ||
|
||
export interface IframeProps { | ||
src?: string; | ||
iframeStyle?: CSSProperties; | ||
} | ||
|
||
type PostMessageParameters = | ||
| [message: unknown, targetOrigin: string, transfer?: Transferable[]] | ||
| [message: unknown, options?: WindowPostMessageOptions]; | ||
|
||
interface IframeRef { | ||
postMessage: (...args: PostMessageParameters) => void; | ||
} | ||
|
||
const IframeComponent = forwardRef<IframeRef, IframeComponentProps>( | ||
LegacyIframeComponent | ||
); | ||
|
||
/** | ||
* 构件 `eo-iframe` | ||
*/ | ||
export | ||
@defineElement("eo-iframe", { | ||
styleTexts: [styleText], | ||
}) | ||
class Iframe extends ReactNextElement implements IframeProps { | ||
/** | ||
* @required | ||
*/ | ||
@property() accessor src: string | undefined; | ||
|
||
/** | ||
* Default style: | ||
* | ||
* ```css | ||
* iframe { | ||
* margin: 0; | ||
* border: 0; | ||
* padding: 0; | ||
* width: 100%; | ||
* height: 100%; | ||
* vertical-align: top; | ||
* } | ||
* ``` | ||
*/ | ||
@property({ attribute: false }) | ||
accessor iframeStyle: CSSProperties | undefined; | ||
|
||
@event({ type: "load" }) | ||
accessor #loadEvent!: EventEmitter<void>; | ||
|
||
#handleLoad = () => { | ||
this.#loadEvent.emit(); | ||
}; | ||
|
||
#iframeRef = createRef<IframeRef>(); | ||
|
||
@method() | ||
postMessage(...args: PostMessageParameters) { | ||
this.#iframeRef.current?.postMessage(...args); | ||
} | ||
|
||
render() { | ||
return ( | ||
<IframeComponent | ||
src={this.src} | ||
iframeStyle={this.iframeStyle} | ||
onLoad={this.#handleLoad} | ||
ref={this.#iframeRef} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export interface IframeComponentProps extends IframeProps { | ||
onLoad: () => void; | ||
} | ||
|
||
export function LegacyIframeComponent( | ||
{ src, iframeStyle, onLoad }: IframeComponentProps, | ||
ref: Ref<IframeRef> | ||
) { | ||
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
postMessage(...args: PostMessageParameters) { | ||
iframeRef.current?.contentWindow?.postMessage( | ||
...(args as Parameters<Window["postMessage"]>) | ||
); | ||
}, | ||
}), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
const iframe = iframeRef.current; | ||
iframe?.addEventListener("load", onLoad); | ||
return () => { | ||
iframe?.removeEventListener("load", onLoad); | ||
}; | ||
}, [onLoad]); | ||
|
||
return <iframe src={src} style={iframeStyle} ref={iframeRef} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
:host { | ||
display: block; | ||
} | ||
|
||
:host([hidden]) { | ||
display: none; | ||
} | ||
|
||
iframe { | ||
border: 0; | ||
margin: 0; | ||
padding: 0; | ||
width: 100%; | ||
height: 100%; | ||
vertical-align: top; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters