-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstyled.ts
191 lines (167 loc) · 5.58 KB
/
styled.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
type ComponentObjectPropsOptions,
type ExtractPropTypes,
type DefineSetupFnComponent,
defineComponent,
h,
inject,
onMounted,
onUnmounted,
reactive,
ref,
watch,
HTMLAttributes,
computed,
} from 'vue'
import domElements, { type SupportedHTMLElements } from '@/src/constants/domElements'
import { type ExpressionType, generateClassName, generateComponentName, insertExpressions, injectStyle, removeStyle } from '@/src/utils'
import { isStyledComponent, isValidElementType, isVueComponent } from '@/src/helper'
import type { DefaultTheme } from './providers/theme'
export type BaseContext<T> = T & { theme: DefaultTheme }
export type PropsDefinition<T> = {
[K in keyof T]: T[K]
}
// 定义 styledComponent 类型
interface StyledComponent<T extends object> {
<P>(
styles: TemplateStringsArray,
...expressions: (
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>[]
)[]
): DefineSetupFnComponent<{ as?: string; props?: P } & ExtractPropTypes<PropsDefinition<T>> & HTMLAttributes>
attrs<A = object>(
attrs: A | ((props: ExtractPropTypes<PropsDefinition<T>>) => A),
): StyledComponent<A & ExtractPropTypes<PropsDefinition<T>>>
}
function baseStyled<T extends object>(target: string | InstanceType<any>, propsDefinition?: PropsDefinition<T>): StyledComponent<T> {
if (!isValidElementType(target)) {
throw Error('The element is invalid.')
}
let defaultAttrs: unknown
function styledComponent<P>(
styles: TemplateStringsArray,
...expressions: (
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>[]
)[]
) {
const cssStringsWithExpression = insertExpressions(styles, expressions)
return createStyledComponent<P>(cssStringsWithExpression)
}
styledComponent.attrs = function <A extends object>(attrs: A) {
defaultAttrs = attrs
return styledComponent
}
function createStyledComponent<P>(cssWithExpression: ExpressionType<any>[]) {
let type: string = target
if (isVueComponent(target)) {
type = 'vue-component'
}
if (isStyledComponent(target)) {
type = 'styled-component'
}
const componentName = generateComponentName(type)
const commonClassName = generateClassName()
const component = defineComponent(
(props, { slots, attrs }) => {
const internalAttrs = computed<Record<string, any>>(() => {
if (typeof defaultAttrs === 'function') {
return defaultAttrs(props)
}
if (typeof defaultAttrs === 'object') {
return defaultAttrs
}
return {}
})
const tailwindClasses = ref<string[]>([])
const internalProps = ref({ class: '', ...internalAttrs.value })
const theme = inject<Record<string, string | number>>('$theme', reactive({}))
let context = {
theme,
...props,
...props.props,
...internalAttrs.value,
}
const defaultClassName = generateClassName()
internalProps.value.class += ` ${defaultClassName} ${commonClassName}`
// Inject the tailwind classes to the class attribute
watch(
tailwindClasses,
(classNames) => {
internalProps.value.class += ` ${classNames.join(' ')}`
},
{ deep: true },
)
watch(
[theme, props],
() => {
context = {
...internalProps.value,
theme,
...props,
...props.props,
}
tailwindClasses.value = injectStyle(defaultClassName, cssWithExpression, context)
},
{
deep: true,
},
)
onMounted(() => {
tailwindClasses.value = injectStyle(defaultClassName, cssWithExpression, context)
})
onUnmounted(() => {
removeStyle(defaultClassName)
})
// Return the render function
return () => {
const node = isVueComponent(target) ? h(target, { as: props.as }) : props.as ?? target
// Extract event handlers from attrs
const eventHandlers: Record<string, (...args: any[]) => any> = {}
Object.entries(attrs).forEach(([key, value]) => {
if (key.startsWith('on')) {
eventHandlers[key] = value as (...args: any[]) => any
}
})
return h(
node,
{
...internalProps.value,
...eventHandlers, // Ensure event handlers are passed correctly
},
slots,
)
}
},
{
name: componentName,
props: {
as: {
type: String,
required: false,
},
props: {
type: Object,
required: false,
},
...propsDefinition,
} as ComponentObjectPropsOptions<{ as?: string; props?: P } & ExtractPropTypes<PropsDefinition<T>>>,
inheritAttrs: true,
},
) as any
component.custom = {
className: commonClassName,
}
return component
}
return styledComponent as StyledComponent<T>
}
/** Append all the supported HTML elements to the styled properties */
const styled = baseStyled as typeof baseStyled & {
[E in SupportedHTMLElements]: ReturnType<typeof baseStyled>
}
domElements.forEach((domElement: SupportedHTMLElements) => {
styled[domElement] = baseStyled(domElement)
})
export { styled, styled as default }