This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into reactor_cleanup_09222024
- Loading branch information
Showing
6 changed files
with
415 additions
and
263 deletions.
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
123 changes: 123 additions & 0 deletions
123
apps/mocksi-lite-next/src/pages/content/main-iframe.tsx
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,123 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { LayoutEvents } from "../events"; | ||
|
||
export enum IframePosition { | ||
BOTTOM_CENTER = "BOTTOM_CENTER", | ||
TOP_RIGHT = "TOP_RIGHT", | ||
BOTTOM_RIGHT = "BOTTOM_RIGHT", | ||
} | ||
|
||
export interface IframeResizeArgs { | ||
height: number; | ||
id?: string; | ||
position: | ||
| IframePosition.BOTTOM_CENTER | ||
| IframePosition.BOTTOM_RIGHT | ||
| IframePosition.TOP_RIGHT; | ||
width: number; | ||
} | ||
|
||
function getIframeStyles({ height, position, width }: IframeResizeArgs) { | ||
if (!height || !width || !position) { | ||
console.error( | ||
"Cannot update iframe size / position, make sure 'request.data.iframe' has 'height', 'width', and 'position' set correctly", | ||
); | ||
return; | ||
} | ||
|
||
const bounds = document.body.getBoundingClientRect(); | ||
|
||
let styles = {}; | ||
switch (position) { | ||
case IframePosition.BOTTOM_CENTER: | ||
styles = { | ||
bottom: "0px", | ||
right: `${bounds.width / 2 - width / 2}px`, | ||
top: "auto", | ||
}; | ||
break; | ||
case IframePosition.BOTTOM_RIGHT: | ||
styles = { | ||
bottom: "10px", | ||
right: "10px", | ||
top: "auto", | ||
}; | ||
break; | ||
case IframePosition.TOP_RIGHT: | ||
styles = { | ||
bottom: "auto", | ||
display: "block", | ||
right: "10px", | ||
top: "10px", | ||
}; | ||
break; | ||
} | ||
|
||
return Object.assign( | ||
{ | ||
bottom: "auto", | ||
display: "block", | ||
height: `${height}px`, | ||
left: "auto", | ||
right: "auto", | ||
top: "auto", | ||
width: `${width}px`, | ||
}, | ||
styles, | ||
); | ||
} | ||
|
||
function MainIframe() { | ||
const iframeRef = React.useRef<HTMLIFrameElement>(null); | ||
|
||
React.useEffect(() => { | ||
chrome.runtime.onMessage.addListener((request) => { | ||
if (iframeRef.current) { | ||
switch (request.message) { | ||
case LayoutEvents.HIDE: | ||
iframeRef.current.style.display = "none"; | ||
break; | ||
case LayoutEvents.RESIZE: | ||
const styles = getIframeStyles(request.data.iframe); | ||
Object.assign(iframeRef.current.style, styles); | ||
break; | ||
case LayoutEvents.SHOW: | ||
iframeRef.current.style.display = "block"; | ||
break; | ||
} | ||
} | ||
return true; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{ReactDOM.createPortal( | ||
<iframe | ||
ref={iframeRef} | ||
seamless={true} | ||
src={`${import.meta.env.VITE_NEST_APP}/extension/main`} | ||
style={{ | ||
backgroundColor: "transparent", | ||
border: "none", | ||
bottom: "10px", | ||
boxShadow: "none", | ||
colorScheme: "light", | ||
display: "block", | ||
height: "600px", | ||
left: "auto", | ||
position: "fixed", | ||
right: "10px", | ||
top: "auto", | ||
width: "500px", | ||
zIndex: 99998, | ||
}} | ||
/>, | ||
document.body, | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default MainIframe; |
Oops, something went wrong.