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.
Open the command prompt. Create a directory for SPFx solution.
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.
In the command prompt, type the below command to open the solution in a code editor of your choice.
Set Theme color to the web part
SPFx allows referring theme colors of the site. Theme colors can be referred to as below.
- .button {
- background-color: "[theme: themePrimary, default: #0078d7]";
- }
Open ThemeDemoWebPart.module.scss file from \src\webparts\themeDemo\.
Make the below changes to the CSS classes.
- .row {
- padding: 20px;
- background-color: "[theme: themeDark, default: #005a9e]";
- }
-
- .button {
- background-color: "[theme: themePrimary, default: #0078d7]";
- border-color: "[theme: themePrimary, default: #0078d7]";
- }
Test the WebPart- On the command prompt, type “gulp serve”.
- Open SharePoint site.
- Navigate to /_layouts/15/workbench.aspx
- Add the webpart to page.
- Web part should have the same color theme as of SharePoint site.
Summary
The theme colors help develop SharePoint Framework web parts consistent with the SharePoint site without any additional development efforts.