Skip to content

Commit 6ab5f0b

Browse files
authored
Merge pull request #14 from v-vibe/feat/embed-style
feat: support embed css, auto clear style and remove `useStyleClassName` hook
2 parents d48fd23 + 9ce5920 commit 6ab5f0b

File tree

13 files changed

+69
-41
lines changed

13 files changed

+69
-41
lines changed

core/helper/create-global-style.ts renamed to core/helper/createGlobalStyle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ExpressionType } from '@/utils'
22
import { defineComponent, DefineSetupFnComponent, h } from 'vue'
33
import { generateComponentName, insertExpressions } from '@/utils'
4-
import { injectStyle } from '@/utils/injectStyle'
4+
import { injectStyle } from '@/utils'
55

66
export const createGlobalStyle = (styles: TemplateStringsArray, ...expressions: ExpressionType[]): DefineSetupFnComponent<any> => {
77
return defineComponent(

core/helper/cssClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ExpressionType, generateClassName, insertExpressions } from '@/utils'
2-
import { injectStyle } from '@/utils/injectStyle'
2+
import { injectStyle } from '@/utils'
33

44
export function cssClass(cssStrings: TemplateStringsArray, ...interpolations: (ExpressionType<any> | ExpressionType<any>[])[]): string {
55
const className = generateClassName()

core/helper/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './withAttrs'
22
export * from './is'
3-
export * from './create-global-style'
3+
export * from './createGlobalStyle'
44
export * from './keyframes'
55
export * from './css'
66
export * from './cssClass'

core/helper/keyframes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { injectStyle } from '@/utils/injectStyle'
2-
import { generateUniqueName } from '@/utils'
1+
import { generateUniqueName, injectStyle } from '@/utils'
32

43
export function keyframes(kfString: TemplateStringsArray): string {
54
const keyframeName = `kf-${generateUniqueName()}`

core/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './useStyledClassName'
1+
// export * from './useStyledClassName'

core/hooks/useStyledClassName.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './providers'
22
export * from './helper'
3-
export * from './hooks'
3+
// export * from './hooks'
44

55
export * from './styled'

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/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './generateName'
22
export * from './insertExpressions'
33
export * from './applyExpressions'
4+
export * from './styleManagement'

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,

core/utils/injectStyle.ts renamed to core/utils/styleManagement.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ function insert(className: string, cssString: string) {
3939
insertedRuleMap[className] = cssTextNode
4040
}
4141

42+
export function removeStyle(className: string): void {
43+
for (const tag of tags) {
44+
for (const node of tag.childNodes) {
45+
if (node.nodeValue?.includes(className)) {
46+
node.remove()
47+
break
48+
}
49+
}
50+
}
51+
}
52+
4253
export function injectStyle<T>(className: string, cssWithExpression: ExpressionType<T>[], context: Record<string, any>): void {
4354
const appliedCss = applyExpressions(cssWithExpression, context).join('')
4455
insert(className, appliedCss)

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

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"isolatedModules": true,
1919
"lib": [
2020
"dom",
21-
"esnext"
21+
"esnext",
22+
"dom.iterable"
2223
],
2324
"paths": {
2425
"@/*": [

0 commit comments

Comments
 (0)