Use Office UI Fabric React components in your SharePoint client-side web part

This article describes how to build a simple web part that uses the DocumentCard component of 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 by using the Office Design Language.

The following image shows a DocumentCard component created with Office UI Fabric React.

Image of a DocumentCard Fabric component in a SharePoint workbench

You can also follow these steps by watching this video on the SharePoint PnP YouTube Channel:



## Create a new web part project
  1. Create a new project directory in your favorite location:
md documentcardexample-webpart
  1. Go to the project directory:
cd documentcardexample-webpart
  1. Make sure you have the latest version of @microsoft/generator-sharepoint installed and create a new web part by running the Yeoman SharePoint generator:
yo @microsoft/sharepoint
  1. When prompted:
  • Accept the default documentcardexample-webpart as your solution name, and select Enter.
  • Select SharePoint Online only (latest), and select Enter.
  • Select Use the current folder for where to place the files.
  • Select N to require the extension to be installed on each site explicitly when it's being used.
  • Select N on the question if solution contains unique permissions.
  • Select WebPart as the client-side component type to be created.
  1. The next set of prompts ask for specific information about your web part:
  • Use DocumentCardExample for your web part name, and select Enter.
  • Accept the default DocumentCardExample description, and select Enter.
  • Select React as the framework, and select Enter.

At this point, Yeoman installs the required dependencies and scaffolds the solution files. This might take a few minutes. Yeoman scaffolds the project to include your DocumentCardExample web part as well.

  1. Next, enter the following to open the web part project in Visual Studio Code:
code .

You now have a web part project with the React framework.

  1. Open package.json from the root folder

You will need to add a reference to the office-ui-fabric-react package. In the dependencies section, add the following line:

"office-ui-fabric-react": "5.132.0",

run npm install restart VS code.

  1. Open DocumentCardExampleWebPart.ts from the src\webparts\documentCardExample folder.

As you can see, the render method creates a react element and renders it in the web part DOM.

  public render(): void {
    const element: React.ReactElement<IDocumentCardExampleProps > = React.createElement(
      DocumentCardExample,
      {
        description: this.properties.description
      }
    );
  1. Open DocumentCardExample.tsx from the src\webparts\documentCardExample\components folder.

This is the main react component that Yeoman added to your project that renders in the web part DOM.

export default class DocumentCardExample extends React.Component<IDocumentCardExampleProps, {}> {
  public render(): React.ReactElement<IDocumentCardExampleProps> {
    return (
      <div className={ styles.documentCardExample }>
        <div className={ styles.container }>
          <div className={ styles.row }>
            <div className={ styles.column }>
              <span className={ styles.title }>Welcome to SharePoint!</span>
              <p className={ styles.subTitle }>Customize SharePoint experiences using web parts.</p>
              <p className={ styles.description }>{escape(this.props.description)}</p>
              <a href="https://aka.ms/spfx" className={ styles.button }>
                <span className={ styles.label }>Learn more</span>
              </a>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

Add an Office UI Fabric component

The new modern experiences in SharePoint use Office UI Fabric and Office UI Fabric React as the default front-end framework for building the new experiences. As a result, SharePoint Framework ships with a default version of Office UI Fabric and Fabric React that matches the version available in SharePoint. This ensures that the web part you are building uses the right version of the Fabric styles and components when deployed to SharePoint.

Because 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.

Note

With the current release of the SharePoint Framework, we recommend that you use the Office UI Fabric and Fabric React that ships with the generator. We do not recommend that you update the Office UI Fabric and Fabric React packages independently because that might conflict with the already available version in SharePoint, and as a result, your web part may fail to function as expected.

To add an Office UI Fabric component

  1. Open DocumentCardExample.tsx from the src\webparts\documentCardExample\components folder.

  2. Add the following import statement to the top of the file to import Fabric React components that we want to use.

import {
  DocumentCard,
  DocumentCardPreview,
  DocumentCardTitle,
  DocumentCardActivity,
  IDocumentCardPreviewProps
} from 'office-ui-fabric-react/lib/DocumentCard';
  1. Delete the current render method, and add the following updated render method:
  public render(): JSX.Element {
    const previewProps: IDocumentCardPreviewProps = {
      previewImages: [
        {
          previewImageSrc: String(require('./document-preview.png')),
          iconSrc: String(require('./icon-ppt.png')),
          width: 318,
          height: 196,
          accentColor: '#ce4b1f'
        }
      ],
    };

    return (
      <DocumentCard onClickHref='http://bing.com'>
        <DocumentCardPreview { ...previewProps } />
        <DocumentCardTitle title='Revenue stream proposal fiscal year 2016 version02.pptx' />
        <DocumentCardActivity
          activity='Created Feb 23, 2016'
          people={
            [
              { name: 'Kat Larrson', profileImageSrc: String(require('./avatar-kat.png')) }
            ]
          }
        />
      </DocumentCard>
    );
  }
  1. Save the file.

In this code, the DocumentCard component includes some extra sections:

  • DocumentCardPreview
  • DocumentCardTitle
  • DocumentCardActivity

The previewProps property includes some properties of the DocumentCardPreview.

  1. Notice the use of the relative path with a require statement to load images. Currently, you need to perform a small configuration in the gulpfile.js to enable these images to get processed properly by webpack.

  2. Open gulpfile.js from the root folder.

  3. Add the following code just above the build.initialize(gulp); code line.

build.configureWebpack.mergeConfig({  
    additionalConfiguration: (generatedConfiguration) => {
        if (build.getConfig().production) {
            var basePath = build.writeManifests.taskConfig.cdnBasePath;
            if (!basePath.endsWith('/')) {
                basePath += '/';
            }
            generatedConfiguration.output.publicPath = basePath;
        }
        else {
            generatedConfiguration.output.publicPath = "/dist/";
        }
        return generatedConfiguration;
    }
});
  1. Save the file.

Your full gulpfile.js file should look as follows.

'use strict';

const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
build.addSuppression(`Warning - [sass] The local CSS class 'ms-Grid' is not camelCase and will not be type-safe.`);

build.configureWebpack.mergeConfig({
  additionalConfiguration: (generatedConfiguration) => {
      if (build.getConfig().production) {
          var basePath = build.writeManifests.taskConfig.cdnBasePath;
          if (!basePath.endsWith('/')) {
              basePath += '/';
          }
          generatedConfiguration.output.publicPath = basePath;
      }
      else {
          generatedConfiguration.output.publicPath = "/dist/";
      }
      return generatedConfiguration;
  }
});

build.initialize(gulp);

Copy the image assets

Copy the following images to your src\webparts\documentCardExample\components folder:

Preview the web part in Workbench

  1. In the console, enter the following to preview your web part in Workbench:
gulp serve
  1. In the toolbox, select your DocumentCardExample web part to add:

Image of a DocumentCard Fabric component in a SharePoint workbench

Note

If you find an issue in the documentation or in the SharePoint Framework, report that to SharePoint engineering by using the issue list at the sp-dev-docs repository. Thanks for your input in advance.

See also