Skip to content

Commit 8c9240b

Browse files
vogtnnmetulev
authored andcommitted
Person-card fixed touch in person component + login refactor (window event) (#228)
* [mgt-person] adding touch event to window to help with hiding person-card * [mgt-login] moving click handle to connected and disconnected callback methods * [mgt-person] refactor touch and click events * [mgt-person] re-adding logic for showpersoncard onclick * [person] separating openPersonCard click to not be attached to window unfactor :( * [mgt-person] fix for rapid open close of person card in touch events * making private variable instead of property, touch disables handleMouseEnter timeouts * removed touch handling and only depend on click event for everything * Clarified window click handler names
1 parent 1b5f703 commit 8c9240b

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

src/components/mgt-login/mgt-login.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ export class MgtLogin extends MgtBaseComponent {
6262
super();
6363
Providers.onProviderUpdated(() => this.loadState());
6464
this.loadState();
65+
this.handleWindowClick = this.handleWindowClick.bind(this);
66+
}
67+
68+
/**
69+
* Invoked each time the custom element is appended into a document-connected element
70+
*
71+
* @memberof MgtLogin
72+
*/
73+
public connectedCallback() {
74+
super.connectedCallback();
75+
window.addEventListener('click', this.handleWindowClick);
76+
}
77+
78+
/**
79+
* Invoked each time the custom element is disconnected from the document's DOM
80+
*
81+
* @memberof MgtLogin
82+
*/
83+
public disconnectedCallback() {
84+
window.removeEventListener('click', this.handleWindowClick);
85+
super.disconnectedCallback();
6586
}
6687

6788
/**
@@ -159,26 +180,6 @@ export class MgtLogin extends MgtBaseComponent {
159180
}
160181
}
161182

162-
/**
163-
* Invoked when the element is first updated. Implement to perform one time
164-
* work on the element after update.
165-
*
166-
* Setting properties inside this method will trigger the element to update
167-
* again after this update cycle completes.
168-
*
169-
* * @param _changedProperties Map of changed properties with old values
170-
*/
171-
protected firstUpdated() {
172-
window.addEventListener('click', (event: MouseEvent) => {
173-
// get popup bounds
174-
const popup = this.renderRoot.querySelector('.popup');
175-
if (popup) {
176-
this._popupRect = popup.getBoundingClientRect();
177-
this._showMenu = false;
178-
}
179-
});
180-
}
181-
182183
/**
183184
* Invoked on each update to perform rendering tasks. This method must return
184185
* a lit-html TemplateResult. Setting properties inside this method will *not*
@@ -197,6 +198,19 @@ export class MgtLogin extends MgtBaseComponent {
197198
`;
198199
}
199200

201+
private handleWindowClick(e: MouseEvent) {
202+
if (e.target === this) {
203+
return;
204+
}
205+
206+
// get popup bounds
207+
const popup = this.renderRoot.querySelector('.popup');
208+
if (popup) {
209+
this._popupRect = popup.getBoundingClientRect();
210+
this._showMenu = false;
211+
}
212+
}
213+
200214
private renderLogIn() {
201215
return html`
202216
<i class="login-icon ms-Icon ms-Icon--Contact"></i>
@@ -270,7 +284,6 @@ export class MgtLogin extends MgtBaseComponent {
270284
}
271285

272286
private onClick(event: MouseEvent) {
273-
event.stopPropagation();
274287
if (this.userDetails) {
275288
// get login button bounds
276289
const loginButton = this.renderRoot.querySelector('.login-button');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $email-font-size: var(--email-font-size, #{$ms-font-size-s});
2020
$email-color: var(--email-color, #{$ms-color-neutralPrimary});
2121

2222
:host {
23-
display: inherit;
23+
display: inline-block;
2424
}
2525

2626
:host .root {

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
99
import { customElement, html, property, PropertyValues, TemplateResult } from 'lit-element';
1010
import { classMap } from 'lit-html/directives/class-map';
11-
import { styleMap } from 'lit-html/directives/style-map';
1211
import { Providers } from '../../Providers';
1312
import { ProviderState } from '../../providers/IProvider';
1413
import '../../styles/fabric-icon-font';
@@ -124,6 +123,11 @@ export class MgtPerson extends MgtTemplatedComponent {
124123
private _mouseLeaveTimeout;
125124
private _mouseEnterTimeout;
126125

126+
constructor() {
127+
super();
128+
this.handleWindowClick = this.handleWindowClick.bind(this);
129+
}
130+
127131
/**
128132
* Synchronizes property values when attributes change.
129133
*
@@ -156,6 +160,26 @@ export class MgtPerson extends MgtTemplatedComponent {
156160
this.loadData();
157161
}
158162

163+
/**
164+
* Invoked each time the custom element is appended into a document-connected element
165+
*
166+
* @memberof MgtPerson
167+
*/
168+
public connectedCallback() {
169+
super.connectedCallback();
170+
window.addEventListener('click', this.handleWindowClick);
171+
}
172+
173+
/**
174+
* Invoked each time the custom element is disconnected from the document's DOM
175+
*
176+
* @memberof MgtPerson
177+
*/
178+
public disconnectedCallback() {
179+
window.removeEventListener('click', this.handleWindowClick);
180+
super.disconnectedCallback();
181+
}
182+
159183
/**
160184
* Invoked on each update to perform rendering tasks. This method must return
161185
* a lit-html TemplateResult. Setting properties inside this method will *not*
@@ -174,9 +198,9 @@ export class MgtPerson extends MgtTemplatedComponent {
174198
return html`
175199
<div
176200
class="root"
201+
@click=${this.handleMouseClick}
177202
@mouseenter=${this.handleMouseEnter}
178203
@mouseleave=${this.handleMouseLeave}
179-
@click=${this.handleMouseClick}
180204
>
181205
${this.renderFlyout(person)}
182206
</div>
@@ -203,6 +227,12 @@ export class MgtPerson extends MgtTemplatedComponent {
203227
}
204228
}
205229

230+
private handleWindowClick(e: MouseEvent) {
231+
if (this.isPersonCardVisible && e.target !== this) {
232+
this.hidePersonCard();
233+
}
234+
}
235+
206236
private async loadData() {
207237
const provider = Providers.globalProvider;
208238

@@ -288,11 +318,9 @@ export class MgtPerson extends MgtTemplatedComponent {
288318
this.requestUpdate();
289319
}
290320

291-
private handleMouseClick() {
292-
if (this.personCardInteraction === PersonCardInteraction.click && !this.isPersonCardVisible) {
321+
private handleMouseClick(e: MouseEvent) {
322+
if (this.personCardInteraction !== PersonCardInteraction.none && !this.isPersonCardVisible) {
293323
this.showPersonCard();
294-
} else {
295-
this.hidePersonCard();
296324
}
297325
}
298326

0 commit comments

Comments
 (0)