@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
81 lines (80 loc) • 3.87 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["alt", "className", "children", "size", "skeleton", "variant", "src", "imgProps", "hasLabel", "backgroundColor", "color"];
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 React from 'react';
import classnames from 'classnames';
import { createSpacingClasses } from '../space/SpacingHelper';
import { createSkeletonClass } from '../skeleton/SkeletonHelper';
import Img from '../../elements/img/Img';
import Context from '../../shared/Context';
import { validateDOMAttributes, extendPropsWithContext, warn } from '../../shared/component-helper';
import AvatarGroup, { AvatarGroupContext } from './AvatarGroup';
import { getColor } from '../../shared/helpers';
export const defaultProps = {
size: 'medium',
variant: 'primary',
skeleton: false
};
const Avatar = localProps => {
const context = React.useContext(Context);
const avatarGroupContext = React.useContext(AvatarGroupContext);
const allProps = extendPropsWithContext(localProps, defaultProps, context === null || context === void 0 ? void 0 : context.Avatar, {
skeleton: context === null || context === void 0 ? void 0 : context.skeleton
}, avatarGroupContext);
const {
alt,
className,
children: childrenProp,
size,
skeleton,
variant,
src,
imgProps,
hasLabel,
backgroundColor,
color
} = allProps,
props = _objectWithoutProperties(allProps, _excluded);
let children = null;
const skeletonClasses = createSkeletonClass('shape', skeleton, context);
const spacingClasses = createSpacingClasses(props);
const childrenIsString = typeof childrenProp === 'string';
if (src || imgProps) {
const imageProps = _objectSpread({
src,
alt
}, imgProps);
children = React.createElement(Img, imageProps);
} else if (childrenIsString) {
const firstLetterUpperCase = childrenProp.charAt(0).toUpperCase();
children = React.createElement("span", {
"aria-hidden": true
}, firstLetterUpperCase);
} else {
children = childrenProp;
}
if (!avatarGroupContext && !hasLabel) {
warn(`Avatar group required: An Avatar requires an Avatar.Group with label description as a parent component. This is to ensure correct semantic and accessibility.`);
}
validateDOMAttributes(allProps, props);
const style = _objectSpread({
'--background-color': getColor(backgroundColor),
'--color': getColor(color)
}, props === null || props === void 0 ? void 0 : props.style);
return React.createElement("span", _extends({
className: classnames(`dnb-avatar dnb-avatar--${variant || 'primary'} dnb-avatar--size-${size || 'medium'}`, skeletonClasses, spacingClasses, className)
}, props, {
style: style
}), childrenIsString && React.createElement("span", {
className: "dnb-sr-only"
}, childrenProp), children);
};
Avatar.Group = AvatarGroup;
export { AvatarGroup };
Avatar._supportsSpacingProps = true;
export default Avatar;
//# sourceMappingURL=Avatar.js.map