Skip to content

Commit 743d66e

Browse files
committed
docs: fix demo not display
1 parent 7d855fb commit 743d66e

File tree

7 files changed

+81
-37
lines changed

7 files changed

+81
-37
lines changed

docs/demo/Link.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
2-
<a href="#">This is a link</a>
2+
<a href="#"><slot /></a>
33
</template>

docs/demo/StyledLink.vue

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

docs/guide/styling-any-component.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const StyledLink = styled(Link)`
3434
</script>
3535
3636
<template>
37-
<StyledLink />
37+
<StyledLink>This is a dark red link</StyledLink>
3838
</template>
3939
```
4040

@@ -49,15 +49,20 @@ You can also style styled components. For example, with `StyledLink`, using `sty
4949
```vue
5050
<script setup lang="ts">
5151
import { styled } from '@vue3-styled-components/package'
52-
import StyledLink from '/demo/StyledLink.vue'
52+
53+
const StyledLink = styled.a`
54+
color: darkred !important;
55+
`
5356
5457
const StyledBlueLink = styled(StyledLink)`
5558
color: blue !important;
5659
`
60+
61+
console.log(StyledLink, StyledBlueLink)
5762
</script>
5863
5964
<template>
60-
<StyledBlueLink />
65+
<StyledBlueLink>This is a blue link</StyledBlueLink>
6166
</template>
6267
```
6368

package/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './styled'
22
export * from './providers'
3-
export { createGlobalStyle } from './injectStyle'
43
export * from './keyframes'

package/injectStyle.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
11
import { applyExpressions, ExpressionType, insertExpressions } from '@/utils'
22

3-
export function injectStyle(className: string, cssWithExpression: (string | ExpressionType)[], context: Record<string, any>) {
4-
const appliedCss = applyExpressions(cssWithExpression, context).join('')
3+
const MAX_SIZE = 65536
54

6-
// Create a style tag and append it to the head
7-
const styleTag = document.createElement('style')
8-
styleTag.innerHTML = `.${className} { ${appliedCss} }`
9-
document.head.appendChild(styleTag)
5+
let ctx: number = 0
6+
const insertedRuleMap: Record<string, Text> = {}
7+
const tags: HTMLStyleElement[] = []
8+
9+
function createStyleTag(): HTMLStyleElement {
10+
const style = document.createElement('style')
11+
document.head.appendChild(style)
12+
tags.push(style)
13+
return style
1014
}
1115

12-
export function createGlobalStyle(css: TemplateStringsArray, ...expressions: string[]) {
13-
// Create a style tag and append it to the head
14-
const styleTag = document.createElement('style')
15-
styleTag.innerHTML = insertExpressions(css, expressions).join('')
16-
document.head.appendChild(styleTag)
16+
function insert(className: string, cssString: string) {
17+
ctx++
18+
19+
let styleTag = tags[tags.length - 1]
20+
21+
if (!styleTag || ctx >= MAX_SIZE) {
22+
styleTag = createStyleTag()
23+
ctx = 0
24+
}
25+
26+
const ruleNode = insertedRuleMap[className]
27+
let rule = `.${className} { ${cssString} }`
28+
29+
if (className === 'global' || className === 'keyframes') {
30+
rule = cssString
31+
}
32+
33+
if (ruleNode) {
34+
ruleNode.data = rule
35+
return
36+
}
37+
const cssTextNode = document.createTextNode(rule)
38+
styleTag.appendChild(cssTextNode)
39+
insertedRuleMap[className] = cssTextNode
40+
}
41+
42+
export function injectStyle(className: string, cssWithExpression: (string | ExpressionType)[], context: Record<string, any>) {
43+
const appliedCss = applyExpressions(cssWithExpression, context).join('')
44+
45+
insert(className, appliedCss)
1746
}

package/keyframes.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { createGlobalStyle } from '@/injectStyle'
1+
import { injectStyle } from '@/injectStyle'
22
import { generateUniqueName } from '@/utils'
33

44
export function keyframes(kfString: TemplateStringsArray) {
55
const keyframeName = `kf-${generateUniqueName()}`
6-
createGlobalStyle`
7-
@keyframes ${keyframeName} {
8-
${kfString.join('')}
9-
}
10-
`
6+
injectStyle(
7+
'keyframes',
8+
[
9+
`
10+
@keyframes ${keyframeName} {
11+
${kfString.join('')}
12+
}
13+
`
14+
],
15+
{}
16+
)
1117
return keyframeName
1218
}

package/styled.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ function baseStyled(target: string | InstanceType<any>, propsFromFactory: Record
6565
type = 'styled-component'
6666
}
6767

68+
// Generate a unique class name
69+
const className = generateClassName()
6870
const componentName = generateComponentName(type)
6971
return defineComponent(
7072
(props) => {
@@ -75,8 +77,6 @@ function baseStyled(target: string | InstanceType<any>, propsFromFactory: Record
7577
...props
7678
}
7779

78-
// Generate a unique class name
79-
const className = generateClassName()
8080
myAttrs.class = className
8181

8282
watchEffect(() => {
@@ -120,6 +120,23 @@ function baseStyled(target: string | InstanceType<any>, propsFromFactory: Record
120120
return styledComponent
121121
}
122122

123+
const createGlobalStyle = (styles: TemplateStringsArray, ...expressions: ExpressionsType) => {
124+
return defineComponent(
125+
(_, { attrs }) => {
126+
const cssStringsWithExpression = insertExpressions(styles, expressions)
127+
injectStyle('global', cssStringsWithExpression, attrs)
128+
return () => {
129+
return h('div', { style: 'display: none' })
130+
}
131+
},
132+
{
133+
name: 'global-style',
134+
inheritAttrs: true,
135+
styled: true
136+
} as any
137+
)
138+
}
139+
123140
/** Append all the supported HTML elements to the styled properties */
124141
const styled = baseStyled as typeof baseStyled & {
125142
[E in SupportedHTMLElements]: StyledComponent
@@ -129,4 +146,4 @@ domElements.forEach((domElement: SupportedHTMLElements) => {
129146
styled[domElement] = baseStyled(domElement)
130147
})
131148

132-
export { styled }
149+
export { styled, createGlobalStyle }

0 commit comments

Comments
 (0)