Skip to content

Commit ae52619

Browse files
committed
feat(styled): support using StyledComponent as selector
1 parent d5bd009 commit ae52619

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

packages/core/src/styled.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
7070
}
7171

7272
const componentName = generateComponentName(type)
73-
return defineComponent(
73+
const commonClassName = generateClassName()
74+
const component = defineComponent(
7475
(props, { slots }) => {
7576
const internalAttrs = computed<Record<string, any>>(() => {
7677
if (typeof defaultAttrs === 'function') {
@@ -95,7 +96,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
9596

9697
const defaultClassName = generateClassName()
9798

98-
internalProps.value.class += ` ${defaultClassName}`
99+
internalProps.value.class += ` ${defaultClassName} ${commonClassName}`
99100

100101
// Inject the tailwind classes to the class attribute
101102
watch(
@@ -157,7 +158,12 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
157158
} as ComponentObjectPropsOptions<{ as?: string; props?: P } & ExtractPropTypes<PropsDefinition<T>>>,
158159
inheritAttrs: true,
159160
},
160-
)
161+
) as any
162+
component.custom = {
163+
className: commonClassName,
164+
}
165+
166+
return component
161167
}
162168

163169
return styledComponent as StyledComponent<T>

packages/core/src/utils/applyExpressions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { isStyledComponent } from '../helper'
2+
13
export function applyExpressions(chunks: any[], executionContext: Record<string, any>, tailwindClasses: string[]): string[] {
24
return chunks.reduce((ruleSet, chunk) => {
35
if (chunk === undefined || chunk === null || chunk === false || chunk === '') {
46
return ruleSet
57
}
8+
69
if (Array.isArray(chunk)) {
710
return [...ruleSet, ...applyExpressions(chunk, executionContext, tailwindClasses)]
811
}
@@ -16,6 +19,10 @@ export function applyExpressions(chunks: any[], executionContext: Record<string,
1619
return ruleSet
1720
}
1821

22+
if (isStyledComponent(chunk)) {
23+
return ruleSet.concat(`.${chunk.custom.className}`)
24+
}
25+
1926
return ruleSet.concat(chunk.toString())
2027
}, [])
2128
}

packages/core/src/utils/insertExpressions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { DefineSetupFnComponent } from 'vue'
12
import { TailwindObject } from '../helper'
23

34
export type ExpressionType<T = Record<string, any>> =
45
| ((props: T) => string | number | ExpressionType | ExpressionType[])
56
| string
67
| number
78
| TailwindObject
9+
| DefineSetupFnComponent<any>
810
| undefined
911
| null
1012

packages/playground/src/App.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ const changeTheme = () => {
2121
theme.bg = 'blue'
2222
}
2323
}
24+
25+
const Container = styled.div`
26+
${Button} {
27+
color: #fff;
28+
}
29+
`
2430
</script>
2531

2632
<template>
@@ -35,6 +41,10 @@ const changeTheme = () => {
3541
>
3642
<Button>777</Button>
3743
</ThemeProvider>
44+
45+
<Container>
46+
<Button>888</Button>
47+
</Container>
3848
</ThemeProvider>
3949
</template>
4050

0 commit comments

Comments
 (0)