import BaseFoundation, { DefaultAdapter } from '../base/foundation'; import warning from '../utils/warning'; export interface AvatarAdapter
, S = Record {
notifyImgState(isImgExist: boolean): void;
notifyLeave(event: any): void;
notifyEnter(event: any): void;
setFocusVisible: (focusVisible: boolean) => void;
setScale: (scale: number) => void;
getAvatarNode: () => HTMLSpanElement
}
export default class AvatarFoundation , S = Record ) {
super({ ...adapter });
}
init() {
const { children } = this.getProps();
if (typeof children === "string") {
this.changeScale();
}
}
destroy() { }
handleImgLoadError() {
const { onError } = this.getProps();
const errorFlag = onError ? onError() : undefined;
if (errorFlag !== false) {
this._adapter.notifyImgState(false);
}
}
handleEnter(e: any) {
this._adapter.notifyEnter(e);
}
handleLeave(e: any) {
this._adapter.notifyLeave(e);
}
handleFocusVisible = (event: any) => {
const { target } = event;
try {
if (target.matches(':focus-visible')) {
this._adapter.setFocusVisible(true);
}
} catch (error) {
warning(true, 'Warning: [Semi Avatar] The current browser does not support the focus-visible');
}
}
handleBlur = () => {
this._adapter.setFocusVisible(false);
}
changeScale = () => {
const { gap } = this.getProps();
const node = this._adapter.getAvatarNode();
const stringNode = node?.firstChild as HTMLSpanElement;
const [nodeWidth, stringNodeWidth] = [node?.offsetWidth || 0, stringNode?.offsetWidth || 0];
if (nodeWidth !== 0 && stringNodeWidth !== 0 && gap * 2 < nodeWidth) {
const scale = nodeWidth - gap * 2 > stringNodeWidth ? 1 : (nodeWidth - gap * 2) / stringNodeWidth;
this._adapter.setScale(scale);
}
}
}