Skip to content

Commit 9f143b2

Browse files
committed
fix(core): [styled] as does not take effect
1 parent 43c9690 commit 9f143b2

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

.prettierrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"semi": false,
2+
"semi": true,
33
"singleQuote": true,
44
"trailingComma": "none",
55
"tabWidth": 2,

core/helper/is.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function isTag(target: any) {
55
}
66

77
export function isStyledComponent(target: any) {
8-
return typeof target === 'object' && 'styled' in target
8+
return typeof target === 'object' && target?.name?.includes('styled')
99
}
1010

1111
export function isVueComponent(target: any) {

core/styled.ts

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import {
2-
defineComponent,
3-
DefineSetupFnComponent,
4-
h,
5-
inject,
6-
onMounted,
7-
PropType,
8-
PublicProps,
9-
reactive,
10-
SlotsType,
11-
useSlots,
12-
watchEffect
13-
} from 'vue'
1+
import { defineComponent, DefineSetupFnComponent, h, inject, onMounted, PropType, PublicProps, reactive, SlotsType, watchEffect } from 'vue'
142
import domElements, { type SupportedHTMLElements } from '@/constants/domElements'
153
import { type ExpressionsType, generateClassName, generateComponentName, insertExpressions } from '@/utils'
164
import { injectStyle } from '@/utils/injectStyle'
@@ -68,14 +56,13 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
6856
styledClassNameMap[componentName] = className
6957

7058
return defineComponent(
71-
(props) => {
59+
(props, { slots }) => {
7260
const myAttrs = { ...attributes }
7361
const theme = reactive(inject<Record<string, string | number>>('$theme', {}))
7462
let context = {
7563
theme,
7664
...props
7765
}
78-
7966
myAttrs.class = className
8067

8168
watchEffect(() => {
@@ -92,9 +79,8 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
9279

9380
// Return the render function
9481
return () => {
95-
const slots = useSlots()
9682
return h(
97-
isVueComponent(target) ? target : props?.as || target,
83+
isVueComponent(target) ? h(target, { as: props.as }) : props.as ?? target,
9884
{
9985
...myAttrs
10086
},
@@ -110,9 +96,8 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
11096
},
11197
...propsDefinition
11298
},
113-
inheritAttrs: true,
114-
styled: true
115-
} as any
99+
inheritAttrs: true
100+
}
116101
)
117102
}
118103

example/App.vue

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ const mixin = css`
5252
const StyledComp7 = styled('button', { color: String })`
5353
${mixin}
5454
`
55+
const BlueButton = styled.button`
56+
width: 120px;
57+
height: 40px;
58+
margin-right: 8px;
59+
padding: 4px 8px;
60+
border-radius: 9999px;
61+
box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
62+
background-color: skyblue;
63+
font-weight: bold;
64+
color: #fff;
65+
`
66+
const LinkButton = styled(BlueButton)`
67+
border: none;
68+
`
5569
</script>
5670

5771
<template>
@@ -61,6 +75,7 @@ const StyledComp7 = styled('button', { color: String })`
6175
<StyledComp5>12345</StyledComp5>
6276
<WithAttrsComp color="red">123</WithAttrsComp>
6377
<StyledComp7 color="blue">123</StyledComp7>
78+
<LinkButton as="a" href="#">Link Button</LinkButton>
6479
</ThemeProvider>
6580
</template>
6681

0 commit comments

Comments
 (0)