Skip to content

Commit a643044

Browse files
authored
Merge pull request #200 from microsoftgraph/nmetulev/person-image-initials-fix
Person - Fixed initials size not updating - fixed alignment of initials and image
2 parents 1db1a7e + e310860 commit a643044

File tree

3 files changed

+79
-52
lines changed

3 files changed

+79
-52
lines changed

src/components/mgt-person/mgt-person.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ mgt-person .user-avatar {
8787
height: $avatar-size;
8888
}
8989

90-
&.row-span-3 {
91-
width: $avatar-size;
92-
height: $avatar-size;
90+
img {
91+
height: 100%;
92+
width: 100%;
9393
}
9494
}
9595

src/components/mgt-person/mgt-person.ts

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
9-
import { customElement, html, property } from 'lit-element';
9+
import { customElement, html, property, PropertyValues } from 'lit-element';
1010
import { classMap } from 'lit-html/directives/class-map';
1111
import { Providers } from '../../Providers';
1212
import { ProviderState } from '../../providers/IProvider';
@@ -26,6 +26,14 @@ import { styles } from './mgt-person-css';
2626
*/
2727
@customElement('mgt-person')
2828
export class MgtPerson extends MgtTemplatedComponent {
29+
/**
30+
* Array of styles to apply to the element. The styles should be defined
31+
* user the `css` tag function.
32+
*/
33+
static get styles() {
34+
return styles;
35+
}
36+
2937
/**
3038
* allows developer to define name of person for component
3139
* @type {string}
@@ -132,14 +140,6 @@ export class MgtPerson extends MgtTemplatedComponent {
132140
}
133141
}
134142

135-
/**
136-
* Array of styles to apply to the element. The styles should be defined
137-
* user the `css` tag function.
138-
*/
139-
static get styles() {
140-
return styles;
141-
}
142-
143143
/**
144144
* Invoked when the element is first updated. Implement to perform one time
145145
* work on the element after update.
@@ -181,6 +181,26 @@ export class MgtPerson extends MgtTemplatedComponent {
181181
`;
182182
}
183183

184+
/**
185+
* Invoked whenever the element is updated. Implement to perform
186+
* post-updating tasks via DOM APIs, for example, focusing an element.
187+
*
188+
* Setting properties inside this method will trigger the element to update
189+
* again after this update cycle completes.
190+
*
191+
* * @param changedProperties Map of changed properties with old values
192+
*/
193+
protected updated(changedProps: PropertyValues) {
194+
super.updated(changedProps);
195+
196+
const initials = this.renderRoot.querySelector('.initials-text') as HTMLElement;
197+
if (initials && initials.parentNode && (initials.parentNode as HTMLElement).getBoundingClientRect) {
198+
const parent = initials.parentNode as HTMLElement;
199+
const height = parent.getBoundingClientRect().height;
200+
initials.style.fontSize = `${height * 0.5}px`;
201+
}
202+
}
203+
184204
private async loadData() {
185205
const provider = Providers.globalProvider;
186206

@@ -357,8 +377,15 @@ export class MgtPerson extends MgtTemplatedComponent {
357377

358378
private renderDetails() {
359379
if (this.showEmail || this.showName) {
380+
const isLarge = this.showEmail && this.showName;
381+
382+
const detailsClasses = {
383+
Details: true,
384+
small: !isLarge
385+
};
386+
360387
return html`
361-
<span class="Details ${this.getImageSizeClass()}">
388+
<span class="${classMap(detailsClasses)}">
362389
${this.renderNameAndEmail()}
363390
</span>
364391
`;
@@ -370,40 +397,55 @@ export class MgtPerson extends MgtTemplatedComponent {
370397
private renderImage() {
371398
if (this.personDetails) {
372399
const title = this.personCardInteraction === PersonCardInteraction.none ? this.personDetails.displayName : '';
373-
374400
const image = this.getImage();
401+
const isLarge = this.showEmail && this.showName;
402+
const imageClasses = {
403+
initials: !image,
404+
'row-span-2': isLarge,
405+
small: !isLarge,
406+
'user-avatar': true
407+
};
408+
409+
let imageHtml;
375410

376411
if (image) {
377-
return html`
378-
<img
379-
class="user-avatar ${this.getImageRowSpanClass()} ${this.getImageSizeClass()}"
380-
title=${title}
381-
aria-label=${title}
382-
alt=${title}
383-
src=${image}
384-
/>
412+
imageHtml = html`
413+
<img alt=${title} src=${image} />
385414
`;
386415
} else {
387-
return html`
388-
<div
389-
class="user-avatar initials ${this.getImageRowSpanClass()} ${this.getImageSizeClass()}"
390-
title=${title}
391-
aria-label=${title}
392-
>
393-
<span class="initials-text" aria-label="${this.getInitials()}">
394-
${this.getInitials()}
395-
</span>
396-
</div>
416+
const initials = this.getInitials();
417+
418+
imageHtml = html`
419+
<img />
420+
<span class="initials-text" aria-label="${initials}">
421+
${initials}
422+
</span>
397423
`;
398424
}
425+
426+
return html`
427+
<div class=${classMap(imageClasses)} title=${title} aria-label=${title}>
428+
${imageHtml}
429+
</div>
430+
`;
399431
}
400432

401433
return this.renderEmptyImage();
402434
}
403435

404436
private renderEmptyImage() {
437+
const isLarge = this.showEmail && this.showName;
438+
439+
const imageClasses = {
440+
'avatar-icon': true,
441+
'ms-Icon': true,
442+
'ms-Icon--Contact': true,
443+
'row-span-2': isLarge,
444+
small: !isLarge
445+
};
446+
405447
return html`
406-
<i class="ms-Icon ms-Icon--Contact avatar-icon ${this.getImageRowSpanClass()} ${this.getImageSizeClass()}"></i>
448+
<i class=${classMap(imageClasses)}></i>
407449
`;
408450
}
409451

@@ -456,20 +498,4 @@ export class MgtPerson extends MgtTemplatedComponent {
456498

457499
return initials;
458500
}
459-
460-
private getImageRowSpanClass() {
461-
if (this.showEmail && this.showName) {
462-
return 'row-span-2';
463-
}
464-
465-
return '';
466-
}
467-
468-
private getImageSizeClass() {
469-
if (!this.showEmail || !this.showName) {
470-
return 'small';
471-
}
472-
473-
return '';
474-
}
475501
}

src/components/templatedComponent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { html } from 'lit-element';
8+
import { html, PropertyValues } from 'lit-element';
99
import { equals } from '../utils/Utils';
1010
import { MgtBaseComponent } from './baseComponent';
1111
import { TemplateHelper } from './templateHelper';
@@ -83,9 +83,10 @@ export abstract class MgtTemplatedComponent extends MgtBaseComponent {
8383
* Setting properties inside this method will trigger the element to update
8484
* again after this update cycle completes.
8585
*
86-
* * @param _changedProperties Map of changed properties with old values
86+
* * @param changedProperties Map of changed properties with old values
8787
*/
88-
protected updated() {
88+
protected updated(changedProperties: PropertyValues) {
89+
super.updated(changedProperties);
8990
this.removeUnusedSlottedElements();
9091
}
9192

0 commit comments

Comments
 (0)