1- import React from 'react'
1+ import React , { type CSSProperties } from 'react'
2+ import { clsx } from 'clsx'
23import type { ResponsiveValue } from '../hooks/useResponsiveValue'
34import { getBreakpointDeclarations } from '../utils/getBreakpointDeclarations'
45import 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'
510
611type Viewport = 'narrow' | 'regular' | 'wide'
712
813export type HiddenProps = {
914 when : Array < Viewport > | Viewport
1015 children : React . ReactNode
16+ className ?: string
17+ style ?: CSSProperties
1118}
19+
1220/* Normalize the value that is received from the prop `when`.
1321 * For array types : ['narrow', 'wide'] -> {narrow: true, wide: true}
1422 * For string types: 'narrow' -> {narrow: true}
1523 */
16- function normalize ( hiddenViewports : Array < Viewport > | Viewport ) : ResponsiveValue < boolean > | null {
24+ function normalize ( hiddenViewports : Array < Viewport > | Viewport ) : ResponsiveValue < boolean > {
1725 // For array types
1826 if ( Array . isArray ( hiddenViewports ) ) {
1927 const breakpoints : ResponsiveValue < boolean > = { }
@@ -30,11 +38,30 @@ function normalize(hiddenViewports: Array<Viewport> | Viewport): ResponsiveValue
3038 }
3139}
3240
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+
3445 // 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+ )
3865}
3966
4067Hidden . displayName = 'Hidden'
0 commit comments