Skip to content

Commit e8531bd

Browse files
authored
docs: disambiguation simplification for non-react solutions (#2054)
* docs: disambiguation simplification for non-react solutions remove importMgtComponentLibrary references removed spfx-utils from sp-mgt package.json reworded custom element load behavior
1 parent b2e5d8b commit e8531bd

File tree

7 files changed

+25
-161
lines changed

7 files changed

+25
-161
lines changed

packages/mgt-components/README.md

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,71 +89,42 @@ The earlier example can be updated to use the disambiguation feature as follows:
8989
9090
> Note: the `import` of `mgt-components` must use a dynamic import to ensure that the disambiguation is applied before the components are imported.
9191
92-
To simplify this pattern when developing SharePoint Framework web parts we have provided helper utilities in the [`mgt-spfx-utils`](https://github.com/microsoftgraph/microsoft-graph-toolkit/tree/main/packages/mgt-spfx-utils) package. Example usages are provided below.
92+
When developing SharePoint Framework web parts the pattern for using disambiguation is based on whether or not the MGT React wrapper library is being used. If you are using React then the helper utility in the [`mgt-spfx-utils`](https://github.com/microsoftgraph/microsoft-graph-toolkit/tree/main/packages/mgt-spfx-utils) package should be used. SharePoint Framework web part example usages are provided below.
9393
9494
### Usage in a SharePoint web part with no framework
9595
96-
The `importMgtComponentsLibrary` helper function wraps a dynamic import of the `@microsoft/mgt-components` library.
96+
A dynamic import of the `@microsoft/mgt-components` library is used after configuring the disambiguation.
9797
98-
A more complete example is available in the [No Framework Web Part Sample](https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/main/samples/sp-mgt/src/webparts/helloWorld/HelloWorldWebPart.ts).
98+
This example is sourced from the [No Framework Web Part Sample](https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/main/samples/sp-mgt/src/webparts/helloWorld/HelloWorldWebPart.ts).
9999
100100
```ts
101101
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
102102
import { Providers } from '@microsoft/mgt-element';
103103
import { SharePointProvider } from '@microsoft/mgt-sharepoint-provider';
104104
import { customElementHelper } from '@microsoft/mgt-element/dist/es6/components/customElementHelper';
105-
import { importMgtComponentsLibrary } from '@microsoft/mgt-spfx-utils';
106105
107106
export default class MgtWebPart extends BaseClientSideWebPart<Record<string, unknown>> {
108-
private _hasImportedMgtScripts = false;
109-
private _errorMessage = '';
110107
111108
protected onInit(): Promise<void> {
109+
customElementHelper.withDisambiguation('foo');
112110
if (!Providers.globalProvider) {
113111
Providers.globalProvider = new SharePointProvider(this.context);
114112
}
115-
customElementHelper.withDisambiguation('foo');
116-
return super.onInit();
117-
}
118-
119-
private onScriptsLoadedSuccessfully() {
120-
this.render();
113+
return import('@microsoft/mgt-components').then(() => super.onInit());
121114
}
122115
123116
public render(): void {
124-
importMgtComponentsLibrary(this._hasImportedMgtScripts, this.onScriptsLoadedSuccessfully, this.setErrorMessage);
125-
126117
this.domElement.innerHTML = `
127118
<section class="${styles.helloWorld} ${this.context.sdks.microsoftTeams ? styles.teams : ''}">
128-
${this._renderMgtComponents()}
129-
${this._renderErrorMessage()}
119+
<mgt-foo-login></mgt-foo-login>
130120
</section>`;
131121
}
132-
133-
private _renderMgtComponents(): string {
134-
return this._hasImportedMgtScripts
135-
? '<mgt-foo-login></mgt-foo-login>'
136-
: '';
137-
}
138-
139-
private setErrorMessage(e?: Error): void {
140-
if (e) this.renderError(e);
141-
142-
this._errorMessage = 'An error ocurred loading MGT scripts';
143-
this.render();
144-
}
145-
146-
private _renderErrorMessage(): string {
147-
return this._errorMessage
148-
? `<span>${this._errorMessage}</span>`
149-
: '';
150-
}
151122
}
152123
```
153124
154125
### Usage in a SharePoint web part using React
155126
156-
The `lazyLoadComponent` helper function leverages `React.lazy` and `React.Suspense` to asynchronously load the components which have a direct dependency on `@microsoft/mgt-react` from the top level web part component.
127+
The `lazyLoadComponent` helper function from [`mgt-spfx-utils`](https://github.com/microsoftgraph/microsoft-graph-toolkit/tree/main/packages/mgt-spfx-utils) leverages `React.lazy` and `React.Suspense` to asynchronously load the components which have a direct dependency on `@microsoft/mgt-react` from the top level web part component.
157128
158129
A complete example is available in the [React SharePoint Web Part Sample](https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/main/samples/sp-webpart/src/webparts/mgtDemo/MgtDemoWebPart.ts).
159130

packages/mgt-spfx-utils/README.md

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,7 @@ Helper functions to simplify lazy loading of Microsoft Graph Toolkit components
88

99
## Installation
1010

11-
To lazy load Microsoft Graph Toolkit components from the library, add the `@microsoft/mgt-spfx-utils` package as a dependency to your SharePoint Framework project. If you use React, also add the `@microsoft/mgt-react` package:
12-
13-
```bash
14-
npm install @microsoft/mgt-spfx-utils
15-
```
16-
17-
or
18-
19-
```bash
20-
yarn add @microsoft/mgt-spfx-utils
21-
```
22-
23-
or when using React:
11+
To lazy load Microsoft Graph Toolkit components in a React web part add both the `@microsoft/mgt-spfx-utils` and `@microsoft/mgt-react` packages as dependencies to your SharePoint Framework project:
2412

2513
```bash
2614
npm install @microsoft/mgt-spfx-utils @microsoft/mgt-react
@@ -38,11 +26,11 @@ Disambiguation is intended to provide developers with a mechanism to use a speci
3826

3927
By disambiguating tag names of Microsoft Graph Toolkit components, you can use your own version of MGT rather than using the centrally deployed `@microsoft/mgt-spfx` package. This allows you to avoid colliding with SharePoint Framework components built by other developers. When disambiguating tag names, MGT is included in the generated SPFx bundle, increasing its size. It is strongly recommended that you use a disambiguation value unique to your organization and solution to avoid collisions with other solutions, e.g. `contoso-hr-extensions`.
4028

41-
> **Important:** Since a given web component tag can only be registered once these approaches **must** be used along with the `customElementHelper.withDisambiguation('foo')` approach as this allows developers to create disambiguated tag names.
29+
> **Important:** Since a given web component tag can only be registered once this approach **must** be used along with the `customElementHelper.withDisambiguation('foo')` as this allows developers to create disambiguated tag names.
4230
4331
### When using no framework web parts
4432

45-
When building SharePoint Framework web parts without a JavaScript framework the `@microsoft/mgt-components` library must be asynchronously loaded after configuring the disambiguation setting. The `importMgtComponentsLibrary` helper function wraps this functionality. Once the `@microsoft/mgt-components` library is loaded you can load components directly in your web part.
33+
When building SharePoint Framework web parts without a JavaScript framework the `@microsoft/mgt-components` library must be asynchronously loaded after configuring the disambiguation setting. No helper library is necessary as dynamic imports are sufficient. After the browser has loaded the `@microsoft/mgt-components` library any tags which had already been rendered to the DOM without an existing custom element registration will automatically have the loaded custom behavior attached to them.
4634

4735
Below is a minimal example web part that demonstrates how to use MGT with disambiguation in SharePoint Framework Web parts. A more complete example is available in the [No Framework Web Part Sample](https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/main/samples/sp-mgt/src/webparts/helloWorld/HelloWorldWebPart.ts).
4836

@@ -51,53 +39,24 @@ import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
5139
import { Providers } from '@microsoft/mgt-element';
5240
import { SharePointProvider } from '@microsoft/mgt-sharepoint-provider';
5341
import { customElementHelper } from '@microsoft/mgt-element/dist/es6/components/customElementHelper';
54-
import { importMgtComponentsLibrary } from '@microsoft/mgt-spfx-utils';
5542

5643
export default class MgtWebPart extends BaseClientSideWebPart<Record<string, unknown>> {
57-
private _hasImportedMgtScripts = false;
58-
private _errorMessage = '';
59-
6044
protected onInit(): Promise<void> {
6145
if (!Providers.globalProvider) {
6246
Providers.globalProvider = new SharePointProvider(this.context);
6347
}
6448
// Use the solution name to ensure unique tag names
6549
customElementHelper.withDisambiguation('spfx-solution-name');
66-
return super.onInit();
67-
}
68-
69-
private onScriptsLoadedSuccessfully() {
70-
this.render();
50+
return import('@microsoft/mgt-components').then(() => super.onInit());
7151
}
7252

7353
public render(): void {
74-
importMgtComponentsLibrary(this._hasImportedMgtScripts, this.onScriptsLoadedSuccessfully, this.setErrorMessage);
7554

7655
this.domElement.innerHTML = `
7756
<section class="${styles.helloWorld} ${this.context.sdks.microsoftTeams ? styles.teams : ''}">
78-
${this._renderMgtComponents()}
79-
${this._renderErrorMessage()}
57+
<mgt-spfx-solution-name-login></mgt-spfx-solution-name-login>
8058
</section>`;
8159
}
82-
83-
private _renderMgtComponents(): string {
84-
return this._hasImportedMgtScripts
85-
? '<mgt-foo-login></mgt-foo-login>'
86-
: '';
87-
}
88-
89-
private setErrorMessage(e?: Error): void {
90-
if (e) this.renderError(e);
91-
92-
this._errorMessage = 'An error ocurred loading MGT scripts';
93-
this.render();
94-
}
95-
96-
private _renderErrorMessage(): string {
97-
return this._errorMessage
98-
? `<span>${this._errorMessage}</span>`
99-
: '';
100-
}
10160
}
10261
```
10362

packages/mgt-spfx-utils/src/importMgtComponentsLibrary.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/mgt-spfx-utils/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import './importMgtComponentsLibrary';
98
import './lazyLoadMgtReactComponent';
109

11-
export * from './importMgtComponentsLibrary';
1210
export * from './lazyLoadMgtReactComponent';

samples/sp-mgt/config/package-solution.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"solution": {
44
"name": "sp-mgt-no-framework-client-side-solution",
55
"id": "8606d7ab-8b1e-461f-b611-416054c65000",
6-
"version": "1.0.0.1",
6+
"version": "1.0.0.2",
77
"includeClientSideAssets": true,
88
"skipFeatureDeployment": true,
99
"isDomainIsolated": false,

samples/sp-mgt/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sp-mgt-no-framework",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"private": true,
55
"main": "lib/index.js",
66
"scripts": {
@@ -15,7 +15,6 @@
1515
"@microsoft/mgt-element": "*",
1616
"@microsoft/mgt-components": "*",
1717
"@microsoft/mgt-sharepoint-provider": "*",
18-
"@microsoft/mgt-spfx-utils": "*",
1918
"@microsoft/sp-core-library": "1.16.1",
2019
"@microsoft/sp-property-pane": "1.16.1",
2120
"@microsoft/sp-webpart-base": "1.16.1",

samples/sp-mgt/src/webparts/helloWorld/HelloWorldWebPart.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,38 @@ import styles from './HelloWorldWebPart.module.scss';
55
import { Providers } from '@microsoft/mgt-element';
66
import { SharePointProvider } from '@microsoft/mgt-sharepoint-provider';
77
import { customElementHelper } from '@microsoft/mgt-element/dist/es6/components/customElementHelper';
8-
import { importMgtComponentsLibrary } from '@microsoft/mgt-spfx-utils';
98

109
export default class HelloWorldWebPart extends BaseClientSideWebPart<Record<string, unknown>> {
11-
private _hasImportedMgtScripts = false;
12-
private _errorMessage = '';
13-
1410
protected onInit(): Promise<void> {
11+
customElementHelper.withDisambiguation('sp-mgt-no-framework-client-side-solution');
1512
if (!Providers.globalProvider) {
1613
Providers.globalProvider = new SharePointProvider(this.context);
1714
}
18-
customElementHelper.withDisambiguation('sp-mgt-no-framework-client-side-solution');
19-
return super.onInit();
20-
}
21-
22-
private onScriptsLoadedSuccessfully() {
23-
this.render();
15+
return import('@microsoft/mgt-components').then(() => super.onInit());
2416
}
2517

2618
public render(): void {
27-
importMgtComponentsLibrary(this._hasImportedMgtScripts, this.onScriptsLoadedSuccessfully, this.setErrorMessage);
28-
2919
this.domElement.innerHTML = `
3020
<section class="${styles.helloWorld} ${this.context.sdks.microsoftTeams ? styles.teams : ''}">
3121
${this._renderMgtComponents()}
32-
${this._renderErrorMessage()}
3322
</section>`;
3423
}
3524

3625
private _renderMgtComponents(): string {
37-
return this._hasImportedMgtScripts
38-
? `
26+
return `
3927
<div class="${styles.container}">
40-
<mgt-contoso-person
28+
<mgt-sp-mgt-no-framework-client-side-solution-person
4129
show-presence
4230
person-query="me"
4331
view="twoLines"
4432
person-card="hover"
45-
></mgt-contoso-person>
46-
<mgt-contoso-people></mgt-contoso-people>
47-
<mgt-contoso-agenda></mgt-contoso-agenda>
48-
<mgt-contoso-people-picker></mgt-contoso-people-picker>
49-
<mgt-contoso-teams-channel-picker></mgt-contoso-teams-channel-picker>
50-
<mgt-contoso-tasks></mgt-contoso-tasks>
33+
></mgt-sp-mgt-no-framework-client-side-solution-person>
34+
<mgt-sp-mgt-no-framework-client-side-solution-people></mgt-sp-mgt-no-framework-client-side-solution-people>
35+
<mgt-sp-mgt-no-framework-client-side-solution-agenda></mgt-sp-mgt-no-framework-client-side-solution-agenda>
36+
<mgt-sp-mgt-no-framework-client-side-solution-people-picker></mgt-sp-mgt-no-framework-client-side-solution-people-picker>
37+
<mgt-sp-mgt-no-framework-client-side-solution-teams-channel-picker></mgt-sp-mgt-no-framework-client-side-solution-teams-channel-picker>
38+
<mgt-sp-mgt-no-framework-client-side-solution-tasks></mgt-sp-mgt-no-framework-client-side-solution-tasks>
5139
</div>
52-
`
53-
: '';
54-
}
55-
56-
private setErrorMessage(e?: Error): void {
57-
if (e) this.renderError(e);
58-
59-
this._errorMessage = 'An error ocurred loading MGT scripts';
60-
this.render();
61-
}
62-
63-
private _renderErrorMessage(): string {
64-
return this._errorMessage
65-
? `
66-
<span class="${styles.error}>
67-
${this._errorMessage}
68-
</span>
69-
`
70-
: '';
40+
`;
7141
}
7242
}

0 commit comments

Comments
 (0)