Skip to content

Commit a3c1fb9

Browse files
committed
use forwardedAs prop in components
1 parent 9faa7e9 commit a3c1fb9

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

.changeset/afraid-eyes-serve.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 forwardedAs prop in styled-react

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type HeaderItemProps as PrimerHeaderItemProps,
44
type HeaderLinkProps as PrimerHeaderLinkProps,
55
Header as PrimerHeader,
6+
type HeaderLinkProps,
67
} from '@primer/react'
78
import {forwardRef} from 'react'
89
import {Box} from './Box'
@@ -11,20 +12,26 @@ import type {SxProp} from '../sx'
1112

1213
type HeaderProps = PrimerHeaderProps & SxProp
1314

14-
const HeaderImpl = forwardRef(function Header(props, ref) {
15+
const StyledHeader = forwardRef(function Header(props, ref) {
1516
return <Box as={PrimerHeader} ref={ref} {...props} />
1617
}) as ForwardRefComponent<'header', HeaderProps>
1718

19+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
20+
const HeaderImpl = ({as, ...props}: HeaderProps) => <StyledHeader forwardedAs={as} {...props} />
21+
1822
type HeaderItemProps = PrimerHeaderItemProps & SxProp
1923

2024
const HeaderItem = forwardRef<HTMLDivElement, HeaderItemProps>(function HeaderItem(props, ref) {
2125
return <Box as={PrimerHeader.Item} ref={ref} {...props} />
2226
})
2327

24-
const HeaderLink = forwardRef<HTMLAnchorElement, PrimerHeaderLinkProps>(function HeaderLink(props, ref) {
28+
const StyledHeaderLink = forwardRef<HTMLAnchorElement, PrimerHeaderLinkProps>(function HeaderLink(props, ref) {
2529
return <Box as={PrimerHeader.Link} ref={ref} {...props} />
2630
})
2731

32+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
33+
const HeaderLink = ({as, ...props}: HeaderLinkProps) => <StyledHeaderLink forwardedAs={as} {...props} />
34+
2835
const Header = Object.assign(HeaderImpl, {
2936
Item: HeaderItem,
3037
Link: HeaderLink,

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ import {sx, type SxProp} from '../sx'
1010
import type {ForwardRefComponent} from '../polymorphic'
1111
import {Box} from './Box'
1212
import type {PropsWithChildren} from 'react'
13+
import React from 'react'
1314

1415
type PageHeaderProps = PrimerPageHeaderProps & SxProp
1516

16-
const PageHeaderImpl: ForwardRefComponent<'div', PageHeaderProps> = styled(
17+
const StyledPageHeader: ForwardRefComponent<'div', PageHeaderProps> = styled(
1718
PrimerPageHeader,
1819
).withConfig<PageHeaderProps>({
1920
shouldForwardProp: prop => prop !== 'sx',
2021
})`
2122
${sx}
2223
`
2324

25+
const PageHeaderImpl = React.forwardRef<HTMLDivElement, PageHeaderProps>(({as, ...props}, ref) => (
26+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
27+
<StyledPageHeader forwardedAs={as} ref={ref} {...props} />
28+
)) as ForwardRefComponent<'div', PageHeaderProps>
29+
2430
type PageHeaderActionsProps = PrimerPageHeaderActionsProps & SxProp
2531

26-
function PageHeaderActions({sx, ...rest}: PageHeaderActionsProps) {
32+
function StyledPageHeaderActions({sx, ...rest}: PageHeaderActionsProps) {
2733
const style: CSSCustomProperties = {}
2834
if (sx) {
2935
// @ts-ignore sx has height attribute
@@ -37,13 +43,16 @@ function PageHeaderActions({sx, ...rest}: PageHeaderActionsProps) {
3743
return <Box {...rest} as={PrimerPageHeader.Actions} style={style} sx={sx} />
3844
}
3945

46+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
47+
const PageHeaderActions = ({as, ...props}: PageHeaderProps) => <StyledPageHeaderActions forwardedAs={as} {...props} />
48+
4049
type PageHeaderTitleProps = PropsWithChildren<PrimerPageHeaderTitleProps> & SxProp
4150

4251
type CSSCustomProperties = {
4352
[key: `--${string}`]: string | number
4453
}
4554

46-
function PageHeaderTitle({sx, ...rest}: PageHeaderTitleProps) {
55+
function StyledPageHeaderTitle({sx, ...rest}: PageHeaderTitleProps) {
4756
const style: CSSCustomProperties = {}
4857
if (sx) {
4958
// @ts-ignore sx can have color attribute
@@ -65,6 +74,9 @@ function PageHeaderTitle({sx, ...rest}: PageHeaderTitleProps) {
6574
return <Box {...rest} as={PrimerPageHeader.Title} style={style} sx={sx} />
6675
}
6776

77+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
78+
const PageHeaderTitle = ({as, ...props}: PageHeaderTitleProps) => <StyledPageHeaderTitle forwardedAs={as} {...props} />
79+
6880
type PageHeaderTitleAreaProps = PropsWithChildren<PrimerPageHeaderTitleAreaProps> & SxProp
6981

7082
const PageHeaderTitleArea: ForwardRefComponent<'div', PageHeaderTitleAreaProps> = styled(
@@ -75,7 +87,7 @@ const PageHeaderTitleArea: ForwardRefComponent<'div', PageHeaderTitleAreaProps>
7587
${sx}
7688
`
7789

78-
type PageHeaderComponent = ForwardRefComponent<'div', PageHeaderProps> & {
90+
type PageHeaderComponentType = ForwardRefComponent<'div', PageHeaderProps> & {
7991
Actions: typeof PageHeaderActions
8092
ContextArea: typeof PrimerPageHeader.ContextArea
8193
ParentLink: typeof PrimerPageHeader.ParentLink
@@ -91,7 +103,7 @@ type PageHeaderComponent = ForwardRefComponent<'div', PageHeaderProps> & {
91103
TrailingAction: typeof PrimerPageHeader.TrailingAction
92104
}
93105

94-
const PageHeader: PageHeaderComponent = Object.assign(PageHeaderImpl, {
106+
const PageHeader: PageHeaderComponentType = Object.assign(PageHeaderImpl, {
95107
Actions: PageHeaderActions,
96108
ContextArea: PrimerPageHeader.ContextArea,
97109
ParentLink: PrimerPageHeader.ParentLink,

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ import {sx, type SxProp} from '../sx'
1111

1212
export type UnderlineNavProps = PrimerUnderlineNavProps & SxProp
1313

14-
const UnderlineNavImpl = forwardRef<HTMLElement, UnderlineNavProps>(function UnderlineNav(props, ref) {
14+
const StyledUnderlineNav = forwardRef<HTMLElement, UnderlineNavProps>(function UnderlineNav(props, ref) {
1515
return <Box as={PrimerUnderlineNav} ref={ref} {...props} />
1616
})
1717

18+
export const UnderlineNavImpl = ({as, ...props}: UnderlineNavProps) => (
19+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
20+
<StyledUnderlineNav forwardedAs={as} {...props} />
21+
)
22+
1823
export type UnderlineNavItemProps = PrimerUnderlineNavItemProps & SxProp
1924

20-
const UnderlineNavItem: ForwardRefComponent<'a', UnderlineNavItemProps> = styled(
25+
const StyledUnderlineNavItem: ForwardRefComponent<'a', UnderlineNavItemProps> = styled(
2126
PrimerUnderlineNav.Item,
2227
).withConfig<UnderlineNavItemProps>({
2328
shouldForwardProp: prop => prop !== 'sx',
2429
})`
2530
${sx}
2631
`
32+
export const UnderlineNavItem = ({as, ...props}: UnderlineNavItemProps) => (
33+
// @ts-ignore forwardedAs is valid here but I don't know how to fix the typescript error
34+
<StyledUnderlineNavItem forwardedAs={as} {...props} />
35+
)
2736

2837
export const UnderlineNav = Object.assign(UnderlineNavImpl, {
2938
Item: UnderlineNavItem,

0 commit comments

Comments
 (0)