Skip to content

feat: support embed css, auto clear style and remove useStyleClassName hook #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExpressionType } from '@/utils'
import { defineComponent, DefineSetupFnComponent, h } from 'vue'
import { generateComponentName, insertExpressions } from '@/utils'
import { injectStyle } from '@/utils/injectStyle'
import { injectStyle } from '@/utils'

export const createGlobalStyle = (styles: TemplateStringsArray, ...expressions: ExpressionType[]): DefineSetupFnComponent<any> => {
return defineComponent(
Expand Down
2 changes: 1 addition & 1 deletion core/helper/cssClass.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExpressionType, generateClassName, insertExpressions } from '@/utils'
import { injectStyle } from '@/utils/injectStyle'
import { injectStyle } from '@/utils'

export function cssClass(cssStrings: TemplateStringsArray, ...interpolations: (ExpressionType<any> | ExpressionType<any>[])[]): string {
const className = generateClassName()
Expand Down
2 changes: 1 addition & 1 deletion core/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './withAttrs'
export * from './is'
export * from './create-global-style'
export * from './createGlobalStyle'
export * from './keyframes'
export * from './css'
export * from './cssClass'
3 changes: 1 addition & 2 deletions core/helper/keyframes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { injectStyle } from '@/utils/injectStyle'
import { generateUniqueName } from '@/utils'
import { generateUniqueName, injectStyle } from '@/utils'

export function keyframes(kfString: TemplateStringsArray): string {
const keyframeName = `kf-${generateUniqueName()}`
Expand Down
2 changes: 1 addition & 1 deletion core/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useStyledClassName'
// export * from './useStyledClassName'
17 changes: 0 additions & 17 deletions core/hooks/useStyledClassName.ts

This file was deleted.

2 changes: 1 addition & 1 deletion core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './providers'
export * from './helper'
export * from './hooks'
// export * from './hooks'

export * from './styled'
35 changes: 22 additions & 13 deletions core/styled.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { defineComponent, DefineSetupFnComponent, h, inject, onMounted, PropType, PublicProps, reactive, SlotsType, watch } from 'vue'
import {
defineComponent,
DefineSetupFnComponent,
h,
inject,
onMounted,
onUnmounted,
PropType,
PublicProps,
reactive,
SlotsType,
watch,
} from 'vue'
import domElements, { type SupportedHTMLElements } from '@/constants/domElements'
import { type ExpressionType, generateClassName, generateComponentName, insertExpressions } from '@/utils'
import { injectStyle } from '@/utils/injectStyle'
import { type ExpressionType, generateClassName, generateComponentName, insertExpressions, injectStyle, removeStyle } from '@/utils'
import { isStyledComponent, isValidElementType, isVueComponent } from '@/helper'
import { useStyledClassName } from '@/hooks'

interface IProps {
as?: SupportedHTMLElements
Expand Down Expand Up @@ -51,13 +61,7 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
type = 'styled-component'
}

// Generate a unique class name
const className = generateClassName()
const componentName = generateComponentName(type)

const { styledClassNameMap } = useStyledClassName()
styledClassNameMap[componentName] = className

return defineComponent(
(props, { slots }) => {
const myAttrs = { ...attributes }
Expand All @@ -66,18 +70,23 @@ function baseStyled(target: string | InstanceType<any>, propsDefinition: Record<
theme,
...props,
}
myAttrs.class = className

myAttrs.class = generateClassName()

watch([theme, props], () => {
context = {
theme,
...props,
}
injectStyle<T>(className, cssWithExpression, context)
injectStyle<T>(myAttrs.class, cssWithExpression, context)
})

onMounted(() => {
injectStyle<T>(className, cssWithExpression, context)
injectStyle<T>(myAttrs.class, cssWithExpression, context)
})

onUnmounted(() => {
removeStyle(myAttrs.class)
})

// Return the render function
Expand Down
1 change: 1 addition & 0 deletions core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './generateName'
export * from './insertExpressions'
export * from './applyExpressions'
export * from './styleManagement'
2 changes: 1 addition & 1 deletion core/utils/insertExpressions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ExpressionType<T = Record<string, any>> = ((props: T) => string | number) | string
export type ExpressionType<T = Record<string, any>> = ((props: T) => string | number | ExpressionType | ExpressionType[]) | string

export function insertExpressions<T>(
strings: TemplateStringsArray,
Expand Down
11 changes: 11 additions & 0 deletions core/utils/injectStyle.ts → core/utils/styleManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ function insert(className: string, cssString: string) {
insertedRuleMap[className] = cssTextNode
}

export function removeStyle(className: string): void {
for (const tag of tags) {
for (const node of tag.childNodes) {
if (node.nodeValue?.includes(className)) {
node.remove()
break
}
}
}
}

export function injectStyle<T>(className: string, cssWithExpression: ExpressionType<T>[], context: Record<string, any>): void {
const appliedCss = applyExpressions(cssWithExpression, context).join('')
insert(className, appliedCss)
Expand Down
28 changes: 26 additions & 2 deletions example/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const StyledComp6 = styled('button', { color: String })<{

const WithAttrsComp = withAttrs(StyledComp6, { disabled: true })

// console.log(useStyledClassName().getStyledClassName(StyledComp6))

const mixin = css<{
color: string
}>`
Expand Down Expand Up @@ -97,6 +95,29 @@ const commonClass = cssClass`
color: #fff;
background-color: red;
`

const testEmbedCss1 = css`
margin: 40px;
background: white;
padding: 10px 20px;
border-radius: 8px;
`
const testEmbedCss2 = css`
margin: 40px;
background: blue;
padding: 10px 20px;
border-radius: 8px;
color: white;
`

const show = ref(true)
const TestEmbedComponent = styled('div', { show: Boolean })`
${(props) => {
return props.show ? testEmbedCss1 : testEmbedCss2
}}
`

// console.log(testEmbedCss1, testEmbedCss2)
</script>

<template>
Expand All @@ -109,6 +130,9 @@ const commonClass = cssClass`
<LinkButton as="a" href="#">Link Button</LinkButton>
<StyledBlueLink :color="color" href="#" @click="update">Styled Link</StyledBlueLink>
<div :class="commonClass">test common class</div>

<TestEmbedComponent :show="show"> White </TestEmbedComponent>
<TestEmbedComponent :show="!show" @click="show = !show"> Blue </TestEmbedComponent>
</ThemeProvider>
</template>

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"isolatedModules": true,
"lib": [
"dom",
"esnext"
"esnext",
"dom.iterable"
],
"paths": {
"@/*": [
Expand Down
Loading