import { type ComponentProps, type ComponentPropsWithRef, type ComponentPropsWithoutRef, type ElementRef, type ElementType, type ForwardedRef, type PropsWithChildren, type ReactElement, forwardRef } from "react"; export function extend( Component: ComponentType, applyAdditionalProps?: ( props: PropsWithChildren & AdditionalProps> & { className?: string; } ) => ComponentProps ) { return forwardRef< ElementRef, ComponentPropsWithoutRef & AdditionalProps >((props, ref) => { // Initialize newProps with a default empty object or the result of applyAdditionalProps let newProps: ComponentProps & AdditionalProps = applyAdditionalProps ? applyAdditionalProps(props as any) : (props as any); // Append className if it exists in both props and newProps if (props.className && newProps.className) { newProps = { ...newProps, className: `${props.className} ${newProps.className}` }; } // @ts-expect-error haven't figured out the correct typing return ; }); } type RenderFunction = ( props: PropsWithChildren & AdditionalProps> & { className?: string; }, ref: ForwardedRef> ) => ReactElement; export function extendComponent( renderFunction: RenderFunction ) { // The extended component using forwardRef to forward the ref to the custom component const ExtendedComponent = forwardRef< ElementRef, ComponentPropsWithRef & AdditionalProps >((props, ref) => { return renderFunction(props as any, ref); }); return ExtendedComponent; } /* export const Content = forwardRef< ElementRef, ComponentPropsWithoutRef >(({ className, ...props }, forwardedRef) => ( )); export const Item = forwardRef< ElementRef, ComponentPropsWithoutRef >(({ className, ...props }, forwardedRef) => ( )); */