Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Generate vars in extendTheme #35739

Merged
merged 89 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
973af92
[joy] Generate vars in `extendTheme`
mnajdova Jan 6, 2023
0c6ad95
fixes
mnajdova Jan 6, 2023
1e3dd19
fixes
mnajdova Jan 6, 2023
315574e
fix palette generation
mnajdova Jan 10, 2023
3e5eee9
Fix color inversion resolving logic
mnajdova Jan 10, 2023
7e1ff8b
[material] Generate vars in extendTheme
mnajdova Jan 10, 2023
707b7a4
Add test
mnajdova Jan 11, 2023
1c17730
move vars parser to extendTheme
siriwatknp Jan 13, 2023
cc2f375
Merge pull request #36 from siriwatknp/extendTheme/generate-vars2
mnajdova Feb 16, 2023
ec2c1af
add default values
mnajdova Feb 16, 2023
fbc5c8d
Merge branch 'material-next/generate-vars-in-extendTheme' into extend…
mnajdova Feb 16, 2023
de86915
Change Material UI's extendTheme
mnajdova Feb 16, 2023
ae73183
Material Next UI updates
mnajdova Feb 16, 2023
5fc8f01
WIP - joy add default css vars values
mnajdova Feb 17, 2023
9521a0e
add default css values to Material UI's extendTheme
mnajdova Feb 17, 2023
edc897e
Fix palette not available in theme.vars
mnajdova Feb 17, 2023
aad2c60
Joy - more css variables defaults added
mnajdova Feb 21, 2023
8d76b1f
Merge branch 'master' into extendTheme/generate-vars
mnajdova Feb 21, 2023
fd85ec6
changes after merging with master
mnajdova Feb 21, 2023
b6f92c9
more CSS variables defaults
mnajdova Feb 21, 2023
7720593
prettier
mnajdova Feb 21, 2023
05c1d81
Fix focus visible variable default value
mnajdova Feb 21, 2023
bef9873
Fix issue for missing vars
mnajdova Feb 21, 2023
576a331
lint & prettier
mnajdova Feb 22, 2023
76e434b
more fixes
mnajdova Feb 22, 2023
0138dfc
prettier
mnajdova Feb 22, 2023
9a01043
revert changes in CssVarsProvider, the only change should be how the …
mnajdova Feb 22, 2023
176dbcf
More default values from custom theme
mnajdova Feb 22, 2023
56fce0b
Final fixes
mnajdova Feb 22, 2023
0de9228
Make options optional
mnajdova Feb 22, 2023
ab2baac
Handle channels as values, use correct resolved palette in the varian…
mnajdova Feb 22, 2023
48b379a
add support for lineHeight values in css vars defaults
mnajdova Feb 22, 2023
5091fcd
fixes
mnajdova Feb 22, 2023
62c77e5
final fixes
mnajdova Feb 22, 2023
8dd6c5d
Don't use CssVarsProvider in Joy UI's regression tests
mnajdova Feb 23, 2023
7f1e4a1
temporarly disable regression tests failing because of timeout
mnajdova Feb 23, 2023
5f7cebb
remove trailing /
mnajdova Feb 23, 2023
7af8584
add png
mnajdova Feb 23, 2023
570be4e
[base] Move hooks to their own directories (#36235)
hbjORbj Feb 21, 2023
40a136c
[Slider] Remove unnecessary `data-focusvisible` attribute (#36091)
ZeeshanTamboli Feb 21, 2023
1060372
[docs][base] List slots in API documentation (#36104)
hbjORbj Feb 21, 2023
bad09ac
[Slider] Miscellaneous improvements (#35941)
ZeeshanTamboli Feb 21, 2023
212e562
[base] Remove `classes` prop from the Base components that have it (#…
hbjORbj Feb 21, 2023
7231468
[docs] Remove See Slots Section text from Material UI slots descripti…
hbjORbj Feb 21, 2023
afa507c
[docs][joy] Build API documentations (#36008)
hbjORbj Feb 21, 2023
9a155a8
Bump Emotion to ^11.10.6 (#36249)
renovate[bot] Feb 22, 2023
a88d82d
Bump nx to ^15.7.2 (#36251)
renovate[bot] Feb 22, 2023
8239fb4
Bump @types/jsdom to ^21.1.0 (#36261)
renovate[bot] Feb 22, 2023
4853012
Bump @testing-library/react to ^14.0.0 (#36260)
renovate[bot] Feb 22, 2023
d1ec41d
Bump yargs to ^17.7.1 (#36258)
renovate[bot] Feb 22, 2023
e0b812a
Bump webpack-bundle-analyzer to ^4.8.0 (#36257)
renovate[bot] Feb 22, 2023
537bb48
Bump typescript-eslint to ^5.53.0 (#36256)
renovate[bot] Feb 22, 2023
4673d86
Bump @chakra-ui/system to ^2.5.1 (#36254)
renovate[bot] Feb 22, 2023
84f48cf
Bump lerna to ^6.5.1 (#36250)
renovate[bot] Feb 22, 2023
63824fb
[docs] Fix code on the Working with Tailwind CSS guide (#36090)
mnajdova Feb 22, 2023
2579b29
[Joy] Select popup should have max-height (#36156)
Vivek-Prajapatii Feb 22, 2023
9078414
Bump @testing-library/dom to ^9.0.0 (#36259)
renovate[bot] Feb 22, 2023
ce8e021
Bump @argos-ci/core to ^0.8.0 (#36253)
renovate[bot] Feb 22, 2023
14b3afa
[Select] Fix incorrect selecting of first element (#36024)
michaldudak Feb 22, 2023
8ddf9fd
[docs][joy] List slots in API documentation (#36271)
hbjORbj Feb 22, 2023
1cb7776
Bump @mdx-js/react to ^2.3.0 (#36144)
renovate[bot] Feb 22, 2023
1577eab
prevent error if theme comes from external
siriwatknp Feb 23, 2023
f7061a1
simplify the code
siriwatknp Feb 23, 2023
ff7fe66
add test
siriwatknp Feb 23, 2023
329aa26
fix undefined fallback
siriwatknp Feb 23, 2023
64b1006
fix focus visible regression
mnajdova Feb 23, 2023
76e1011
Merge branch 'extendTheme/generate-vars' of https://github.com/mnajdo…
siriwatknp Feb 24, 2023
a5b48ca
light should come last for soft color inversion
siriwatknp Feb 24, 2023
c602938
shorten the fn name
siriwatknp Feb 24, 2023
e442707
use optional chaining
siriwatknp Feb 24, 2023
7f37346
ensure that light is the default
siriwatknp Feb 24, 2023
70b4fc1
use default theme
siriwatknp Feb 24, 2023
65527cf
invoke cssVarsProvider only in extendTheme
mnajdova Feb 24, 2023
06e89e5
Update cssVarsParser test
mnajdova Feb 24, 2023
b6bdfe1
fix lint
mnajdova Feb 24, 2023
34ced86
prettier
mnajdova Feb 24, 2023
753ea85
refactored createCssVarsTheme
mnajdova Feb 24, 2023
5c657de
improve readibility
mnajdova Feb 24, 2023
d93ab8c
remove type cast
mnajdova Feb 24, 2023
91c85d9
fixes
mnajdova Feb 24, 2023
64094b2
Merge branch 'master' into extendTheme/generate-vars
mnajdova Feb 27, 2023
e27cb0e
prettier
mnajdova Feb 27, 2023
27a6f5b
fix type issue
mnajdova Feb 27, 2023
f443527
Merge branch 'master' of https://github.com/mui/material-ui into exte…
siriwatknp Mar 2, 2023
98b8ad3
mode is already provided in the default
siriwatknp Mar 2, 2023
478eaaf
Add back the CssVarsProvider
mnajdova Mar 2, 2023
6cc38d9
Use createCssVarsTheme in the regression tests
mnajdova Mar 2, 2023
d930896
Check again the timeout tests
mnajdova Mar 2, 2023
d12e77b
fix default getColorSchemeSelector
siriwatknp Mar 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions packages/mui-joy/src/Box/Box.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ describe('Joy <Box />', () => {
refInstanceof: window.HTMLDivElement,
}));

it('respects theme from context', () => {
const { container } = render(
<ThemeProvider
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
theme={{
it('respects theme from context', function test() {
const isJSDOM = /jsdom/.test(window.navigator.userAgent);

if (isJSDOM) {
this.skip();
}

const theme = extendTheme({
colorSchemes: {
light: {
palette: {
primary: {
main: 'rgb(255, 0, 0)',
},
},
}}
>
},
},
});
const { container } = render(
<CssVarsProvider theme={theme}>
<Box color="primary.main" />
</ThemeProvider>,
</CssVarsProvider>,
);

expect(container.firstChild).toHaveComputedStyle({
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-joy/src/CssBaseline/CssBaseline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { GlobalStyles } from '@mui/system';
import { Theme, DefaultColorScheme, ColorSystem } from '../styles/types';
import { Components } from '../styles/components';
import defaultTheme from '../styles/defaultTheme';
import { CssBaselineProps } from './CssBaselineProps';

/**
Expand Down Expand Up @@ -71,6 +72,7 @@ function CssBaseline(props: CssBaselineProps) {
...colorSchemeStyles,
};
}}
defaultTheme={defaultTheme}
/>
{children}
</React.Fragment>
Expand Down
9 changes: 8 additions & 1 deletion packages/mui-joy/src/styles/ColorInversion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ interface ColorInversionProviderProps {
export function ColorInversionProvider({ children, variant }: ColorInversionProviderProps) {
const theme = useSystemTheme(defaultTheme);
return (
<ColorInversion.Provider value={variant ? theme.colorInversionConfig[variant] : undefined}>
<ColorInversion.Provider
value={
variant
? // `theme` could come from other emotion/styled-components context.
(theme.colorInversionConfig ?? defaultTheme.colorInversionConfig)[variant]
: undefined
}
>
{children}
</ColorInversion.Provider>
);
Expand Down
14 changes: 4 additions & 10 deletions packages/mui-joy/src/styles/CssVarsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { deepmerge } from '@mui/utils';
import { unstable_createCssVarsProvider as createCssVarsProvider } from '@mui/system';
import extendTheme, { CssVarsThemeOptions } from './extendTheme';
import defaultTheme from './defaultTheme';
import { CssVarsThemeOptions } from './extendTheme';
import { createSoftInversion, createSolidInversion } from './variantUtils';
import type { Theme, DefaultColorScheme, ExtendedColorScheme } from './types';

const shouldSkipGeneratingVar = (keys: string[]) =>
!!keys[0].match(/^(typography|variants|breakpoints|colorInversion|colorInversionConfig)$/) ||
!!keys[0].match(/sxConfig$/) || // ends with sxConfig
(keys[0] === 'palette' && !!keys[1]?.match(/^(mode)$/)) ||
(keys[0] === 'focus' && keys[1] !== 'thickness');

const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssVarsProvider<
DefaultColorScheme | ExtendedColorScheme
>({
theme: extendTheme(),
theme: defaultTheme,
attribute: 'data-joy-color-scheme',
modeStorageKey: 'joy-mode',
colorSchemeStorageKey: 'joy-color-scheme',
Expand All @@ -35,7 +30,6 @@ const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssV
);
return mergedTheme;
},
shouldSkipGeneratingVar,
});

export { CssVarsProvider, useColorScheme, getInitColorSchemeScript, shouldSkipGeneratingVar };
export { CssVarsProvider, useColorScheme, getInitColorSchemeScript };
2 changes: 1 addition & 1 deletion packages/mui-joy/src/styles/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ describe('[Joy] ThemeProvider', () => {
</ThemeProvider>,
);

expect(container.firstChild?.textContent).to.equal('100rem');
expect(container.firstChild?.textContent).to.equal('var(--joy-fontSize-md, 100rem)');
});
});
5 changes: 3 additions & 2 deletions packages/mui-joy/src/styles/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { ThemeProvider as SystemThemeProvider, useTheme as useSystemTheme } from '@mui/system';
import defaultTheme, { getThemeWithVars } from './defaultTheme';
import defaultTheme from './defaultTheme';
import extendTheme from './extendTheme';
import type { CssVarsThemeOptions } from './extendTheme';

export const useTheme = () => {
Expand All @@ -14,7 +15,7 @@ export default function ThemeProvider({
theme?: CssVarsThemeOptions;
}>) {
return (
<SystemThemeProvider theme={themeInput ? getThemeWithVars(themeInput) : defaultTheme}>
<SystemThemeProvider theme={themeInput ? extendTheme(themeInput) : defaultTheme}>
{children}
</SystemThemeProvider>
);
Expand Down
27 changes: 6 additions & 21 deletions packages/mui-joy/src/styles/defaultTheme.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect } from 'chai';
import { getThemeWithVars } from './defaultTheme';
import defaultTheme from './defaultTheme';

describe('defaultTheme', () => {
it('the output contains required fields', () => {
const result = getThemeWithVars();
Object.keys(result).forEach((field) => {
Object.keys(defaultTheme).forEach((field) => {
expect([
'breakpoints',
'components',
Expand Down Expand Up @@ -32,28 +31,14 @@ describe('defaultTheme', () => {
'getColorSchemeSelector',
'unstable_sxConfig',
'unstable_sx',
'shouldSkipGeneratingVar',
'generateCssVars',
]).to.includes(field);
});
});

it('the generated palette has correct colorChannel', () => {
const result = getThemeWithVars({
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
colorSchemes: {
light: {
palette: {
primary: {
mainChannel: '12 12 12',
},
},
},
},
});
expect(result.palette.primary.mainChannel).to.equal('12 12 12');
});

it('the generated palette always has mode and color scheme as `light`', () => {
const result = getThemeWithVars();
expect(result.palette.mode).to.equal('light');
expect(result.palette.colorScheme).to.equal('light');
expect(defaultTheme.palette.mode).to.equal('light');
expect(defaultTheme.palette.colorScheme).to.equal('light');
});
});
79 changes: 1 addition & 78 deletions packages/mui-joy/src/styles/defaultTheme.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,5 @@
import { deepmerge } from '@mui/utils';
import extendTheme from './extendTheme';
import type { CssVarsThemeOptions, ColorSystemOptions } from './extendTheme';
import type { Theme, RuntimeColorSystem } from './types';
import { createSoftInversion, createSolidInversion } from './variantUtils';

export const getThemeWithVars = (
themeInput?: Omit<CssVarsThemeOptions, 'colorSchemes'> & ColorSystemOptions,
) => {
const {
colorSchemes,
focus,
fontFamily,
fontSize,
fontWeight,
letterSpacing,
lineHeight,
radius,
shadow,
zIndex,
palette: paletteInput,
colorInversion: colorInversionInput,
...restTheme
} = extendTheme(themeInput);
const colorSchemePalette = deepmerge(
colorSchemes[paletteInput?.colorScheme || 'light'].palette,
paletteInput,
);
const {
mode = 'light',
colorScheme = 'light',
...palette
} = colorSchemePalette as RuntimeColorSystem['palette'];

const theme = {
focus,
fontFamily,
fontSize,
fontWeight,
letterSpacing,
lineHeight,
radius,
shadow,
zIndex,
...restTheme,
colorSchemes: {
...colorSchemes,
[colorScheme]: palette,
},
palette: {
...palette,
mode,
colorScheme,
},
vars: {
focus,
fontFamily,
fontSize,
fontWeight,
letterSpacing,
lineHeight,
radius,
shadow,
palette,
zIndex,
},
getColorSchemeSelector: () => '&',
} as unknown as Theme;

theme.colorInversion = deepmerge(
{
soft: createSoftInversion(theme),
solid: createSolidInversion(theme),
},
colorInversionInput,
);
return theme;
};

const defaultTheme = getThemeWithVars();
const defaultTheme = extendTheme();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG, I love this! ❤️ 😍


export default defaultTheme;
40 changes: 37 additions & 3 deletions packages/mui-joy/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,61 @@ describe('extendTheme', () => {
'colorInversionConfig',
'variants',
'cssVarPrefix',
'palette',
'vars',
'getColorSchemeSelector',
'colorInversion',
'unstable_sxConfig',
'unstable_sx',
'shouldSkipGeneratingVar',
'generateCssVars',
]).to.includes(field);
});
});

it('should have the vars object', () => {
const theme = extendTheme();
const keys = [
'radius',
'shadow',
'focus',
'fontFamily',
'fontSize',
'fontWeight',
'lineHeight',
'letterSpacing',
'palette',
'shadowRing',
'shadowChannel',
];

Object.keys(keys).forEach((key) => {
expect(theme[key]).to.deep.equal(theme.vars[key]);
});
});

it('should have joy default css var prefix', () => {
const theme = extendTheme();
expect(theme.cssVarPrefix).to.equal('joy');
expect(theme.typography.body1.fontSize).to.equal('var(--joy-fontSize-md)');
expect(theme.typography.body1.fontSize).to.equal('var(--joy-fontSize-md, 1rem)');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vars coming from extendTheme need to have the default values.

});

it('should have custom css var prefix', () => {
const theme = extendTheme({ cssVarPrefix: 'foo' });
expect(theme.cssVarPrefix).to.equal('foo');
expect(theme.typography.body1.fontSize).to.equal('var(--foo-fontSize-md)');
expect(theme.typography.body1.fontSize).to.equal('var(--foo-fontSize-md, 1rem)');
});

it('should have no css var prefix', () => {
const theme = extendTheme({ cssVarPrefix: '' });
expect(theme.cssVarPrefix).to.equal('');
expect(theme.typography.body1.fontSize).to.equal('var(--fontSize-md)');
expect(theme.typography.body1.fontSize).to.equal('var(--fontSize-md, 1rem)');
});

it('should accept custom fontSize value', () => {
const theme = extendTheme({ fontSize: { md: '2rem' } });
expect(theme.cssVarPrefix).to.equal('joy');
expect(theme.typography.body1.fontSize).to.equal('var(--joy-fontSize-md, 2rem)');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default values should respect the custom values from the theme input

});

it('should have custom --variant-borderWidth', () => {
Expand Down Expand Up @@ -133,6 +166,7 @@ describe('extendTheme', () => {
);

expect(styles).to.deep.equal({
// No default value as the CssVarsProvider is used
borderRadius: 'var(--joy-radius-md)',
});
});
Expand Down