Skip to content

Commit a2e5671

Browse files
jonrohanjoshblack
andauthored
Bug Fix for Text component and styled system arguments (#5079)
* Fix for Text component and styled system arguments * Create wet-apples-hide.md * Update packages/react/src/Text/Text.tsx Co-authored-by: Josh Black <joshblack@github.com> * Remove semicolons and fix logical operator * Remove semicolon in includesSystemProps function --------- Co-authored-by: Josh Black <joshblack@github.com>
1 parent 3b7bf41 commit a2e5671

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

.changeset/wet-apples-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Correctly pass styled system typography and common props to the `Box` component in the `Text` component when the CSS modules feature flag is enabled.

packages/react/src/Text/Text.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,27 @@ const StyledText = styled.span<StyledTextProps>`
5858
${sx};
5959
`
6060

61+
const COMMON_PROP_NAMES = new Set(Object.keys(COMMON))
62+
const TYPOGRAPHY_PROP_NAMES = new Set(Object.keys(TYPOGRAPHY))
63+
64+
const includesSystemProps = (props: StyledTextProps) => {
65+
if (props.sx) {
66+
return true
67+
}
68+
69+
return Object.keys(props).some(prop => {
70+
return TYPOGRAPHY_PROP_NAMES.has(prop) || COMMON_PROP_NAMES.has(prop)
71+
})
72+
}
73+
6174
const Text = forwardRef(({as: Component = 'span', className, size, weight, ...props}, forwardedRef) => {
6275
const enabled = useFeatureFlag('primer_react_css_modules_ga')
6376

6477
const innerRef = React.useRef<HTMLElement>(null)
6578
useRefObjectAsForwardedRef(forwardedRef, innerRef)
66-
6779
if (enabled) {
68-
if (props.sx) {
80+
// If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
81+
if (includesSystemProps(props)) {
6982
return (
7083
// @ts-ignore shh
7184
<Box
@@ -81,7 +94,6 @@ const Text = forwardRef(({as: Component = 'span', className, size, weight, ...pr
8194
}
8295

8396
return (
84-
// @ts-ignore shh
8597
<Component
8698
className={clsx(className, classes.Text)}
8799
data-size={size}
@@ -94,7 +106,6 @@ const Text = forwardRef(({as: Component = 'span', className, size, weight, ...pr
94106
}
95107

96108
return (
97-
// @ts-ignore shh
98109
<StyledText
99110
as={Component}
100111
className={className}

0 commit comments

Comments
 (0)