Skip to content

Commit

Permalink
feat(core/avatar): add avatar component (#598)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Leroux <daniel.leroux@siemens.com>
  • Loading branch information
goncalosard and danielleroux authored Jun 28, 2023
1 parent 4c385e5 commit 6bcda4f
Show file tree
Hide file tree
Showing 33 changed files with 636 additions and 38 deletions.
18 changes: 18 additions & 0 deletions packages/angular-test-app/src/preview-examples/avatar-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Component } from '@angular/core';

@Component({
selector: 'app-example',
template: `
<ix-avatar image="https://ui-avatars.com/api/?name=John+Doe"></ix-avatar>
`,
})
export default class AvatarImage {}
16 changes: 16 additions & 0 deletions packages/angular-test-app/src/preview-examples/avatar-initials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Component } from '@angular/core';

@Component({
selector: 'app-example',
template: ` <ix-avatar initials="JD"></ix-avatar> `,
})
export default class AvatarInitials {}
16 changes: 16 additions & 0 deletions packages/angular-test-app/src/preview-examples/avatar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Component } from '@angular/core';

@Component({
selector: 'app-example',
template: ` <ix-avatar></ix-avatar> `,
})
export default class Avatar {}
22 changes: 22 additions & 0 deletions packages/angular/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ export class IxApplicationHeader {
export declare interface IxApplicationHeader extends Components.IxApplicationHeader {}


@ProxyCmp({
inputs: ['image', 'initials']
})
@Component({
selector: 'ix-avatar',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['image', 'initials'],
})
export class IxAvatar {
protected el: HTMLElement;
constructor(c: ChangeDetectorRef, r: ElementRef, protected z: NgZone) {
c.detach();
this.el = r.nativeElement;
}
}


export declare interface IxAvatar extends Components.IxAvatar {}


@ProxyCmp({
inputs: ['applicationName', 'hideHeader']
})
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/declare-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const DIRECTIVES = [
d.IxAnimatedTab,
d.IxAnimatedTabs,
d.IxApplicationHeader,
d.IxAvatar,
d.IxBasicNavigation,
d.IxBlind,
d.IxBreadcrumb,
Expand Down
69 changes: 69 additions & 0 deletions packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,73 @@
"parts": [],
"listeners": []
},
{
"dirPath": "./src/components/avatar",
"filePath": "./src/components/avatar/avatar.tsx",
"fileName": "avatar.tsx",
"readmePath": "./src/components/avatar/readme.md",
"usagesDir": "./src/components/avatar/usage",
"tag": "ix-avatar",
"overview": "",
"usage": {},
"docs": "",
"docsTags": [
{
"name": "since",
"text": "2.0.0"
}
],
"encapsulation": "shadow",
"dependents": [
"ix-menu-avatar"
],
"dependencies": [],
"dependencyGraph": {
"ix-menu-avatar": [
"ix-avatar"
]
},
"props": [
{
"name": "image",
"type": "string",
"mutable": false,
"attr": "image",
"reflectToAttr": false,
"docs": "Display a avatar image",
"docsTags": [],
"values": [
{
"type": "string"
}
],
"optional": false,
"required": false
},
{
"name": "initials",
"type": "string",
"mutable": false,
"attr": "initials",
"reflectToAttr": false,
"docs": "Display the initials of the user. Will be overwritten by image",
"docsTags": [],
"values": [
{
"type": "string"
}
],
"optional": false,
"required": false
}
],
"methods": [],
"events": [],
"styles": [],
"slots": [],
"parts": [],
"listeners": []
},
{
"dirPath": "./src/components/basic-navigation",
"filePath": "./src/components/basic-navigation/basic-navigation.tsx",
Expand Down Expand Up @@ -6611,11 +6678,13 @@
"encapsulation": "shadow",
"dependents": [],
"dependencies": [
"ix-avatar",
"ix-dropdown",
"ix-menu-avatar-item"
],
"dependencyGraph": {
"ix-menu-avatar": [
"ix-avatar",
"ix-dropdown",
"ix-menu-avatar-item"
],
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ export namespace Components {
*/
"name": string;
}
/**
* @since 2.0.0
*/
interface IxAvatar {
/**
* Display a avatar image
*/
"image": string;
/**
* Display the initials of the user. Will be overwritten by image
*/
"initials": string;
}
interface IxBasicNavigation {
/**
* Application name
Expand Down Expand Up @@ -2154,6 +2167,15 @@ declare global {
prototype: HTMLIxApplicationHeaderElement;
new (): HTMLIxApplicationHeaderElement;
};
/**
* @since 2.0.0
*/
interface HTMLIxAvatarElement extends Components.IxAvatar, HTMLStencilElement {
}
var HTMLIxAvatarElement: {
prototype: HTMLIxAvatarElement;
new (): HTMLIxAvatarElement;
};
interface HTMLIxBasicNavigationElement extends Components.IxBasicNavigation, HTMLStencilElement {
}
var HTMLIxBasicNavigationElement: {
Expand Down Expand Up @@ -2702,6 +2724,7 @@ declare global {
"ix-animated-tab": HTMLIxAnimatedTabElement;
"ix-animated-tabs": HTMLIxAnimatedTabsElement;
"ix-application-header": HTMLIxApplicationHeaderElement;
"ix-avatar": HTMLIxAvatarElement;
"ix-basic-navigation": HTMLIxBasicNavigationElement;
"ix-blind": HTMLIxBlindElement;
"ix-breadcrumb": HTMLIxBreadcrumbElement;
Expand Down Expand Up @@ -2845,6 +2868,19 @@ declare namespace LocalJSX {
*/
"name"?: string;
}
/**
* @since 2.0.0
*/
interface IxAvatar {
/**
* Display a avatar image
*/
"image"?: string;
/**
* Display the initials of the user. Will be overwritten by image
*/
"initials"?: string;
}
interface IxBasicNavigation {
/**
* Application name
Expand Down Expand Up @@ -4875,6 +4911,7 @@ declare namespace LocalJSX {
"ix-animated-tab": IxAnimatedTab;
"ix-animated-tabs": IxAnimatedTabs;
"ix-application-header": IxApplicationHeader;
"ix-avatar": IxAvatar;
"ix-basic-navigation": IxBasicNavigation;
"ix-blind": IxBlind;
"ix-breadcrumb": IxBreadcrumb;
Expand Down Expand Up @@ -4971,6 +5008,10 @@ declare module "@stencil/core" {
"ix-animated-tab": LocalJSX.IxAnimatedTab & JSXBase.HTMLAttributes<HTMLIxAnimatedTabElement>;
"ix-animated-tabs": LocalJSX.IxAnimatedTabs & JSXBase.HTMLAttributes<HTMLIxAnimatedTabsElement>;
"ix-application-header": LocalJSX.IxApplicationHeader & JSXBase.HTMLAttributes<HTMLIxApplicationHeaderElement>;
/**
* @since 2.0.0
*/
"ix-avatar": LocalJSX.IxAvatar & JSXBase.HTMLAttributes<HTMLIxAvatarElement>;
"ix-basic-navigation": LocalJSX.IxBasicNavigation & JSXBase.HTMLAttributes<HTMLIxBasicNavigationElement>;
"ix-blind": LocalJSX.IxBlind & JSXBase.HTMLAttributes<HTMLIxBlindElement>;
"ix-breadcrumb": LocalJSX.IxBreadcrumb & JSXBase.HTMLAttributes<HTMLIxBreadcrumbElement>;
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/components/avatar/avatar.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

@import 'common-variables';
@import 'mixins/text-truncation';
@import 'mixins/hover';

:host {
display: flex;
position: relative;
width: fit-content;

.avatar {
display: flex;
align-items: center;

> .avatar-image {
height: 2rem;
width: 2rem;
min-height: 2rem;
min-width: 2rem;
border-radius: 100px;
}

> .avatar-initials {
display: flex;
align-items: center;
justify-content: center;
height: 2rem;
width: 2rem;
min-height: 2rem;
min-width: 2rem;
border-radius: 100px;
background-color: var(--theme-color-component-3);
}

#avatar-path-background {
fill: var(--theme-avatar--background);
}

#avatar-path-person {
fill: var(--theme-color-4);
}
}
}
81 changes: 81 additions & 0 deletions packages/core/src/components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

function DefaultAvatar(props: { initials?: string }) {
const { initials } = props;

if (initials) {
return <div class={'avatar-initials'}>{initials}</div>;
}

return (
<svg
class={'avatar-image'}
xmlns="http://www.w3.org/2000/svg"
width="32"
height="32"
viewBox="0 0 32 32"
>
<g fill="none" fill-rule="evenodd">
<path
id="avatar-path-background"
d="M16 0C7.163 0 0 7.163 0 16s7.163 16 16 16 16-7.163
16-16c0-4.243-1.686-8.313-4.686-11.314C24.314 1.686 20.244 0 16 0z"
/>
<path
id="avatar-path-person"
d="M17.897 17.91c3.8-.018 7.358 1.875 9.485 5.046-2.417 3.999-6.734 6.434-11.382
6.42-4.648.014-8.965-2.421-11.382-6.42 2.127-3.171 5.685-5.064
9.485-5.045h3.794zM15.821 2.129c3.682 0 6.667 2.984 6.667 6.666 0 3.682-2.985
6.667-6.667 6.667s-6.667-2.985-6.667-6.667 2.985-6.666 6.667-6.666z"
/>
</g>
</svg>
);
}

import { Component, Element, h, Host, Prop } from '@stencil/core';

/**
* @since 2.0.0
*/
@Component({
tag: 'ix-avatar',
styleUrl: 'avatar.scss',
shadow: true,
})
export class Avatar {
@Element() hostElement: HTMLIxAvatarElement;

/**
* Display a avatar image
*
*/
@Prop() image: string;

/**
* Display the initials of the user. Will be overwritten by image
*
*/
@Prop() initials: string;

render() {
return (
<Host>
<li class="avatar">
{this.image ? (
<img src={this.image} class="avatar-image"></img>
) : (
<DefaultAvatar initials={this.initials} />
)}
</li>
</Host>
);
}
}
Loading

0 comments on commit 6bcda4f

Please sign in to comment.