give
/
src
/
DonationForms
/
resources
/
registrars
/
templates
/
fields
/
Consent
/
createIframePortal.tsx
ConsentModal.tsx
2 years ago
ShowTerms.tsx
2 years ago
createIframePortal.tsx
1 year ago
index.tsx
2 years ago
styles.scss
2 years ago
createIframePortal.tsx
121 lines
| 1 | import {createPortal} from 'react-dom'; |
| 2 | import {createRoot} from 'react-dom/client'; |
| 3 | import {useEffect, useRef} from 'react'; |
| 4 | |
| 5 | import './styles.scss'; |
| 6 | |
| 7 | /** |
| 8 | * @since 3.14.0 |
| 9 | * Creates a portal to the Top Level document, rendering children elements within an iframe. |
| 10 | */ |
| 11 | export default function createIframePortal(children, targetElement = window.top.document.body) { |
| 12 | const iframeRef = useRef<HTMLIFrameElement | null>(null); |
| 13 | const rootRef = useRef<any>(null); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | const iframe = iframeRef.current; |
| 17 | if (!iframe) return; |
| 18 | |
| 19 | // Wait for iframe to be ready |
| 20 | const setupIframe = () => { |
| 21 | const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; |
| 22 | |
| 23 | // Clear existing content |
| 24 | iframeDoc.open(); |
| 25 | iframeDoc.write('<!DOCTYPE html><html><head></head><body></body></html>'); |
| 26 | iframeDoc.close(); |
| 27 | |
| 28 | async function renderContent() { |
| 29 | try { |
| 30 | await fetchStylesheets(iframeDoc); |
| 31 | // Create a container in the iframe |
| 32 | const iframeContainer = iframeDoc.createElement('div'); |
| 33 | iframeContainer.id = 'givewp-fields-consent-iframe-container'; |
| 34 | iframeDoc.body.appendChild(iframeContainer); |
| 35 | |
| 36 | // Create root and render |
| 37 | rootRef.current = createRoot(iframeContainer); |
| 38 | rootRef.current.render(children); |
| 39 | } catch (error) { |
| 40 | console.error('Error loading stylesheets:', error); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | renderContent(); |
| 45 | }; |
| 46 | |
| 47 | iframe.onload = setupIframe; |
| 48 | |
| 49 | if (iframe.contentDocument?.readyState === 'complete') { |
| 50 | setupIframe(); |
| 51 | } |
| 52 | }, [children, targetElement]); |
| 53 | |
| 54 | return createPortal( |
| 55 | <iframe |
| 56 | ref={iframeRef} |
| 57 | id={'givewp-fields-consent-iframe-portal'} |
| 58 | style={{ |
| 59 | position: 'fixed', |
| 60 | border: 'none', |
| 61 | top: '0', |
| 62 | left: '0', |
| 63 | minHeight: '100%', |
| 64 | minWidth: '100%', |
| 65 | // Required to be visible in the Visual Form Builder. |
| 66 | zIndex: '9999999999', |
| 67 | }} |
| 68 | />, |
| 69 | targetElement |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 3.14.0 |
| 75 | * Fetches stylesheets from the originating document and injects them into the new iframe document's head. |
| 76 | * This allows user provided styles to be applied to the iframe content. |
| 77 | * Returns a promise that resolves when all stylesheets are loaded. |
| 78 | */ |
| 79 | export async function fetchStylesheets(iframeDoc: Document) { |
| 80 | const styleSheets = Array.from(document.styleSheets); |
| 81 | |
| 82 | // Promisify the loading of each stylesheet |
| 83 | const loadStylesheet = (styleSheet) => { |
| 84 | return new Promise<void>((resolve, reject) => { |
| 85 | try { |
| 86 | if (styleSheet.href) { |
| 87 | // For external stylesheets |
| 88 | const newLink = document.createElement('link'); |
| 89 | newLink.rel = 'stylesheet'; |
| 90 | newLink.href = styleSheet.href; |
| 91 | newLink.onload = () => resolve(); |
| 92 | newLink.onerror = reject; |
| 93 | iframeDoc.head.appendChild(newLink); |
| 94 | } else if (styleSheet.cssRules) { |
| 95 | // For <style/> tags |
| 96 | const newStyle = document.createElement('style'); |
| 97 | Array.from(styleSheet.cssRules).forEach((rule: {cssText: string}) => { |
| 98 | newStyle.appendChild(document.createTextNode(rule.cssText)); |
| 99 | }); |
| 100 | iframeDoc.head.appendChild(newStyle); |
| 101 | resolve(); |
| 102 | } else { |
| 103 | const styleElement = styleSheet.ownerNode; |
| 104 | if (styleElement) { |
| 105 | const clonedStyle = styleElement.cloneNode(true); |
| 106 | iframeDoc.head.appendChild(clonedStyle); |
| 107 | resolve(); |
| 108 | } else { |
| 109 | resolve(); |
| 110 | } |
| 111 | } |
| 112 | } catch (error) { |
| 113 | reject(error); |
| 114 | } |
| 115 | }); |
| 116 | }; |
| 117 | |
| 118 | const promises = styleSheets.map(loadStylesheet); |
| 119 | return await Promise.all(promises); |
| 120 | } |
| 121 |