Open jahirfiquitiva opened 1 year ago
Before finding this library I was using a custom implementation which was working good enough although without many strict types.
anyway, my version of your templateFunctionFactory
accepted an element of type WebTarget
which I got from the styled-components
repo, as is as follows:
import type { ExoticComponent, ComponentType } from 'react';
type AnyComponent<P = unknown> = ExoticComponent<P> | ComponentType<P>;
type KnownTarget = keyof JSX.IntrinsicElements | AnyComponent;
export type WebTarget = string | KnownTarget;
...
I hope this can help you set an initial step for better support of any component
Here's the code for the function that I created in case it helps too
const twx = (classes: TemplateStringsArray): string => {
const cleanClasses = classes
.join(' ')
.split(/\r?\n/)
.map((it) => it.trim());
return twMerge(cleanClasses.join(' ').trim());
};
function baseStyled<T>(tag: WebTarget) {
return (classes: TemplateStringsArray | string): FC => {
const Component = tag;
// eslint-disable-next-line react/display-name
return (
props?: ComponentProps<typeof Component> & { as?: ElementType } & T,
) => {
const { as: asTag, ...otherProps } = props || {};
const FinalComponentTag = asTag || Component;
return (
<FinalComponentTag
{...otherProps}
className={cx(
Array.isArray(classes)
? twx(classes as TemplateStringsArray)
: (classes as string),
otherProps.className,
)}
/>
);
};
};
}
@MathiasGilson would you mind looking into this?
I would like this library to support styling any component and not just the ones created with
tw
I have tried styling 2 pre-built components from Next without success.
1. Image
As you can see, it does not recognize the Image properties as valid
2. Link with the styles of a Button
Having created a
StyledButton
component withtw
(const StyledButton = tw.button
), I want aLink
component to look like thatStyledButton
The whole error reads:
Basically it is expecting
otherProps
to be of the type of props forStyledButton
, even when I have set it to render$as={Link}
and therefore should work withLink
props