Skip to content

Commit 9ce5920

Browse files
committed
feat(core): [styled] support embed css
1 parent 0678b03 commit 9ce5920

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

core/styled.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { defineComponent, DefineSetupFnComponent, h, inject, onMounted, PropType, PublicProps, reactive, SlotsType, watch } from 'vue'
1+
import {
2+
defineComponent,
3+
DefineSetupFnComponent,
4+
h,
5+
inject,
6+
onMounted,
7+
onUnmounted,
8+
PropType,
9+
PublicProps,
10+
reactive,
11+
SlotsType,
12+
watch,
13+
} from 'vue'
214
import domElements, { type SupportedHTMLElements } from '@/constants/domElements'
3-
import { type ExpressionType, generateClassName, generateComponentName, insertExpressions } from '@/utils'
4-
import { injectStyle } from '@/utils/injectStyle'
15+
import { type ExpressionType, generateClassName, generateComponentName, insertExpressions, injectStyle, removeStyle } from '@/utils'
516
import { isStyledComponent, isValidElementType, isVueComponent } from '@/helper'
6-
import { useStyledClassName } from '@/hooks'
717

818
interface IProps {
919
as?: SupportedHTMLElements
@@ -51,13 +61,7 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
5161
type = 'styled-component'
5262
}
5363

54-
// Generate a unique class name
55-
const className = generateClassName()
5664
const componentName = generateComponentName(type)
57-
58-
const { styledClassNameMap } = useStyledClassName()
59-
styledClassNameMap[componentName] = className
60-
6165
return defineComponent(
6266
(props, { slots }) => {
6367
const myAttrs = { ...attributes }
@@ -66,18 +70,23 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
6670
theme,
6771
...props,
6872
}
69-
myAttrs.class = className
73+
74+
myAttrs.class = generateClassName()
7075

7176
watch([theme, props], () => {
7277
context = {
7378
theme,
7479
...props,
7580
}
76-
injectStyle<T>(className, cssWithExpression, context)
81+
injectStyle<T>(myAttrs.class, cssWithExpression, context)
7782
})
7883

7984
onMounted(() => {
80-
injectStyle<T>(className, cssWithExpression, context)
85+
injectStyle<T>(myAttrs.class, cssWithExpression, context)
86+
})
87+
88+
onUnmounted(() => {
89+
removeStyle(myAttrs.class)
8190
})
8291

8392
// Return the render function

core/utils/insertExpressions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type ExpressionType<T = Record<string, any>> = ((props: T) => string | number) | string
1+
export type ExpressionType<T = Record<string, any>> = ((props: T) => string | number | ExpressionType | ExpressionType[]) | string
22

33
export function insertExpressions<T>(
44
strings: TemplateStringsArray,

example/App.vue

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ const StyledComp6 = styled('button', { color: String })<{
4949
5050
const WithAttrsComp = withAttrs(StyledComp6, { disabled: true })
5151
52-
// console.log(useStyledClassName().getStyledClassName(StyledComp6))
53-
5452
const mixin = css<{
5553
color: string
5654
}>`
@@ -97,6 +95,29 @@ const commonClass = cssClass`
9795
color: #fff;
9896
background-color: red;
9997
`
98+
99+
const testEmbedCss1 = css`
100+
margin: 40px;
101+
background: white;
102+
padding: 10px 20px;
103+
border-radius: 8px;
104+
`
105+
const testEmbedCss2 = css`
106+
margin: 40px;
107+
background: blue;
108+
padding: 10px 20px;
109+
border-radius: 8px;
110+
color: white;
111+
`
112+
113+
const show = ref(true)
114+
const TestEmbedComponent = styled('div', { show: Boolean })`
115+
${(props) => {
116+
return props.show ? testEmbedCss1 : testEmbedCss2
117+
}}
118+
`
119+
120+
// console.log(testEmbedCss1, testEmbedCss2)
100121
</script>
101122

102123
<template>
@@ -109,6 +130,9 @@ const commonClass = cssClass`
109130
<LinkButton as="a" href="#">Link Button</LinkButton>
110131
<StyledBlueLink :color="color" href="#" @click="update">Styled Link</StyledBlueLink>
111132
<div :class="commonClass">test common class</div>
133+
134+
<TestEmbedComponent :show="show"> White </TestEmbedComponent>
135+
<TestEmbedComponent :show="!show" @click="show = !show"> Blue </TestEmbedComponent>
112136
</ThemeProvider>
113137
</template>
114138

0 commit comments

Comments
 (0)