-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
withFluentProvider.tsx
55 lines (46 loc) · 1.7 KB
/
withFluentProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as React from 'react';
import { FluentProvider } from '@fluentui/react-provider';
import {
Theme,
teamsDarkTheme,
teamsHighContrastTheme,
teamsLightTheme,
webDarkTheme,
webLightTheme,
} from '@fluentui/react-theme';
import { defaultTheme, ThemeIds } from '../theme';
import { DIR_ID, THEME_ID } from '../constants';
import { FluentStoryContext } from '../hooks';
import { isDecoratorDisabled } from '../utils/isDecoratorDisabled';
const themes: Record<ThemeIds, Theme> = {
'web-light': webLightTheme,
'web-dark': webDarkTheme,
'teams-light': teamsLightTheme,
'teams-dark': teamsDarkTheme,
'teams-high-contrast': teamsHighContrastTheme,
} as const;
const findTheme = (themeId?: ThemeIds) => {
return themeId ? themes[themeId] : null;
};
export const withFluentProvider = (StoryFn: () => JSX.Element, context: FluentStoryContext) => {
const { globals, parameters } = context;
const { mode } = parameters;
if (isDecoratorDisabled(context, 'FluentProvider')) {
return StoryFn();
}
const isVrTest = mode === 'vr-test';
const dir = parameters.dir ?? globals[DIR_ID] ?? 'ltr';
const globalTheme = findTheme(globals[THEME_ID]);
const paramTheme = findTheme(parameters.fluentTheme);
const theme = paramTheme ?? globalTheme ?? themes[defaultTheme.id];
return (
<FluentProvider theme={theme} dir={dir}>
{isVrTest ? StoryFn() : <FluentExampleContainer theme={theme}>{StoryFn()}</FluentExampleContainer>}
</FluentProvider>
);
};
const FluentExampleContainer: React.FC<{ theme: Theme }> = props => {
const { theme } = props;
const backgroundColor = theme.colorNeutralBackground2;
return <div style={{ padding: '48px 24px', backgroundColor }}>{props.children}</div>;
};