@@ -19,12 +19,12 @@ import {
1919 RefAttributes
2020} from "react" ;
2121
22- export { $$styled } from "./runtime .js" ;
22+ import { tags , SupportedElements } from "./tags .js" ;
2323
24- type KeyofIntrinsicElements = keyof JSX . IntrinsicElements ;
24+ export { $$styled } from "./runtime.js" ;
2525
2626// == Main =====================================================================
27- export function styled <
27+ function styledImpl <
2828 Props ,
2929 Component extends ForwardRefExoticComponent < Props > ,
3030 RulesVariants extends VariantGroups | undefined = undefined ,
@@ -41,7 +41,7 @@ export function styled<
4141 RulesProps
4242> ;
4343
44- export function styled <
44+ function styledImpl <
4545 Props ,
4646 Component extends ComponentType < Props > ,
4747 RulesVariants extends VariantGroups | undefined = undefined ,
@@ -58,7 +58,7 @@ export function styled<
5858 RulesProps
5959> ;
6060
61- export function styled <
61+ function styledImpl <
6262 Props extends object ,
6363 Component extends ElementType ,
6464 RulesVariants extends VariantGroups | undefined = undefined ,
@@ -75,8 +75,8 @@ export function styled<
7575 RulesProps
7676> ;
7777
78- export function styled <
79- Component extends KeyofIntrinsicElements ,
78+ function styledImpl <
79+ Component extends SupportedElements ,
8080 RulesVariants extends VariantGroups | undefined = undefined ,
8181 RulesToggleVariants extends VariantDefinitions | undefined = undefined ,
8282 RulesProps extends ComplexPropDefinitions < PropTarget > | undefined = undefined
@@ -91,11 +91,11 @@ export function styled<
9191 RulesProps
9292> ;
9393
94- export function styled <
94+ function styledImpl <
9595 Component extends
9696 | ForwardRefExoticComponent < unknown >
9797 | ComponentType < unknown >
98- | KeyofIntrinsicElements
98+ | SupportedElements
9999 | ComponentWithAs < object , ElementType > ,
100100 RulesVariants extends VariantGroups | undefined = undefined ,
101101 RulesToggleVariants extends VariantDefinitions | undefined = undefined ,
@@ -115,23 +115,58 @@ export function styled<
115115 ) ;
116116}
117117
118- type IntrinsicProps < TComponent > = TComponent extends KeyofIntrinsicElements
118+ type TaggedStyled = {
119+ [ Tag in SupportedElements ] : <
120+ RulesVariants extends VariantGroups | undefined = undefined ,
121+ RulesToggleVariants extends VariantDefinitions | undefined = undefined ,
122+ RulesProps extends
123+ | ComplexPropDefinitions < PropTarget >
124+ | undefined = undefined
125+ > (
126+ options : PatternOptions < RulesVariants , RulesToggleVariants , RulesProps >
127+ ) => StyledComponent <
128+ IntrinsicProps < Tag > ,
129+ Tag ,
130+ RulesVariants ,
131+ RulesToggleVariants ,
132+ RulesProps
133+ > ;
134+ } ;
135+
136+ const taggedStyled : TaggedStyled = { } as TaggedStyled ;
137+
138+ function createStyledComponent (
139+ tag : SupportedElements
140+ ) : TaggedStyled [ SupportedElements ] {
141+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
142+ // @ts -ignore Prevent TS too complex type inference error
143+ return ( options : unknown ) => styledImpl ( tag , options ) ;
144+ }
145+
146+ tags . forEach ( ( tag ) => {
147+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
148+ // @ts -ignore Prevent TS too complex type inference error
149+ taggedStyled [ tag ] = createStyledComponent ( tag ) ;
150+ } ) ;
151+
152+ export const styled = Object . assign ( styledImpl , taggedStyled as TaggedStyled ) ;
153+
154+ type IntrinsicProps < TComponent > = TComponent extends SupportedElements
119155 ? JSX . IntrinsicElements [ TComponent ]
120156 : never ;
121157
122- type InferStyledComponentProps < Component > =
123- Component extends KeyofIntrinsicElements
124- ? IntrinsicProps < Component >
125- : Component extends ComponentType < infer ComponentTypeProps >
126- ? ComponentTypeProps
127- : Component extends ForwardRefExoticComponent < infer ForwardRefProps >
128- ? ForwardRefProps
129- : Component extends ComponentWithAs < infer WithAsProps , ElementType >
130- ? WithAsProps
131- : never ;
158+ type InferStyledComponentProps < Component > = Component extends SupportedElements
159+ ? IntrinsicProps < Component >
160+ : Component extends ComponentType < infer ComponentTypeProps >
161+ ? ComponentTypeProps
162+ : Component extends ForwardRefExoticComponent < infer ForwardRefProps >
163+ ? ForwardRefProps
164+ : Component extends ComponentWithAs < infer WithAsProps , ElementType >
165+ ? WithAsProps
166+ : never ;
132167
133168type InferStyledComponentElement < Component > =
134- Component extends KeyofIntrinsicElements
169+ Component extends SupportedElements
135170 ? Component
136171 : Component extends ElementType
137172 ? Component
0 commit comments