Created
March 2, 2020 00:57
-
-
Save erming/a53dcbef58ddb64c10de8479c2568ac7 to your computer and use it in GitHub Desktop.
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
"dependencies": { | |
"preact": "^10.2.1", | |
"preact-compat": "^3.19.0" | |
} |
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
import { Fragment } from "preact"; | |
import { useState, useLayoutEffect } from "preact/hooks"; | |
import { createPortal } from "preact/compat"; | |
export default ({ children, into }) => { | |
const [target, setTarget] = useState(null); | |
useLayoutEffect(() => { | |
setTarget(document.querySelector(into)); | |
}, []); | |
if (target) { | |
return createPortal( | |
<Fragment>{children}</Fragment>, | |
target | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment