Skip to content

Commit f262549

Browse files
committed
Updated Office UI Fabric tutorial with latest guidance
1 parent 4d44fc2 commit f262549

File tree

1 file changed

+80
-43
lines changed

1 file changed

+80
-43
lines changed

docs/spfx/web-parts/get-started/use-fabric-react-components.md

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Use Office UI Fabric React components in your SharePoint client-side web part
22

3-
#### **Important:** You must upgrade existing projects to use @microsoft/sp-build-web@1.0.1 or later to use Office UI Fabric React. See instructions at the end of this article.
3+
> **Important:** You must upgrade existing projects to use @microsoft/sp-build-web@1.0.1 or later to use Office UI Fabric React. Easiest way to achieve this is to ensure that you have latest package by running `npm install -g @microsoft/generator-sharepoint` in your console before completing this tutorial.
44
55
This article describes how to build a simple web part that uses the DocumentCard component of [Office UI Fabric React](https://github.com/OfficeDev/office-ui-fabric-react). Office UI Fabric React is the front-end framework for building experiences for Office and Office 365. Fabric React includes a robust collection of responsive, mobile-first components that make it easy for you to create web experiences using the Office Design Language.
66

@@ -40,10 +40,10 @@ yo @microsoft/sharepoint
4040
When prompted:
4141

4242
* Accept the default **documentcardexample-webpart** as your solution name and choose **Enter**.
43-
* Select **Use the current folder** as the ___location for the files.
44-
* Select **React** as the framework and choose **Enter**.
43+
* Select **WebPart** as the type of the client-side component and choose **Enter**
4544
* Use **DocumentCardExample** for your web part name and choose **Enter**.
4645
* Accept the default **DocumentCardExample description** and choose **Enter**.
46+
* Select **React** as the framework and choose **Enter**.
4747

4848
At this point, Yeoman will install the required dependencies and scaffold the solution files. This might take a few minutes. Yeoman will scaffold the project to include your DocumentCardExample web part as well.
4949

@@ -102,7 +102,7 @@ The *new modern experiences* in SharePoint use Office UI Fabric and Office UI Fa
102102
103103
Since we chose React as our framework when creating the solution, the generator installed the right version of Office UI Fabric React as well. You can directly import the Fabric components in your react components without any additional work.
104104
105-
>**Note:** With the initial release of the SharePoint Framework, it is recommended to use the Office UI Fabric and Fabric React that ships with the generator. It is not recommended to update the Office UI Fabric and Fabric React packages independently as it might conflict with the already available version in SharePoint and as a result your web part may fail to function as expected. There will be updates on this area after GA.
105+
>**Note:** With the initial release of the SharePoint Framework, it is recommended to use the Office UI Fabric and Fabric React that ships with the generator. It is not recommended to update the Office UI Fabric and Fabric React packages independently as it might conflict with the already available version in SharePoint and as a result your web part may fail to function as expected.
106106
107107
Open **DocumentCardExample.tsx** from the **src\webparts\documentCardExample\components** folder.
108108
@@ -121,34 +121,34 @@ import {
121121
Delete the current `render` method and add the following updated `render` method:
122122
123123
```ts
124-
public render(): JSX.Element {
125-
const previewProps: IDocumentCardPreviewProps = {
126-
previewImages: [
127-
{
128-
previewImageSrc: String(require('document-preview.png')),
129-
iconSrc: String(require('icon-ppt.png')),
130-
width: 318,
131-
height: 196,
132-
accentColor: '#ce4b1f'
133-
}
134-
],
135-
};
136-
137-
return (
138-
<DocumentCard onClickHref='http://bing.com'>
139-
<DocumentCardPreview { ...previewProps } />
140-
<DocumentCardTitle title='Revenue stream proposal fiscal year 2016 version02.pptx' />
141-
<DocumentCardActivity
142-
activity='Created Feb 23, 2016'
143-
people={
144-
[
145-
{ name: 'Kat Larrson', profileImageSrc: String(require('avatar-kat.png')) }
146-
]
147-
}
148-
/>
149-
</DocumentCard>
150-
);
151-
}
124+
public render(): JSX.Element {
125+
const previewProps: IDocumentCardPreviewProps = {
126+
previewImages: [
127+
{
128+
previewImageSrc: String(require('./document-preview.png')),
129+
iconSrc: String(require('./icon-ppt.png')),
130+
width: 318,
131+
height: 196,
132+
accentColor: '#ce4b1f'
133+
}
134+
],
135+
};
136+
137+
return (
138+
<DocumentCard onClickHref='http://bing.com'>
139+
<DocumentCardPreview { ...previewProps } />
140+
<DocumentCardTitle title='Revenue stream proposal fiscal year 2016 version02.pptx' />
141+
<DocumentCardActivity
142+
activity='Created Feb 23, 2016'
143+
people={
144+
[
145+
{ name: 'Kat Larrson', profileImageSrc: String(require('./avatar-kat.png')) }
146+
]
147+
}
148+
/>
149+
</DocumentCard>
150+
);
151+
}
152152
```
153153
Save the file.
154154

@@ -160,21 +160,63 @@ In this code, the DocumentCard component includes some extra sections:
160160

161161
The `previewProps` property includes some properties of the DocumentCardPreview.
162162

163-
Notice the use of relative path with a `require` statement to load images. Currently, you need to use the webpack public path plugin and input the file's relative path from your source file or folder to the `lib` folder. This should be the same as your current working source ___location.
163+
Notice the use of relative path with a `require` statement to load images. Currently, you need to perform small configuration in the gulpfile.js to enable these images to get processed properly by webpack.
164164

165-
Open **DocumentCardExampleWebPart.ts** from the **src\webparts\documentCardExample** folder.
165+
Open **gulpfile.js** from the **root** folder.
166166

167-
Add the following code at the top of the file to require the webpack public path plugin.
167+
Add the following code just above the `build.initialize(gulp);` code line.
168168

169-
```ts
170-
require('set-webpack-public-path!');
169+
```js
170+
build.configureWebpack.mergeConfig({
171+
additionalConfiguration: (generatedConfiguration) => {
172+
if (build.getConfig().production) {
173+
var basePath = build.writeManifests.taskConfig.cdnBasePath;
174+
if (!basePath.endsWith('/')) {
175+
basePath += '/';
176+
}
177+
generatedConfiguration.output.publicPath = basePath;
178+
}
179+
else {
180+
generatedConfiguration.output.publicPath = "/dist/";
181+
}
182+
return generatedConfiguration;
183+
}
184+
});
171185
```
172186

173187
Save the file.
174188

189+
Your full **gulpfile.js** file should look as follows.
190+
191+
```js
192+
'use strict';
193+
194+
const gulp = require('gulp');
195+
const build = require('@microsoft/sp-build-web');
196+
197+
build.configureWebpack.mergeConfig({
198+
additionalConfiguration: (generatedConfiguration) => {
199+
if (build.getConfig().production) {
200+
var basePath = build.writeManifests.taskConfig.cdnBasePath;
201+
if (!basePath.endsWith('/')) {
202+
basePath += '/';
203+
}
204+
generatedConfiguration.output.publicPath = basePath;
205+
}
206+
else {
207+
generatedConfiguration.output.publicPath = "/dist/";
208+
}
209+
return generatedConfiguration;
210+
}
211+
});
212+
213+
build.initialize(gulp);
214+
215+
```
216+
175217
### Copy the image assets
176218

177-
Copy the following images to your **src\webparts\documentCardExample** folder:
219+
Copy the following images to your **src\webparts\documentCardExample\components** folder:
178220

179221
* [avatar-kat.png](https://github.com/SharePoint/sp-dev-docs/blob/master/assets/avatar-kat.png)
180222
* [icon-ppt.png](https://github.com/SharePoint/sp-dev-docs/tree/master/assets/icon-ppt.png)
@@ -191,8 +233,3 @@ gulp serve
191233
In the toolbox, select your `DocumentCardExample` web part to add:
192234

193235
![Image of a DocumentCard Fabric component in a SharePoint workbench](../../../../images/fabric-components-doc-card-view-ex.png)
194-
195-
196-
## Updating an existing project
197-
198-
In your project's `package.json`, update the `@microsoft/sp-build-web` dependency to at least version 1.0.1, delete your project's `node_modules` directory, and run `npm install`.

0 commit comments

Comments
 (0)