Add a Generic Box Styled Component for Third-Party Components #267
Closed
divineniiquaye
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
|
Since this doesn't yet exist in Uniwind, anyone looking to use this feature, here's a workaround to double Uniwind as a component and config. import { ComponentPropsWithRef, ElementType, useMemo } from 'react'
import { Uniwind as UniwindConfig, withUniwind } from 'uniwind'
type AnyObject = Record<PropertyKey, any>
type StyleToClass<K extends PropertyKey> = K extends 'style' ? 'className'
: K extends `${infer StyleProp}Style` ? `${StyleProp}ClassName`
: never
type ColorPropToClass<K extends PropertyKey> = K extends 'color' ? 'colorClassName'
: K extends `${string}Color` | `${string}color${string}` ? `${K}ClassName`
: never
type ApplyUniwind<TProps extends AnyObject> =
& {
[K in keyof TProps as StyleToClass<K>]?: string
}
& {
[K in keyof TProps as ColorPropToClass<K>]?: string
}
& TProps
type BoxProps<T extends ElementType> = {
as: T
} & ApplyUniwind<Omit<ComponentPropsWithRef<T>, 'as'>>
const Box = <T extends ElementType>({ as, ...props }: BoxProps<T>) => {
const StyledComponent = useMemo(() => withUniwind(as as any), [as])
return <StyledComponent {...props} />
}
export const Uniwind = Object.assign(
Box,
UniwindConfig,
) as typeof UniwindConfig & typeof Box |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I don't really understand how is it going to make migration smoother and limit boilerplate. You can simply reexport components wrapped with |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Uniwind currently provides the withUniwind HOC to enable styling for third-party components, which works well. However, it introduces some friction when used repeatedly across an app, especially when dealing with many external components.
I’d like to propose adding a generic styled component (e.g. Box) that can style any component, including third-party ones, while preserving proper type inference.
Current Approach
Today, styling third-party components requires explicitly wrapping each component with withUniwind:
This works, but it:
Proposed API
Introduce a generic styled component such as Box that internally uses withUniwind and can render any component via an as prop:
Key Expectations
Thanks for the great work on Uniwind — this feature would make third-party integration even smoother 🚀
Beta Was this translation helpful? Give feedback.
All reactions