Created
February 12, 2024 10:22
-
-
Save typeofweb/06b01e6248c57d0d35c20462cc06eb91 to your computer and use it in GitHub Desktop.
`asChild` pattern in React
This file contains 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
// https://www.jacobparis.com/content/react-as-child | |
import clsx from "clsx"; | |
import { isValidElement, cloneElement, Children } from "react"; | |
export const Slot = ({ | |
children, | |
...props | |
}: React.HTMLAttributes<HTMLElement> & { | |
children?: React.ReactNode; | |
}) => { | |
if (isValidElement(children)) { | |
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | |
return cloneElement(children, { | |
...props, | |
...children.props, | |
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access | |
className: clsx(children.props.className, props.className), | |
}); | |
} | |
if (Children.count(children) > 1) { | |
Children.only(null); | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment