Skip to content

Instantly share code, notes, and snippets.

@erming
Created March 2, 2020 00:57
Show Gist options
  • Save erming/a53dcbef58ddb64c10de8479c2568ac7 to your computer and use it in GitHub Desktop.
Save erming/a53dcbef58ddb64c10de8479c2568ac7 to your computer and use it in GitHub Desktop.
"dependencies": {
"preact": "^10.2.1",
"preact-compat": "^3.19.0"
}
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