Skip to content

Commit

Permalink
Update src/client/components/common/componentUtils.tsx
Browse files Browse the repository at this point in the history
Co-authored-by: Przemysław Żydek <[email protected]>
  • Loading branch information
JuroUhlar and TheUnderScorer authored Jul 8, 2024
1 parent 92cd7af commit 21f46bd
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/client/components/common/componentUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import classnames from 'classnames';
import React from 'react';
import React, { ComponentType, RefAttributes } from 'react';

/**
* Reusable function if you just need to extend an unstyled primitive (like a Radix component) with a classname
* https://www.radix-ui.com/primitives/docs/guides/styling#extending-a-primitive
*/
export function extendUnstyledPrimitiveWithClass<T extends React.ElementType>(
Component: T,
export function extendUnstyledPrimitiveWithClass<P extends { className?: string }, R>(
Component: ComponentType<P & RefAttributes<R>>,
className: string,
displayName: string,
) {
type ComponentRef = React.ElementRef<T>;
type ComponentProps = React.ComponentPropsWithoutRef<T & React.RefAttributes<T>>;

const WrappedComponent = React.forwardRef<ComponentRef, ComponentProps>(
({ className: incomingClassName, ...props }, ref) => (
// @ts-ignore Don't know how to fix this
<Component ref={ref} className={classnames(className, incomingClassName)} {...props} />
),
);
const WrappedComponent = React.forwardRef<R, P>(({ className: incomingClassName, ...props }, ref) => (
<Component ref={ref} className={classnames(className, incomingClassName)} {...(props as P)} />
));

WrappedComponent.displayName = displayName;
return WrappedComponent;
}

0 comments on commit 21f46bd

Please sign in to comment.