@atlaskit/avatar
Version:
An avatar is a visual representation of a user or entity.
156 lines (151 loc) • 6.79 kB
JavaScript
/* avatar.tsx generated by @compiled/babel-plugin v0.36.1 */
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./avatar.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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';
var packageName = "@atlaskit/avatar";
var packageVersion = "25.0.2";
var 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)
*/
var Avatar = /*#__PURE__*/forwardRef(function (_ref, ref) {
var analyticsContext = _ref.analyticsContext,
_ref$appearance = _ref.appearance,
appearance = _ref$appearance === void 0 ? 'circle' : _ref$appearance,
label = _ref.label,
borderColor = _ref.borderColor,
children = _ref.children,
href = _ref.href,
isDisabled = _ref.isDisabled,
name = _ref.name,
onClick = _ref.onClick,
presence = _ref.presence,
sizeProp = _ref.size,
src = _ref.src,
stackIndex = _ref.stackIndex,
status = _ref.status,
target = _ref.target,
testId = _ref.testId,
_ref$as = _ref.as,
AvatarContainer = _ref$as === void 0 ? 'div' : _ref$as;
var _useAnalyticsEvents = useAnalyticsEvents(),
createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
var context = useAvatarContext();
var size = sizeProp || (context === null || context === void 0 ? void 0 : context.size) || 'medium';
var customPresenceNode = /*#__PURE__*/isValidElement(presence) ? presence : null;
var customStatusNode = /*#__PURE__*/isValidElement(status) ? status : null;
var isValidIconSize = size !== 'xxlarge' && size !== 'xsmall';
var lastAnalytics = useRef(analyticsContext);
var labelId = useId();
useEffect(function () {
lastAnalytics.current = analyticsContext;
}, [analyticsContext]);
var onClickHandler = useCallback(function (event) {
if (isDisabled || typeof onClick !== 'function') {
return;
}
var analyticsEvent = createAnalyticsEvent({
action: 'clicked',
actionSubject: 'avatar',
attributes: {
componentName: 'avatar',
packageName: packageName,
packageVersion: packageVersion
}
});
/**
* To avoid wrapping this component in AnalyticsContext we manually
* push the parent context's meta data into the context.
*/
var context = _objectSpread({
componentName: 'avatar',
packageName: packageName,
packageVersion: packageVersion
}, lastAnalytics.current);
analyticsEvent.context.push(context);
/**
* Replicating the logic in the `withAnalyticsEvents` HOC.
*/
var clone = analyticsEvent.clone();
if (clone) {
clone.fire('atlaskit');
}
onClick(event, analyticsEvent);
}, [createAnalyticsEvent, isDisabled, onClick]);
var isPresence = isValidIconSize && presence && !status;
var 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
var defaultLabel = [name, isStatus && !customStatusNode && "(".concat(status, ")"), isPresence && !customPresenceNode && "(".concat(presence, ")")].filter(Boolean).join(' ');
var isInteractive = onClick || href || isDisabled;
var 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: appearance,
borderColor: borderColor,
href: href,
isDisabled: isDisabled,
label: label || defaultLabel,
onClick: isInteractive ? onClickHandler : undefined,
ref: ref,
size: size,
stackIndex: stackIndex,
target: target,
testId: testId ? "".concat(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 && "".concat(testId, "--label"),
id: labelId,
hidden: true
}, defaultLabel) : undefined));
});
Avatar.displayName = 'Avatar';
export default Avatar;