-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move HomeScreen to React component
- Loading branch information
1 parent
8793c7b
commit ead85f8
Showing
12 changed files
with
456 additions
and
339 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Metastream Webview</title> | ||
<link rel="icon" type="image/png" href="/images/icon-192.png" sizes="192x192" /> | ||
<link rel="icon" type="image/png" href="/images/icon-512.png" sizes="512x512" /> | ||
</head> | ||
<body></body> | ||
</html> |
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,56 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { createPortal } from 'react-dom' | ||
|
||
interface Props { | ||
container: HTMLElement | ||
children: React.ReactNode | ||
} | ||
|
||
export const Portal = ({ children, container }: Props) => { | ||
const [isReady, setIsReady] = useState(false) | ||
|
||
const copyStyleSheets = () => { | ||
const remoteDocument = container.ownerDocument | ||
if (remoteDocument) { | ||
// remove existing stylesheets | ||
Array.from(remoteDocument.styleSheets).forEach(stylesheet => { | ||
if (stylesheet.ownerNode) stylesheet.ownerNode.remove() | ||
}) | ||
|
||
// add all stylesheets from main document | ||
Array.from(document.styleSheets).forEach(stylesheet => { | ||
if (stylesheet.ownerNode) { | ||
remoteDocument.head.appendChild(stylesheet.ownerNode.cloneNode(true)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
useEffect(function componentDidMount() { | ||
const stylesheetObserver = new MutationObserver(list => { | ||
const shouldCopyStyles = list.some(record => record.type === 'childList') | ||
if (shouldCopyStyles) { | ||
copyStyleSheets() | ||
} | ||
}) | ||
|
||
stylesheetObserver.observe(document.head, { childList: true }) | ||
|
||
copyStyleSheets() | ||
|
||
// Need to wait a bit for stylesheets to load to prevent flashing content. | ||
let timeoutId = setTimeout(() => { | ||
setIsReady(true) | ||
}, 60) | ||
|
||
return function componentWillUnmount() { | ||
stylesheetObserver.disconnect() | ||
|
||
if (timeoutId) { | ||
clearTimeout(timeoutId) | ||
} | ||
} | ||
}, []) | ||
|
||
return createPortal(isReady ? children : <></>, container) | ||
} |
Oops, something went wrong.