-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update src/client/components/common/componentUtils.tsx
Co-authored-by: Przemysław Żydek <[email protected]>
- Loading branch information
1 parent
92cd7af
commit 21f46bd
Showing
1 changed file
with
7 additions
and
12 deletions.
There are no files selected for viewing
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
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; | ||
} | ||
|