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

chore(Text): Remove CSS modules feature flag from Text component #5061

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
591bc87
Remove css modules feature flag from Text
jonrohan Oct 3, 2024
bd1d55e
Create green-rice-clean.md
jonrohan Oct 4, 2024
5579f67
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 7, 2024
f2637cb
Add COMMON and TYPOGRAPHY imports in Text.tsx
jonrohan Oct 7, 2024
435fe82
Rename `StyledTextProps` to `TextProps`
jonrohan Oct 7, 2024
a692e45
Update snapshots
jonrohan Oct 7, 2024
aadd4cd
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 7, 2024
25f2627
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 15, 2024
2c81297
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 17, 2024
2a68437
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 23, 2024
b009a8b
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Oct 24, 2024
db68332
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Nov 5, 2024
73aaa8d
Totally change all the typescript
jonrohan Nov 5, 2024
4da3b3e
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Nov 13, 2024
38a2260
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Nov 27, 2024
dd52bbe
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Dec 2, 2024
9b71733
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Dec 4, 2024
ca7edc5
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Dec 10, 2024
0ed7a78
Merge branch 'main' into css_modules_remove_feature_flag_text
jonrohan Jan 27, 2025
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/green-rice-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Remove CSS modules feature flag from Text component
7 changes: 6 additions & 1 deletion packages/react/src/Text/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {render as HTMLRender} from '@testing-library/react'
import axe from 'axe-core'

describe('Text', () => {
behavesAsComponent({Component: Text})
behavesAsComponent({
Component: Text,
options: {
skipDisplayName: true,
},
})

checkExports('Text', {
default: Text,
Expand Down
108 changes: 29 additions & 79 deletions packages/react/src/Text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,67 +1,25 @@
import {clsx} from 'clsx'
import styled, {type StyledComponent} from 'styled-components'
import React, {forwardRef} from 'react'
import React from 'react'
import type {SystemCommonProps, SystemTypographyProps} from '../constants'
import {COMMON, TYPOGRAPHY} from '../constants'
import type {SxProp} from '../sx'
import sx from '../sx'
import {useFeatureFlag} from '../FeatureFlags'
import Box from '../Box'
import {useRefObjectAsForwardedRef} from '../hooks'
import classes from './Text.module.css'
import type {ComponentProps} from '../utils/types'

type StyledTextProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
as?: React.ComponentType<any> | keyof JSX.IntrinsicElements
export type TextProps<As extends React.ElementType> = {
as?: As
size?: 'large' | 'medium' | 'small'
weight?: 'light' | 'normal' | 'medium' | 'semibold'
} & SystemTypographyProps &
} & DistributiveOmit<React.ComponentPropsWithRef<React.ElementType extends As ? 'span' : As>, 'as'> &
SystemTypographyProps &
SystemCommonProps &
SxProp

const StyledText = styled.span<StyledTextProps>`
${TYPOGRAPHY};
${COMMON};

&:where([data-size='small']) {
font-size: var(--text-body-size-small, 0.75rem);
line-height: var(--text-body-lineHeight-small, 1.6666);
}

&:where([data-size='medium']) {
font-size: var(--text-body-size-medium, 0.875rem);
line-height: var(--text-body-lineHeight-medium, 1.4285);
}

&:where([data-size='large']) {
font-size: var(--text-body-size-large, 1rem);
line-height: var(--text-body-lineHeight-large, 1.5);
}

&:where([data-weight='light']) {
font-weight: var(--base-text-weight-light, 300);
}

&:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal, 400);
}

&:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium, 500);
}

&:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold, 600);
}

${sx};
`

const COMMON_PROP_NAMES = new Set(Object.keys(COMMON))
const TYPOGRAPHY_PROP_NAMES = new Set(Object.keys(TYPOGRAPHY))

const includesSystemProps = (props: StyledTextProps) => {
function includesSystemProps<As extends React.ElementType>(props: TextProps<As>) {
if (props.sx) {
return true
}
Expand All @@ -71,55 +29,47 @@ const includesSystemProps = (props: StyledTextProps) => {
})
}

const Text = forwardRef(({as: Component = 'span', className, size, weight, ...props}, forwardedRef) => {
const enabled = useFeatureFlag('primer_react_css_modules_ga')

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function Text<As extends React.ElementType>(props: TextProps<As>, forwardedRef: React.ForwardedRef<any>) {
const {as: Component = 'span', className, size, weight, ...rest} = props
const innerRef = React.useRef<HTMLElement>(null)
useRefObjectAsForwardedRef(forwardedRef, innerRef)
if (enabled) {
// If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
if (includesSystemProps(props)) {
return (
// @ts-ignore shh
<Box
as={Component}
className={clsx(className, classes.Text)}
data-size={size}
data-weight={weight}
{...props}
// @ts-ignore shh
ref={innerRef}
/>
)
}

// If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
if (includesSystemProps(rest)) {
return (
<Component
<Box
as={Component}
className={clsx(className, classes.Text)}
data-size={size}
data-weight={weight}
{...props}
// @ts-ignore shh
{...rest}
ref={innerRef}
/>
)
}

return (
<StyledText
as={Component}
className={className}
<Component
className={clsx(className, classes.Text)}
data-size={size}
data-weight={weight}
{...props}
// @ts-ignore shh
{...rest}
ref={innerRef}
/>
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as StyledComponent<'span', any, StyledTextProps, never>
}

// eslint-disable-next-line @typescript-eslint/ban-types
type FixedForwardRef = <T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode,
) => (props: P & React.RefAttributes<T>) => React.ReactNode

const fixedForwardRef = React.forwardRef as FixedForwardRef

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DistributiveOmit<T, TOmitted extends PropertyKey> = T extends any ? Omit<T, TOmitted> : never

Text.displayName = 'Text'

export type TextProps = ComponentProps<typeof StyledText>
export default Text
export default fixedForwardRef(Text)
Original file line number Diff line number Diff line change
Expand Up @@ -944,39 +944,8 @@ exports[`TextInputWithTokens renders a truncated set of tokens 1`] = `
}

.c9 {
font-size: 16px;
color: var(--fgColor-muted,var(--color-fg-muted,#656d76));
}

.c9:where([data-size='small']) {
font-size: var(--text-body-size-small,0.75rem);
line-height: var(--text-body-lineHeight-small,1.6666);
}

.c9:where([data-size='medium']) {
font-size: var(--text-body-size-medium,0.875rem);
line-height: var(--text-body-lineHeight-medium,1.4285);
}

.c9:where([data-size='large']) {
font-size: var(--text-body-size-large,1rem);
line-height: var(--text-body-lineHeight-large,1.5);
}

.c9:where([data-weight='light']) {
font-weight: var(--base-text-weight-light,300);
}

.c9:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal,400);
}

.c9:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium,500);
}

.c9:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold,600);
font-size: 16px;
}

.c7 {
Expand Down Expand Up @@ -1475,7 +1444,7 @@ exports[`TextInputWithTokens renders a truncated set of tokens 1`] = `
</span>
</span>
<span
className="c9"
className="c9 Text"
color="fg.muted"
fontSize={2}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,12 @@

exports[`has default theme 1`] = `
.c0 {
color: var(--fgColor-default,var(--color-fg-default,#1F2328));
margin-bottom: 4px;
}

.c0:where([data-size='small']) {
font-size: var(--text-body-size-small,0.75rem);
line-height: var(--text-body-lineHeight-small,1.6666);
}

.c0:where([data-size='medium']) {
font-size: var(--text-body-size-medium,0.875rem);
line-height: var(--text-body-lineHeight-medium,1.4285);
}

.c0:where([data-size='large']) {
font-size: var(--text-body-size-large,1rem);
line-height: var(--text-body-lineHeight-large,1.5);
}

.c0:where([data-weight='light']) {
font-weight: var(--base-text-weight-light,300);
}

.c0:where([data-weight='normal']) {
font-weight: var(--base-text-weight-normal,400);
}

.c0:where([data-weight='medium']) {
font-weight: var(--base-text-weight-medium,500);
}

.c0:where([data-weight='semibold']) {
font-weight: var(--base-text-weight-semibold,600);
color: var(--fgColor-default,var(--color-fg-default,#1F2328));
}

<span
class="c0"
class="c0 Text"
color="fg.default"
>
Hello
Expand Down
Loading