Skip to content

Commit 3e1d3a3

Browse files
Merge pull request SharePoint#6692 from andrewconnell/updateFabricStylesDocs
Migrate CSS change from wiki to docs
2 parents d14cb62 + 9e2d9f8 commit 3e1d3a3

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

docs/spfx/office-ui-fabric-integration.md

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Using Office UI Fabric Core and Fabric React in SharePoint Framework
33
description: Information about the Fabric Core and Fabric React packages, and challenges with using CSS.
4-
ms.date: 05/13/2019
4+
ms.date: 02/09/2021
55
ms.prod: sharepoint
66
localization_priority: Priority
77
---
@@ -14,36 +14,38 @@ Microsoft uses Fabric Core and Fabric React in SharePoint. Microsoft regularly p
1414

1515
The challenge with Global CSS styles is explained in the following presentation within the context of React and JavaScript: [React CSS in JS](https://speakerdeck.com/vjeux/react-css-in-js).
1616

17-
To achieve reliability, one of the main problems we need to solve is that of **Global CSS styles**. This accounts to not using global class names in the HTML markup and instead using Fabric Core mixins and variables in the Sass declaration file. This involves importing the Fabric Core's Sass declarations in your Sass file and then consuming the variables and mixins appropriately.
17+
To achieve reliability, one of the main problems we need to solve is that of **Global CSS styles**. This accounts to not using global class names in the HTML markup and instead using Fabric Core mixins and variables in the Sass declaration file. This involves importing the Fabric Core's Sass declarations in your Sass file and then consuming the variables and mixins appropriately.
1818

1919
## Goals
2020

21-
The goal of the SharePoint Framework is to allow both Microsoft and customers to build rich, beautiful, and consistent user experiences on top of SharePoint.
21+
The goal of the SharePoint Framework is to allow both Microsoft and customers to build rich, beautiful, and consistent user experiences on top of SharePoint.
2222

2323
In accordance with these goals, following are the key design principles:
2424

25-
* Customers should be able to smoothly and reliably consume Fabric Core and Fabric React in their solutions.
26-
* Microsoft will roll out updated experiences that consume updated versions of Fabric Core and Fabric React in SharePoint without conflicting with customer's solutions.
27-
* Customers can customize and override the default styles, designs, and components to tailor the solution's needs.
25+
- Customers should be able to smoothly and reliably consume Fabric Core and Fabric React in their solutions.
26+
- Microsoft will roll out updated experiences that consume updated versions of Fabric Core and Fabric React in SharePoint without conflicting with customer's solutions.
27+
- Customers can customize and override the default styles, designs, and components to tailor the solution's needs.
2828

2929
There are two parts of the Office UI Fabric that are available for use by developers:
3030

31-
* [Office UI Fabric Core](https://developer.microsoft.com/fabric). A set of core styles, typography, a responsive grid, animations, icons, and other fundamental building blocks of the overall design language.
31+
- [Office UI Fabric Core](https://developer.microsoft.com/fabric). A set of core styles, typography, a responsive grid, animations, icons, and other fundamental building blocks of the overall design language.
3232

33-
* [Office UI Fabric React](https://developer.microsoft.com/fabric#/components). A set of React components built on top of the Fabric design language for use in React-based projects.
33+
- [Office UI Fabric React](https://developer.microsoft.com/fabric#/components). A set of React components built on top of the Fabric design language for use in React-based projects.
3434

3535
## Office UI Fabric Core package
3636

37-
The SharePoint Framework Fabric Core npm package **([@microsoft/sp-office-ui-fabric-core](https://www.npmjs.com/package/@microsoft/sp-office-ui-fabric-core))** contains a subset of supported Fabric Core styles that can be safely consumed within a SharePoint Framework component.
37+
The SharePoint Framework Fabric Core npm package **([@microsoft/sp-office-ui-fabric-core](https://www.npmjs.com/package/@microsoft/sp-office-ui-fabric-core))** contains a subset of supported Fabric Core styles that can be safely consumed within a SharePoint Framework component.
3838

3939
The following core styles are supported in the package:
40+
4041
- Typography
4142
- Layouts
4243
- Colors
4344
- Themes
4445
- Localization
4546

4647
The following are not yet supported in the package:
48+
4749
- Animations
4850
- Icons
4951

@@ -53,41 +55,39 @@ Starting with the SharePoint Framework Yeoman generator version 1.3.4, the defau
5355

5456
To use the Fabric Core package in your existing project, install the package as a dependency:
5557

56-
```
58+
```console
5759
npm install @microsoft/sp-office-ui-fabric-core --save
5860
```
5961

60-
After it's installed, you can then import the Fabric Core Sass declarations in your Sass definition file and use the mixins and variables as described in the following section.
62+
After it's installed, you can then import the Fabric Core Sass declarations in your Sass definition file and use the mixins and variables as described in the following section.
6163

6264
### Use Fabric Core styles
6365

6466
To use the Fabric Core styles, you need to import the `SPFabricCore` declarations into your Sass file.
6567

66-
> [!NOTE]
68+
> [!NOTE]
6769
> Make sure you have the **\@microsoft/sp-office-ui-fabric-core** npm package installed.
6870
6971
```css
7072
@import '~@microsoft/sp-office-ui-fabric-core/dist/sass/SPFabricCore.scss';
7173
```
7274

73-
<br/>
74-
7575
Now you can use the core styles as mixins and variables.
7676

7777
```css
7878
.row {
79-
@include ms-Grid-row;
80-
@include ms-fontColor-white;
81-
background-color: $ms-color-themeDark;
82-
padding: 20px;
83-
}
84-
```
79+
@include ms-Grid-row;
80+
@include ms-fontColor-white;
81+
background-color: $ms-color-themeDark;
82+
padding: 20px;
83+
}
84+
```
8585

8686
## Office UI Fabric React
8787

8888
We recommend that you use the versions of the Office UI Fabric React package included in the project in the SharePoint Framework's Yeoman generator. For instance, the SharePoint Framework v1.7.0 release uses Fabric React v5.131.0
8989

90-
> [!NOTE]
90+
> [!NOTE]
9191
> Fabric React versions 2.x or older are not supported in SharePoint Framework.
9292
9393
After the Fabric React package is installed, you can import the required components from the Fabric React bundle.
@@ -110,7 +110,7 @@ The Fabric React package includes the supported Fabric Core styles used in the F
110110

111111
Because the **\@microsoft/sp-office-ui-fabric-core** package is already installed in your solution by the Yeoman generator, we recommend that you uninstall that package if you decide to use Fabric components and reduce your component bundle size.
112112

113-
```
113+
```console
114114
npm uninstall @microsoft/sp-office-ui-fabric-core --save
115115
```
116116

@@ -124,29 +124,29 @@ You can then import the core styles from the Sass declarations available in the
124124

125125
Fabric components in your solution are locked to the specific version of Fabric React that you installed. You need to update the Fabric React package to get any new components if they are available in a newer package version. Because the Fabric components are included in the component bundle, it may increase the size of your component bundle. However, this approach is the only approach that is officially supported when Office UI Fabric React is being used in SharePoint Framework solutions.
126126

127-
128127
## The CSS challenge with Office UI Fabric
129128

130-
The following concepts and references provide insights on the challenge with using Office UI Fabric in the context of client-side web parts.
129+
The following concepts and references provide insights on the challenge with using Office UI Fabric in the context of client-side web parts.
131130

132131
### Global CSS styles
133132

134133
How to avoid Global CSS styles at all costs is a big problem. Today, both Fabric Core and Fabric React have global styles. A lack of any native solutions from the browser to manage the style scoping makes this a very difficult problem.
135-
136-
- [Scope CSS](https://developer.mozilla.org/docs/Web/CSS/:scope) is in early stages of discussion.
137-
- iframes are not a good option to isolate styles.
138-
- [Web Components](https://developer.mozilla.org/docs/Web/Web_Components) is another standard that talks about scoped styles but is still in the discussion stage.
139-
- [The React: CSS in JS](https://speakerdeck.com/vjeux/react-css-in-js) discussion explains the problem well.
140-
134+
135+
- [Scope CSS](https://developer.mozilla.org/docs/Web/CSS/:scope) is in early stages of discussion.
136+
- iframes are not a good option to isolate styles.
137+
- [Web Components](https://developer.mozilla.org/docs/Web/Web_Components) is another standard that talks about scoped styles but is still in the discussion stage.
138+
- [The React: CSS in JS](https://speakerdeck.com/vjeux/react-css-in-js) discussion explains the problem well.
139+
141140
There is plenty of other documentation on the web about the solutions to the global namespace menace.
142141

143142
### CSS specificity
144143

145144
How [CSS specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity) applies to web UI. Higher specificity styles override lower specificity styles, but the key thing to understand is how the specificity rules apply. In general, the precedence order from highest to lowest is as follows:
146-
- The style attribute (for example, `style="background:red;"`)
147-
- ID selectors (`#myDiv { }`)
148-
- Class selectors (`.myCssClass{}`), attribute selectors (`[type="radio"]`), and pseudo-classes (`:hover`)
149-
- Type selectors (`h1`)
145+
146+
- The style attribute (for example, `style="background:red;"`)
147+
- ID selectors (`#myDiv { }`)
148+
- Class selectors (`.myCssClass{}`), attribute selectors (`[type="radio"]`), and pseudo-classes (`:hover`)
149+
- Type selectors (`h1`)
150150

151151
**Loading order**. If two classes are applied on an element and have the same specificity, the class that is loaded later takes precedence. As shown in the following code, the button appears red. If the order of the style tags is changed, the button appears green. This is an important concept, and if not used correctly, the user experience can become load order-dependent (that is, inconsistent).
152152

@@ -176,7 +176,7 @@ The web part div would be assigned this scope, that is `<div data-sp-webpart cla
176176

177177
The rest of the page would contain unscoped Office UI Fabric Core styles. This way, as per CSS specificity rules, the scoped CSS would take precedence within the web part div. The web part and its contents would align to the specific version of the Office UI Fabric Core that the developer had chosen.
178178

179-
*Overriding* Fabric Core styles would not be supported.
179+
*Overriding* Fabric Core styles would not be supported.
180180

181181
```javascript
182182
// Sample of how the scoping would work.
@@ -203,7 +203,7 @@ After analyzing this strategy for a while, it was decided that the descendant se
203203
204204
#### Lack of reliable nesting support
205205
206-
This approach only works if the Microsoft user experience (that is, page and first-party web parts) use an unscoped version of the Office UI Fabric Core such as `ms-Icon--List`, while the third-party web parts only use scoped Office UI Fabric Core CSS as explained earlier.
206+
This approach only works if the Microsoft user experience (that is, page and first-party web parts) use an unscoped version of the Office UI Fabric Core such as `ms-Icon--List`, while the third-party web parts only use scoped Office UI Fabric Core CSS as explained earlier.
207207
208208
The reason being that the specificity of the scoped CSS applied on the web part overrides the unscoped CSS on the page. Keep in mind, as explained earlier, that if CSS specificity of the two classes is the same, their loading order plays a role in how the CSS classes are applied. The class that loads later takes precedence. Hence, the higher specificity of the scoped CSS is important in getting a consistent experience.
209209
@@ -218,8 +218,6 @@ Furthermore, multiple extensions, one contained in the other, cannot use differe
218218
</div>
219219
```
220220
221-
<br/>
222-
223221
Here's a more simplistic sample demonstrating the challenge:
224222
225223
```HTML
@@ -261,7 +259,7 @@ Here's a more simplistic sample demonstrating the challenge:
261259
<br/>
262260
<span>This test depicts that a descendant selector with the same specificity does not allow nesting.
263261
All buttons below do not take the innermost scope (i.e. they should be different colors), but they are all red.
264-
Further, if you change the ordering of the style tags, the colors will change. (i.e. the UI is load order dependant.)</span>
262+
Further, if you change the ordering of the style tags, the colors will change. (i.e. the UI is load order dependant.)</span>
265263
<div class='scope1'>
266264
<button class='myButton'</button>
267265
<div class='scope2'>
@@ -279,17 +277,33 @@ Here's a more simplistic sample demonstrating the challenge:
279277
280278
#### Leakage from unscoped classes
281279
282-
There is another problem with descendant selectors. Note in the previous example that the height and the width styles from the unscoped myButton class are applied to all the buttons. This implies that a change in that style could inadvertently break HTML by using scoped markup.
280+
There is another problem with descendant selectors. Note in the previous example that the height and the width styles from the unscoped myButton class are applied to all the buttons. This implies that a change in that style could inadvertently break HTML by using scoped markup.
283281
284282
Say for example, for some reason at the app level we decide to make height 0 px on the myButton class. That results in all third-party web parts that use the myButton class to have a height of 0 px (that is, a serious regression in the user experience).
285283
284+
## Safely use legacy Office UI Fabric Styles (after SPFx v1.8.2)
285+
286+
> [!IMPORTANT]
287+
> The following guidance applies to SharePoint Framework >= 1.8.2
288+
289+
Ensure that the webpart manifest demands legacy Fabric core styles be loaded onto the page. This is done by specifying `loadLegacyFabricCss: true` in the solution manifest.
290+
291+
Prior to the release of SPFx v1.8.2, a host page bug was present where legacy Fabric core styles were loaded unscoped. Therefore, if another solution demanded the styles, then all other solutions on the page were able to use them. This led to solutions working "by chance" when not rendered in isolation.
292+
293+
To address this bug, the styles were scoped to `.ms-SPLegacyFabricBlock` class and applied to SPFx solutions that demand the Fabric core stylesheet be loaded. To provide a migration path for solutions reliant on the side-effect above, the `.ms-SPLegacyFabricBlock` class is applied to all `<div>`'s exposed to third-party solutions. During that time, modify the solution(s) impacted to declare the dependency on legacy Fabric core styles.
294+
295+
> [!IMPORTANT]
296+
> Eventually the explicit reference to `.ms-SPLegacyFabricBlock` **will be removed** from the DOM for solutions that don't declare their dependency. This change will be broadly communicated via existing channels prior to removing this class.
297+
298+
In the event of a solution that is running in DOM elements that are not provided by the SPFx (*which is generally unsupported*), you'll need to apply the `.ms-SPLegacyFabricBlock` class yourself.
299+
286300
## Usage of the Office UI Fabric icons in SPFx components
287301
288-
There are changes on how to use Office UI Fabric icons in the rendering of the SharePoint Framework solutions starting from the SharePoint Framework version 1.8.2.
302+
There are changes on how to use Office UI Fabric icons in the rendering of the SharePoint Framework solutions starting from the SharePoint Framework v1.8.2.
289303
290304
### Legacy way of using icons (before SPFx 1.8.2)
291305
292-
```HTML
306+
```html
293307
<i className={css('ms-Icon', 'ms-Icon--RecurringEvent')}></i>
294308
```
295309
@@ -301,9 +315,9 @@ Solutions build with *no JavaScript framework* option.
301315
1. Make code changes similar to below code, to get the required icon into your code:
302316
303317
```typescript
304-
import { getIconClassName } from '@uifabric/styling';
318+
import { getIconClassName } from '@uifabric/styling';
305319

306-
return `<i class="${getIconClassName('Mail')}" />`;
320+
return `<i class="${getIconClassName('Mail')}" />`;
307321
```
308322
309323
Solutions build with *React* option or by using *React* in general.
@@ -312,9 +326,9 @@ Solutions build with *React* option or by using *React* in general.
312326
2. Make code changes similar to below code, to get the required icon into your code:
313327
314328
```typescript
315-
import { Icon } from 'office-ui-fabric-react/lib/Icon';
329+
import { Icon } from 'office-ui-fabric-react/lib/Icon';
316330

317-
<Icon iconName='Mail' />
331+
<Icon iconName='Mail' />
318332
```
319333
320334
## See also

docs/spfx/release-1.8.2.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: SharePoint Framework v1.8.2 release notes
33
description: Release notes for the SharePoint Framework v1.8.2 release
4-
ms.date: 07/03/2020
4+
ms.date: 02/09/2021
55
ms.prod: sharepoint
66
localization_priority: Priority
77
---
@@ -42,7 +42,8 @@ This is a minor patch release that mostly focuses on build and tooling issues, w
4242
- For the React-scaffolded projects, we now explicitly add **office-ui-fabric-react** (OUIFR) v6 to **package.json**. We were finding a common pattern where people weren't adding OUIFR to **package.json**, but were using it.
4343
- OUIFR would end up in the **node_modules** folder because of downstream dependencies. While the solution would compile, it could cause issues in the future such as when the solution was upgraded to use a different version of the framework. In this case, a different version of OUIFR would get used.
4444
- In general, using packages in this pattern (*via shadow dependencies*) is a bad thing. You're more than welcome to remove the reference in **package.json** if you aren't using OUIFR in your solution, or change the version if needed. We also updated the type declarations for the React packages to match the versions of React / React DOM referenced in the release.
45-
- Support for [Node.js 10](https://nodejs.org/). Node.js v8 is still supported, but will be removed in a future release.
45+
- Added `.ms-SPLegacyFabricBlock` CSS class to SPFx solutions that load the Office UI Fabric core stylesheets on demand (*see [Safely use legacy Office UI Fabric Styles (after SPFx v1.8.2)](office-ui-fabric-integration.md#safely-use-legacy-office-ui-fabric-styles-after-spfx-v182) for more detail*)
46+
- Support for [Node.js 10](https://nodejs.org/). Node.js v8 is still supported, but will be removed in a future release.
4647

4748
### Updates added to SharePoint Online
4849

0 commit comments

Comments
 (0)