Skip to content

Commit 013c200

Browse files
committed
chore: use styled wrapper instead of Box (#6962)
1 parent be7cdbf commit 013c200

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

.changeset/purple-waves-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/styled-react": patch
3+
---
4+
5+
chore: use styled wrapper instead of Box for Label, Token and Spinner

packages/styled-react/src/components/Label.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import {type LabelProps as PrimerLabelProps, Label as PrimerLabel, Box} from '@primer/react'
2-
import {type SxProp} from '../sx'
1+
import {type LabelProps as PrimerLabelProps, Label as PrimerLabel} from '@primer/react'
2+
import {sx, type SxProp} from '../sx'
33
import {forwardRef} from 'react'
44
import type {ForwardRefComponent} from '../polymorphic'
5+
import styled from 'styled-components'
56

67
type LabelProps = PrimerLabelProps & SxProp & {as?: React.ElementType}
78

8-
const StyledLabel = forwardRef(function Label(props, ref) {
9-
return <Box as={PrimerLabel} ref={ref} {...props} />
10-
}) as ForwardRefComponent<'span', LabelProps>
9+
const StyledLabel: ForwardRefComponent<'span', LabelProps> = styled(PrimerLabel).withConfig({
10+
shouldForwardProp: prop => (prop as keyof LabelProps) !== 'sx',
11+
})<LabelProps>`
12+
${sx}
13+
`
1114

1215
const Label = forwardRef<HTMLElement, LabelProps>(({as, ...props}, ref) => {
1316
return <StyledLabel {...props} {...(as ? {forwardedAs: as} : {})} ref={ref} />
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import {Box, Spinner as PrimerSpinner, type SpinnerProps as PrimerSpinnerProps} from '@primer/react'
2-
import React from 'react'
3-
import type {SxProp} from '../sx'
1+
import {Spinner as PrimerSpinner, type SpinnerProps as PrimerSpinnerProps} from '@primer/react'
2+
import {sx, type SxProp} from '../sx'
3+
import styled from 'styled-components'
44

55
export type SpinnerProps = PrimerSpinnerProps & SxProp
66

7-
export function Spinner(props: SpinnerProps) {
8-
return <Box as={PrimerSpinner} {...props} />
9-
}
7+
export const Spinner = styled(PrimerSpinner).withConfig({
8+
shouldForwardProp: prop => (prop as keyof SpinnerProps) !== 'sx',
9+
})<SpinnerProps>`
10+
${sx}
11+
`
Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
1-
import React from 'react'
2-
import {
3-
type TokenProps as PrimerTokenProps,
4-
type SxProp,
5-
Token as PrimerToken,
6-
useTheme,
7-
theme as defaultTheme,
8-
} from '@primer/react'
9-
import css from '@styled-system/css'
1+
import React, {forwardRef} from 'react'
2+
import {type TokenProps as PrimerTokenProps, Token as PrimerToken} from '@primer/react'
3+
import {sx, type SxProp} from '../sx'
104
import type {ForwardRefComponent} from '../polymorphic'
115
import type {PropsWithChildren} from 'react'
6+
import styled from 'styled-components'
127

138
type TokenProps = PropsWithChildren<PrimerTokenProps> & SxProp
149

15-
const Token: ForwardRefComponent<'a' | 'button' | 'span', TokenProps> = React.forwardRef<HTMLElement, TokenProps>(
16-
({sx: sxProp, style, ...rest}, ref) => {
17-
const contextTheme = useTheme()
18-
const theme = contextTheme.theme || defaultTheme
10+
const StyledToken: ForwardRefComponent<'a' | 'button' | 'span', TokenProps> = styled(PrimerToken).withConfig({
11+
shouldForwardProp: prop => (prop as keyof TokenProps) !== 'sx',
12+
})<TokenProps>`
13+
${sx}
14+
`
1915

20-
// If no sx prop is provided, just return PrimerToken directly
21-
if (!sxProp) {
22-
return <PrimerToken {...rest} style={style} ref={ref} />
23-
}
24-
25-
// Convert sx to CSS styles using the theme context
26-
const sxStyles = css(sxProp)(theme)
27-
const mergedStyle = {...sxStyles, ...style}
28-
29-
return <PrimerToken {...rest} style={mergedStyle} ref={ref} />
30-
},
31-
) as ForwardRefComponent<'a' | 'button' | 'span', TokenProps>
16+
const Token = forwardRef<HTMLElement, TokenProps>(({as, ...props}, ref) => {
17+
return <StyledToken {...props} {...(as ? {forwardedAs: as} : {})} ref={ref} />
18+
}) as ForwardRefComponent<'a' | 'button' | 'span', TokenProps>
3219

3320
export {Token, type TokenProps}

0 commit comments

Comments
 (0)