8
8
PropType ,
9
9
PublicProps ,
10
10
reactive ,
11
+ ref ,
11
12
SlotsType ,
12
13
watch ,
13
14
} from 'vue'
@@ -65,14 +66,26 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
65
66
const componentName = generateComponentName ( type )
66
67
return defineComponent (
67
68
( props , { slots } ) => {
68
- const myAttrs = { ...attributes }
69
+ const tailwindClasses = ref < string [ ] > ( [ ] )
70
+ const myAttrs = ref ( { ...attributes } )
69
71
const theme = inject < Record < string , string | number > > ( '$theme' , reactive ( { } ) )
70
72
let context = {
71
73
theme,
72
74
...props ,
73
75
}
74
76
75
- myAttrs . class = generateClassName ( )
77
+ const defaultClassName = generateClassName ( )
78
+
79
+ myAttrs . value . class = defaultClassName
80
+
81
+ // Inject the tailwind classes to the class attribute
82
+ watch (
83
+ tailwindClasses ,
84
+ ( classNames ) => {
85
+ myAttrs . value . class = `${ defaultClassName } ${ classNames . join ( ' ' ) } `
86
+ } ,
87
+ { deep : true } ,
88
+ )
76
89
77
90
watch (
78
91
[ theme , props ] ,
@@ -81,19 +94,19 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
81
94
theme,
82
95
...props ,
83
96
}
84
- injectStyle < T & { theme : DefaultTheme } > ( myAttrs . class , cssWithExpression , context )
97
+ tailwindClasses . value = injectStyle < T & { theme : DefaultTheme } > ( defaultClassName , cssWithExpression , context )
85
98
} ,
86
99
{
87
100
deep : true ,
88
101
} ,
89
102
)
90
103
91
104
onMounted ( ( ) => {
92
- injectStyle < T & { theme : DefaultTheme } > ( myAttrs . class , cssWithExpression , context )
105
+ tailwindClasses . value = injectStyle < T & { theme : DefaultTheme } > ( defaultClassName , cssWithExpression , context )
93
106
} )
94
107
95
108
onUnmounted ( ( ) => {
96
- removeStyle ( myAttrs . class )
109
+ removeStyle ( myAttrs . value . class )
97
110
} )
98
111
99
112
// Return the render function
@@ -102,7 +115,7 @@ function baseStyled<P extends Record<string, any>>(target: string | InstanceType
102
115
return h (
103
116
node ,
104
117
{
105
- ...myAttrs ,
118
+ ...myAttrs . value ,
106
119
} ,
107
120
slots ,
108
121
)
0 commit comments