Skip to content

Commit

Permalink
chore: VisibleWhenVisualTest fix for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Oct 25, 2023
1 parent 6c0440c commit 78688c6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/dnb-design-system-portal/src/shared/tags/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* All inline tags for Markdown
*/

import React from 'react'
import React, { useState, useEffect } from 'react'
import CodeBlock from './CodeBlock'
import { Checkbox, Input } from '@dnb/eufemia/src/components'
import { Ul, Ol, Dl, Li, P, Hr, Blockquote, Code } from '@dnb/eufemia/src'
Expand Down Expand Up @@ -51,11 +51,29 @@ export default {
Copy,
VisibilityByTheme,
VisibleWhenVisualTest: ({ children }) => {
const [mounted, setMounted] = useState(false)

useEffect(() => {
// Wait for component to mount,
// as we will not know if IS_TEST(?data-visual-test=true) before mounting when ran in SSR.
setMounted(true)
}, [])

if (!mounted) return null
if (typeof globalThis !== 'undefined' && globalThis.IS_TEST) {
return children
}
},
VisibleWhenNotVisualTest: ({ children }) => {
const [mounted, setMounted] = useState(false)

useEffect(() => {
// Wait for component to mount,
// as we will not know if IS_TEST(?data-visual-test=true) before mounting when ran in SSR.
setMounted(true)
}, [])

if (!mounted) return null
if (typeof globalThis !== 'undefined' && !globalThis.IS_TEST) {
return children
}
Expand Down

0 comments on commit 78688c6

Please sign in to comment.