Skip to content

Commit 2fc5b48

Browse files
committed
feat: implement automatic addition of vendor prefixes to CSS styles and add support for nested CSS in legacy browsers(#19)
1 parent ce5eef7 commit 2fc5b48

File tree

7 files changed

+46
-31
lines changed

7 files changed

+46
-31
lines changed

core/helper/createGlobalStyle.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { ExpressionType } from '@/utils'
22
import { defineComponent, DefineSetupFnComponent, h } from 'vue'
3-
import { generateComponentName, insertExpressions } from '@/utils'
3+
import { generateComponentName, generateUniqueName, 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()}`
910
const cssStringsWithExpression = insertExpressions(styles, expressions)
10-
injectStyle('global', cssStringsWithExpression, attrs)
11+
injectStyle(golbalClassName, cssStringsWithExpression, attrs)
1112
return () => {
1213
return h('div', { style: 'display: none' })
1314
}

core/utils/styleManagement.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { applyExpressions, ExpressionType } from '@/utils/index'
2+
import { compile, middleware, prefixer, serialize, stringify } from 'stylis'
23

34
const MAX_SIZE = 65536
45

@@ -24,11 +25,7 @@ function insert(className: string, cssString: string) {
2425
}
2526

2627
const ruleNode = insertedRuleMap[className]
27-
let rule = `.${className} { ${cssString} }`
28-
29-
if (className === 'global' || /^kf-.+/.test(className)) {
30-
rule = cssString
31-
}
28+
const rule = cssString
3229

3330
if (ruleNode) {
3431
ruleNode.data = rule
@@ -42,7 +39,7 @@ function insert(className: string, cssString: string) {
4239
export function removeStyle(className: string): void {
4340
for (const tag of tags) {
4441
for (const node of tag.childNodes) {
45-
if (node.nodeValue?.includes(className)) {
42+
if (node.nodeValue?.startsWith(`.${className}`)) {
4643
node.remove()
4744
break
4845
}
@@ -52,5 +49,6 @@ export function removeStyle(className: string): void {
5249

5350
export function injectStyle<T>(className: string, cssWithExpression: ExpressionType<T>[], context: Record<string, any>): void {
5451
const appliedCss = applyExpressions(cssWithExpression, context).join('')
55-
insert(className, appliedCss)
52+
const compiledCss = serialize(compile(`.${className}{${appliedCss}}`), middleware([prefixer, stringify]))
53+
insert(className, compiledCss)
5654
}

example/App.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script setup lang="ts">
2-
import { styled, ThemeProvider, keyframes, withAttrs, css, cssClass } from '@vvide/vue-styled-components'
2+
import { styled, ThemeProvider, keyframes, withAttrs, css, cssClass } from '@vvibe/vue-styled-components'
33
import Component from './Component.vue'
44
import { ref } from 'vue'
5+
import { IconInner } from './Test'
56
67
const theme = ref({ primary: 'green', error: 'red' })
78
@@ -25,6 +26,7 @@ const update = () => {
2526
}
2627
2728
const StyledComp3 = styled(Component)`
29+
position: sticky;
2830
background: ${(props) => props.theme.primary};
2931
`
3032
const StyledComp4 = styled.div`
@@ -118,10 +120,12 @@ const TestEmbedComponent = styled('div', { show: Boolean })`
118120
`
119121
120122
// console.log(testEmbedCss1, testEmbedCss2)
123+
const visible = ref(true)
121124
</script>
122125

123126
<template>
124127
<ThemeProvider :theme="theme">
128+
<div @click="visible = !visible">Test Remove</div>
125129
<StyledComp3 @click="update">12345</StyledComp3>
126130
<StyledComp4>12345</StyledComp4>
127131
<StyledComp5>12345</StyledComp5>
@@ -133,6 +137,10 @@ const TestEmbedComponent = styled('div', { show: Boolean })`
133137

134138
<TestEmbedComponent :show="show"> White </TestEmbedComponent>
135139
<TestEmbedComponent :show="!show" @click="show = !show"> Blue </TestEmbedComponent>
140+
141+
<IconInner color="red" size="55"> 666 </IconInner>
142+
143+
<BlueButton v-if="visible" />
136144
</ThemeProvider>
137145
</template>
138146

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535
"prepare": "husky",
3636
"release": "semantic-release"
3737
},
38-
"dependencies": {
38+
"peerDependencies": {
3939
"vue": "^3.4.21"
4040
},
4141
"devDependencies": {
4242
"@commitlint/cli": "^19.2.1",
4343
"@commitlint/config-conventional": "^19.1.0",
4444
"@semantic-release/changelog": "^6.0.3",
4545
"@semantic-release/git": "^10.0.1",
46+
"@types/stylis": "^4.2.6",
4647
"@typescript-eslint/eslint-plugin": "^7.4.0",
4748
"@typescript-eslint/parser": "^7.4.0",
4849
"@vitejs/plugin-vue": "^5.0.4",
@@ -70,9 +71,7 @@
7071
"access": "public",
7172
"provenance": true
7273
},
73-
"pnpm": {
74-
"overrides": {
75-
"vue-template-compiler@>=2.0.0 <3.0.0": ">=3.0.0"
76-
}
74+
"dependencies": {
75+
"stylis": "^4.3.2"
7776
}
7877
}

pnpm-lock.yaml

Lines changed: 23 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@/*": [
2727
"./core/*"
2828
],
29-
"@vvide/vue-styled-components": [
29+
"@vvibe/vue-styled-components": [
3030
"./core"
3131
]
3232
},

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineConfig({
1919
resolve: {
2020
alias: {
2121
'@/': new URL('./core/', import.meta.url).pathname,
22-
'@vvide/vue-styled-components': new URL('./core/', import.meta.url).pathname,
22+
'@vvibe/vue-styled-components': new URL('./core/', import.meta.url).pathname,
2323
},
2424
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.less'],
2525
},

0 commit comments

Comments
 (0)