Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Portal warning on Next.js rebase #2198

Open
wants to merge 3 commits into
base: reakit
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/reakit/src/Dialog/__tests__/Dialog-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ test("render visible", () => {
expect(baseElement).toMatchInlineSnapshot(`
<body>
<div />
<div
aria-hidden="true"
class="__reakit-focus-trap"
style="position: fixed;"
tabindex="0"
/>
<div
class="__reakit-portal"
>
Expand All @@ -63,12 +57,6 @@ test("render visible", () => {
dialog
</div>
</div>
<div
aria-hidden="true"
class="__reakit-focus-trap"
style="position: fixed;"
tabindex="0"
/>
</body>
`);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/reakit/src/Dialog/__utils/useFocusTrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function useFocusTrap(

if (!portal) {
warning(
true,
"Can't trap focus within modal dialog because either `ref` wasn't passed to component or the component wasn't rendered within a portal",
!!portalRef.current,
"Can't trap focus within modal dialog because `ref` wasn't passed to component.",
"See https://reakit.io/docs/dialog"
);
return undefined;
Expand All @@ -61,7 +61,7 @@ export function useFocusTrap(
if (beforeElement.current) removeFromDOM(beforeElement.current);
if (afterElement.current) removeFromDOM(afterElement.current);
};
}, [portalRef, shouldTrap]);
}, [dialogRef, portalRef, shouldTrap]);

// Focus trap
React.useEffect(() => {
Expand Down
27 changes: 10 additions & 17 deletions packages/reakit/src/Portal/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import { useIsomorphicEffect } from "reakit-utils/useIsomorphicEffect";
import { canUseDOM } from "reakit-utils/canUseDOM";

export type PortalProps = {
Expand All @@ -23,23 +22,18 @@ export function Portal({ children }: PortalProps) {
// otherwise it's document.body
// https://github.com/reakit/reakit/issues/513
const context = React.useContext(PortalContext) || getBodyElement();
const [hostNode] = React.useState(() => {
if (canUseDOM) {
const element = document.createElement("div");
element.className = Portal.__className;
return element;
}
// ssr
return null;
});

useIsomorphicEffect(() => {
if (!hostNode || !context) return undefined;
context.appendChild(hostNode);
const [hostNode, setHostNode] = React.useState<HTMLDivElement | null>(null);

React.useEffect(() => {
if (!context) return undefined;
const element = document.createElement("div");
element.className = Portal.__className;
setHostNode(element);
context.appendChild(element);
return () => {
context.removeChild(hostNode);
context.removeChild(element);
};
}, [hostNode, context]);
}, [context]);

if (hostNode) {
return ReactDOM.createPortal(
Expand All @@ -49,7 +43,6 @@ export function Portal({ children }: PortalProps) {
hostNode
);
}

// ssr
return null;
}
Expand Down