Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Hidden): Convert Hidden to CSS modules behind feature flag #5403

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-starfishes-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Convert `Hidden` to CSS modules behind team feature flag
13 changes: 13 additions & 0 deletions packages/react/src/Hidden/Hidden.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.Hidden {
@media screen and (--viewportRange-narrow) {
display: var(--hiddenDisplay-narrow, block);
}

@media screen and (--viewportRange-regular) {
display: var(--hiddenDisplay-regular, block);
}

@media screen and (--viewportRange-wide) {
display: var(--hiddenDisplay-wide, block);
}
}
39 changes: 33 additions & 6 deletions packages/react/src/Hidden/Hidden.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import React from 'react'
import React, {type CSSProperties} from 'react'
import {clsx} from 'clsx'
import type {ResponsiveValue} from '../hooks/useResponsiveValue'
import {getBreakpointDeclarations} from '../utils/getBreakpointDeclarations'
import Box from '../Box'
import {useFeatureFlag} from '../FeatureFlags'
import classes from './Hidden.module.css'

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'

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

export type HiddenProps = {
when: Array<Viewport> | Viewport
children: React.ReactNode
className?: string
style?: CSSProperties
}

/* Normalize the value that is received from the prop `when`.
* For array types : ['narrow', 'wide'] -> {narrow: true, wide: true}
* For string types: 'narrow' -> {narrow: true}
*/
function normalize(hiddenViewports: Array<Viewport> | Viewport): ResponsiveValue<boolean> | null {
function normalize(hiddenViewports: Array<Viewport> | Viewport): ResponsiveValue<boolean> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalize method always returns an object

// For array types
if (Array.isArray(hiddenViewports)) {
const breakpoints: ResponsiveValue<boolean> = {}
Expand All @@ -30,11 +38,30 @@ function normalize(hiddenViewports: Array<Viewport> | Viewport): ResponsiveValue
}
}

export const Hidden = ({when, children}: HiddenProps) => {
export const Hidden = ({when, className, style, children}: HiddenProps) => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
const normalizedStyles = normalize(when)

// Get breakpoint declarations for the normalized ResponsiveValue object
const styles = getBreakpointDeclarations(normalize(when), 'display', () => 'none')
// Render the children with the styles
return styles ? <Box sx={styles}>{children}</Box> : null
const breakpointSx = getBreakpointDeclarations(normalizedStyles, 'display', () => 'none')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

styles was always an object so I removed the check here


return enabled ? (
<div
className={clsx(className, {[classes.Hidden]: enabled})}
style={
{
'--hiddenDisplay-narrow': normalizedStyles.narrow ? 'none' : undefined,
'--hiddenDisplay-regular': normalizedStyles.regular ? 'none' : undefined,
'--hiddenDisplay-wide': normalizedStyles.wide ? 'none' : undefined,
...style,
} as CSSProperties
}
>
{children}
</div>
) : (
<Box sx={breakpointSx}>{children}</Box>
)
}

Hidden.displayName = 'Hidden'
Loading