Skip to content

Commit a4bce0c

Browse files
committed
Added 'VariantThemeProvider' Control
1 parent d74c7ff commit a4bce0c

File tree

10 files changed

+281
-36
lines changed

10 files changed

+281
-36
lines changed
11.1 MB
Loading
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Variant Theme Provider
2+
3+
This control is a super set of the Theme Provider control, which not only allows you to pass the FluentUI Theme to the child controls, but also allows you to apply "variants" or generate a theme starting from the three basic colors, primary color, text color and color background.
4+
5+
The idea comes from the possibility of "highlighting" a Web Part in the page or section where it is contained.
6+
7+
By default, the SharePoint Modern Pages allow to change the set of colors which are then applied to all controls, but this can only be done at the site level (changing the site theme) or at the page section level (applying variations from the site theme).
8+
9+
With this control we have the possibility to apply the variants to the single Web Part or to a portion of it.
10+
11+
Here is an example of the control in action inside a Web Part:
12+
13+
![Variant Theme Provider control](../assets/VariantThemeProvider.gif)
14+
15+
## How to use this control in your solutions
16+
17+
- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../../#getting-started) page for more information about installing the dependency.
18+
- In your component file, import the `VariantThemeProvider` control as follows:
19+
20+
```TypeScript
21+
import { VariantThemeProvider, VariantType, IThemeColors } from "@pnp/spfx-controls-react/lib/VariantThemeProvider";
22+
```
23+
24+
- Example on use the `VariantThemeProvider` control with the 'Neutral' variant on custom theme generated from the 'themeColors' property:
25+
26+
```TSX
27+
const customThemeColors: IThemeColors = {
28+
primaryColor: "#0078d4",
29+
textColor: "#323130",
30+
backgroundColor: "#ffffff"
31+
}
32+
<VariantThemeProvider
33+
themeColors={customThemeColors}
34+
variantType={VariantType.Neutral}>
35+
{/* Child controls */}
36+
</VariantThemeProvider>
37+
```
38+
39+
- Example on use the `VariantThemeProvider` control with the 'Strong' variant on theme passed with the from the 'theme' property:
40+
41+
```TSX
42+
<VariantThemeProvider
43+
theme={theme}
44+
variantType={VariantType.Strong}>
45+
{/* Child controls */}
46+
</VariantThemeProvider>
47+
```
48+
49+
## Implementation
50+
51+
The `VariantThemeProvider` control can be configured with the following properties (inherits all the properties present in the FluentUI ThemeProvider control):
52+
53+
| Property | Type | Required | Description |
54+
| ---- | ---- | ---- | ---- |
55+
| as | React.ElementType | no | A component that should be used as the root element of the ThemeProvider component. |
56+
| ref | React.Ref<HTMLElement> | no | Optional ref to the root element. |
57+
| theme | PartialTheme \| Theme | no | Defines the theme provided by the user. |
58+
| renderer | StyleRenderer | no | Optional interface for registering dynamic styles. Defaults to using `merge-styles`. Use this to opt into a particular rendering implementation, such as `emotion`, `styled-components`, or `jss`. Note: performance will differ between all renders. Please measure your scenarios before using an alternative implementation. |
59+
| applyTo | element \| body \| none | no | Defines where body-related theme is applied to. Setting to 'element' will apply body styles to the root element of this control. Setting to 'body' will apply body styles to document body. Setting to 'none' will not apply body styles to either element or body. |
60+
| variantType | VariantType | no | Variant type to apply to the theme. |
61+
| themeColors | IThemeColors | no | Object used to generate a new theme from colors. |
62+
63+
Interface `IThemeColors`
64+
65+
| Property | Type | Required | Description |
66+
| ---- | ---- | ---- | ---- |
67+
| primaryColor | string | yes | Primary Color of the theme. |
68+
| textColor | string | yes | Text Color of the theme. |
69+
| backgroundColor | string | yes | Background Color of the theme. |
70+
71+
Enum `VariantType`
72+
73+
| Type | Description |
74+
| ---- | ---- |
75+
| None | Apply the theme without variations. |
76+
| Neutral | Apply the 'Neutral' variation to the theme. |
77+
| Soft | Apply the 'Soft' variation to the theme. |
78+
| Strong | Apply the 'Strong' variation to the theme. |
79+
80+
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/wiki/controls/VariantThemeProvider)

package-lock.json

Lines changed: 25 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@fluentui/react-hooks": "^8.2.6",
2424
"@fluentui/react-northstar": "0.51.3",
2525
"@fluentui/react-theme-provider": "^0.18.5",
26+
"@fluentui/scheme-utilities": "^8.2.12",
2627
"@fluentui/theme": "^2.4.7",
2728
"@microsoft/mgt-react": "2.3.0",
2829
"@microsoft/mgt-spfx": "2.3.0",

src/VariantThemeProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './controls/variantThemeProvider/index';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ThemeProvider } from "@fluentui/react-theme-provider/lib/ThemeProvider";
2+
import { IPartialTheme, ITheme } from "office-ui-fabric-react/lib/Styling";
3+
import * as React from "react";
4+
import { useCallback } from "react";
5+
import { generateThemeFromColors, generateThemeVariant, getDefaultTheme } from "./VariantThemeProviderHelpers";
6+
import { VariantThemeProviderProps, VariantType } from "./VariantThemeProviderProps";
7+
8+
export const VariantThemeProvider = (props: VariantThemeProviderProps) => {
9+
const themeToApply = useCallback(
10+
() => {
11+
let workingVariantType = (props.variantType) ? props.variantType : VariantType.None;
12+
let workingTheme: IPartialTheme | ITheme;
13+
14+
if (props.themeColors) {
15+
workingTheme = generateThemeFromColors(props.themeColors.primaryColor, props.themeColors.textColor, props.themeColors.backgroundColor);
16+
} else {
17+
if (props.theme) {
18+
workingTheme = props.theme;
19+
} else {
20+
workingTheme = getDefaultTheme();
21+
}
22+
}
23+
24+
let themeVariantToApply = (props.variantType == VariantType.None)
25+
? workingTheme
26+
: generateThemeVariant(workingTheme, workingVariantType);
27+
28+
return themeVariantToApply;
29+
},
30+
[props.theme, props.themeColors, props.variantType]);
31+
32+
return (
33+
<ThemeProvider
34+
{...props}
35+
theme={themeToApply()}>
36+
{props.children}
37+
</ThemeProvider>
38+
);
39+
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { getNeutralVariant, getSoftVariant, getStrongVariant } from "@fluentui/scheme-utilities/lib/variants";
2+
import { getColorFromString, isDark } from "office-ui-fabric-react/lib/Color";
3+
import { createTheme, getTheme, IPartialTheme, ITheme } from "office-ui-fabric-react/lib/Styling";
4+
import { BaseSlots, ThemeGenerator, themeRulesStandardCreator } from "office-ui-fabric-react/lib/ThemeGenerator";
5+
import { VariantType } from "./VariantThemeProviderProps";
6+
7+
export const generateThemeVariant = (theme: IPartialTheme | ITheme, themeType: VariantType): IPartialTheme | ITheme => {
8+
let currentTheme: IPartialTheme | ITheme;
9+
10+
switch (themeType) {
11+
case VariantType.None: currentTheme = theme;
12+
break;
13+
case VariantType.Neutral: currentTheme = getNeutralVariant(theme);
14+
break;
15+
case VariantType.Soft: currentTheme = getSoftVariant(theme);
16+
break;
17+
case VariantType.Strong: currentTheme = getStrongVariant(theme);
18+
break;
19+
default: currentTheme = theme;
20+
break;
21+
}
22+
23+
return currentTheme;
24+
};
25+
26+
export const getDefaultTheme = (): ITheme => {
27+
let currentTheme;
28+
const themeColorsFromWindow: any = (window as any)?.__themeState__?.theme;
29+
if (themeColorsFromWindow) {
30+
currentTheme = createTheme({
31+
palette: themeColorsFromWindow
32+
});
33+
}
34+
else
35+
currentTheme = getTheme();
36+
37+
return currentTheme;
38+
};
39+
40+
export const generateThemeFromColors = (primaryColor: string, textColor: string, backgroundColor: string): ITheme => {
41+
const themeRules = themeRulesStandardCreator();
42+
const colors = {
43+
primaryColor: getColorFromString(primaryColor)!,
44+
textColor: getColorFromString(textColor)!,
45+
backgroundColor: getColorFromString(backgroundColor)!,
46+
};
47+
const currentIsDark = isDark(themeRules[BaseSlots[BaseSlots.backgroundColor]].color!);
48+
49+
ThemeGenerator.insureSlots(themeRules, currentIsDark);
50+
ThemeGenerator.setSlot(
51+
themeRules[BaseSlots[BaseSlots.primaryColor]],
52+
colors.primaryColor,
53+
currentIsDark,
54+
true,
55+
true
56+
);
57+
ThemeGenerator.setSlot(
58+
themeRules[BaseSlots[BaseSlots.foregroundColor]],
59+
colors.textColor,
60+
currentIsDark,
61+
true,
62+
true
63+
);
64+
ThemeGenerator.setSlot(
65+
themeRules[BaseSlots[BaseSlots.backgroundColor]],
66+
colors.backgroundColor,
67+
currentIsDark,
68+
true,
69+
true
70+
);
71+
72+
const themeAsJson: { [key: string]: string; } = ThemeGenerator.getThemeAsJson(themeRules);
73+
const generatedTheme = createTheme({
74+
palette: themeAsJson,
75+
isInverted: currentIsDark,
76+
});
77+
78+
return generatedTheme;
79+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ThemeProviderProps } from "@fluentui/react-theme-provider/lib/ThemeProvider.types";
2+
3+
export interface VariantThemeProviderProps extends ThemeProviderProps {
4+
variantType?: VariantType;
5+
themeColors?: IThemeColors;
6+
}
7+
8+
export enum VariantType {
9+
None,
10+
Neutral,
11+
Soft,
12+
Strong
13+
}
14+
15+
export interface IThemeColors {
16+
primaryColor: string;
17+
textColor: string;
18+
backgroundColor: string;
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './VariantThemeProvider';
2+
export * from './VariantThemeProviderHelpers';
3+
export * from './VariantThemeProviderProps';

0 commit comments

Comments
 (0)