@@ -11,7 +11,7 @@ import {
1111 isValidElement ,
1212 memo ,
1313} from 'react'
14- import { composeRefs } from '../utils/compose-refs.ts'
14+ import { useComposedRefs } from '../utils/compose-refs.ts'
1515
1616export interface PolymorphicProps {
1717 /**
@@ -47,22 +47,22 @@ const withAsChild = (Component: React.ElementType) => {
4747 const Comp = memo (
4848 forwardRef < unknown , ArkPropsWithRef < typeof Component > > ( ( props , ref ) => {
4949 const { asChild, children, ...restProps } = props
50+ const onlyChild =
51+ asChild && isValidElement < Record < string , unknown > > ( children ) ? Children . only ( children ) : undefined
52+ const childRef = onlyChild ? getRef ( onlyChild ) : undefined
53+ const composedRef = useComposedRefs ( ref , childRef )
5054
5155 if ( ! asChild ) {
5256 return createElement ( Component , { ...restProps , ref } , children )
5357 }
5458
55- if ( ! isValidElement < Record < string , unknown > > ( children ) ) {
59+ if ( ! onlyChild ) {
5660 return null
5761 }
5862
59- const onlyChild : React . ReactElement < Record < string , unknown > > = Children . only ( children )
60-
61- const childRef = getRef ( onlyChild )
62-
6363 return cloneElement ( onlyChild , {
6464 ...mergeProps ( restProps , onlyChild . props ) ,
65- ref : ref ? composeRefs ( ref , childRef ) : childRef ,
65+ ref : ref ? composedRef : childRef ,
6666 } )
6767 } ) ,
6868 )
0 commit comments