UNPKG

@atlaskit/avatar

Version:

An avatar is a visual representation of a user or entity.

165 lines (160 loc) 5.89 kB
/* avatar.tsx generated by @compiled/babel-plugin v0.39.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 { useAvatarContext } from './context'; import AvatarImage from './internal/avatar-image'; import { AvatarContentContext } from './internal/content-context'; import { EnsureIsInsideAvatarContext } from './internal/ensure-is-inside-avatar-context'; import getCustomElement from './internal/get-custom-element'; import PresenceWrapper from './internal/presence-wrapper'; import StatusWrapper from './internal/status-wrapper'; const packageName = "@atlaskit/avatar"; const packageVersion = "25.11.1"; 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', isDecorative = false, imgLoading, 'aria-controls': ariaControls, 'aria-expanded': ariaExpanded, 'aria-haspopup': ariaHasPopup }, 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 || ariaHasPopup; 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 && !isDecorative ? labelId : undefined, style: { zIndex: stackIndex }, className: ax(["_12ji1r31 _1qu2glyw _12y3idpf _1e0c1o8l _kqswh2mm"]) }, /*#__PURE__*/React.createElement(AvatarContentContext.Provider, { value: { as: getCustomElement(isDisabled, href, onClick, ariaHasPopup), appearance, borderColor, href, isDisabled, label: label || defaultLabel, onClick: isInteractive ? onClickHandler : undefined, ref, size, stackIndex, target, testId: testId ? `${testId}--inner` : undefined, 'aria-controls': ariaControls, 'aria-expanded': ariaExpanded, 'aria-haspopup': ariaHasPopup, avatarImage: /*#__PURE__*/React.createElement(AvatarImage, { alt: !containerShouldBeImage && src ? name : undefined, src: src, appearance: appearance, size: size, testId: testId, imgLoading: imgLoading }) } }, 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;