-
Notifications
You must be signed in to change notification settings - Fork 608
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
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0547789
fix(Stack): correctly forward a Ref
francinelucca 1e4f433
Create loud-ants-taste.md
francinelucca 7ef2763
Update packages/react/src/Stack/Stack.tsx
francinelucca a66b3f4
fix stack types
francinelucca 455d486
Merge branch 'main' into fix/stack-forward-ref
francinelucca 5ddf06b
Update packages/react/src/Stack/Stack.tsx
francinelucca 069f235
Merge branch 'main' into fix/stack-forward-ref
francinelucca 22c0c72
add import
francinelucca bf01578
remove unused var
francinelucca ee7e43e
Merge branch 'main' into fix/stack-forward-ref
francinelucca 0a1754d
Merge branch 'main' into fix/stack-forward-ref
francinelucca 2ea0a39
type fixes
francinelucca dd37bd8
Merge branch 'fix/stack-forward-ref' of github.com:primer/react into …
francinelucca a116c27
Merge branch 'main' into fix/stack-forward-ref
francinelucca b463596
remove unused import
francinelucca 9b47199
Merge branch 'fix/stack-forward-ref' of github.com:primer/react into …
francinelucca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/react": patch | ||
--- | ||
|
||
fix(Stack): correctly forward a Ref |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
@@ -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<{ | ||
/** | ||
|
@@ -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>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think since we're casting There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.