Skip to content

Commit ccd9805

Browse files
committed
fix(helper): [create-global-style] cannot apply style correctly
1 parent 9f612e8 commit ccd9805

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

core/helper/createGlobalStyle.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { ExpressionType } from '@/utils'
22
import { defineComponent, DefineSetupFnComponent, h } from 'vue'
3-
import { generateComponentName, generateUniqueName, insertExpressions } from '@/utils'
3+
import { generateComponentName, insertExpressions } from '@/utils'
44
import { injectStyle } from '@/utils'
55

66
export const createGlobalStyle = (styles: TemplateStringsArray, ...expressions: ExpressionType[]): DefineSetupFnComponent<any> => {
77
return defineComponent(
88
(_, { attrs }) => {
9-
const golbalClassName = `global-${generateUniqueName()}`
109
const cssStringsWithExpression = insertExpressions(styles, expressions)
11-
injectStyle(golbalClassName, cssStringsWithExpression, attrs)
10+
injectStyle('', cssStringsWithExpression, attrs)
1211
return () => {
1312
return h('div', { style: 'display: none' })
1413
}

core/utils/styleManagement.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export function removeStyle(className: string): void {
4949

5050
export function injectStyle<T>(className: string, cssWithExpression: ExpressionType<T>[], context: Record<string, any>): void {
5151
const appliedCss = applyExpressions(cssWithExpression, context).join('')
52-
const compiledCss = serialize(compile(`.${className}{${appliedCss}}`), middleware([prefixer, stringify]))
52+
let cssString = appliedCss
53+
if (className !== '') {
54+
cssString = `.${className}{${appliedCss}}`
55+
}
56+
const compiledCss = serialize(compile(cssString), middleware([prefixer, stringify]))
5357
insert(className, compiledCss)
5458
}

example/App.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { styled, ThemeProvider, keyframes, withAttrs, css, cssClass } from '@vvibe/vue-styled-components'
2+
import { styled, ThemeProvider, keyframes, withAttrs, css, cssClass, createGlobalStyle } from '@vvibe/vue-styled-components'
33
import Component from './Component.vue'
44
import { ref } from 'vue'
55
@@ -120,10 +120,17 @@ const TestEmbedComponent = styled('div', { show: Boolean })`
120120
121121
// console.log(testEmbedCss1, testEmbedCss2)
122122
const visible = ref(true)
123+
124+
const Global = createGlobalStyle`
125+
body {
126+
display: flex
127+
}
128+
`
123129
</script>
124130

125131
<template>
126132
<ThemeProvider :theme="theme">
133+
<Global />
127134
<div @click="visible = !visible">Test Remove</div>
128135
<StyledComp3 @click="update">12345</StyledComp3>
129136
<StyledComp4>12345</StyledComp4>

0 commit comments

Comments
 (0)