|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import React, { ReactNode } from 'react'; |
| 4 | +import React, { ReactNode, useEffect, useRef } from 'react'; |
5 | 5 | import { METADATA_ATTRIBUTE, getAnalyticsMetadataAttribute, getAnalyticsLabelAttribute } from '../attributes'; |
| 6 | +import ReactDOM from 'react-dom'; |
6 | 7 |
|
7 | 8 | export const ComponentOne = ({ malformed }: { malformed?: boolean }) => ( |
8 | 9 | <div |
@@ -65,3 +66,82 @@ export const ComponentThree = ({ children }: { children?: ReactNode }) => ( |
65 | 66 | </div> |
66 | 67 | </div> |
67 | 68 | ); |
| 69 | + |
| 70 | +const NestedIframe = () => { |
| 71 | + const ref = useRef<HTMLDivElement>(null); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + const container = ref.current; |
| 75 | + if (!container) { |
| 76 | + return; |
| 77 | + } |
| 78 | + const iframeEl = container.ownerDocument.createElement('iframe'); |
| 79 | + iframeEl.id = 'iframe-2'; |
| 80 | + container.appendChild(iframeEl); |
| 81 | + |
| 82 | + const iframeDocument = iframeEl.contentDocument!; |
| 83 | + iframeDocument.open(); |
| 84 | + iframeDocument.writeln('<!DOCTYPE html>'); |
| 85 | + iframeDocument.close(); |
| 86 | + iframeDocument.body.innerHTML = |
| 87 | + '<div><div data-awsui-analytics="{"component":{"name":"ComponentThree"}}"><div id="sub-sub-target">inside iframe inside iframe</div><div id="id:portal-2"></div></div><div data-awsui-referrer-id="id:portal-2"><div data-awsui-analytics="{"component":{"name":"ComponentThreeInPortal"}}"> </div></div></div>'; |
| 88 | + |
| 89 | + return () => { |
| 90 | + container.removeChild(iframeEl); |
| 91 | + }; |
| 92 | + }); |
| 93 | + |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <h1>Nested title</h1> |
| 97 | + <div |
| 98 | + {...getAnalyticsMetadataAttribute({ |
| 99 | + component: { name: 'ComponentTwo', label: { selector: 'h1', root: 'body' } }, |
| 100 | + })} |
| 101 | + > |
| 102 | + <div>inside iframe</div> |
| 103 | + <div id="sub-target"> |
| 104 | + <div ref={ref}></div>;<div id="id:portal-1"></div> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + <div data-awsui-referrer-id="id:portal-1"> |
| 108 | + <div {...getAnalyticsMetadataAttribute({ component: { name: 'ComponentTwoInPortal' } })}> </div> |
| 109 | + </div> |
| 110 | + </> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export const AppWithIframe = () => { |
| 115 | + const ref = useRef<HTMLDivElement>(null); |
| 116 | + |
| 117 | + useEffect(() => { |
| 118 | + const container = ref.current; |
| 119 | + if (!container) { |
| 120 | + return; |
| 121 | + } |
| 122 | + const iframeEl = container.ownerDocument.createElement('iframe'); |
| 123 | + iframeEl.id = 'iframe-1'; |
| 124 | + container.appendChild(iframeEl); |
| 125 | + |
| 126 | + const iframeDocument = iframeEl.contentDocument!; |
| 127 | + iframeDocument.open(); |
| 128 | + iframeDocument.writeln('<!DOCTYPE html>'); |
| 129 | + iframeDocument.close(); |
| 130 | + |
| 131 | + const innerAppRoot = iframeDocument.createElement('div'); |
| 132 | + iframeDocument.body.appendChild(innerAppRoot); |
| 133 | + ReactDOM.render(<NestedIframe />, innerAppRoot); |
| 134 | + return () => { |
| 135 | + ReactDOM.unmountComponentAtNode(innerAppRoot); |
| 136 | + container.removeChild(iframeEl); |
| 137 | + }; |
| 138 | + }); |
| 139 | + |
| 140 | + return ( |
| 141 | + <div {...getAnalyticsMetadataAttribute({ component: { name: 'ComponentOne' } })}> |
| 142 | + <h1>Main title</h1> |
| 143 | + <div ref={ref}></div>; |
| 144 | + <iframe src="https://www.amazon.com/" /> |
| 145 | + </div> |
| 146 | + ); |
| 147 | +}; |
0 commit comments