Skip to content

Commit 5e32b20

Browse files
committed
feat(attrs): support using attrs value in style
1 parent 93014ed commit 5e32b20

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

packages/core/src/styled.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { type ExpressionType, generateClassName, generateComponentName, insertEx
1717
import { isStyledComponent, isValidElementType, isVueComponent } from '@/src/helper'
1818
import type { DefaultTheme } from './providers/theme'
1919

20-
type Attrs = Record<string, any>
21-
2220
export type BaseContext<T> = T & { theme: DefaultTheme }
2321

2422
export type PropsDefinition<T> = {
@@ -35,14 +33,14 @@ interface StyledComponent<T extends object> {
3533
)[]
3634
): DefineSetupFnComponent<{ as?: string; props?: P } & ExtractPropTypes<PropsDefinition<T>> & HTMLAttributes>
3735

38-
attrs<A extends Attrs = Record<string, any>>(attrs: A): StyledComponent<T>
36+
attrs<A extends object>(attrs: A): StyledComponent<A & T>
3937
}
4038

4139
function baseStyled<T extends object>(target: string | InstanceType<any>, propsDefinition?: PropsDefinition<T>): StyledComponent<T> {
4240
if (!isValidElementType(target)) {
4341
throw Error('The element is invalid.')
4442
}
45-
let attributes: Attrs = {}
43+
let attributes = {}
4644
function styledComponent<P>(
4745
styles: TemplateStringsArray,
4846
...expressions: (
@@ -54,7 +52,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
5452
return createStyledComponent<P>(cssStringsWithExpression)
5553
}
5654

57-
styledComponent.attrs = function <A extends Attrs = Record<string, any>>(attrs: A) {
55+
styledComponent.attrs = function <A extends object>(attrs: A) {
5856
attributes = attrs
5957
return styledComponent
6058
}
@@ -72,23 +70,24 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
7270
return defineComponent(
7371
(props, { slots }) => {
7472
const tailwindClasses = ref<string[]>([])
75-
const myAttrs = ref({ class: '', ...attributes })
73+
const internalProps = ref({ class: '', ...attributes })
7674
const theme = inject<Record<string, string | number>>('$theme', reactive({}))
7775
let context = {
7876
theme,
7977
...props,
8078
...props.props,
79+
...attributes,
8180
}
8281

8382
const defaultClassName = generateClassName()
8483

85-
myAttrs.value.class += ` ${defaultClassName}`
84+
internalProps.value.class += ` ${defaultClassName}`
8685

8786
// Inject the tailwind classes to the class attribute
8887
watch(
8988
tailwindClasses,
9089
(classNames) => {
91-
myAttrs.value.class += ` ${classNames.join(' ')}`
90+
internalProps.value.class += ` ${classNames.join(' ')}`
9291
},
9392
{ deep: true },
9493
)
@@ -97,6 +96,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
9796
[theme, props],
9897
() => {
9998
context = {
99+
...internalProps.value,
100100
theme,
101101
...props,
102102
...props.props,
@@ -109,6 +109,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
109109
)
110110

111111
onMounted(() => {
112+
console.log(internalProps.value)
112113
tailwindClasses.value = injectStyle(defaultClassName, cssWithExpression, context)
113114
})
114115

@@ -122,7 +123,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
122123
return h(
123124
node,
124125
{
125-
...myAttrs.value,
126+
...internalProps.value,
126127
},
127128
slots,
128129
)
@@ -146,7 +147,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
146147
)
147148
}
148149

149-
return styledComponent
150+
return styledComponent as StyledComponent<T>
150151
}
151152

152153
/** Append all the supported HTML elements to the styled properties */

0 commit comments

Comments
 (0)