Skip to content

Commit

Permalink
chore: VisibilityByTheme fix for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Oct 25, 2023
1 parent 25df6fd commit 3343e01
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/dnb-eufemia/src/shared/VisibilityByTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState, useEffect } from 'react'
import useTheme from './useTheme'
import type { ThemeNames, ThemeProps } from './Theme'

Expand Down Expand Up @@ -30,11 +30,19 @@ export default function VisibilityByTheme({
visible,
hidden,
}: VisibilityByThemeProps) {
const [mounted, setMounted] = useState(false)

const theme = useTheme()

const visibleList = Array.isArray(visible) ? visible : [visible]
const hiddenList = Array.isArray(hidden) ? hidden : [hidden]

useEffect(() => {
// Wait for component to mount,
// as we will not know the theme before mounting when ran in SSR.
setMounted(true)
}, [])

if (visible) {
if (!visibleList.some(match(theme))) {
return null
Expand All @@ -45,6 +53,7 @@ export default function VisibilityByTheme({
}
}

if (!mounted) return null
return children as JSX.Element

function match(theme: ThemeProps) {
Expand Down

0 comments on commit 3343e01

Please sign in to comment.