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

[system] Move stylesheet generator to extendTheme #41446

Merged
merged 44 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
cd9611f
wip
siriwatknp Feb 20, 2024
713706f
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Feb 28, 2024
3496e02
move excludeVariablesFromRoot logic to extendTheme
siriwatknp Feb 28, 2024
8c8a05e
add generateStyleSheets
siriwatknp Feb 29, 2024
cd20c72
use generateStyleSheets in zero
siriwatknp Feb 29, 2024
6d44f8d
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 11, 2024
01da8e7
replace generateCssVars with generateThemeVars
siriwatknp Mar 11, 2024
9835ba4
add more tests
siriwatknp Mar 11, 2024
c03b060
cleanup
siriwatknp Mar 11, 2024
02fe7cc
remove workaround
siriwatknp Mar 11, 2024
049ae90
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 11, 2024
8df84b1
finish consistency
siriwatknp Mar 11, 2024
b47cb75
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 11, 2024
087806c
fix test
siriwatknp Mar 11, 2024
e70a9b4
fix all regression
siriwatknp Mar 11, 2024
9cecf5c
fix scoped CSS baseline
siriwatknp Mar 11, 2024
71ea18c
fix types
siriwatknp Mar 11, 2024
284ebb9
use data-* attribute
siriwatknp Mar 12, 2024
fa4251c
run docs:typescript:formatted
siriwatknp Mar 12, 2024
0fc887d
restore changes
siriwatknp Mar 12, 2024
6f2a7c2
fix test
siriwatknp Mar 12, 2024
5e609df
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 12, 2024
4a77f79
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 12, 2024
42c2b43
fix createCssVarsTheme
siriwatknp Mar 12, 2024
ee261f5
fix prepareCssVars to get fresh css
siriwatknp Mar 12, 2024
3c13b45
fix extendTheme
siriwatknp Mar 12, 2024
39e2df4
move getSelector to private function
siriwatknp Mar 12, 2024
8145253
update createCssVarsTheme
siriwatknp Mar 12, 2024
a38fcbd
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 12, 2024
c9f14e4
attach all properties of default color scheme
siriwatknp Mar 12, 2024
56aaf2f
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 12, 2024
1bef6e9
fix md3 extendTheme
siriwatknp Mar 12, 2024
b57f622
fix test
siriwatknp Mar 12, 2024
fff2ef3
fix tests
siriwatknp Mar 12, 2024
cd54f59
trigger build
siriwatknp Mar 13, 2024
858da1c
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 13, 2024
0eacbdc
Update packages/mui-system/src/cssVars/createCssVarsProvider.js
siriwatknp Mar 13, 2024
cc6b678
Update packages/mui-system/src/cssVars/createCssVarsProvider.test.js
siriwatknp Mar 13, 2024
b9ded68
Update packages/mui-system/src/cssVars/createCssVarsProvider.test.js
siriwatknp Mar 13, 2024
a3eaebc
Merge branch 'master' of https://github.com/mui/material-ui into mate…
siriwatknp Mar 13, 2024
b167fa0
fix getSelector value
siriwatknp Mar 13, 2024
0bba432
add tests for utils
siriwatknp Mar 13, 2024
f090bc1
Merge branch 'next' of https://github.com/mui/material-ui into materi…
siriwatknp Mar 20, 2024
bb8759f
remove unnecessary private export
siriwatknp Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions apps/pigment-css-next-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,20 @@ const theme = extendTheme({
},
},
},
getSelector: function getSelector(colorScheme, css) {
if (colorScheme && colorScheme === 'dark') {
return {
'@media (prefers-color-scheme: dark)': {
':root': css,
},
};
}
return ':root';
},
});

// TODO: Fix this from the Material UI side in a separate PR
theme.palette = theme.colorSchemes.light.palette;
theme.getColorSchemeSelector = (colorScheme) => {
return `@media (prefers-color-scheme: ${colorScheme})`;
};
const { css: rootCss } = theme.generateCssVars();
const { css: lightCss } = theme.generateCssVars('light');
const { css: darkCss } = theme.generateCssVars('dark');
theme.generateCssVars = (colorScheme) => {
if (colorScheme === 'dark') {
return {
css: darkCss,
selector: {
'@media (prefers-color-scheme: dark)': {
':root': darkCss,
},
},
};
}
if (colorScheme === 'light') {
return { css: lightCss, selector: ':root' };
}
return { css: rootCss, selector: ':root' };
};

/**
* @type {PigmentOptions}
Expand Down
35 changes: 12 additions & 23 deletions apps/pigment-css-vite-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,20 @@ import Pages from 'vite-plugin-pages';
import { pigment } from '@pigment-css/vite-plugin';
import { experimental_extendTheme as extendTheme } from '@mui/material/styles';

const theme = extendTheme();

// TODO: Fix this from the Material UI side in a separate PR
theme.palette = theme.colorSchemes.light.palette;
theme.getColorSchemeSelector = (colorScheme) => {
return `@media (prefers-color-scheme: ${colorScheme})`;
};
const { css: rootCss } = theme.generateCssVars();
const { css: lightCss } = theme.generateCssVars('light');
const { css: darkCss } = theme.generateCssVars('dark');
theme.generateCssVars = (colorScheme) => {
if (colorScheme === 'dark') {
return {
css: darkCss,
selector: {
const theme = extendTheme({
getSelector: function getSelector(colorScheme, css) {
if (colorScheme && colorScheme === 'dark') {
return {
'@media (prefers-color-scheme: dark)': {
':root': darkCss,
':root': css,
},
},
};
}
if (colorScheme === 'light') {
return { css: lightCss, selector: ':root' };
}
return { css: rootCss, selector: ':root' };
};
}
return ':root';
},
});
theme.getColorSchemeSelector = (colorScheme) => {
return `@media (prefers-color-scheme: ${colorScheme})`;
};

export default defineConfig({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const darkColorScheme = {
};

function extendTheme({ cssVarPrefix = 'system-demo' } = {}) {
const { vars: themeVars, generateCssVars } = prepareCssVars(
const { vars: themeVars, ...params } = prepareCssVars(
{
colorSchemes: {
light: lightColorScheme,
Expand All @@ -54,11 +54,11 @@ function extendTheme({ cssVarPrefix = 'system-demo' } = {}) {
// ... any other objects independent of color-scheme,
// like fontSizes, spacing etc
vars: themeVars,
generateCssVars,
palette: {
...lightColorScheme.palette,
colorScheme: 'light',
},
...params,
};

return theme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ type Theme = {
palette: {
colorScheme: 'light' | 'dark';
} & (typeof lightColorScheme)['palette'];
vars: ReturnType<typeof prepareCssVars>['vars'];
generateCssVars: ReturnType<typeof prepareCssVars>['generateCssVars'];
};
} & ReturnType<typeof prepareCssVars>;

const lightColorScheme = {
palette: {
Expand Down Expand Up @@ -47,7 +45,7 @@ const darkColorScheme = {
};

function extendTheme({ cssVarPrefix = 'system-demo' } = {}) {
const { vars: themeVars, generateCssVars } = prepareCssVars(
const { vars: themeVars, ...params } = prepareCssVars(
{
colorSchemes: {
light: lightColorScheme,
Expand All @@ -66,11 +64,11 @@ function extendTheme({ cssVarPrefix = 'system-demo' } = {}) {
// ... any other objects independent of color-scheme,
// like fontSizes, spacing etc
vars: themeVars,
generateCssVars,
palette: {
...lightColorScheme.palette,
colorScheme: 'light',
},
...params,
};

return theme;
Expand Down
7 changes: 6 additions & 1 deletion packages/mui-joy/src/styles/defaultTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ describe('defaultTheme', () => {
it('the output contains required fields', () => {
Object.keys(defaultTheme).forEach((field) => {
expect([
'attribute',
'colorSchemeSelector',
'defaultColorScheme',
'breakpoints',
'components',
'colorSchemes',
Expand All @@ -17,6 +20,7 @@ describe('defaultTheme', () => {
'palette',
'shadowRing',
'shadowChannel',
'shadowOpacity',
'getCssVar',
'spacing',
'radius',
Expand All @@ -30,7 +34,8 @@ describe('defaultTheme', () => {
'unstable_sxConfig',
'unstable_sx',
'shouldSkipGeneratingVar',
'generateCssVars',
'generateStyleSheets',
'generateThemeVars',
'applyStyles',
]).to.includes(field);
});
Expand Down
9 changes: 8 additions & 1 deletion packages/mui-joy/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ describe('extendTheme', () => {
const result = extendTheme();
Object.keys(result).forEach((field) => {
expect([
'attribute',
'breakpoints',
'colorSchemeSelector',
'components',
'colorSchemes',
'defaultColorScheme',
'focus',
'fontSize',
'fontFamily',
Expand All @@ -20,6 +23,9 @@ describe('extendTheme', () => {
'spacing',
'radius',
'shadow',
'shadowRing',
'shadowChannel',
'shadowOpacity',
'zIndex',
'typography',
'variants',
Expand All @@ -30,7 +36,8 @@ describe('extendTheme', () => {
'unstable_sxConfig',
'unstable_sx',
'shouldSkipGeneratingVar',
'generateCssVars',
'generateStyleSheets',
'generateThemeVars',
'applyStyles',
]).to.includes(field);
});
Expand Down
52 changes: 45 additions & 7 deletions packages/mui-joy/src/styles/extendTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ export interface CssVarsThemeOptions extends Partial2Level<ThemeScalesOptions> {
* value = 'var(--test)'
*/
shouldSkipGeneratingVar?: (keys: string[], value: string | number) => boolean;
/**
* If provided, it will be used to create a selector for the color scheme.
* This is useful if you want to use class or data-* attributes to apply the color scheme.
*
* The callback receives the colorScheme with the possible values of:
* - undefined: the selector for tokens that are not color scheme dependent
* - string: the selector for the color scheme
*
* @example
* // class selector
* (colorScheme) => colorScheme !== 'light' ? `.theme-${colorScheme}` : ":root"
*
* @example
* // data-* attribute selector
* (colorScheme) => colorScheme !== 'light' ? `[data-theme="${colorScheme}"`] : ":root"
*/
getSelector?: (
colorScheme: SupportedColorScheme | undefined,
css: Record<string, any>,
) => string | Record<string, any>;
}

export const createGetCssVar = (cssVarPrefix = 'joy') =>
Expand All @@ -94,6 +114,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
components: componentsInput,
variants: variantsInput,
shouldSkipGeneratingVar = defaultShouldSkipGeneratingVar,
getSelector,
...scalesInput
} = themeOptions || {};
const getCssVar = createGetCssVar(cssVarPrefix);
Expand Down Expand Up @@ -523,6 +544,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {

const theme = {
colorSchemes,
defaultColorScheme: 'light',
...mergedScales,
breakpoints: createBreakpoints(breakpoints ?? {}),
components: deepmerge(
Expand Down Expand Up @@ -565,7 +587,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
cssVarPrefix,
getCssVar,
spacing: createSpacing(spacing),
} as unknown as Theme; // Need type casting due to module augmentation inside the repo
} as unknown as Theme & { attribute: string; colorSchemeSelector: string }; // Need type casting due to module augmentation inside the repo

/**
Color channels generation
Expand Down Expand Up @@ -610,15 +632,29 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
const parserConfig = {
prefix: cssVarPrefix,
shouldSkipGeneratingVar,
getSelector:
getSelector ||
((colorScheme) => {
if (theme.defaultColorScheme === colorScheme) {
return `${theme.colorSchemeSelector}, [${theme.attribute}="${colorScheme}"]`;
}
if (colorScheme) {
return `[${theme.attribute}="${colorScheme}"]`;
}
return theme.colorSchemeSelector;
}),
};

const { vars: themeVars, generateCssVars } = prepareCssVars<Theme, ThemeVars>(
const { vars, generateThemeVars, generateStyleSheets } = prepareCssVars<Theme, ThemeVars>(
// @ts-ignore property truDark is missing from colorSchemes
{ colorSchemes, ...mergedScales },
parserConfig,
);
theme.vars = themeVars;
theme.generateCssVars = generateCssVars;
theme.attribute = 'data-joy-color-scheme';
theme.colorSchemeSelector = ':root';
theme.vars = vars;
theme.generateThemeVars = generateThemeVars;
theme.generateStyleSheets = generateStyleSheets;
theme.unstable_sxConfig = {
...defaultSxConfig,
...themeOptions?.unstable_sxConfig,
Expand All @@ -630,9 +666,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
});
};
theme.getColorSchemeSelector = (colorScheme: SupportedColorScheme) =>
colorScheme === 'light'
? '&'
: `&[data-joy-color-scheme="${colorScheme}"], [data-joy-color-scheme="${colorScheme}"] &`;
`[${theme.attribute}="${colorScheme}"] &`;

const createVariantInput = { getCssVar, palette: theme.colorSchemes.light.palette };
theme.variants = deepmerge(
Expand All @@ -657,6 +691,10 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
variantsInput,
);

Object.entries(theme.colorSchemes[theme.defaultColorScheme]).forEach(([key, value]) => {
// @ts-ignore
theme[key] = value;
});
theme.palette = {
...theme.colorSchemes.light.palette,
colorScheme: 'light',
Expand Down
7 changes: 3 additions & 4 deletions packages/mui-joy/src/styles/types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export type ThemeCssVar = OverridableStringUnion<NormalizeVars<ThemeVars>, Theme

export interface Theme extends ThemeScales, RuntimeColorSystem {
colorSchemes: Record<DefaultColorScheme | ExtendedColorScheme, ColorSystem>;
defaultColorScheme: DefaultColorScheme | ExtendedColorScheme;
focus: Focus;
typography: TypographySystem;
variants: Variants;
Expand All @@ -106,10 +107,8 @@ export interface Theme extends ThemeScales, RuntimeColorSystem {
vars: ThemeVars;
getCssVar: (field: ThemeCssVar, ...vars: ThemeCssVar[]) => string;
getColorSchemeSelector: (colorScheme: DefaultColorScheme | ExtendedColorScheme) => string;
generateCssVars: (colorScheme?: DefaultColorScheme | ExtendedColorScheme) => {
css: Record<string, string | number>;
vars: ThemeVars;
};
generateThemeVars: () => ThemeVars;
generateStyleSheets: () => Record<string, any>[];
/**
* A function to determine if the key, value should be attached as CSS Variable
* `keys` is an array that represents the object path keys.
Expand Down
2 changes: 0 additions & 2 deletions packages/mui-material-next/src/styles/CssVarsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
THEME_ID,
SupportedColorScheme,
private_createTypography as createTypography,
private_excludeVariablesFromRoot as excludeVariablesFromRoot,
} from '@mui/material/styles';
import { Theme } from './Theme.types';
import defaultTheme from './defaultTheme';
Expand Down Expand Up @@ -41,7 +40,6 @@ const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssV
};
return newTheme;
},
excludeVariablesFromRoot,
});

export { useColorScheme, getInitColorSchemeScript, CssVarsProvider };
4 changes: 0 additions & 4 deletions packages/mui-material-next/src/styles/Theme.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,6 @@ export interface Theme extends Omit<MD2Theme, 'vars'> {
shape: MD3Shape;
};
};
generateCssVars: (colorScheme?: SupportedColorScheme) => {
css: Record<string, string | number>;
vars: Theme['vars'];
};
}

export type SxProps = SystemSxProps<Theme>;
Loading
Loading