UNPKG

@carbon/ibm-products

Version:
149 lines (147 loc) 5.23 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_index = require("../../node_modules/classnames/index.js"); const require_settings = require("../../settings.js"); const require_devtools = require("../../global/js/utils/devtools.js"); require("../../global/js/utils/props-helper.js"); const require_TooltipTrigger = require("../TooltipTrigger/TooltipTrigger.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let _carbon_react = require("@carbon/react"); let _carbon_react_icons = require("@carbon/react/icons"); //#region src/components/UserAvatar/UserAvatar.tsx /** * Copyright IBM Corp. 2024, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default); const blockClass = `${require_settings.pkg.prefix}--user-avatar`; const componentName = "UserAvatar"; const UserAvatar = react.default.forwardRef((props, ref) => { const { backgroundColor = "order-1-cyan", className, image, imageDescription, name, renderIcon: RenderIcon, size = "md", tooltipText, tooltipAlignment = "bottom", ...rest } = props; const carbonPrefix = (0, _carbon_react.usePrefix)(); const iconSize = { sm: 16, md: 20, lg: 24, xl: 32 }; const formatInitials = () => { const parts = name?.split(" ") || []; const firstChar = parts[0].charAt(0).toUpperCase(); const secondChar = parts[0].charAt(1).toUpperCase(); if (parts.length === 1) return firstChar + secondChar; const lastChar = parts[parts.length - 1].charAt(0).toUpperCase(); const initials = [firstChar]; if (lastChar) initials.push(lastChar); return "".concat(...initials); }; const getItem = () => { const iconProps = { size: iconSize[size] }; if (image) return /* @__PURE__ */ react.default.createElement("img", { alt: imageDescription, src: image, className: `${blockClass}__photo ${blockClass}__photo--${size}` }); if (RenderIcon) return /* @__PURE__ */ react.default.createElement(RenderIcon, iconProps); if (name) return formatInitials(); return /* @__PURE__ */ react.default.createElement(_carbon_react_icons.User, iconProps); }; const Avatar = () => /* @__PURE__ */ react.default.createElement("div", { ...rest, className: (0, import_classnames.default)(blockClass, className, `${blockClass}--${backgroundColor}`, `${blockClass}--${size}`, {}), ref, ...require_devtools.getDevtoolsProps(componentName) }, getItem()); if (tooltipText) return /* @__PURE__ */ react.default.createElement(_carbon_react.Tooltip, { align: tooltipAlignment, label: tooltipText, className: `${blockClass}__tooltip ${carbonPrefix}--icon-tooltip` }, /* @__PURE__ */ react.default.createElement(require_TooltipTrigger.TooltipTrigger, null, /* @__PURE__ */ react.default.createElement(Avatar, null))); return /* @__PURE__ */ react.default.createElement(Avatar, null); }); UserAvatar.displayName = componentName; UserAvatar.propTypes = { /** * Provide the background color need to be set for UserAvatar. Background color will be set based on lighter or darker theme. * For example: if you select order-5-purple, it will take $purple-60 for lighter theme and $purple-50 for darker theme. */ backgroundColor: prop_types.default.oneOf([ "order-1-cyan", "order-2-gray", "order-3-green", "order-4-magenta", "order-5-purple", "order-6-teal", "order-7-cyan", "order-8-gray", "order-9-green", "order-10-magenta", "order-11-purple", "order-12-teal" ]), /** * Provide an optional class to be applied to the containing node. */ className: prop_types.default.string, /** * When passing the image prop, supply a full path to the image to be displayed. */ /**@ts-ignore */ image: prop_types.default.string, /** * When passing the image prop use the imageDescription prop to describe the image for screen reader. */ /**@ts-ignore */ imageDescription: prop_types.default.string, /** * When passing the name prop, either send the initials to be used or the user's full name. The first two capital letters of the user's name will be used as the name. */ name: prop_types.default.string, /** * Provide a custom icon to use if you need to use an icon other than the default one */ /**@ts-ignore */ renderIcon: prop_types.default.oneOfType([ prop_types.default.func, prop_types.default.object, prop_types.default.string ]), /** * Set the size of the avatar circle */ size: prop_types.default.oneOf([ "xl", "lg", "md", "sm" ]), /** * Specify how the trigger should align with the tooltip */ tooltipAlignment: prop_types.default.oneOf([ "top", "top-left", "top-right", "bottom", "bottom-left", "bottom-right", "left", "right" ]), /** * Pass in the display name to have it shown on hover */ tooltipText: prop_types.default.string }; //#endregion exports.UserAvatar = UserAvatar;