Skip to content

Commit 877497c

Browse files
authoredDec 11, 2024··
feat(Hidden): Convert Hidden to CSS modules behind feature flag (#5403)
* Convert Hidden to CSS modules behind feature flag * Add changeset * Add block fallback for display variable
1 parent 204ded0 commit 877497c

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed
 

‎.changeset/sixty-starfishes-grab.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Convert `Hidden` to CSS modules behind team feature flag
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.Hidden {
2+
@media screen and (--viewportRange-narrow) {
3+
display: var(--hiddenDisplay-narrow, block);
4+
}
5+
6+
@media screen and (--viewportRange-regular) {
7+
display: var(--hiddenDisplay-regular, block);
8+
}
9+
10+
@media screen and (--viewportRange-wide) {
11+
display: var(--hiddenDisplay-wide, block);
12+
}
13+
}

‎packages/react/src/Hidden/Hidden.tsx

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
import React from 'react'
1+
import React, {type CSSProperties} from 'react'
2+
import {clsx} from 'clsx'
23
import type {ResponsiveValue} from '../hooks/useResponsiveValue'
34
import {getBreakpointDeclarations} from '../utils/getBreakpointDeclarations'
45
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'
510

611
type Viewport = 'narrow' | 'regular' | 'wide'
712

813
export 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

4067
Hidden.displayName = 'Hidden'

0 commit comments

Comments
 (0)
Please sign in to comment.