1
- import React from 'react'
1
+ import React , { type CSSProperties } from 'react'
2
+ import { clsx } from 'clsx'
2
3
import type { ResponsiveValue } from '../hooks/useResponsiveValue'
3
4
import { getBreakpointDeclarations } from '../utils/getBreakpointDeclarations'
4
5
import Box from '../Box'
6
+ import { useFeatureFlag } from '../FeatureFlags'
7
+ import classes from './Hidden.module.css'
8
+
9
+ const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'
5
10
6
11
type Viewport = 'narrow' | 'regular' | 'wide'
7
12
8
13
export type HiddenProps = {
9
14
when : Array < Viewport > | Viewport
10
15
children : React . ReactNode
16
+ className ?: string
17
+ style ?: CSSProperties
11
18
}
19
+
12
20
/* Normalize the value that is received from the prop `when`.
13
21
* For array types : ['narrow', 'wide'] -> {narrow: true, wide: true}
14
22
* For string types: 'narrow' -> {narrow: true}
15
23
*/
16
- function normalize ( hiddenViewports : Array < Viewport > | Viewport ) : ResponsiveValue < boolean > | null {
24
+ function normalize ( hiddenViewports : Array < Viewport > | Viewport ) : ResponsiveValue < boolean > {
17
25
// For array types
18
26
if ( Array . isArray ( hiddenViewports ) ) {
19
27
const breakpoints : ResponsiveValue < boolean > = { }
@@ -30,11 +38,30 @@ function normalize(hiddenViewports: Array<Viewport> | Viewport): ResponsiveValue
30
38
}
31
39
}
32
40
33
- export const Hidden = ( { when, children} : HiddenProps ) => {
41
+ export const Hidden = ( { when, className, style, children} : HiddenProps ) => {
42
+ const enabled = useFeatureFlag ( CSS_MODULES_FEATURE_FLAG )
43
+ const normalizedStyles = normalize ( when )
44
+
34
45
// Get breakpoint declarations for the normalized ResponsiveValue object
35
- const styles = getBreakpointDeclarations ( normalize ( when ) , 'display' , ( ) => 'none' )
36
- // Render the children with the styles
37
- return styles ? < Box sx = { styles } > { children } </ Box > : null
46
+ const breakpointSx = getBreakpointDeclarations ( normalizedStyles , 'display' , ( ) => 'none' )
47
+
48
+ return enabled ? (
49
+ < div
50
+ className = { clsx ( className , { [ classes . Hidden ] : enabled } ) }
51
+ style = {
52
+ {
53
+ '--hiddenDisplay-narrow' : normalizedStyles . narrow ? 'none' : undefined ,
54
+ '--hiddenDisplay-regular' : normalizedStyles . regular ? 'none' : undefined ,
55
+ '--hiddenDisplay-wide' : normalizedStyles . wide ? 'none' : undefined ,
56
+ ...style ,
57
+ } as CSSProperties
58
+ }
59
+ >
60
+ { children }
61
+ </ div >
62
+ ) : (
63
+ < Box sx = { breakpointSx } > { children } </ Box >
64
+ )
38
65
}
39
66
40
67
Hidden . displayName = 'Hidden'
0 commit comments