Skip to content

Commit 2d37fab

Browse files
chore: ActionList use forwardedAs in subcomponents (#6995)
Co-authored-by: Copilot <[email protected]>
1 parent 701a9d0 commit 2d37fab

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

.changeset/real-cooks-melt.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: ActionList use forwardedAs in subcomponents

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ type PrimerActionListTrailingActionProps = React.ComponentProps<typeof PrimerAct
1818

1919
export type ActionListProps<As extends React.ElementType = 'ul'> = PrimerActionListProps<As> & SxProp
2020
export type ActionListItemProps = React.PropsWithChildren<PrimerActionListItemProps & SxProp>
21-
export type ActionListLinkItemProps = React.PropsWithChildren<PrimerActionListLinkItemProps & SxProp>
22-
export type ActionListGroupProps = React.PropsWithChildren<PrimerActionListGroupProps & SxProp>
21+
export type ActionListLinkItemProps = React.PropsWithChildren<PrimerActionListLinkItemProps & SxProp> & {
22+
as?: React.ElementType
23+
}
24+
export type ActionListGroupProps = React.PropsWithChildren<PrimerActionListGroupProps & SxProp> & {
25+
as?: React.ElementType
26+
}
2327
export type ActionListDividerProps = React.PropsWithChildren<PrimerActionListDividerProps & SxProp>
24-
export type ActionListLeadingVisualProps = React.PropsWithChildren<PrimerActionListLeadingVisualProps & SxProp>
25-
export type ActionListTrailingVisualProps = React.PropsWithChildren<PrimerActionListTrailingVisualProps & SxProp>
28+
export type ActionListLeadingVisualProps = React.PropsWithChildren<PrimerActionListLeadingVisualProps & SxProp> & {
29+
as?: React.ElementType
30+
}
31+
export type ActionListTrailingVisualProps = React.PropsWithChildren<PrimerActionListTrailingVisualProps & SxProp> & {
32+
as?: React.ElementType
33+
}
2634
export type ActionListTrailingActionProps = React.PropsWithChildren<PrimerActionListTrailingActionProps & SxProp>
2735

2836
const StyledActionList = styled(PrimerActionList).withConfig({
@@ -40,14 +48,23 @@ const ActionListImpl = React.forwardRef(function ActionListImpl<As extends React
4048
return <StyledActionList ref={ref} {...rest} {...(as ? {forwardedAs: as} : {})} />
4149
})
4250

43-
const ActionListLinkItem: ForwardRefComponent<'a', ActionListLinkItemProps> & SlotMarker = styled(
51+
const StyledActionListLinkItem: ForwardRefComponent<'a', ActionListLinkItemProps> & SlotMarker = styled(
4452
PrimerActionList.LinkItem,
4553
).withConfig<ActionListLinkItemProps>({
4654
shouldForwardProp: prop => prop !== 'sx',
4755
})`
4856
${sx}
4957
`
5058

59+
const ActionListLinkItem = React.forwardRef<HTMLAnchorElement, ActionListLinkItemProps>(
60+
({children, as, ...props}, ref) => (
61+
<StyledActionListLinkItem ref={ref} {...props} {...(as ? {forwardedAs: as} : {})}>
62+
{children}
63+
</StyledActionListLinkItem>
64+
),
65+
) as ForwardRefComponent<'a', ActionListLinkItemProps> & SlotMarker
66+
ActionListLinkItem.displayName = 'ActionList.LinkItem'
67+
5168
type TrailingActionElements = 'button' | 'a'
5269
const StyledActionListTrailingAction = styled(PrimerActionList.TrailingAction).withConfig({
5370
shouldForwardProp: (prop: keyof ActionListTrailingActionProps) => prop !== 'sx',
@@ -82,14 +99,21 @@ const ActionListItem = React.forwardRef<HTMLLIElement, ActionListItemProps>(({ch
8299
</StyledActionListItem>
83100
)) as ForwardRefComponent<'li', ActionListItemProps> & SlotMarker
84101

85-
const ActionListGroup: React.ComponentType<ActionListGroupProps> & SlotMarker = styled(
102+
const StyledActionListGroup: React.ComponentType<ActionListGroupProps> & SlotMarker = styled(
86103
PrimerActionList.Group,
87104
).withConfig<ActionListGroupProps>({
88105
shouldForwardProp: prop => prop !== 'sx',
89106
})`
90107
${sx}
91108
`
92109

110+
const ActionListGroup: React.ComponentType<ActionListGroupProps> & SlotMarker = ({children, as, ...props}) => (
111+
<StyledActionListGroup {...props} {...(as ? {forwardedAs: as} : {})}>
112+
{children}
113+
</StyledActionListGroup>
114+
)
115+
ActionListGroup.displayName = 'ActionList.Group'
116+
93117
const ActionListDivider: React.ComponentType<ActionListDividerProps> & SlotMarker = styled(
94118
PrimerActionList.Divider,
95119
).withConfig<ActionListDividerProps>({
@@ -98,22 +122,44 @@ const ActionListDivider: React.ComponentType<ActionListDividerProps> & SlotMarke
98122
${sx}
99123
`
100124

101-
const ActionListLeadingVisual: React.ComponentType<ActionListLeadingVisualProps> & SlotMarker = styled(
125+
const StyledActionListLeadingVisual: React.ComponentType<ActionListLeadingVisualProps> & SlotMarker = styled(
102126
PrimerActionList.LeadingVisual,
103127
).withConfig<ActionListLeadingVisualProps>({
104128
shouldForwardProp: prop => prop !== 'sx',
105129
})`
106130
${sx}
107131
`
108132

109-
const ActionListTrailingVisual: React.ComponentType<ActionListTrailingVisualProps> & SlotMarker = styled(
133+
const ActionListLeadingVisual: React.ComponentType<ActionListLeadingVisualProps> & SlotMarker = ({
134+
children,
135+
as,
136+
...props
137+
}) => (
138+
<StyledActionListLeadingVisual {...props} {...(as ? {forwardedAs: as} : {})}>
139+
{children}
140+
</StyledActionListLeadingVisual>
141+
)
142+
ActionListLeadingVisual.displayName = 'ActionList.LeadingVisual'
143+
144+
const StyledActionListTrailingVisual: React.ComponentType<ActionListTrailingVisualProps> & SlotMarker = styled(
110145
PrimerActionList.TrailingVisual,
111146
).withConfig<ActionListTrailingVisualProps>({
112147
shouldForwardProp: prop => prop !== 'sx',
113148
})`
114149
${sx}
115150
`
116151

152+
const ActionListTrailingVisual: React.ComponentType<ActionListTrailingVisualProps> & SlotMarker = ({
153+
children,
154+
as,
155+
...props
156+
}) => (
157+
<StyledActionListTrailingVisual {...props} {...(as ? {forwardedAs: as} : {})}>
158+
{children}
159+
</StyledActionListTrailingVisual>
160+
)
161+
ActionListTrailingVisual.displayName = 'ActionList.TrailingVisual'
162+
117163
export const ActionList: typeof ActionListImpl & {
118164
Item: typeof ActionListItem
119165
LinkItem: typeof ActionListLinkItem

0 commit comments

Comments
 (0)