refactor: cssinjs

pull/6213/head
tangjinzhou 2023-01-25 10:28:10 +08:00
parent 10b52e0072
commit c0d7d041b4
4 changed files with 91 additions and 88 deletions

View File

@ -21,6 +21,7 @@ import { supportLayer } from '../util';
import useGlobalCache from './useGlobalCache'; import useGlobalCache from './useGlobalCache';
import canUseDom from '../../canUseDom'; import canUseDom from '../../canUseDom';
import { removeCSS, updateCSS } from '../../../vc-util/Dom/dynamicCSS'; import { removeCSS, updateCSS } from '../../../vc-util/Dom/dynamicCSS';
import type { Ref } from 'vue';
import { computed } from 'vue'; import { computed } from 'vue';
import type { VueNode } from '../../type'; import type { VueNode } from '../../type';
@ -286,20 +287,20 @@ function Empty() {
* Register a style to the global style sheet. * Register a style to the global style sheet.
*/ */
export default function useStyleRegister( export default function useStyleRegister(
info: { info: Ref<{
theme: Theme<any, any>; theme: Theme<any, any>;
token: any; token: any;
path: string[]; path: string[];
hashId?: string; hashId?: string;
layer?: string; layer?: string;
}, }>,
styleFn: () => CSSInterpolation, styleFn: () => CSSInterpolation,
) { ) {
const styleContext = useStyleInject(); const styleContext = useStyleInject();
const tokenKey = computed(() => info.token._tokenKey as string); const tokenKey = computed(() => info.value.token._tokenKey as string);
const fullPath = computed(() => [tokenKey.value, ...info.path]); const fullPath = computed(() => [tokenKey.value, ...info.value.path]);
// Check if need insert style // Check if need insert style
let isMergedClientSide = isClientSide; let isMergedClientSide = isClientSide;
@ -315,7 +316,7 @@ export default function useStyleRegister(
() => { () => {
const styleObj = styleFn(); const styleObj = styleFn();
const { hashPriority, container, transformers, linters } = styleContext; const { hashPriority, container, transformers, linters } = styleContext;
const { path, hashId, layer } = info; const { path, hashId, layer } = info.value;
const [parsedStyle, effectStyle] = parseStyle(styleObj, { const [parsedStyle, effectStyle] = parseStyle(styleObj, {
hashId, hashId,
hashPriority, hashPriority,
@ -371,17 +372,16 @@ export default function useStyleRegister(
return (node: VueNode) => { return (node: VueNode) => {
let styleNode: VueNode; let styleNode: VueNode;
if (!styleContext.ssrInline || isMergedClientSide || !styleContext.defaultCache) { if (!styleContext.ssrInline || isMergedClientSide || !styleContext.defaultCache) {
styleNode = <Empty />; styleNode = <Empty />;
} else { } else {
styleNode = ( styleNode = (
<style <style
{...{ {...{
[ATTR_TOKEN]: cacheStyle[1], [ATTR_TOKEN]: cacheStyle.value[1],
[ATTR_MARK]: cacheStyle[2], [ATTR_MARK]: cacheStyle.value[2],
}} }}
innerHTML={cacheStyle[0]} innerHTML={cacheStyle.value[0]}
/> />
); );
} }

View File

@ -17,6 +17,7 @@ import { tuple, withInstall } from '../_util/type';
import { cloneElement } from '../_util/vnode'; import { cloneElement } from '../_util/vnode';
import type { NodeMouseEventHandler } from '../vc-tree/contextTypes'; import type { NodeMouseEventHandler } from '../vc-tree/contextTypes';
import useConfigInject from '../_util/hooks/useConfigInject'; import useConfigInject from '../_util/hooks/useConfigInject';
import useStyle from './style';
const iconMapFilled = { const iconMapFilled = {
success: CheckCircleFilled, success: CheckCircleFilled,
@ -69,6 +70,7 @@ const Alert = defineComponent({
props: alertProps(), props: alertProps(),
setup(props, { slots, emit, attrs, expose }) { setup(props, { slots, emit, attrs, expose }) {
const { prefixCls, direction } = useConfigInject('alert', props); const { prefixCls, direction } = useConfigInject('alert', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const closing = ref(false); const closing = ref(false);
const closed = ref(false); const closed = ref(false);
const alertNode = ref(); const alertNode = ref();
@ -125,6 +127,7 @@ const Alert = defineComponent({
[`${prefixClsValue}-banner`]: !!banner, [`${prefixClsValue}-banner`]: !!banner,
[`${prefixClsValue}-closable`]: closable, [`${prefixClsValue}-closable`]: closable,
[`${prefixClsValue}-rtl`]: direction.value === 'rtl', [`${prefixClsValue}-rtl`]: direction.value === 'rtl',
[hashId.value]: true,
}); });
const closeIcon = closable ? ( const closeIcon = closable ? (
@ -164,7 +167,8 @@ const Alert = defineComponent({
node.style.maxHeight = '0px'; node.style.maxHeight = '0px';
}, },
}); });
return closed.value ? null : ( return wrapSSR(
closed.value ? null : (
<Transition {...transitionProps}> <Transition {...transitionProps}>
<div <div
role="alert" role="alert"
@ -185,6 +189,7 @@ const Alert = defineComponent({
{closeIcon} {closeIcon}
</div> </div>
</Transition> </Transition>
),
); );
}; };
}, },

View File

@ -19,7 +19,7 @@ import type { FullToken } from './util/genComponentStyleHook';
import genComponentStyleHook from './util/genComponentStyleHook'; import genComponentStyleHook from './util/genComponentStyleHook';
import statisticToken, { merge as mergeToken, statistic } from './util/statistic'; import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
import type { VueNode } from '../_util/type'; import type { VueNode } from '../_util/type';
import type { ComputedRef, InjectionKey } from 'vue'; import type { ComputedRef, InjectionKey, Ref } from 'vue';
import { computed, inject } from 'vue'; import { computed, inject } from 'vue';
const defaultTheme = createTheme(defaultDerivative); const defaultTheme = createTheme(defaultDerivative);
@ -89,7 +89,7 @@ export function useToken(): [
]; ];
} }
export type UseComponentStyleResult = [(node: VueNode) => VueNode, string]; export type UseComponentStyleResult = [(node: VueNode) => VueNode, Ref<string>];
export type GenerateStyle< export type GenerateStyle<
ComponentToken extends object = AliasToken, ComponentToken extends object = AliasToken,

View File

@ -7,6 +7,8 @@ import type { UseComponentStyleResult } from '../internal';
import { mergeToken, statisticToken, useToken } from '../internal'; import { mergeToken, statisticToken, useToken } from '../internal';
import type { ComponentTokenMap, GlobalToken } from '../interface'; import type { ComponentTokenMap, GlobalToken } from '../interface';
import useConfigInject from 'ant-design-vue/es/_util/hooks/useConfigInject'; import useConfigInject from 'ant-design-vue/es/_util/hooks/useConfigInject';
import type { Ref } from 'vue';
import { computed } from 'vue';
export type OverrideTokenWithoutDerivative = ComponentTokenMap; export type OverrideTokenWithoutDerivative = ComponentTokenMap;
export type OverrideComponent = keyof OverrideTokenWithoutDerivative; export type OverrideComponent = keyof OverrideTokenWithoutDerivative;
@ -42,36 +44,34 @@ export default function genComponentStyleHook<ComponentName extends OverrideComp
| OverrideTokenWithoutDerivative[ComponentName] | OverrideTokenWithoutDerivative[ComponentName]
| ((token: GlobalToken) => OverrideTokenWithoutDerivative[ComponentName]), | ((token: GlobalToken) => OverrideTokenWithoutDerivative[ComponentName]),
) { ) {
return (prefixCls: string): UseComponentStyleResult => { return (prefixCls: Ref<string>): UseComponentStyleResult => {
const [theme, token, hashId] = useToken(); const [theme, token, hashId] = useToken();
const { getPrefixCls, iconPrefixCls } = useConfigInject('', {}); const { rootPrefixCls, iconPrefixCls } = useConfigInject('', {});
const rootPrefixCls = getPrefixCls(); const sharedInfo = computed(() => {
return {
// Generate style for all a tags in antd component.
useStyleRegister(
{
theme: theme.value, theme: theme.value,
token: token.value, token: token.value,
hashId: hashId.value, hashId: hashId.value,
path: ['Shared', rootPrefixCls], path: ['Shared', rootPrefixCls.value],
}, };
() => [ });
// Generate style for all a tags in antd component.
useStyleRegister(sharedInfo, () => [
{ {
// Link // Link
'&': genLinkStyle(token.value), '&': genLinkStyle(token.value),
}, },
], ]);
); const componentInfo = computed(() => {
return {
return [
useStyleRegister(
{
theme: theme.value, theme: theme.value,
token: token.value, token: token.value,
hashId: hashId.value, hashId: hashId.value,
path: [component, prefixCls, iconPrefixCls.value], path: [component, prefixCls.value, iconPrefixCls.value],
}, };
() => { });
return [
useStyleRegister(componentInfo, () => {
const { token: proxyToken, flush } = statisticToken(token.value); const { token: proxyToken, flush } = statisticToken(token.value);
const defaultComponentToken = const defaultComponentToken =
@ -80,32 +80,30 @@ export default function genComponentStyleHook<ComponentName extends OverrideComp
: getDefaultToken; : getDefaultToken;
const mergedComponentToken = { ...defaultComponentToken, ...token.value[component] }; const mergedComponentToken = { ...defaultComponentToken, ...token.value[component] };
const componentCls = `.${prefixCls}`; const componentCls = `.${prefixCls.value}`;
const mergedToken = mergeToken< const mergedToken = mergeToken<
TokenWithCommonCls<GlobalTokenWithComponent<OverrideComponent>> TokenWithCommonCls<GlobalTokenWithComponent<OverrideComponent>>
>( >(
proxyToken, proxyToken,
{ {
componentCls, componentCls,
prefixCls, prefixCls: prefixCls.value,
iconCls: `.${iconPrefixCls}`, iconCls: `.${iconPrefixCls.value}`,
antCls: `.${rootPrefixCls}`, antCls: `.${rootPrefixCls.value}`,
}, },
mergedComponentToken, mergedComponentToken,
); );
const styleInterpolation = styleFn(mergedToken as unknown as FullToken<ComponentName>, { const styleInterpolation = styleFn(mergedToken as unknown as FullToken<ComponentName>, {
hashId: hashId.value, hashId: hashId.value,
prefixCls, prefixCls: prefixCls.value,
rootPrefixCls, rootPrefixCls: rootPrefixCls.value,
iconPrefixCls: iconPrefixCls.value, iconPrefixCls: iconPrefixCls.value,
overrideComponentToken: token.value[component], overrideComponentToken: token.value[component],
}); });
flush(component, mergedComponentToken); flush(component, mergedComponentToken);
return [genCommonStyle(token.value, prefixCls), styleInterpolation]; return [genCommonStyle(token.value, prefixCls.value), styleInterpolation];
}, }),
), hashId,
hashId.value,
]; ];
}; };
} }