Skip to content

Commit fc0f0d8

Browse files
authored
refactor(empty): less to cssinjs (#6230)
* refactor(empty): less to cssinjs * chore: remove unuse code * fix: reactivity lose
1 parent a205615 commit fc0f0d8

File tree

8 files changed

+244
-279
lines changed

8 files changed

+244
-279
lines changed

components/empty/empty.tsx

+65-36
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,73 @@
1-
import useConfigInject from '../config-provider/hooks/useConfigInject';
1+
import { useToken } from '../theme/internal';
2+
import { TinyColor } from '@ctrl/tinycolor';
3+
import type { CSSProperties } from 'vue';
4+
import { defineComponent, computed } from 'vue';
25

3-
const Empty = () => {
4-
const { getPrefixCls } = useConfigInject('empty', {});
5-
const prefixCls = getPrefixCls('empty-img-default');
6+
const Empty = defineComponent({
7+
setup() {
8+
const [, token] = useToken();
69

7-
return (
8-
<svg class={prefixCls} width="184" height="152" viewBox="0 0 184 152">
9-
<g fill="none" fill-rule="evenodd">
10-
<g transform="translate(24 31.67)">
11-
<ellipse class={`${prefixCls}-ellipse`} cx="67.797" cy="106.89" rx="67.797" ry="12.668" />
12-
<path
13-
class={`${prefixCls}-path-1`}
14-
d="M122.034 69.674L98.109 40.229c-1.148-1.386-2.826-2.225-4.593-2.225h-51.44c-1.766 0-3.444.839-4.592 2.225L13.56 69.674v15.383h108.475V69.674z"
15-
/>
16-
<path
17-
class={`${prefixCls}-path-2`}
18-
d="M101.537 86.214L80.63 61.102c-1.001-1.207-2.507-1.867-4.048-1.867H31.724c-1.54 0-3.047.66-4.048 1.867L6.769 86.214v13.792h94.768V86.214z"
19-
transform="translate(13.56)"
20-
/>
21-
<path
22-
class={`${prefixCls}-path-3`}
23-
d="M33.83 0h67.933a4 4 0 0 1 4 4v93.344a4 4 0 0 1-4 4H33.83a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4z"
24-
/>
10+
const themeStyle = computed<CSSProperties>(() => {
11+
const bgColor = new TinyColor(token.value.colorBgBase);
12+
13+
// Dark Theme need more dark of this
14+
if (bgColor.toHsl().l < 0.5) {
15+
return {
16+
opacity: 0.65,
17+
};
18+
}
19+
return {};
20+
});
21+
22+
return () => (
23+
<svg
24+
style={themeStyle.value}
25+
width="184"
26+
height="152"
27+
viewBox="0 0 184 152"
28+
xmlns="http://www.w3.org/2000/svg"
29+
>
30+
<g fill="none" fill-rule="evenodd">
31+
<g transform="translate(24 31.67)">
32+
<ellipse
33+
fill-opacity=".8"
34+
fill="#F5F5F7"
35+
cx="67.797"
36+
cy="106.89"
37+
rx="67.797"
38+
ry="12.668"
39+
/>
40+
<path
41+
d="M122.034 69.674L98.109 40.229c-1.148-1.386-2.826-2.225-4.593-2.225h-51.44c-1.766 0-3.444.839-4.592 2.225L13.56 69.674v15.383h108.475V69.674z"
42+
fill="#AEB8C2"
43+
/>
44+
<path
45+
d="M101.537 86.214L80.63 61.102c-1.001-1.207-2.507-1.867-4.048-1.867H31.724c-1.54 0-3.047.66-4.048 1.867L6.769 86.214v13.792h94.768V86.214z"
46+
fill="url(#linearGradient-1)"
47+
transform="translate(13.56)"
48+
/>
49+
<path
50+
d="M33.83 0h67.933a4 4 0 0 1 4 4v93.344a4 4 0 0 1-4 4H33.83a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4z"
51+
fill="#F5F5F7"
52+
/>
53+
<path
54+
d="M42.678 9.953h50.237a2 2 0 0 1 2 2V36.91a2 2 0 0 1-2 2H42.678a2 2 0 0 1-2-2V11.953a2 2 0 0 1 2-2zM42.94 49.767h49.713a2.262 2.262 0 1 1 0 4.524H42.94a2.262 2.262 0 0 1 0-4.524zM42.94 61.53h49.713a2.262 2.262 0 1 1 0 4.525H42.94a2.262 2.262 0 0 1 0-4.525zM121.813 105.032c-.775 3.071-3.497 5.36-6.735 5.36H20.515c-3.238 0-5.96-2.29-6.734-5.36a7.309 7.309 0 0 1-.222-1.79V69.675h26.318c2.907 0 5.25 2.448 5.25 5.42v.04c0 2.971 2.37 5.37 5.277 5.37h34.785c2.907 0 5.277-2.421 5.277-5.393V75.1c0-2.972 2.343-5.426 5.25-5.426h26.318v33.569c0 .617-.077 1.216-.221 1.789z"
55+
fill="#DCE0E6"
56+
/>
57+
</g>
2558
<path
26-
class={`${prefixCls}-path-4`}
27-
d="M42.678 9.953h50.237a2 2 0 0 1 2 2V36.91a2 2 0 0 1-2 2H42.678a2 2 0 0 1-2-2V11.953a2 2 0 0 1 2-2zM42.94 49.767h49.713a2.262 2.262 0 1 1 0 4.524H42.94a2.262 2.262 0 0 1 0-4.524zM42.94 61.53h49.713a2.262 2.262 0 1 1 0 4.525H42.94a2.262 2.262 0 0 1 0-4.525zM121.813 105.032c-.775 3.071-3.497 5.36-6.735 5.36H20.515c-3.238 0-5.96-2.29-6.734-5.36a7.309 7.309 0 0 1-.222-1.79V69.675h26.318c2.907 0 5.25 2.448 5.25 5.42v.04c0 2.971 2.37 5.37 5.277 5.37h34.785c2.907 0 5.277-2.421 5.277-5.393V75.1c0-2.972 2.343-5.426 5.25-5.426h26.318v33.569c0 .617-.077 1.216-.221 1.789z"
59+
d="M149.121 33.292l-6.83 2.65a1 1 0 0 1-1.317-1.23l1.937-6.207c-2.589-2.944-4.109-6.534-4.109-10.408C138.802 8.102 148.92 0 161.402 0 173.881 0 184 8.102 184 18.097c0 9.995-10.118 18.097-22.599 18.097-4.528 0-8.744-1.066-12.28-2.902z"
60+
fill="#DCE0E6"
2861
/>
62+
<g transform="translate(149.65 15.383)" fill="#FFF">
63+
<ellipse cx="20.654" cy="3.167" rx="2.849" ry="2.815" />
64+
<path d="M5.698 5.63H0L2.898.704zM9.259.704h4.985V5.63H9.259z" />
65+
</g>
2966
</g>
30-
<path
31-
class={`${prefixCls}-path-5`}
32-
d="M149.121 33.292l-6.83 2.65a1 1 0 0 1-1.317-1.23l1.937-6.207c-2.589-2.944-4.109-6.534-4.109-10.408C138.802 8.102 148.92 0 161.402 0 173.881 0 184 8.102 184 18.097c0 9.995-10.118 18.097-22.599 18.097-4.528 0-8.744-1.066-12.28-2.902z"
33-
/>
34-
<g class={`${prefixCls}-g`} transform="translate(149.65 15.383)">
35-
<ellipse cx="20.654" cy="3.167" rx="2.849" ry="2.815" />
36-
<path d="M5.698 5.63H0L2.898.704zM9.259.704h4.985V5.63H9.259z" />
37-
</g>
38-
</g>
39-
</svg>
40-
);
41-
};
67+
</svg>
68+
);
69+
},
70+
});
4271

4372
Empty.PRESENTED_IMAGE_DEFAULT = true;
4473

components/empty/index.tsx

+64-59
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,90 @@
1-
import type { CSSProperties, FunctionalComponent, PropType } from 'vue';
1+
import { defineComponent } from 'vue';
2+
import type { CSSProperties, PropType, ExtractPropTypes } from 'vue';
23
import classNames from '../_util/classNames';
34
import LocaleReceiver from '../locale-provider/LocaleReceiver';
45
import DefaultEmptyImg from './empty';
56
import SimpleEmptyImg from './simple';
67
import { filterEmpty } from '../_util/props-util';
78
import PropTypes from '../_util/vue-types';
8-
import type { VueNode } from '../_util/type';
99
import { withInstall } from '../_util/type';
1010
import useConfigInject from '../config-provider/hooks/useConfigInject';
1111

12+
import useStyle from './style';
13+
1214
const defaultEmptyImg = <DefaultEmptyImg />;
1315
const simpleEmptyImg = <SimpleEmptyImg />;
1416

1517
interface Locale {
1618
description?: string;
1719
}
1820

19-
export interface EmptyProps {
20-
prefixCls?: string;
21-
class?: any;
22-
style?: string | CSSProperties;
23-
imageStyle?: CSSProperties;
24-
image?: VueNode | null;
25-
description?: VueNode;
26-
}
21+
export const emptyProps = () => ({
22+
prefixCls: String,
23+
class: PropTypes.any,
24+
style: [String, Object] as PropType<string | CSSProperties>,
25+
imageStyle: Object as PropType<CSSProperties>,
26+
image: PropTypes.any,
27+
description: PropTypes.any,
28+
});
2729

28-
interface EmptyType extends FunctionalComponent<EmptyProps> {
29-
displayName: string;
30-
PRESENTED_IMAGE_DEFAULT: VueNode;
31-
PRESENTED_IMAGE_SIMPLE: VueNode;
32-
}
30+
export type EmptyProps = Partial<ExtractPropTypes<ReturnType<typeof emptyProps>>>;
3331

34-
const Empty: EmptyType = (props, { slots = {}, attrs }) => {
35-
const { direction, prefixCls: prefixClsRef } = useConfigInject('empty', props);
36-
const prefixCls = prefixClsRef.value;
32+
const Empty = defineComponent({
33+
name: 'AEmpty',
34+
inheritAttrs: false,
35+
props: emptyProps(),
36+
setup(props, { slots = {}, attrs }) {
37+
const { direction, prefixCls: prefixClsRef } = useConfigInject('empty', props);
3738

38-
const {
39-
image = defaultEmptyImg,
40-
description = slots.description?.() || undefined,
41-
imageStyle,
42-
class: className = '',
43-
...restProps
44-
} = { ...props, ...attrs };
39+
const [wrapSSR, hashId] = useStyle(prefixClsRef);
4540

46-
return (
47-
<LocaleReceiver
48-
componentName="Empty"
49-
children={(locale: Locale) => {
50-
const des = typeof description !== 'undefined' ? description : locale.description;
51-
const alt = typeof des === 'string' ? des : 'empty';
52-
let imageNode: EmptyProps['image'] = null;
41+
return () => {
42+
const prefixCls = prefixClsRef.value;
43+
const {
44+
image = defaultEmptyImg,
45+
description = slots.description?.() || undefined,
46+
imageStyle,
47+
class: className = '',
48+
...restProps
49+
} = { ...props, ...attrs };
5350

54-
if (typeof image === 'string') {
55-
imageNode = <img alt={alt} src={image} />;
56-
} else {
57-
imageNode = image;
58-
}
51+
return wrapSSR(
52+
<LocaleReceiver
53+
componentName="Empty"
54+
children={(locale: Locale) => {
55+
const des = typeof description !== 'undefined' ? description : locale.description;
56+
const alt = typeof des === 'string' ? des : 'empty';
57+
let imageNode: EmptyProps['image'] = null;
5958

60-
return (
61-
<div
62-
class={classNames(prefixCls, className, {
63-
[`${prefixCls}-normal`]: image === simpleEmptyImg,
64-
[`${prefixCls}-rtl`]: direction.value === 'rtl',
65-
})}
66-
{...restProps}
67-
>
68-
<div class={`${prefixCls}-image`} style={imageStyle}>
69-
{imageNode}
70-
</div>
71-
{des && <p class={`${prefixCls}-description`}>{des}</p>}
72-
{slots.default && (
73-
<div class={`${prefixCls}-footer`}>{filterEmpty(slots.default())}</div>
74-
)}
75-
</div>
76-
);
77-
}}
78-
/>
79-
);
80-
};
59+
if (typeof image === 'string') {
60+
imageNode = <img alt={alt} src={image} />;
61+
} else {
62+
imageNode = image;
63+
}
8164

82-
Empty.displayName = 'AEmpty';
65+
return (
66+
<div
67+
class={classNames(prefixCls, className, hashId.value, {
68+
[`${prefixCls}-normal`]: image === simpleEmptyImg,
69+
[`${prefixCls}-rtl`]: direction.value === 'rtl',
70+
})}
71+
{...restProps}
72+
>
73+
<div class={`${prefixCls}-image`} style={imageStyle}>
74+
{imageNode}
75+
</div>
76+
{des && <p class={`${prefixCls}-description`}>{des}</p>}
77+
{slots.default && (
78+
<div class={`${prefixCls}-footer`}>{filterEmpty(slots.default())}</div>
79+
)}
80+
</div>
81+
);
82+
}}
83+
/>,
84+
);
85+
};
86+
},
87+
});
8388

8489
Empty.PRESENTED_IMAGE_DEFAULT = defaultEmptyImg;
8590
Empty.PRESENTED_IMAGE_SIMPLE = simpleEmptyImg;

components/empty/simple.tsx

+33-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
import useConfigInject from '../config-provider/hooks/useConfigInject';
1+
import { TinyColor } from '@ctrl/tinycolor';
2+
import { computed, defineComponent } from 'vue';
3+
import { useToken } from '../theme/internal';
24

3-
const Simple = () => {
4-
const { getPrefixCls } = useConfigInject('empty', {});
5-
const prefixCls = getPrefixCls('empty-img-simple');
5+
const Simple = defineComponent({
6+
setup() {
7+
const [, token] = useToken();
68

7-
return (
8-
<svg class={prefixCls} width="64" height="41" viewBox="0 0 64 41">
9-
<g transform="translate(0 1)" fill="none" fill-rule="evenodd">
10-
<ellipse class={`${prefixCls}-ellipse`} fill="#F5F5F5" cx="32" cy="33" rx="32" ry="7" />
11-
<g class={`${prefixCls}-g`} fill-rule="nonzero" stroke="#D9D9D9">
12-
<path d="M55 12.76L44.854 1.258C44.367.474 43.656 0 42.907 0H21.093c-.749 0-1.46.474-1.947 1.257L9 12.761V22h46v-9.24z" />
13-
<path
14-
d="M41.613 15.931c0-1.605.994-2.93 2.227-2.931H55v18.137C55 33.26 53.68 35 52.05 35h-40.1C10.32 35 9 33.259 9 31.137V13h11.16c1.233 0 2.227 1.323 2.227 2.928v.022c0 1.605 1.005 2.901 2.237 2.901h14.752c1.232 0 2.237-1.308 2.237-2.913v-.007z"
15-
fill="#FAFAFA"
16-
class={`${prefixCls}-path`}
17-
/>
9+
const color = computed(() => {
10+
const { colorFill, colorFillTertiary, colorFillQuaternary, colorBgContainer } = token.value;
11+
12+
return {
13+
borderColor: new TinyColor(colorFill).onBackground(colorBgContainer).toHexString(),
14+
shadowColor: new TinyColor(colorFillTertiary).onBackground(colorBgContainer).toHexString(),
15+
contentColor: new TinyColor(colorFillQuaternary)
16+
.onBackground(colorBgContainer)
17+
.toHexString(),
18+
};
19+
});
20+
21+
return () => (
22+
<svg width="64" height="41" viewBox="0 0 64 41" xmlns="http://www.w3.org/2000/svg">
23+
<g transform="translate(0 1)" fill="none" fill-rule="evenodd">
24+
<ellipse fill={color.value.shadowColor} cx="32" cy="33" rx="32" ry="7" />
25+
<g fill-rule="nonzero" stroke={color.value.borderColor}>
26+
<path d="M55 12.76L44.854 1.258C44.367.474 43.656 0 42.907 0H21.093c-.749 0-1.46.474-1.947 1.257L9 12.761V22h46v-9.24z" />
27+
<path
28+
d="M41.613 15.931c0-1.605.994-2.93 2.227-2.931H55v18.137C55 33.26 53.68 35 52.05 35h-40.1C10.32 35 9 33.259 9 31.137V13h11.16c1.233 0 2.227 1.323 2.227 2.928v.022c0 1.605 1.005 2.901 2.237 2.901h14.752c1.232 0 2.237-1.308 2.237-2.913v-.007z"
29+
fill={color.value.contentColor}
30+
/>
31+
</g>
1832
</g>
19-
</g>
20-
</svg>
21-
);
22-
};
33+
</svg>
34+
);
35+
},
36+
});
2337

2438
Simple.PRESENTED_IMAGE_SIMPLE = true;
2539
export default Simple;

0 commit comments

Comments
 (0)