Skip to content

Commit 0678b03

Browse files
committed
feat(utils): [style-management] auto clear style when the component unmounted
1 parent beb42e7 commit 0678b03

File tree

7 files changed

+18
-6
lines changed

7 files changed

+18
-6
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/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/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)

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)