SharePoint Framework - Theme Colors


The SharePoint Framework client-side web part uses the blue theme color by default, irrespective of the theme color of the SharePoint site. If SharePoint site has a different theme applied than the blue one, the SPFx web part stands out and does not provide the consistent look and feel with the site.
In this article, we will explore an option of theme colors to make the SPFx web parts look consistent with the SharePoint Site theme. 

The Visual Mismatch

Consider an example that SharePoint site has a green color theme. The SPFx web part with the default blue color theme looks inconsistent with the site.
Create SPFx Solution
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-theme-colors  
Navigate to the above-created directory.
  1. cd spfx-theme-colors  
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
SharePoint Framework - Theme Colors
Solution Name
Hit Enter to have a default name (spfx-theme-colors in this case) or type in any other name for your solution.
Selected choice: Hit Enter
Target for component
Here, we can select the target environment where we are planning to deploy the client webpart; i.e. SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
Deployment option
Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
Type of client-side component to create
We can choose to create a client-side webpart or an extension. Choose the webpart option.
Selected choice: WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice: ThemeDemo
Web part description
Hit Enter to select the default description or type in any other value.
Selected choice: Theme Colors with SPFx
Framework to use
Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout).
Selected choice: No JavaScript Framework / React
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
  1. npm shrinkwrap  
In the command prompt, type the below command to open the solution in a code editor of your choice.
  1. code .  

Set Theme color to the web part

SPFx allows referring theme colors of the site. Theme colors can be referred to as below.
  1. .button {  
  2.     background-color: "[theme: themePrimary, default: #0078d7]";  
  3. }  
Open ThemeDemoWebPart.module.scss file from \src\webparts\themeDemo\.
Make the below changes to the CSS classes.
  1. .row {  
  2.     padding20px;  
  3.     background-color"[theme: themeDark, default: #005a9e]";  
  4. }  
  6. .button {  
  7.     background-color"[theme: themePrimary, default: #0078d7]";  
  8.     border-color"[theme: themePrimary, default: #0078d7]";  
  9. }  
Test the WebPart
  1. On the command prompt, type “gulp serve”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx
  4. Add the webpart to page.
  5. Web part should have the same color theme as of SharePoint site.
 SharePoint Framework - Theme Colors
The theme colors help develop SharePoint Framework web parts consistent with the SharePoint site without any additional development efforts.