UNPKG

@braineet/ui

Version:

Braineet design system

121 lines (119 loc) 4.33 kB
var _excluded = ["id", "color", "size", "name", "src", "iconName", "iconColor", "customIcon", "shape", "altImg"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import React from 'react'; import chroma from 'chroma-js'; import { useTheme } from 'styled-components'; import { getInitials } from '../../utils'; import Box from '../box'; import Icon from '../icon'; import { StyledAvatar, StyledText, StyledImg, StyledAvatarBadge, StyledCircle, StyledIcon, ColorBadge } from './styles'; /** The `Avatar` component. */ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; var getColorProps = function getColorProps(color, theme, id) { if (color === 'automatic') { return theme.helpers.getAvatarProps({ color: color, id: id }); } var backgroundColor = theme.colors[color] || color; try { var constrast = chroma(backgroundColor).get('lab.l') < 70 ? 'dark' : 'light'; return { color: constrast === 'dark' ? theme.colors.white : theme.colors.black, backgroundColor: backgroundColor }; } catch (error) { return theme.helpers.getFills({ color: color }); } }; var Avatar = /*#__PURE__*/React.forwardRef(function (_ref, ref) { var _name$replace; var id = _ref.id, color = _ref.color, size = _ref.size, name = _ref.name, src = _ref.src, iconName = _ref.iconName, iconColor = _ref.iconColor, customIcon = _ref.customIcon, shape = _ref.shape, altImg = _ref.altImg, restProps = _objectWithoutPropertiesLoose(_ref, _excluded); var theme = useTheme(); // Define the avatar size var globalSize = ['xxs', 'xs', 'sm', 'md', 'lg'].includes(size) ? size : 'md'; var _getColorProps = getColorProps(color, theme, (id == null ? void 0 : id.length) || (name == null || (_name$replace = name.replace(' ', '')) == null ? void 0 : _name$replace.length)), colorProps = _getColorProps.color, backgroundColor = _getColorProps.backgroundColor; // Get text color and background color of avatar badge var _theme$helpers$getFil = theme.helpers.getFills({ appearance: 'subtle', color: iconColor }), iconColorAvatarBadge = _theme$helpers$getFil.color, backgroundColorAvatarBadge = _theme$helpers$getFil.backgroundColor; var initials = getInitials(name, '', { oneLetter: size === 'xxs' }); return /*#__PURE__*/_jsxs(StyledAvatar, _extends({ sizeItem: globalSize, variant: shape, ref: ref }, restProps, { children: [!src ? /*#__PURE__*/_jsx(StyledText, { size: size, sizeItem: globalSize, backgroundColor: backgroundColor, fontFamily: "display", color: colorProps, children: customIcon && /*#__PURE__*/_jsx(Icon, { name: customIcon }) || initials || /*#__PURE__*/_jsx(Icon, { name: "user" }) }) : /*#__PURE__*/_jsx(Box, { width: "100%", height: "100%", overflow: "hidden", borderRadius: "inherit", children: /*#__PURE__*/_jsx(StyledImg, { src: src, alt: altImg }) }), iconName && typeof iconName === 'string' && /*#__PURE__*/_jsx(StyledAvatarBadge, { children: /*#__PURE__*/_jsx(StyledCircle, { backgroundColor: backgroundColorAvatarBadge, children: /*#__PURE__*/_jsx(StyledIcon, { color: iconColorAvatarBadge, name: iconName, size: 16 }) }) }), iconColor && !iconName && iconColor !== 'black' && /*#__PURE__*/_jsx(StyledAvatarBadge, { children: /*#__PURE__*/_jsx(StyledCircle, { backgroundColor: "white", children: /*#__PURE__*/_jsx(ColorBadge, { backgroundColor: iconColorAvatarBadge }) }) })] })); }); Avatar.defaultProps = { color: 'automatic', size: 'md', name: '', id: '', src: null, iconName: null, iconColor: 'black', shape: 'circle', altImg: '' }; export default Avatar;