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,

0 commit comments

Comments
 (0)