@@ -11,6 +11,7 @@ import {
1111 ref ,
1212 watch ,
1313 HTMLAttributes ,
14+ computed ,
1415} from 'vue'
1516import domElements , { type SupportedHTMLElements } from '@/src/constants/domElements'
1617import { type ExpressionType , generateClassName , generateComponentName , insertExpressions , injectStyle , removeStyle } from '@/src/utils'
@@ -33,14 +34,16 @@ interface StyledComponent<T extends object> {
3334 ) [ ]
3435 ) : DefineSetupFnComponent < { as ?: string ; props ?: P } & ExtractPropTypes < PropsDefinition < T > > & HTMLAttributes >
3536
36- attrs < A extends object > ( attrs : A ) : StyledComponent < A & T >
37+ attrs < A = object > (
38+ attrs : A | ( ( props : ExtractPropTypes < PropsDefinition < T > > ) => A ) ,
39+ ) : StyledComponent < A & ExtractPropTypes < PropsDefinition < T > > >
3740}
3841
3942function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > ) : StyledComponent < T > {
4043 if ( ! isValidElementType ( target ) ) {
4144 throw Error ( 'The element is invalid.' )
4245 }
43- let attributes = { }
46+ let defaultAttrs : unknown
4447 function styledComponent < P > (
4548 styles : TemplateStringsArray ,
4649 ...expressions : (
@@ -53,7 +56,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
5356 }
5457
5558 styledComponent . attrs = function < A extends object > ( attrs : A ) {
56- attributes = attrs
59+ defaultAttrs = attrs
5760 return styledComponent
5861 }
5962
@@ -69,14 +72,25 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
6972 const componentName = generateComponentName ( type )
7073 return defineComponent (
7174 ( props , { slots } ) => {
75+ const internalAttrs = computed < Record < string , any > > ( ( ) => {
76+ if ( typeof defaultAttrs === 'function' ) {
77+ return defaultAttrs ( props )
78+ }
79+ if ( typeof defaultAttrs === 'object' ) {
80+ return defaultAttrs
81+ }
82+ return { }
83+ } )
84+
7285 const tailwindClasses = ref < string [ ] > ( [ ] )
73- const internalProps = ref ( { class : '' , ...attributes } )
86+ const internalProps = ref ( { class : '' , ...internalAttrs . value } )
7487 const theme = inject < Record < string , string | number > > ( '$theme' , reactive ( { } ) )
88+
7589 let context = {
7690 theme,
7791 ...props ,
7892 ...props . props ,
79- ...attributes ,
93+ ...internalAttrs . value ,
8094 }
8195
8296 const defaultClassName = generateClassName ( )
@@ -109,7 +123,6 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
109123 )
110124
111125 onMounted ( ( ) => {
112- console . log ( internalProps . value )
113126 tailwindClasses . value = injectStyle ( defaultClassName , cssWithExpression , context )
114127 } )
115128
0 commit comments