Skip to content

Commit a341f17

Browse files
nmetulevshweaver-MSFTvogtn
authored
Added template support to Login Component (#416)
* Added button type property * added templates to mgt-login * cleanup * cleaned up story and fixed datacontext bug Co-authored-by: Shane Weaver <[email protected]> Co-authored-by: Nicolas Vogt <[email protected]>
1 parent dff3961 commit a341f17

File tree

2 files changed

+111
-29
lines changed

2 files changed

+111
-29
lines changed

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

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import { classMap } from 'lit-html/directives/class-map';
1010
import { IDynamicPerson } from '../../graph/types';
1111
import { Providers } from '../../Providers';
1212
import { ProviderState } from '../../providers/IProvider';
13-
import '../../styles/fabric-icon-font';
14-
import { MgtBaseComponent } from '../baseComponent';
15-
import '../mgt-person/mgt-person';
1613
import { MgtFlyout } from '../sub-components/mgt-flyout/mgt-flyout';
14+
import { MgtTemplatedComponent } from '../templatedComponent';
1715
import { styles } from './mgt-login-css';
1816

17+
import '../../styles/fabric-icon-font';
18+
import '../mgt-person/mgt-person';
19+
1920
/**
2021
* Web component button and flyout control to facilitate Microsoft identity platform authentication
2122
*
@@ -29,6 +30,11 @@ import { styles } from './mgt-login-css';
2930
* @fires logoutInitiated - Fired when logout is initiated by the user
3031
* @fires logoutCompleted - Fired when logout completed
3132
*
33+
* @template signed-in-button-content (dataContext: {personDetails, personImage})
34+
* @template signed-out-button-content (dataContext: null)
35+
* @template flyout-commands (dataContext: {handleSignOut})
36+
* @template flyout-person-details (dataContext: {personDetails, personImage})
37+
*
3238
* @cssprop --font-size - {Length} Login font size
3339
* @cssprop --font-weight - {Length} Login font weight
3440
* @cssprop --height - {String} Login height percentage
@@ -41,7 +47,7 @@ import { styles } from './mgt-login-css';
4147
* @cssprop --popup-command-font-size - {Length} Popup command font size
4248
*/
4349
@customElement('mgt-login')
44-
export class MgtLogin extends MgtBaseComponent {
50+
export class MgtLogin extends MgtTemplatedComponent {
4551
/**
4652
* Array of styles to apply to the element. The styles should be defined
4753
* using the `css` tag function.
@@ -228,33 +234,66 @@ export class MgtLogin extends MgtBaseComponent {
228234
if (!this.userDetails) {
229235
return;
230236
}
231-
const avatarSize = 'large';
232-
233237
return html`
234238
<div class="popup">
235239
<div class="popup-content">
236240
<div>
237-
<mgt-person
238-
.personDetails=${this.userDetails}
239-
.personImage=${this._image}
240-
.avatarSize=${avatarSize}
241-
show-name
242-
show-email
243-
/>
241+
${this.renderFlyoutPersonDetails(this.userDetails, this._image)}
244242
</div>
245243
<div class="popup-commands">
246-
<ul>
247-
<li>
248-
<button class="popup-command" @click=${this.logout} aria-label="Sign Out">
249-
Sign Out
250-
</button>
251-
</li>
252-
</ul>
244+
${this.renderFlyoutCommands()}
253245
</div>
254246
</div>
255247
</div>
256248
`;
257249
}
250+
251+
/**
252+
* Render the flyout person details.
253+
*
254+
* @protected
255+
* @returns
256+
* @memberof MgtLogin
257+
*/
258+
protected renderFlyoutPersonDetails(personDetails: IDynamicPerson, personImage: string) {
259+
const template = this.renderTemplate('flyout-person-details', { personDetails, personImage });
260+
return (
261+
template ||
262+
html`
263+
<mgt-person
264+
.personDetails=${personDetails}
265+
.personImage=${personImage}
266+
.avatarSize="large"
267+
.showName=${true}
268+
.showEmail=${true}
269+
/>
270+
`
271+
);
272+
}
273+
274+
/**
275+
* Render the flyout commands.
276+
*
277+
* @protected
278+
* @returns
279+
* @memberof MgtLogin
280+
*/
281+
protected renderFlyoutCommands() {
282+
const template = this.renderTemplate('flyout-commands', { handleSignOut: () => this.logout() });
283+
return (
284+
template ||
285+
html`
286+
<ul>
287+
<li>
288+
<button class="popup-command" @click=${this.logout} aria-label="Sign Out">
289+
Sign Out
290+
</button>
291+
</li>
292+
</ul>
293+
`
294+
);
295+
}
296+
258297
/**
259298
* Render the button content.
260299
*
@@ -264,23 +303,52 @@ export class MgtLogin extends MgtBaseComponent {
264303
*/
265304
protected renderButtonContent() {
266305
if (this.userDetails) {
267-
const avatarSize = 'small';
268-
return html`
306+
return this.renderSignedInButtonContent(this.userDetails, this._image);
307+
} else {
308+
return this.renderSignedOutButtonContent();
309+
}
310+
}
311+
312+
/**
313+
* Render the button content when the user is signed in.
314+
*
315+
* @protected
316+
* @returns
317+
* @memberof MgtLogin
318+
*/
319+
protected renderSignedInButtonContent(personDetails: IDynamicPerson, personImage: string) {
320+
const template = this.renderTemplate('signed-in-button-content', { personDetails, personImage });
321+
return (
322+
template ||
323+
html`
269324
<mgt-person
270325
.personDetails=${this.userDetails}
271326
.personImage=${this._image}
272-
.avatarSize=${avatarSize}
273-
show-name
327+
.avatarSize="small"
328+
.showName=${true}
274329
/>
275-
`;
276-
} else {
277-
return html`
330+
`
331+
);
332+
}
333+
334+
/**
335+
* Render the button content when the user is not signed in.
336+
*
337+
* @protected
338+
* @returns
339+
* @memberof MgtLogin
340+
*/
341+
protected renderSignedOutButtonContent() {
342+
const template = this.renderTemplate('signed-out-button-content', null);
343+
return (
344+
template ||
345+
html`
278346
<i class="login-icon ms-Icon ms-Icon--Contact"></i>
279347
<span aria-label="Sign In">
280348
Sign In
281349
</span>
282-
`;
283-
}
350+
`
351+
);
284352
}
285353

286354
/**

stories/components/login.stories.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ export default {
2828
export const Login = () => html`
2929
<mgt-login></mgt-login>
3030
`;
31+
32+
export const Templates = () => html`
33+
<mgt-login>
34+
<template data-type="signed-in-button-content">
35+
{{personDetails.givenName}}
36+
</template>
37+
<template data-type="flyout-commands">
38+
<div>
39+
<button data-props="{@click: handleSignOut}">Sign Out</button>
40+
<button>Go to my profile</button>
41+
</div>
42+
</template>
43+
</mgt-login>
44+
`;

0 commit comments

Comments
 (0)