Skip to content

Commit 5a659fb

Browse files
waldekmastykarzVesaJuvonen
authored andcommitted
Added guidance on using theme colors in customizations (SharePoint#725)
1 parent 89399a6 commit 5a659fb

6 files changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Use theme colors in your SharePoint Framework customizations
2+
3+
When building SharePoint Framework customizations you should use theme colors, so that your customizations looks like a part of the site. This article explains how can you refer to the theme colors of the context site in your SharePoint Framework solution.
4+
5+
> **Note:** although this article uses SharePoint Framework client-side web part as example, the described techniques apply to all types of SharePoint Framework customizations.
6+
7+
## Fixed colors vs. theme colors
8+
9+
When you scaffold a new SharePoint Framework client-side web part, it uses a fixed blue palette. When you add such web part on a modern site, using a different color scheme, it stands out and doesn't look like a part of the site.
10+
11+
![SharePoint Framework client-side web part using the blue color scheme on a modern team site using the red theme](../../images/themed-styles-blue-web-part-red-site.png)
12+
13+
When using fixed colors, you decide upfront which colors you want to use for which elements. This can lead to a situation like the one just illustrated, where a blue web part is displayed on a red team site, standing out unnecessarily. In most cases, you should strive to leverage the theme colors of the context site, so that your solution doesn't stand out but looks like a part of the site.
14+
15+
Instead of using fixed colors, SharePoint Framework allows you to refer to theme colors of the context site. As a result, if your web part is placed on a site using red theme, it will use the red palette as well, and if it's placed on a site using the blue theme, it will automatically adjust itself to use the blue palette. All of this is done automatically without any changes to the web part code in between.
16+
17+
## Using theme colors in the SharePoint Framework
18+
19+
When working with fixed colors, you specify them in CSS properties, for example:
20+
21+
```css
22+
.button {
23+
background-color: #0078d7;
24+
}
25+
```
26+
27+
To use a theme color instead, replace the fixed color with a theme token:
28+
29+
```css
30+
.button {
31+
background-color: "[theme: themePrimary, default: #0078d7]";
32+
}
33+
```
34+
35+
When your SharePoint Framework customization is loading on the page, the **@microsoft/load-themed-styles** package, which is a part of the SharePoint Framework, will look for theme tokens in CSS files and try to replace them with the corresponding color from the current theme. If the value for the specified token is not available, SharePoint Framework will use the value specified using the **default** parameter instead, which is why it's important that you always include it.
36+
37+
Following theme tokens are available for you to use:
38+
39+
Token|Default value on a modern team site using the red palette|Remarks
40+
-----|--------------------------------|-----------
41+
`backgroundImageUri`|`none`|
42+
`themeAccent`|`#ee0410`|
43+
`themeAccentTranslucent10`|`rgba(238, 4, 16, 0.10)`|
44+
`themeDark`|`#b3030c`|Used for action icons in the command bar and as hover color
45+
`themeDarkAlt`|`#b3030c`|
46+
`themeDarker`|`#770208`|
47+
`themeLight`|`#fd969b`|
48+
`themeLightAlt`|`#fd969b`|
49+
`themeLighter`|`#fecacd`|
50+
`themeLighterAlt`|`#fecacd`|
51+
`themePrimary`|`#ee0410`|Primary theme color. Used for icons and default buttons
52+
`themeSecondary`|`#fc6169`|
53+
`themeTertiary`|`#fd969b`|
54+
`themeTertiaryAlt`|`#fd969b`|
55+
56+
> **Note:** there are more tokens registered with the SharePoint Framework. While all of them have values specified on classic sites, only the subset mentioned earlier has values on modern SharePoint sites. For the complete list of available tokens, see the value of the `window.__themeState__.theme` property using the console in your web browser's developer tools.
57+
58+
## Use theme colors in your customizations
59+
60+
When you scaffold a new SharePoint Framework client-side web part, by default, it uses the fixed blue palette. Following steps describe the necessary adjustments to have the web part use theme colors instead.
61+
62+
> **Note:** the following steps apply to a SharePoint Framework client-side web part named _HelloWorld_ built using React. For web parts built using different libraries and other types of customizations, you might need to adjust the modifications accordingly.
63+
64+
In the code editor open the **./src/webparts/helloWorld/components/HelloWorld.tsx** file and from the div with class **ms-Grid-row** remove the **ms-bgColor-themeDark** class.
65+
66+
![The 'ms-bgColor-themeDark' class selected in Visual Studio Code editor](../../images/themed-styles-ms-bgcolor-themedark-class.png)
67+
68+
Next, in the same folder, open the **HelloWorld.module.scss** file. Change the `.row` selector to:
69+
70+
```css
71+
.row {
72+
padding: 20px;
73+
background-color: "[theme: themeDark, default: #005a9e]";
74+
}
75+
```
76+
77+
![The .row selector extended with background color](../../images/themed-styles-row-class.png)
78+
79+
In the `.button` selector, change the `background-color` and `border-color` properties to:
80+
81+
```css
82+
.button {
83+
/* ... */
84+
background-color: "[theme: themePrimary, default: #0078d7]";
85+
border-color: "[theme: themePrimary, default: #0078d7]";
86+
/* ... */
87+
}
88+
```
89+
90+
![The .button selector updated with references to theme colors](../../images/themed-styles-button-class.png)
91+
92+
When you add the web part to a site, the colors used by the web part will automatically adapt to the theme colors used by the current site.
93+
94+
![Side-by-side view of the same web part displayed in two sites using different colors. The web part follows the color scheme of each web site](../../images/themed-styles-side-by-side.png)
95+
96+
## More information
97+
98+
* [How to use Theme Colors in SPFX Web Parts](http://www.n8d.at/blog/how-to-use-theme-colors-in-spfx-web-parts/) by Stefan Bauer (Office Development MVP)
Loading

images/themed-styles-button-class.png

221 KB
Loading
Loading

images/themed-styles-row-class.png

216 KB
Loading

images/themed-styles-side-by-side.png

190 KB
Loading

0 commit comments

Comments
 (0)