-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathAvatar.tsx
221 lines (203 loc) · 6.65 KB
/
Avatar.tsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import type { CustomSlotsType, VueNode } from '../_util/type';
import type { CSSProperties, ExtractPropTypes, PropType } from 'vue';
import { computed, defineComponent, nextTick, onMounted, shallowRef, watch } from 'vue';
import { getPropsSlot } from '../_util/props-util';
import PropTypes from '../_util/vue-types';
import useBreakpoint from '../_util/hooks/useBreakpoint';
import type { Breakpoint, ScreenSizeMap } from '../_util/responsiveObserve';
import { responsiveArray } from '../_util/responsiveObserve';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import ResizeObserver from '../vc-resize-observer';
import eagerComputed from '../_util/eagerComputed';
import useStyle from './style';
import { useAvatarInjectContext } from './AvatarContext';
export type AvatarSize = 'large' | 'small' | 'default' | number | ScreenSizeMap;
export const avatarProps = () => ({
prefixCls: String,
shape: { type: String as PropType<'circle' | 'square'>, default: 'circle' },
size: {
type: [Number, String, Object] as PropType<AvatarSize>,
default: (): AvatarSize => 'default',
},
src: String,
/** Srcset of image avatar */
srcset: String,
icon: PropTypes.any,
alt: String,
gap: Number,
draggable: { type: Boolean, default: undefined },
crossOrigin: String as PropType<'' | 'anonymous' | 'use-credentials'>,
loadError: {
type: Function as PropType<() => boolean>,
},
});
export type AvatarProps = Partial<ExtractPropTypes<ReturnType<typeof avatarProps>>>;
const Avatar = defineComponent({
name: 'AAvatar',
inheritAttrs: false,
props: avatarProps(),
slots: Object as CustomSlotsType<{
icon: any;
default: any;
}>,
setup(props, { slots, attrs }) {
const isImgExist = shallowRef(true);
const isMounted = shallowRef(false);
const scale = shallowRef(1);
const avatarChildrenRef = shallowRef<HTMLElement>(null);
const avatarNodeRef = shallowRef<HTMLElement>(null);
const { prefixCls } = useConfigInject('avatar', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const avatarCtx = useAvatarInjectContext();
const size = computed(() => {
return props.size === 'default' ? avatarCtx.size : props.size;
});
const screens = useBreakpoint();
const responsiveSize = eagerComputed(() => {
if (typeof props.size !== 'object') {
return undefined;
}
const currentBreakpoint: Breakpoint = responsiveArray.find(screen => screens.value[screen])!;
const currentSize = props.size[currentBreakpoint];
return currentSize;
});
const responsiveSizeStyle = (hasIcon: boolean) => {
if (responsiveSize.value) {
return {
width: `${responsiveSize.value}px`,
height: `${responsiveSize.value}px`,
lineHeight: `${responsiveSize.value}px`,
fontSize: `${hasIcon ? responsiveSize.value / 2 : 18}px`,
};
}
return {};
};
const setScaleParam = () => {
if (!avatarChildrenRef.value || !avatarNodeRef.value) {
return;
}
const childrenWidth = avatarChildrenRef.value.offsetWidth; // offsetWidth avoid affecting be transform scale
const nodeWidth = avatarNodeRef.value.offsetWidth;
// denominator is 0 is no meaning
if (childrenWidth !== 0 && nodeWidth !== 0) {
const { gap = 4 } = props;
if (gap * 2 < nodeWidth) {
scale.value =
nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1;
}
}
};
const handleImgLoadError = () => {
const { loadError } = props;
const errorFlag = loadError?.();
if (errorFlag !== false) {
isImgExist.value = false;
}
};
watch(
() => props.src,
() => {
nextTick(() => {
isImgExist.value = true;
scale.value = 1;
});
},
);
watch(
() => props.gap,
() => {
nextTick(() => {
setScaleParam();
});
},
);
onMounted(() => {
nextTick(() => {
setScaleParam();
isMounted.value = true;
});
});
return () => {
const { shape, src, alt, srcset, draggable, crossOrigin } = props;
const mergeShape = avatarCtx.shape ?? shape;
const icon = getPropsSlot(slots, props, 'icon');
const pre = prefixCls.value;
const classString = {
[`${attrs.class}`]: !!attrs.class,
[pre]: true,
[`${pre}-lg`]: size.value === 'large',
[`${pre}-sm`]: size.value === 'small',
[`${pre}-${mergeShape}`]: true,
[`${pre}-image`]: src && isImgExist.value,
[`${pre}-icon`]: icon,
[hashId.value]: true,
};
const sizeStyle: CSSProperties =
typeof size.value === 'number'
? {
width: `${size.value}px`,
height: `${size.value}px`,
lineHeight: `${size.value}px`,
fontSize: icon ? `${size.value / 2}px` : '18px',
}
: {};
const children: VueNode = slots.default?.();
let childrenToRender;
if (src && isImgExist.value) {
childrenToRender = (
<img
draggable={draggable}
src={src}
srcset={srcset}
onError={handleImgLoadError}
alt={alt}
crossorigin={crossOrigin}
/>
);
} else if (icon) {
childrenToRender = icon;
} else if (isMounted.value || scale.value !== 1) {
const transformString = `scale(${scale.value}) translateX(-50%)`;
const childrenStyle: CSSProperties = {
msTransform: transformString,
WebkitTransform: transformString,
transform: transformString,
};
const sizeChildrenStyle =
typeof size.value === 'number'
? {
lineHeight: `${size.value}px`,
}
: {};
childrenToRender = (
<ResizeObserver onResize={setScaleParam}>
<span
class={`${pre}-string`}
ref={avatarChildrenRef}
style={{ ...sizeChildrenStyle, ...childrenStyle }}
>
{children}
</span>
</ResizeObserver>
);
} else {
childrenToRender = (
<span class={`${pre}-string`} ref={avatarChildrenRef} style={{ opacity: 0 }}>
{children}
</span>
);
}
return wrapSSR(
<span
{...attrs}
ref={avatarNodeRef}
class={classString}
style={[sizeStyle, responsiveSizeStyle(!!icon), attrs.style as CSSProperties]}
>
{childrenToRender}
</span>,
);
};
},
});
export default Avatar;