1
1
import type React from 'react'
2
- import { type ElementType } from 'react'
2
+ import { forwardRef , type ElementType } from 'react'
3
3
import type { ResponsiveValue } from '../hooks/useResponsiveValue'
4
4
import { getResponsiveAttributes } from '../internal/utils/getResponsiveAttributes'
5
5
import classes from './Stack.module.css'
6
6
import { clsx } from 'clsx'
7
- import { toggleSxComponent } from '../internal/utils/toggleSxComponent'
7
+ import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../utils/polymorphic'
8
+ import { BoxWithFallback } from '../internal/components/BoxWithFallback'
8
9
9
10
type GapScale = 'none' | 'condensed' | 'normal' | 'spacious'
10
11
type Gap = GapScale | ResponsiveValue < GapScale >
@@ -67,35 +68,40 @@ type StackProps<As> = React.PropsWithChildren<{
67
68
className ?: string
68
69
} >
69
70
70
- const StackBaseComponent = toggleSxComponent ( 'div' ) as React . ComponentType < StackProps < React . ElementType > >
71
- function Stack < As extends ElementType > ( {
72
- as,
73
- children,
74
- align = 'stretch' ,
75
- direction = 'vertical' ,
76
- gap,
77
- justify = 'start' ,
78
- padding = 'none' ,
79
- wrap = 'nowrap' ,
80
- className,
81
- ...rest
82
- } : StackProps < As > & React . ComponentPropsWithoutRef < ElementType extends As ? As : 'div' > ) {
83
- return (
84
- < StackBaseComponent
85
- as = { as }
86
- { ...rest }
87
- className = { clsx ( className , classes . Stack ) }
88
- { ...getResponsiveAttributes ( 'gap' , gap ) }
89
- { ...getResponsiveAttributes ( 'direction' , direction ) }
90
- { ...getResponsiveAttributes ( 'align' , align ) }
91
- { ...getResponsiveAttributes ( 'wrap' , wrap ) }
92
- { ...getResponsiveAttributes ( 'justify' , justify ) }
93
- { ...getResponsiveAttributes ( 'padding' , padding ) }
94
- >
95
- { children }
96
- </ StackBaseComponent >
97
- )
98
- }
71
+ const Stack = forwardRef (
72
+ (
73
+ {
74
+ as,
75
+ children,
76
+ align = 'stretch' ,
77
+ direction = 'vertical' ,
78
+ gap,
79
+ justify = 'start' ,
80
+ padding = 'none' ,
81
+ wrap = 'nowrap' ,
82
+ className,
83
+ ...rest
84
+ } ,
85
+ forwardedRef ,
86
+ ) => {
87
+ return (
88
+ < BoxWithFallback
89
+ as = { as }
90
+ ref = { forwardedRef }
91
+ { ...rest }
92
+ className = { clsx ( className , classes . Stack ) }
93
+ { ...getResponsiveAttributes ( 'gap' , gap ) }
94
+ { ...getResponsiveAttributes ( 'direction' , direction ) }
95
+ { ...getResponsiveAttributes ( 'align' , align ) }
96
+ { ...getResponsiveAttributes ( 'wrap' , wrap ) }
97
+ { ...getResponsiveAttributes ( 'justify' , justify ) }
98
+ { ...getResponsiveAttributes ( 'padding' , padding ) }
99
+ >
100
+ { children }
101
+ </ BoxWithFallback >
102
+ )
103
+ } ,
104
+ ) as PolymorphicForwardRefComponent < ElementType , StackProps < ElementType > >
99
105
100
106
type StackItemProps < As > = React . PropsWithChildren < {
101
107
/**
@@ -111,25 +117,19 @@ type StackItemProps<As> = React.PropsWithChildren<{
111
117
className ?: string
112
118
} >
113
119
114
- const StackItemBaseComponent = toggleSxComponent ( 'div' ) as React . ComponentType < StackItemProps < React . ElementType > >
115
- function StackItem < As extends ElementType > ( {
116
- as,
117
- children,
118
- grow,
119
- className,
120
- ...rest
121
- } : StackItemProps < As > & React . ComponentPropsWithoutRef < ElementType extends As ? As : 'div' > ) {
120
+ const StackItem = forwardRef ( ( { as, children, grow, className, ...rest } , forwardedRef ) => {
122
121
return (
123
- < StackItemBaseComponent
122
+ < BoxWithFallback
124
123
as = { as }
124
+ ref = { forwardedRef }
125
125
{ ...rest }
126
126
className = { clsx ( className , classes . StackItem ) }
127
127
{ ...getResponsiveAttributes ( 'grow' , grow ) }
128
128
>
129
129
{ children }
130
- </ StackItemBaseComponent >
130
+ </ BoxWithFallback >
131
131
)
132
- }
132
+ } ) as PolymorphicForwardRefComponent < ElementType , StackProps < ElementType > >
133
133
134
134
export { Stack , StackItem }
135
135
export type { StackProps , StackItemProps }
0 commit comments