diff --git a/src/components/molecules/OakModal/OakModal.test.tsx b/src/components/molecules/OakModal/OakModal.test.tsx index 8ab8fd98..c906cfdd 100644 --- a/src/components/molecules/OakModal/OakModal.test.tsx +++ b/src/components/molecules/OakModal/OakModal.test.tsx @@ -20,16 +20,26 @@ jest.mock("react-dom", () => { }); describe(OakModal, () => { - it("matches snapshot", () => { + it("does not render until mounted on the client", () => { const tree = create( {}} footerSlot="Modal footer"> Modal content , - ).toJSON(); + ); + + expect(tree.toJSON()).toBeNull(); + }); + + it("matches snapshot when mounted", async () => { + const result = renderWithTheme( + {}}> + Modal content + , + ); - expect(tree).toMatchSnapshot(); + expect(result.container).toMatchSnapshot(); }); it("calls onClose when the close button is clicked", () => { diff --git a/src/components/molecules/OakModal/OakModal.tsx b/src/components/molecules/OakModal/OakModal.tsx index 41f7722c..771afb26 100644 --- a/src/components/molecules/OakModal/OakModal.tsx +++ b/src/components/molecules/OakModal/OakModal.tsx @@ -1,6 +1,7 @@ import React, { HTMLAttributes, ReactNode, + useEffect, useLayoutEffect, useRef, useState, @@ -79,7 +80,7 @@ const logoSrc = `https://${process.env.NEXT_PUBLIC_OAK_ASSETS_HOST}/${process.en export const OakModal = ({ children, footerSlot, - domContainer = document.body, + domContainer, isOpen, onClose, ...rest @@ -109,6 +110,17 @@ export const OakModal = ({ }; }, [canaryElement]); + // `createPortal` is not supported in SSR so we can only render when mounted on the client + const [isMounted, setIsMounted] = useState(false); + + useEffect(() => { + setIsMounted(true); + }, []); + + if (!isMounted) { + return null; + } + return createPortal( )} , - domContainer, + domContainer ?? document.body, ); }; diff --git a/src/components/molecules/OakModal/__snapshots__/OakModal.test.tsx.snap b/src/components/molecules/OakModal/__snapshots__/OakModal.test.tsx.snap index 13b37454..fcd007d0 100644 --- a/src/components/molecules/OakModal/__snapshots__/OakModal.test.tsx.snap +++ b/src/components/molecules/OakModal/__snapshots__/OakModal.test.tsx.snap @@ -1,23 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`OakModal matches snapshot 1`] = ` -[ -
, - .c0 { +exports[`OakModal matches snapshot when mounted 1`] = ` +.c0 { position: fixed; inset: 0rem; background: #222222; @@ -193,16 +177,21 @@ exports[`OakModal matches snapshot 1`] = ` letter-spacing: 0.0115rem; } -.c8 { - object-fit: contain; -} - .c20 { -webkit-filter: invert(10%) sepia(1%) saturate(236%) hue-rotate(314deg) brightness(95%) contrast(91%); filter: invert(10%) sepia(1%) saturate(236%) hue-rotate(314deg) brightness(95%) contrast(91%); object-fit: contain; } +.c8 { + background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9Im5vbmUiIHZpZXdCb3g9IjAgMCA2NCA2NCI+PHBhdGggZmlsbD0iIzIyMiIgZD0iTTI4Ljc3OSAxOS4xNzZhMjcuMTkxIDI3LjE5MSAwIDAgMC0zLjggMS42IDE2LjcgMTYuNyAwIDAgMC03LjEgOC40YzAgLjEtLjEuMi0uMS4zLS43IDIuNC0uNiAyIDEuMyAyLjMgMS45LjMgMSAuNSAxIDEuMy0uMSA4LjggNC4xIDE1LjEgMTEuNCAxOS42YTEuNSAxLjUgMCAwIDAgMS43LjJjNS43LTIuNiA5LjMtNyAxMC4zLTEzLjJhMSAxIDAgMCAxIDEtMWwzLS4yYy44IDAgMS4zLjIgMS4yIDEuMmExNy45IDE3LjkgMCAwIDEtMy4yIDkuMiAyMy43IDIzLjcgMCAwIDEtMTAuOSA5LjEgNS40MDEgNS40MDEgMCAwIDEtNC41LS4yIDI2LjI5OCAyNi4yOTggMCAwIDEtOC41LTYuNiAyNS45IDI1LjkgMCAwIDEtNi40LTE0LjRjMC0uNi0uMi0uNy0uOC0uOC0yLjUtLjQtMi41LS4xLTIuMy0yLjlhMTkuMyAxOS4zIDAgMCAxIDEwLjgtMTYuNiAzOC45OTkgMzguOTk5IDAgMCAxIDUuNy0yLjEgMi4xIDIuMSAwIDAgMCAuOS0xLjMgMTQuMSAxNC4xIDAgMCAxIDMuNS02LjNsLjMtLjNjMS45LTIgMi42LTIgNC4zLjJsLjQuNWMxLjEgMS4xIDEgMS41LS4xIDIuNmExMS45IDExLjkgMCAwIDAtMy4yIDQuNCAxNi45IDE2LjkgMCAwIDEgNy41IDIuM2M1LjcgMy41IDkuMiA4LjMgOS45IDE1IC4wMTYuOTAxLS4wMTcgMS44MDItLjEgMi43IDAgLjgtLjYgMS0xLjIgMS4yYTE2LjEgMTYuMSAwIDAgMS0xMS0uNyAxNy45MDEgMTcuOTAxIDAgMCAxLTEwLjktMTMuNiA5Ljc5NiA5Ljc5NiAwIDAgMS0uMS0xLjlabTE4LjEgMTIuMmMuNC01LjUtNi45LTEyLjYtMTMtMTIuMS41IDYuNSA3LjYgMTIuOCAxMyAxMi4xWiIgb3BhY2l0eT0iLjEiLz48L3N2Zz4=); + background-color: #e7f6f5; + background-size: 4rem; + background-position: center; + background-repeat: no-repeat; + object-fit: contain; +} + .c12 { background: none; color: inherit; @@ -281,140 +270,97 @@ exports[`OakModal matches snapshot 1`] = ` transform: translateX(0); } -
+