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

Bug Fix for Text component and styled system arguments #5079

Merged
merged 5 commits into from
Oct 7, 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/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
Loading