11import {
2+ ComponentObjectPropsOptions ,
3+ ExtractPropTypes ,
24 defineComponent ,
3- DefineSetupFnComponent ,
45 h ,
56 inject ,
67 onMounted ,
78 onUnmounted ,
8- PropType ,
9- PublicProps ,
109 reactive ,
1110 ref ,
12- SlotsType ,
1311 watch ,
1412} from 'vue'
1513import domElements , { type SupportedHTMLElements } from '@/src/constants/domElements'
1614import { type ExpressionType , generateClassName , generateComponentName , insertExpressions , injectStyle , removeStyle } from '@/src/utils'
1715import { isStyledComponent , isValidElementType , isVueComponent } from '@/src/helper'
1816import { DefaultTheme } from './providers/theme'
1917
20- interface IProps {
21- as ?: PropType < SupportedHTMLElements >
22- }
23-
24- type ComponentCustomProps = PublicProps & {
25- styled : boolean
26- }
27-
28- export type StyledComponentType < P = any > = DefineSetupFnComponent < IProps & P , any , SlotsType , IProps & P , ComponentCustomProps >
29-
30- type StyledFactory = < T = Record < string , any > > (
31- styles : TemplateStringsArray ,
32- ...expressions : ( ExpressionType < T & { theme : DefaultTheme } > | ExpressionType < T & { theme : DefaultTheme } > [ ] ) [ ]
33- ) => StyledComponentType
34- type StyledComponent = StyledFactory & {
35- attrs : < T extends Record < string , unknown > > ( attrs : T ) => StyledFactory
36- }
3718type Attrs = Record < string , any >
3819
39- function baseStyled < P extends Record < string , any > > ( target : string | InstanceType < any > , propsDefinition ?: P & IProps ) : StyledComponent {
20+ type BaseContext < T > = T & { theme : DefaultTheme }
21+ type PropsDefinition < T > = {
22+ [ K in keyof T ] : T [ K ]
23+ }
24+ function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > ) {
4025 if ( ! isValidElementType ( target ) ) {
4126 throw Error ( 'The element is invalid.' )
4227 }
4328 let attributes : Attrs = { }
44- function styledComponent < T > (
29+ function styledComponent < P > (
4530 styles : TemplateStringsArray ,
46- ...expressions : ( ExpressionType < T > | ExpressionType < T > [ ] ) [ ]
47- ) : StyledComponentType {
48- const cssStringsWithExpression = insertExpressions < T > ( styles , expressions )
49- return createStyledComponent < T > ( cssStringsWithExpression )
31+ ...expressions : (
32+ | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > >
33+ | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > > [ ]
34+ ) [ ]
35+ ) {
36+ const cssStringsWithExpression = insertExpressions ( styles , expressions )
37+ return createStyledComponent < P > ( cssStringsWithExpression )
5038 }
5139
52- styledComponent . attrs = function < T extends Record < string , any > > ( attrs : T ) : StyledComponent {
40+ styledComponent . attrs = function < A extends Attrs = Record < string , any > > ( attrs : A ) {
5341 attributes = attrs
5442 return styledComponent
5543 }
5644
57- function createStyledComponent < T > ( cssWithExpression : ExpressionType < T & { theme : DefaultTheme } > [ ] ) : StyledComponentType {
45+ function createStyledComponent < P > ( cssWithExpression : ExpressionType < any > [ ] ) {
5846 let type : string = target
5947 if ( isVueComponent ( target ) ) {
6048 type = 'vue-component'
@@ -96,15 +84,15 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
9684 ...props ,
9785 ...props . props ,
9886 }
99- tailwindClasses . value = injectStyle < T & { theme : DefaultTheme } > ( defaultClassName , cssWithExpression , context )
87+ tailwindClasses . value = injectStyle ( defaultClassName , cssWithExpression , context )
10088 } ,
10189 {
10290 deep : true ,
10391 } ,
10492 )
10593
10694 onMounted ( ( ) => {
107- tailwindClasses . value = injectStyle < T & { theme : DefaultTheme } > ( defaultClassName , cssWithExpression , context )
95+ tailwindClasses . value = injectStyle ( defaultClassName , cssWithExpression , context )
10896 } )
10997
11098 onUnmounted ( ( ) => {
@@ -127,13 +115,15 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
127115 name : componentName ,
128116 props : {
129117 as : {
130- type : String as PropType < SupportedHTMLElements > ,
118+ type : String ,
119+ required : false ,
131120 } ,
132121 props : {
133- type : Object as PropType < P > ,
122+ type : Object ,
123+ required : false ,
134124 } ,
135125 ...propsDefinition ,
136- } ,
126+ } as ComponentObjectPropsOptions < { as ?: string ; props ?: P } & ExtractPropTypes < PropsDefinition < T > > > ,
137127 inheritAttrs : true ,
138128 } ,
139129 )
@@ -144,7 +134,7 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
144134
145135/** Append all the supported HTML elements to the styled properties */
146136const styled = baseStyled as typeof baseStyled & {
147- [ E in SupportedHTMLElements ] : StyledComponent
137+ [ E in SupportedHTMLElements ] : ReturnType < typeof baseStyled >
148138}
149139
150140domElements . forEach ( ( domElement : SupportedHTMLElements ) => {
0 commit comments