Skip to content

Commit

Permalink
Access ref from props before element.ref
Browse files Browse the repository at this point in the history
  • Loading branch information
chungweileong94 committed Apr 2, 2024
1 parent b32a933 commit 82bff21
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/react/slot/src/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ const SlotClone = React.forwardRef<any, SlotCloneProps>((props, forwardedRef) =>
const { children, ...slotProps } = props;

if (React.isValidElement(children)) {
// Accessing the ref from props, else fallback to element.ref
// https://github.com/facebook/react/pull/28348
const childrenRef = children.props.ref ?? (children as any).ref;
return React.cloneElement(children, {
...mergeProps(slotProps, children.props),
ref: forwardedRef ? composeRefs(forwardedRef, (children as any).ref) : (children as any).ref,
ref: forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef,
});
}

Expand Down

0 comments on commit 82bff21

Please sign in to comment.