Skip to content

Commit a2dd640

Browse files
- Upgrade EnhancedThemeProvider
* make the context compatible with any SPFx component context * fixing folder name to be consistent with other components * update documentation (typo, content)
1 parent 5cd8462 commit a2dd640

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed
Binary file not shown.

docs/documentation/docs/controls/EnhancedThemeProvider.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Enhanced Theme Provider
22

33
The reasons behind this control are many and concern the use of Fluent UI controls currently officially supported by SPFx, that is:
4-
- `Problems with Teams theme support`, when hosting a Web Part like Tab or Personal App and specifically the lack of support by this version of Fluent UI React of the high contrast theme.
5-
- `Lack of basic style`, such as fonts, for basic HTML elements when creating `Web Parts hosted in Teams as Tabs or personal App`.
6-
- Lack of basic style, such as fonts, for basic HTML elements when creating `Web Parts in "isDomainIsolated" mode`, aka the Isolated Web Parts.
74

8-
Therefore, the control is to be considered as a sort of `wrapper for all react and non-react controls` that you want to add to the WebPart.
5+
- `Problems with Teams theme support`, when hosting a web part like Tab or Personal App and specifically the lack of support by this version of Fluent UI React of the high contrast theme.
6+
- `Lack of basic style`, such as fonts, for basic HTML elements when creating `web parts hosted in Teams as Tabs or personal App`.
7+
- Lack of basic style, such as fonts, for basic HTML elements when creating `web parts in "isDomainIsolated" mode`, aka the Isolated web parts.
8+
9+
Therefore, the control is to be considered as a sort of `wrapper for all react and non-react controls` that you want to add to the web part.
910

1011
The control `extends the functionality of the Fluent UI ThemeProvider control` (currently in version 7) by adding some logic thanks to the information contained in the 'context' property, that is:
11-
- If the Web Part is hosted inside SharePoint, the theme passed through the 'Theme' property will be used or the default one of the current site will be taken.
12+
13+
- If the web part is hosted inside SharePoint, the theme passed through the 'Theme' property will be used or the default one of the current site will be taken.
1214
- If the web part is hosted within Teams, the "Theme" property will be ignored and using the "Context" property checks which theme is currently applied and adds a handler to notify when the theme is changed. This allows you to manage the change of theme in Teams in real-time, without having to reload the Tab or the Personal App.
1315

1416
Example of use in SharePoint in a `SharePointFullPage - Isolated web parts` (note that the titles H1, H2, H3 and the paragraph are normal html tags that automatically take the font and color style from the control):
@@ -99,7 +101,7 @@ The `EnhancedThemeProvider` control can be configured with the following propert
99101

100102
| Property | Type | Required | Description |
101103
| ---- | ---- | ---- | ---- |
102-
| context | WebPartContext | yes | Set the context from the SPFx Web Part. |
104+
| context | BaseComponentContext | yes | Sets the context from the SPFx component (a web part, an application customizer or a form customizer). |
103105
| as | React.ElementType | no | A component that should be used as the root element of the ThemeProvider component. |
104106
| ref | React.Ref<HTMLElement> | no | Optional ref to the root element. |
105107
| theme | PartialTheme \| Theme | no | Defines the theme provided by the user. |

src/EnhancedThemeProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './controls/EnhancedThemeProvider/index';
1+
export * from './controls/enhancedThemeProvider/index';

src/controls/EnhancedThemeProvider/IEnhancedThemeProviderProps.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/controls/EnhancedThemeProvider/EnhancedThemeProvider.tsx renamed to src/controls/enhancedThemeProvider/EnhancedThemeProvider.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { useCallback, useEffect, useState } from 'react';
77
import { fluentUITeamsDarkTheme } from '../../common/fluentUIThemes/FluentUITeamsDarkTheme';
88
import { fluentUITeamsDefaultTheme } from '../../common/fluentUIThemes/FluentUITeamsDefaultTheme';
99
import { fluentUITeamsHighContrastTheme } from '../../common/fluentUIThemes/FluentUITeamsHighContrastTheme';
10-
import { IEnhancedThemeProviderProps } from './IEnhancedThemeProviderProps';
10+
import IEnhancedThemeProviderProps from './IEnhancedThemeProviderProps';
1111
import { ThemeContext, useTheme } from '@fluentui/react-theme-provider';
1212
import * as telemetry from '../../common/telemetry';
13+
import { IMicrosoftTeams, WebPartContext } from '@microsoft/sp-webpart-base';
1314

1415
declare const window: Window & {
1516
__themeState__: {
@@ -48,13 +49,14 @@ const EnhancedThemeProvider = (props: IEnhancedThemeProviderProps): JSX.Element
4849
}, []);
4950

5051
useEffect(() => {
51-
setIsInTeams((props.context.sdks.microsoftTeams) ? true : false);
52+
setIsInTeams(props.context instanceof WebPartContext && props.context.sdks.microsoftTeams ? true : false);
5253
}, [props.context]);
5354

5455
useEffect(() => {
5556
if (isInTeams) {
56-
setTeamsThemeName(props.context.sdks.microsoftTeams?.context?.theme);
57-
props.context.sdks?.microsoftTeams?.teamsJs?.registerOnThemeChangeHandler((theme: string) => {
57+
const teamsInstance: IMicrosoftTeams = (props.context as WebPartContext).sdks.microsoftTeams;
58+
setTeamsThemeName(teamsInstance.context?.theme);
59+
teamsInstance.teamsJs?.registerOnThemeChangeHandler((theme: string) => {
5860
setTeamsThemeName(theme);
5961
});
6062
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ThemeProviderProps } from '@fluentui/react-theme-provider';
2+
import { BaseComponentContext } from '@microsoft/sp-component-base';
3+
4+
export default interface IEnhancedThemeProviderProps extends ThemeProviderProps {
5+
/**
6+
* Set the context from the SPFx component.
7+
*/
8+
context: BaseComponentContext;
9+
}

0 commit comments

Comments
 (0)