Skip to content

Commit

Permalink
Bug Fix for Text component and styled system arguments (#5079)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jonrohan and joshblack authored Oct 7, 2024
1 parent 3b7bf41 commit a2e5671
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-apples-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Correctly pass styled system typography and common props to the `Box` component in the `Text` component when the CSS modules feature flag is enabled.
19 changes: 15 additions & 4 deletions packages/react/src/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,27 @@ const StyledText = styled.span<StyledTextProps>`
${sx};
`

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

const includesSystemProps = (props: StyledTextProps) => {
if (props.sx) {
return true
}

return Object.keys(props).some(prop => {
return TYPOGRAPHY_PROP_NAMES.has(prop) || COMMON_PROP_NAMES.has(prop)
})
}

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

const innerRef = React.useRef<HTMLElement>(null)
useRefObjectAsForwardedRef(forwardedRef, innerRef)

if (enabled) {
if (props.sx) {
// If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
if (includesSystemProps(props)) {
return (
// @ts-ignore shh
<Box
Expand All @@ -81,7 +94,6 @@ const Text = forwardRef(({as: Component = 'span', className, size, weight, ...pr
}

return (
// @ts-ignore shh
<Component
className={clsx(className, classes.Text)}
data-size={size}
Expand All @@ -94,7 +106,6 @@ const Text = forwardRef(({as: Component = 'span', className, size, weight, ...pr
}

return (
// @ts-ignore shh
<StyledText
as={Component}
className={className}
Expand Down

0 comments on commit a2e5671

Please sign in to comment.