Created
May 5, 2025 18:06
-
-
Save bmingles/02451e09d218cdbe61d8c506e556562b to your computer and use it in GitHub Desktop.
Deephaven Theme - Iframe Tester
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Deephaven Iframe Tester</title> | |
<style> | |
html, | |
body { | |
background-color: green; | |
margin: 0; | |
font-family: sans-serif; | |
} | |
body::before { | |
color: white; | |
font-size: 2rem; | |
position: absolute; | |
top: 0; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
content: 'Parent Container'; | |
width: 100%; | |
height: 100%; | |
} | |
iframe { | |
display: block; | |
position: relative; | |
width: 100vw; | |
height: 100vh; | |
border: none; | |
} | |
#btnUpdateTheme { | |
position: absolute; | |
bottom: 0; | |
padding: 0.5rem 1rem; | |
} | |
</style> | |
</head> | |
<body> | |
<iframe | |
src="http://localhost:4000?theme=external-theme&preloadTransparentTheme=true" | |
></iframe> | |
<button id="btnUpdateTheme" onclick="onUpdateClick()">Update Theme</button> | |
<script> | |
const MSG_REQUEST_GET_THEME = | |
'io.deephaven.message.ThemeModel.requestExternalTheme'; | |
const MSG_REQUEST_SET_THEME = | |
'io.deephaven.message.ThemeModel.requestSetTheme'; | |
const iframeEl = document.querySelector('iframe'); | |
const updateBtnEl = document.getElementById('btnUpdateTheme'); | |
const childWindow = iframeEl.contentWindow; | |
window.addEventListener('message', onMessage); | |
updateBtnEl.addEventListener('click', onUpdateClick); | |
/** Handle postMessage events from the child iframe */ | |
function onMessage({ data, origin, source }) { | |
if (origin !== 'http://localhost:4000') { | |
return; | |
} | |
if (data.message === MSG_REQUEST_GET_THEME) { | |
source.postMessage( | |
{ | |
id: data.id, | |
payload: { | |
name: 'Iframe External Theme', | |
cssVars: { | |
'--dh-color-bg': 'red', | |
}, | |
}, | |
}, | |
origin | |
); | |
} | |
} | |
/** Handle click on the update button */ | |
function onUpdateClick() { | |
childWindow?.postMessage( | |
{ | |
message: MSG_REQUEST_SET_THEME, | |
payload: { | |
name: 'Iframe External Theme', | |
cssVars: { | |
'--dh-color-bg': getRandomHexColor(), | |
}, | |
}, | |
}, | |
'http://localhost:4000' | |
); | |
} | |
function getRandomHexColor() { | |
return `#${Math.floor(Math.random() * 16777215) | |
.toString(16) | |
.padStart(6, '0')}`; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment