Skip to content

Commit 4c69b38

Browse files
authored
refactor(Text): Text component to use CSS modules behind primer_react_css_modules_team feature flag (#4874)
* refactor(Text): To CSS modules behind primer_react_css_modules_team feature flag * Create tidy-clocks-marry.md * Adjust playground argtypes for tsc * Make this minor release
1 parent fff7f65 commit 4c69b38

File tree

5 files changed

+135
-16
lines changed

5 files changed

+135
-16
lines changed

.changeset/tidy-clocks-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Refactor `Text` to CSS modules behind primer_react_css_modules_team feature flag

e2e/components/Text.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ test.describe('Text', () => {
4444
test('default @vrt', async ({page}) => {
4545
await visit(page, {
4646
id: story.id,
47+
globals: {
48+
featureFlags: {
49+
primer_react_css_modules_team: true,
50+
},
51+
},
52+
})
53+
54+
// Default state
55+
expect(await page.screenshot()).toMatchSnapshot(`Text.${story.title}.png`)
56+
})
57+
58+
test('default (styled-system) @vrt', async ({page}) => {
59+
await visit(page, {
60+
id: story.id,
61+
globals: {
62+
featureFlags: {
63+
primer_react_css_modules_team: false,
64+
},
65+
},
4766
})
4867

4968
// Default state
@@ -53,6 +72,23 @@ test.describe('Text', () => {
5372
test('axe @aat', async ({page}) => {
5473
await visit(page, {
5574
id: story.id,
75+
globals: {
76+
featureFlags: {
77+
primer_react_css_modules_team: true,
78+
},
79+
},
80+
})
81+
await expect(page).toHaveNoViolations()
82+
})
83+
84+
test('axe (styled-system) @aat', async ({page}) => {
85+
await visit(page, {
86+
id: story.id,
87+
globals: {
88+
featureFlags: {
89+
primer_react_css_modules_team: false,
90+
},
91+
},
5692
})
5793
await expect(page).toHaveNoViolations()
5894
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.Text {
2+
&:where([data-size='small']) {
3+
font-size: var(--text-body-size-small);
4+
line-height: var(--text-body-lineHeight-small);
5+
}
6+
7+
&:where([data-size='medium']) {
8+
font-size: var(--text-body-size-medium);
9+
line-height: var(--text-body-lineHeight-medium);
10+
}
11+
12+
&:where([data-size='large']) {
13+
font-size: var(--text-body-size-large);
14+
line-height: var(--text-body-lineHeight-large);
15+
}
16+
17+
&:where([data-weight='light']) {
18+
font-weight: var(--base-text-weight-light);
19+
}
20+
21+
&:where([data-weight='normal']) {
22+
font-weight: var(--base-text-weight-normal);
23+
}
24+
25+
&:where([data-weight='medium']) {
26+
font-weight: var(--base-text-weight-medium);
27+
}
28+
29+
&:where([data-weight='semibold']) {
30+
font-weight: var(--base-text-weight-semibold);
31+
}
32+
}

packages/react/src/Text/Text.stories.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@ export default {
99

1010
export const Default = () => <Text>Default Text</Text>
1111

12-
export const Playground: StoryFn<typeof Text> = args => <Text {...args}>{args.text}</Text>
12+
export const Playground: StoryFn<typeof Text> = args => <Text {...args}>Playground</Text>
1313

1414
Playground.args = {
15-
text: 'Playground',
1615
as: 'span',
1716
size: 'medium',
1817
weight: 'normal',
1918
}
2019

2120
Playground.argTypes = {
22-
text: {
23-
type: 'string',
24-
},
2521
as: {
2622
type: 'string',
2723
},
@@ -37,12 +33,6 @@ Playground.argTypes = {
3733
disable: true,
3834
},
3935
},
40-
forwardedAs: {
41-
controls: false,
42-
table: {
43-
disable: true,
44-
},
45-
},
4636
size: {
4737
control: {
4838
type: 'radio',

packages/react/src/Text/Text.tsx

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import cx from 'clsx'
12
import styled from 'styled-components'
3+
import React, {forwardRef} from 'react'
24
import type {SystemCommonProps, SystemTypographyProps} from '../constants'
35
import {COMMON, TYPOGRAPHY} from '../constants'
46
import type {SxProp} from '../sx'
57
import sx from '../sx'
8+
import {useFeatureFlag} from '../FeatureFlags'
9+
import Box from '../Box'
10+
import {useRefObjectAsForwardedRef} from '../hooks'
11+
import classes from './Text.module.css'
612
import type {ComponentProps} from '../utils/types'
13+
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
714

815
type StyledTextProps = {
916
size?: 'large' | 'medium' | 'small'
@@ -12,10 +19,7 @@ type StyledTextProps = {
1219
SystemCommonProps &
1320
SxProp
1421

15-
const Text = styled.span.attrs<StyledTextProps>(({size, weight}) => ({
16-
'data-size': size,
17-
'data-weight': weight,
18-
}))<StyledTextProps>`
22+
const StyledText = styled.span<StyledTextProps>`
1923
${TYPOGRAPHY};
2024
${COMMON};
2125
@@ -52,5 +56,57 @@ const Text = styled.span.attrs<StyledTextProps>(({size, weight}) => ({
5256
5357
${sx};
5458
`
55-
export type TextProps = ComponentProps<typeof Text>
59+
60+
const Text = forwardRef(({as: Component = 'span', className, size, weight, ...props}, forwardedRef) => {
61+
const enabled = useFeatureFlag('primer_react_css_modules_team')
62+
63+
const innerRef = React.useRef<HTMLElement>(null)
64+
useRefObjectAsForwardedRef(forwardedRef, innerRef)
65+
66+
if (enabled) {
67+
if (props.sx) {
68+
return (
69+
// @ts-ignore shh
70+
<Box
71+
as={Component}
72+
className={cx(className, classes.Text)}
73+
data-size={size}
74+
data-weight={weight}
75+
{...props}
76+
// @ts-ignore shh
77+
ref={innerRef}
78+
/>
79+
)
80+
}
81+
82+
return (
83+
// @ts-ignore shh
84+
<Component
85+
className={cx(className, classes.Text)}
86+
data-size={size}
87+
data-weight={weight}
88+
{...props}
89+
// @ts-ignore shh
90+
ref={innerRef}
91+
/>
92+
)
93+
}
94+
95+
return (
96+
// @ts-ignore shh
97+
<StyledText
98+
as={Component}
99+
className={className}
100+
data-size={size}
101+
data-weight={weight}
102+
{...props}
103+
// @ts-ignore shh
104+
ref={innerRef}
105+
/>
106+
)
107+
}) as PolymorphicForwardRefComponent<'span', StyledTextProps>
108+
109+
Text.displayName = 'Text'
110+
111+
export type TextProps = ComponentProps<typeof StyledText>
56112
export default Text

0 commit comments

Comments
 (0)