UNPKG

@elastic/eui

Version:

Elastic UI Component Library

117 lines (112 loc) 6.05 kB
import _extends from "@babel/runtime/helpers/extends"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["className", "color", "imageUrl", "initials", "initialsLength", "iconType", "iconSize", "iconColor", "name", "size", "type", "isDisabled", "style"], _excluded2 = ["casing"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React from 'react'; import classNames from 'classnames'; import { isColorDark, hexToRgb, isValidHex } from '../../services/color'; import { euiPaletteColorBlindBehindText, toInitials, useEuiTheme } from '../../services'; import { EuiIcon } from '../icon'; import { euiAvatarStyles } from './avatar.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var SIZES = ['s', 'm', 'l', 'xl']; export var TYPES = ['space', 'user']; export var CASING = ['capitalize', 'uppercase', 'lowercase', 'none']; /** * The avatar can only display one type of content, * initials, or image, or iconType */ export var EuiAvatar = function EuiAvatar(_ref) { var _classNames; var className = _ref.className, color = _ref.color, imageUrl = _ref.imageUrl, initials = _ref.initials, initialsLength = _ref.initialsLength, iconType = _ref.iconType, iconSize = _ref.iconSize, iconColor = _ref.iconColor, name = _ref.name, _ref$size = _ref.size, size = _ref$size === void 0 ? 'm' : _ref$size, _ref$type = _ref.type, type = _ref$type === void 0 ? 'user' : _ref$type, _ref$isDisabled = _ref.isDisabled, isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled, style = _ref.style, props = _objectWithoutProperties(_ref, _excluded); var _props$casing = props.casing, casing = _props$casing === void 0 ? type === 'space' ? 'none' : 'uppercase' : _props$casing, rest = _objectWithoutProperties(props, _excluded2); var euiTheme = useEuiTheme(); var styles = euiAvatarStyles(euiTheme); var visColors = euiPaletteColorBlindBehindText(); var isPlain = color === 'plain'; var isSubdued = color === 'subdued'; var classes = classNames('euiAvatar', (_classNames = {}, _defineProperty(_classNames, "euiAvatar--".concat(size), size), _defineProperty(_classNames, "euiAvatar--".concat(type), type), _defineProperty(_classNames, 'euiAvatar-isDisabled', isDisabled), _classNames), className); var cssStyles = [styles.euiAvatar, styles[type], styles[size], styles[casing], isPlain && styles.plain, isSubdued && styles.subdued, isDisabled && styles.isDisabled]; checkValidInitials(initials); var avatarStyle = _objectSpread({}, style); var iconCustomColor = iconColor; var isNamedColor = color === 'plain' || color === 'subdued' || color === null; if (!isNamedColor) { checkValidColor(color); var assignedColor = color || visColors[Math.floor(name.length % visColors.length)]; var textColor = isColorDark.apply(void 0, _toConsumableArray(hexToRgb(assignedColor))) ? '#FFFFFF' : '#000000'; avatarStyle.backgroundColor = assignedColor; avatarStyle.color = textColor; // Allow consumers to let the icons keep their default color (like app icons) // when passing `iconColor = null`, otherwise continue to pass on `iconColor` or adjust with textColor iconCustomColor = iconColor || iconColor === null ? iconColor : textColor; } if (imageUrl) { avatarStyle.backgroundImage = "url(".concat(imageUrl, ")"); } var content; if (!imageUrl && !iconType) { // Create the initials var calculatedInitials = toInitials(name, initialsLength, initials); content = ___EmotionJSX("span", { "aria-hidden": "true" }, calculatedInitials); } else if (iconType) { content = ___EmotionJSX(EuiIcon, { className: "euiAvatar__icon", size: iconSize || size, type: iconType, color: iconCustomColor === null ? undefined : iconCustomColor }); } return ___EmotionJSX("div", _extends({ css: cssStyles, className: classes, style: avatarStyle, "aria-label": isDisabled ? undefined : name, role: isDisabled ? 'presentation' : 'img', title: name }, rest), content); }; // TODO: Migrate to a service export var checkValidColor = function checkValidColor(color) { var validHex = color && isValidHex(color) || color === 'plain' || color === 'subdued'; if (color && !validHex) { throw new Error('EuiAvatar needs to pass a valid color. This can either be a three ' + 'or six character hex value'); } }; function checkValidInitials(initials) { // Must be a string of 1 or 2 characters if (initials && initials.length > 2) { console.warn('EuiAvatar only accepts a max of 2 characters for the initials as a string. It is displaying only the first 2 characters.'); } }