Skip to content

Commit

Permalink
fix(KaizenProvider): await document before rendering ToastNotificatio…
Browse files Browse the repository at this point in the history
…nsList
  • Loading branch information
HeartSquared committed Nov 24, 2023
1 parent 0182b49 commit 5e2c90a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-days-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kaizen/components": patch
---

Update KaizenProvider to await document before rendering ToastNotificationsList
38 changes: 26 additions & 12 deletions packages/components/src/KaizenProvider/KaizenProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { useEffect, useState } from "react"
import { ToastNotificationsList } from "~components/Notification"
import { ToastNotificationProvider } from "~components/Notification/ToastNotification/context/ToastNotificationContext"
import { FontDefinitions } from "./subcomponents/FontDefinitions"
Expand All @@ -12,16 +12,30 @@ export type KaizenProviderProps = {
export const KaizenProvider = ({
children,
locale = "en",
}: KaizenProviderProps): JSX.Element => (
<OptionalIntlProvider locale={locale}>
<>
<ToastNotificationProvider>
<ToastNotificationsList />
{children}
</ToastNotificationProvider>
<FontDefinitions />
</>
</OptionalIntlProvider>
)
}: KaizenProviderProps): JSX.Element => {
const [documentIsAvailable, setDocumentIsAvailable] = useState<boolean>(false)
const [notificationsList, setNotificationsList] = useState<JSX.Element>()

useEffect(() => {
// SSR does not have a document, which is required for ToastNotificationsList.
// Await document render before rendering the component.
if (document !== undefined) {
setNotificationsList(<ToastNotificationsList />)
setDocumentIsAvailable(true)
}
}, [documentIsAvailable])

return (
<OptionalIntlProvider locale={locale}>
<>
<ToastNotificationProvider>
{notificationsList}
{children}
</ToastNotificationProvider>
<FontDefinitions />
</>
</OptionalIntlProvider>
)
}

KaizenProvider.displayName = "KaizenProvider"

0 comments on commit 5e2c90a

Please sign in to comment.