Skip to content

Commit

Permalink
Change default theme from a const object to a function that returns t…
Browse files Browse the repository at this point in the history
…he object (#2483)

### Platforms Impacted
- [x] iOS
- [x] macOS
- [x] win32 (Office)
- [x] windows
- [x] android

### Description of changes

Update the default theme fluent object to be a method that returns an object. 

This issue is blocking #2481:
- the updated designs for iOS tokens include some new tokens that aren't in the default theme
- mapPipelineToTheme.ios.ts was updated to reflect these designs
- however, the default theme is still getting created and is trying to running the iOS code for mapPipelineToTheme, which causes failures because the iOS alias token set now includes tokens that aren't in the default theme (win32) token set
- changing the default theme from a const object to a method prevents the default theme from getting created automatically

### Verification

No visual changes
Check if CI passes

### Pull request checklist

This PR has considered (when applicable):
- [ ] Automated Tests
- [ ] Documentation and examples
- [ ] Keyboard Accessibility
- [ ] Voiceover
- [ ] Internationalization and Right-to-left Layouts
  • Loading branch information
lyzhan7 authored Jan 6, 2023
1 parent 8257914 commit 984ee82
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@fluentui-react-native/default-theme",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@fluentui-react-native/framework",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@fluentui-react-native/theme",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@fluentui-react-native/win32-theme",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@uifabricshared/foundation-compose",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Change default theme const to a method",
"packageName": "@uifabricshared/theming-react-native",
"email": "78454019+lyzhan7@users.noreply.github.com",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/deprecated/foundation-compose/src/useStyling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function useStylingCore<TProps, TSlotProps extends ISlotProps, TTokens extends o
lookupOverride?: IOverrideLookup,
): IWithTokens<TSlotProps, TTokens> {
// get the theme value from the context (or the default theme if it is not set)
const theme = useTheme() || defaultFluentTheme;
const theme = useTheme() || defaultFluentTheme();

// resolve the array of settings for these options
lookupOverride = lookupOverride || props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { defaultFluentTheme } from '@fluentui-react-native/default-theme';
* @deprecated
*/
export function getBaselinePlatformTheme(): ITheme {
return defaultFluentTheme;
return defaultFluentTheme();
}
2 changes: 1 addition & 1 deletion packages/framework/framework/src/useFluentTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import { Theme, useTheme } from '@fluentui-react-native/theme-types';
* @returns - a valid Theme object
*/
export function useFluentTheme(): Theme {
return useTheme() || defaultFluentTheme;
return useTheme() || defaultFluentTheme();
}
2 changes: 1 addition & 1 deletion packages/framework/theme/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const createMyCustomTheme = () => {
// create the reference
const themeRef = new ThemeReference(
// base it on the default fluent theme
defaultFluentTheme,
defaultFluentTheme(),
// mix in some constant values to override
{
colors: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ beforeAll(() => {
});

it('defaultFluentTheme test', () => {
expect(defaultFluentTheme).toMatchSnapshot();
expect(defaultFluentTheme()).toMatchSnapshot();
});

it('defaultFluentDarkTheme test', () => {
expect(defaultFluentDarkTheme).toMatchSnapshot();
expect(defaultFluentDarkTheme()).toMatchSnapshot();
});

describe('createDefaultTheme test', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/theming/default-theme/src/createDefaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function createDefaultTheme(options: ThemeOptions = {}): ThemeReference {
const current = getCurrentAppearance(options.appearance, options.defaultAppearance || 'light');
switch (current) {
case 'light':
return defaultFluentTheme;
return defaultFluentTheme();
case 'dark':
return defaultFluentDarkTheme;
return defaultFluentDarkTheme();
case 'highContrast':
return defaultFluentHighConstrastTheme;
return defaultFluentHighConstrastTheme();
default:
assertNever(current);
}
Expand Down
63 changes: 39 additions & 24 deletions packages/theming/default-theme/src/defaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Platform } from 'react-native';
import { getStockWebPalette, getStockWebDarkPalette, getStockWebHCPalette } from './defaultColors';
import { globalTokens } from '@fluentui-react-native/theme-tokens';
import { createShadowAliasTokens } from './createAliasTokens';
import { memoize } from '@fluentui-react-native/memo-cache';

function _defaultTypography(): Typography {
const defaultsDict = {
Expand Down Expand Up @@ -77,29 +78,43 @@ export function defaultSpacing(): Spacing {
return { s2: '4px', s1: '8px', m: '16px', l1: '20px', l2: '32px' };
}

export const defaultFluentTheme: Theme = {
colors: getStockWebPalette(),
typography: _defaultTypography(),
spacing: defaultSpacing(),
shadows: createShadowAliasTokens('light'),
components: {},
host: { appearance: 'light' },
};
function defaultFluentThemeWorker(): Theme {
return {
colors: getStockWebPalette(),
typography: _defaultTypography(),
spacing: defaultSpacing(),
shadows: createShadowAliasTokens('light'),
components: {},
host: { appearance: 'light' },
};
}

export const defaultFluentTheme = memoize(defaultFluentThemeWorker);

export const defaultFluentDarkTheme: Theme = {
colors: getStockWebDarkPalette(),
typography: defaultFluentTheme.typography,
shadows: createShadowAliasTokens('dark'),
spacing: defaultFluentTheme.spacing,
components: {},
host: { appearance: 'dark' },
};
function defaultFluentDarkThemeWorker(): Theme {
const defaultTheme = defaultFluentTheme();
return {
colors: getStockWebDarkPalette(),
typography: defaultTheme.typography,
shadows: createShadowAliasTokens('dark'),
spacing: defaultTheme.spacing,
components: {},
host: { appearance: 'dark' },
};
}

export const defaultFluentDarkTheme = memoize(defaultFluentDarkThemeWorker);

function defaultFluentHighConstrastThemeWorker(): Theme {
const defaultTheme = defaultFluentTheme();
return {
colors: getStockWebHCPalette(),
typography: defaultTheme.typography,
shadows: createShadowAliasTokens('highContrast'),
spacing: defaultTheme.spacing,
components: {},
host: { appearance: 'highContrast' },
};
}

export const defaultFluentHighConstrastTheme: Theme = {
colors: getStockWebHCPalette(),
typography: defaultFluentTheme.typography,
shadows: createShadowAliasTokens('highContrast'),
spacing: defaultFluentTheme.spacing,
components: {},
host: { appearance: 'highContrast' },
};
export const defaultFluentHighConstrastTheme = memoize(defaultFluentHighConstrastThemeWorker);
4 changes: 2 additions & 2 deletions packages/theming/win32-theme/src/getThemeTypography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createFontAliasTokens } from './createFontAliasTokens';

export function win32Typography(): Typography {
const win32Dict = {
sizes: defaultFluentTheme.typography.sizes,
weights: defaultFluentTheme.typography.weights,
sizes: defaultFluentTheme().typography.sizes,
weights: defaultFluentTheme().typography.weights,
// hard coded until we support new fontFamily format
families: {
primary: 'Segoe UI',
Expand Down

0 comments on commit 984ee82

Please sign in to comment.