Skip to content

fix(Stack): correctly forward a Ref #6111

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

Merged
merged 16 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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/loud-ants-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

fix(Stack): correctly forward a Ref
107 changes: 60 additions & 47 deletions packages/react/src/Stack/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type React from 'react'
import {type ElementType} from 'react'
import {forwardRef, type ElementType} from 'react'
import type {ResponsiveValue} from '../hooks/useResponsiveValue'
import {getResponsiveAttributes} from '../internal/utils/getResponsiveAttributes'
import classes from './Stack.module.css'
import {clsx} from 'clsx'
import {toggleSxComponent} from '../internal/utils/toggleSxComponent'
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'

type GapScale = 'none' | 'condensed' | 'normal' | 'spacious'
type Gap = GapScale | ResponsiveValue<GapScale>
Expand Down Expand Up @@ -68,34 +69,40 @@ type StackProps<As> = React.PropsWithChildren<{
}>

const StackBaseComponent = toggleSxComponent('div') as React.ComponentType<StackProps<React.ElementType>>
function Stack<As extends ElementType>({
as,
children,
align = 'stretch',
direction = 'vertical',
gap,
justify = 'start',
padding = 'none',
wrap = 'nowrap',
className,
...rest
}: StackProps<As> & React.ComponentPropsWithoutRef<ElementType extends As ? As : 'div'>) {
return (
<StackBaseComponent
as={as}
{...rest}
className={clsx(className, classes.Stack)}
{...getResponsiveAttributes('gap', gap)}
{...getResponsiveAttributes('direction', direction)}
{...getResponsiveAttributes('align', align)}
{...getResponsiveAttributes('wrap', wrap)}
{...getResponsiveAttributes('justify', justify)}
{...getResponsiveAttributes('padding', padding)}
>
{children}
</StackBaseComponent>
)
}
const Stack = forwardRef(
<As extends ElementType>(
{
as,
children,
align = 'stretch',
direction = 'vertical',
gap,
justify = 'start',
padding = 'none',
wrap = 'nowrap',
className,
...rest
}: StackProps<As> & React.ComponentPropsWithRef<ElementType extends As ? As : 'div'>,
forwardedRef: React.Ref<As> | undefined,
) => {
return (
<StackBaseComponent
as={as}
ref={forwardedRef}
{...rest}
className={clsx(className, classes.Stack)}
{...getResponsiveAttributes('gap', gap)}
{...getResponsiveAttributes('direction', direction)}
{...getResponsiveAttributes('align', align)}
{...getResponsiveAttributes('wrap', wrap)}
{...getResponsiveAttributes('justify', justify)}
{...getResponsiveAttributes('padding', padding)}
>
{children}
</StackBaseComponent>
)
},
) as PolymorphicForwardRefComponent<ElementType, StackProps<ElementType>>

type StackItemProps<As> = React.PropsWithChildren<{
/**
Expand All @@ -112,24 +119,30 @@ type StackItemProps<As> = React.PropsWithChildren<{
}>

const StackItemBaseComponent = toggleSxComponent('div') as React.ComponentType<StackItemProps<React.ElementType>>
function StackItem<As extends ElementType>({
as,
children,
grow,
className,
...rest
}: StackItemProps<As> & React.ComponentPropsWithoutRef<ElementType extends As ? As : 'div'>) {
return (
<StackItemBaseComponent
as={as}
{...rest}
className={clsx(className, classes.StackItem)}
{...getResponsiveAttributes('grow', grow)}
>
{children}
</StackItemBaseComponent>
)
}
const StackItem = forwardRef(
<As extends ElementType>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since we're casting StackItem below with as that the generic here and the StackItemProps/ref can also be removed 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah my bad, fixed!

{
as,
children,
grow,
className,
...rest
}: StackItemProps<As> & React.ComponentPropsWithRef<ElementType extends As ? As : 'div'>,
forwardRef: React.Ref<As> | undefined,
) => {
return (
<StackItemBaseComponent
as={as}
ref={forwardRef}
{...rest}
className={clsx(className, classes.StackItem)}
{...getResponsiveAttributes('grow', grow)}
>
{children}
</StackItemBaseComponent>
)
},
) as PolymorphicForwardRefComponent<ElementType, StackProps<ElementType>>

export {Stack, StackItem}
export type {StackProps, StackItemProps}
Loading