@atlaskit/avatar
Version:
An avatar is a visual representation of a user or entity.
154 lines (149 loc) • 5.42 kB
JavaScript
/* avatar.tsx generated by @compiled/babel-plugin v0.36.1 */
import "./avatar.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { forwardRef, isValidElement, useCallback, useEffect, useRef } from 'react';
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
import { useId } from '@atlaskit/ds-lib/use-id';
import { AvatarContent } from './avatar-content';
import { AvatarContentContext, EnsureIsInsideAvatarContext, useAvatarContext } from './context';
import AvatarImage from './internal/avatar-image';
import { PresenceWrapper } from './presence';
import { StatusWrapper } from './status';
import { getCustomElement } from './utilities';
const packageName = "@atlaskit/avatar";
const packageVersion = "25.0.2";
const containerStyles = null;
// eslint-disable-next-line @repo/internal/react/consistent-types-definitions
/**
* __Avatar__
*
* An avatar is a visual representation of a user or entity.
*
* - [Examples](https://atlassian.design/components/avatar/examples)
* - [Code](https://atlassian.design/components/avatar/code)
* - [Usage](https://atlassian.design/components/avatar/usage)
*/
const Avatar = /*#__PURE__*/forwardRef(({
analyticsContext,
appearance = 'circle',
label,
borderColor,
children,
href,
isDisabled,
name,
onClick,
presence,
size: sizeProp,
src,
stackIndex,
status,
target,
testId,
as: AvatarContainer = 'div'
}, ref) => {
const {
createAnalyticsEvent
} = useAnalyticsEvents();
const context = useAvatarContext();
const size = sizeProp || (context === null || context === void 0 ? void 0 : context.size) || 'medium';
const customPresenceNode = /*#__PURE__*/isValidElement(presence) ? presence : null;
const customStatusNode = /*#__PURE__*/isValidElement(status) ? status : null;
const isValidIconSize = size !== 'xxlarge' && size !== 'xsmall';
const lastAnalytics = useRef(analyticsContext);
const labelId = useId();
useEffect(() => {
lastAnalytics.current = analyticsContext;
}, [analyticsContext]);
const onClickHandler = useCallback(event => {
if (isDisabled || typeof onClick !== 'function') {
return;
}
const analyticsEvent = createAnalyticsEvent({
action: 'clicked',
actionSubject: 'avatar',
attributes: {
componentName: 'avatar',
packageName,
packageVersion
}
});
/**
* To avoid wrapping this component in AnalyticsContext we manually
* push the parent context's meta data into the context.
*/
const context = {
componentName: 'avatar',
packageName,
packageVersion,
...lastAnalytics.current
};
analyticsEvent.context.push(context);
/**
* Replicating the logic in the `withAnalyticsEvents` HOC.
*/
const clone = analyticsEvent.clone();
if (clone) {
clone.fire('atlaskit');
}
onClick(event, analyticsEvent);
}, [createAnalyticsEvent, isDisabled, onClick]);
const isPresence = isValidIconSize && presence && !status;
const isStatus = isValidIconSize && status;
// add presence or status to the label by default if presence and status are passed as a string
// if status or presence are nodes this is not added to the label by default
const defaultLabel = [name, isStatus && !customStatusNode && `(${status})`, isPresence && !customPresenceNode && `(${presence})`].filter(Boolean).join(' ');
const isInteractive = onClick || href || isDisabled;
const containerShouldBeImage = Boolean(!isInteractive && defaultLabel);
return /*#__PURE__*/React.createElement(EnsureIsInsideAvatarContext.Provider, {
value: true
}, /*#__PURE__*/React.createElement(AvatarContainer, {
"data-testid": testId,
role: containerShouldBeImage ? 'img' : undefined,
"aria-labelledby": containerShouldBeImage ? labelId : undefined,
style: {
zIndex: stackIndex
},
className: ax(["_12ji1r31 _1qu2glyw _12y3idpf _1e0c1o8l _kqswh2mm"])
}, /*#__PURE__*/React.createElement(AvatarContentContext.Provider, {
value: {
as: getCustomElement(isDisabled, href, onClick),
appearance,
borderColor,
href,
isDisabled,
label: label || defaultLabel,
onClick: isInteractive ? onClickHandler : undefined,
ref,
size,
stackIndex,
target,
testId: testId ? `${testId}--inner` : undefined,
avatarImage: /*#__PURE__*/React.createElement(AvatarImage, {
alt: !containerShouldBeImage && src ? name : undefined,
src: src,
appearance: appearance,
size: size,
testId: testId
})
}
}, children || /*#__PURE__*/React.createElement(AvatarContent, null)), isPresence && /*#__PURE__*/React.createElement(PresenceWrapper, {
appearance: appearance,
size: size,
presence: typeof presence === 'string' ? presence : undefined,
testId: testId
}, customPresenceNode), isStatus && /*#__PURE__*/React.createElement(StatusWrapper, {
appearance: appearance,
size: size,
borderColor: borderColor,
status: typeof status === 'string' ? status : undefined,
testId: testId
}, customStatusNode), containerShouldBeImage ? /*#__PURE__*/React.createElement("span", {
"data-testid": testId && `${testId}--label`,
id: labelId,
hidden: true
}, defaultLabel) : undefined));
});
Avatar.displayName = 'Avatar';
export default Avatar;