@carbon/ibm-products-web-components
Version:
Carbon for IBM Products Web Components
140 lines (136 loc) • 4.93 kB
JavaScript
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { __decorate } from 'tslib';
import { LitElement, html } from 'lit';
import { property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { prefix, carbonPrefix } from '../../globals/settings.js';
import HostListenerMixin from '@carbon/web-components/es/globals/mixins/host-listener.js';
import { carbonElement } from '@carbon/web-components/es/globals/decorators/carbon-element.js';
import styles from './user-avatar.scss.js';
import '@carbon/web-components/es/components/tooltip/index.js';
import User from '@carbon/web-components/es/icons/user/16';
/**
* @license
*
* Copyright IBM Corp. 2025, 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.
*/
const blockClass = `${prefix}--user-avatar`;
/**
* Useravatar.
*
* @element c4p-user-avatar
* */
let CDSUseravatar = class CDSUseravatar extends HostListenerMixin(LitElement) {
constructor() {
super(...arguments);
/**
* Specify the alignment of the tooltip.
*/
this.tooltipAlignment = 'bottom';
/**
* Provide the background color need to be set for UserAvatar.
*/
this.backgroundColor = 'order-1-cyan';
}
render() {
const getItem = () => {
var _a;
if (this.image) {
const imageClasses = classMap({
[`${blockClass}__photo`]: true,
[`${blockClass}__photo--${this.size}`]: this.size,
});
return html ` <img
alt="${this.imageDescription}"
src="${this.image}"
class="${imageClasses}"
/>`;
}
if (this.querySelector('[slot="rendericon"]')) {
return html `<slot name="rendericon"></slot>`;
}
if (this.name) {
const parts = ((_a = this.name) === null || _a === void 0 ? void 0 : _a.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);
}
return html `${User({ slot: 'rendericon' })} `;
};
const { tooltipText, tooltipAlignment, size, backgroundColor, theme } = this;
const classes = classMap({
[`${blockClass}`]: true,
[`${blockClass}--${size}`]: size,
[`${blockClass}--${backgroundColor}`]: backgroundColor,
[`${blockClass}--${theme}`]: theme,
});
const Avatar = () => html `<div class="${classes}">${getItem()}</div>`;
if (tooltipText) {
return html `<cds-tooltip
align=${tooltipAlignment}
aria-label=${tooltipText}
class=${`${blockClass}__tooltip ${carbonPrefix}--icon-tooltip`}
>
<button class=${`${blockClass}__tooltip-trigger`} role="button">
${Avatar()}
</button>
<cds-tooltip-content
class=${`${blockClass}__tooltip-content`}
id="content"
>
${tooltipText}
</cds-tooltip-content>
</cds-tooltip>`;
}
else {
return html ` ${Avatar()}`;
}
}
};
CDSUseravatar.styles = styles;
__decorate([
property({ reflect: true, attribute: 'tooltip-text' })
], CDSUseravatar.prototype, "tooltipText", void 0);
__decorate([
property({ reflect: true, attribute: 'tooltip-alignment' })
], CDSUseravatar.prototype, "tooltipAlignment", void 0);
__decorate([
property({ reflect: true })
], CDSUseravatar.prototype, "name", void 0);
__decorate([
property({ reflect: true })
], CDSUseravatar.prototype, "size", void 0);
__decorate([
property({ reflect: true })
], CDSUseravatar.prototype, "image", void 0);
__decorate([
property({ reflect: true, attribute: 'image-description' })
], CDSUseravatar.prototype, "imageDescription", void 0);
__decorate([
property({ reflect: true, attribute: 'background-color' })
], CDSUseravatar.prototype, "backgroundColor", void 0);
__decorate([
property({ reflect: true })
], CDSUseravatar.prototype, "theme", void 0);
CDSUseravatar = __decorate([
carbonElement(`${prefix}-user-avatar`)
], CDSUseravatar);
var CDSUseravatar$1 = CDSUseravatar;
export { CDSUseravatar$1 as default };
//# sourceMappingURL=user-avatar.js.map