Skip to content

Commit

Permalink
[material-ui][joy-ui] Move typography CSS variables to font (#42003)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp authored Apr 24, 2024
1 parent ab7b2f1 commit fc8fd64
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/mui-joy/src/styles/defaultTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('defaultTheme', () => {
'components',
'colorSchemes',
'focus',
'font',
'fontSize',
'fontFamily',
'fontWeight',
Expand Down
26 changes: 25 additions & 1 deletion packages/mui-joy/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('extendTheme', () => {
'colorSchemes',
'defaultColorScheme',
'focus',
'font',
'fontSize',
'fontFamily',
'fontWeight',
Expand Down Expand Up @@ -51,6 +52,7 @@ describe('extendTheme', () => {
'radius',
'shadow',
'focus',
'font',
'fontFamily',
'fontSize',
'fontWeight',
Expand Down Expand Up @@ -156,7 +158,7 @@ describe('extendTheme', () => {
describe('typography', () => {
it('produce typography token by default', () => {
const theme = extendTheme();
expect(Object.keys(theme.vars.typography)).to.deep.equal([
expect(Object.keys(theme.vars.font)).to.deep.equal([
'h1',
'h2',
'h3',
Expand All @@ -170,6 +172,28 @@ describe('extendTheme', () => {
'body-xs',
]);
});

it('access font vars', () => {
const theme = extendTheme();
expect(
theme.unstable_sx({
font: 'h1',
}),
).to.deep.equal({
font: 'var(--joy-font-h1, var(--joy-fontWeight-xl, 700) var(--joy-fontSize-xl4, 2.25rem)/var(--joy-lineHeight-xs, 1.33334) var(--joy-fontFamily-display, "Inter", var(--joy-fontFamily-fallback, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol")))',
});
});

it('use provided value if no font', () => {
const theme = extendTheme();
expect(
theme.unstable_sx({
font: 'var(--custom-font)',
}),
).to.deep.equal({
font: 'var(--custom-font)',
});
});
});

describe('theme.unstable_sx', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-joy/src/styles/extendTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
light: lightColorSystem,
dark: darkColorSystem,
},
font: {},
fontSize,
fontFamily,
fontWeight,
Expand Down Expand Up @@ -606,7 +607,7 @@ export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme {
cssVarPrefix,
getCssVar,
spacing: getSpacingVal(spacing),
typography: prepareTypographyVars(mergedScales.typography),
font: { ...prepareTypographyVars(mergedScales.typography), ...mergedScales.font },
} as unknown as Theme & { attribute: string; colorSchemeSelector: string }; // Need type casting due to module augmentation inside the repo
theme = cssContainerQueries(theme);

Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/styles/shouldSkipGeneratingVar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function shouldSkipGeneratingVar(keys: string[]) {
return (
!!keys[0].match(/^(variants|breakpoints)$/) ||
!!keys[0].match(/^(typography|variants|breakpoints)$/) ||
!!keys[0].match(/sxConfig$/) || // ends with sxConfig
(keys[0] === 'palette' && !!keys[1]?.match(/^(mode)$/)) ||
(keys[0] === 'focus' && keys[1] !== 'thickness')
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/styles/types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface ColorSystemVars extends Omit<ColorSystem, 'palette'> {
palette: Omit<ColorSystem['palette'], 'mode'>;
}
export interface ThemeVars extends ThemeScales, ColorSystemVars {
typography: ExtractTypographyTokens<TypographySystem>;
font: ExtractTypographyTokens<TypographySystem>;
}

export interface ThemeCssVarOverrides {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export interface CssVarsThemeOptions extends Omit<ThemeOptions, 'palette' | 'com

// should not include keys defined in `shouldSkipGeneratingVar` and have value typeof function
export interface ThemeVars {
font: ExtractTypographyTokens<Theme['typography']>;
palette: Omit<
ColorSystem['palette'],
| 'colorScheme'
Expand All @@ -337,7 +338,6 @@ export interface ThemeVars {
shape: Theme['shape'];
spacing: string;
zIndex: ZIndex;
typography: ExtractTypographyTokens<Theme['typography']>;
}

type Split<T, K extends keyof T = keyof T> = K extends string | number
Expand Down
3 changes: 1 addition & 2 deletions packages/mui-material/src/styles/experimental_extendTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default function extendTheme(options = {}, ...args) {
overlays: colorSchemesInput.dark?.overlays || defaultDarkOverlays,
},
},
typography: prepareTypographyVars(muiTheme.typography),
font: { ...prepareTypographyVars(muiTheme.typography), ...muiTheme.font },
spacing: getSpacingVal(input.spacing),
};

Expand Down Expand Up @@ -437,7 +437,6 @@ export default function extendTheme(options = {}, ...args) {
theme.getColorSchemeSelector = (colorScheme) => `[${theme.attribute}="${colorScheme}"] &`;
theme.spacing = theme.generateSpacing();
theme.shouldSkipGeneratingVar = shouldSkipGeneratingVar;
theme.typography = muiTheme.typography;
theme.unstable_sxConfig = {
...defaultSxConfig,
...input?.unstable_sxConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ describe('experimental_extendTheme', () => {
describe('typography', () => {
it('produce typography token by default', () => {
const theme = extendTheme();
expect(Object.keys(theme.vars.typography)).to.deep.equal([
expect(Object.keys(theme.vars.font)).to.deep.equal([
'h1',
'h2',
'h3',
Expand All @@ -428,6 +428,28 @@ describe('experimental_extendTheme', () => {
'inherit',
]);
});

it('access font vars', () => {
const theme = extendTheme();
expect(
theme.unstable_sx({
font: 'h1',
}),
).to.deep.equal({
font: 'var(--mui-font-h1, 300 6rem/1.167 "Roboto", "Helvetica", "Arial", sans-serif)',
});
});

it('use provided value if no font', () => {
const theme = extendTheme();
expect(
theme.unstable_sx({
font: 'var(--custom-font)',
}),
).to.deep.equal({
font: 'var(--custom-font)',
});
});
});

describe('container queries', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function shouldSkipGeneratingVar(keys: string[]) {
return (
!!keys[0].match(/(cssVarPrefix|mixins|breakpoints|direction|transitions)/) ||
!!keys[0].match(/(cssVarPrefix|typography|mixins|breakpoints|direction|transitions)/) ||
!!keys[0].match(/sxConfig$/) || // ends with sxConfig
(keys[0] === 'palette' && !!keys[1]?.match(/(mode|contrastThreshold|tonalOffset)/))
);
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-system/src/cssVars/prepareTypographyVars.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { createTheme } from '@mui/material/styles';
import prepareTypographyTokens from './prepareTypographyVars';
import prepareTypographyVars from './prepareTypographyVars';

describe('prepareTypographyVars', () => {
it('should prepare typography vars', () => {
const theme = createTheme();
expect(prepareTypographyTokens(theme.typography)).to.deep.equal({
expect(prepareTypographyVars(theme.typography)).to.deep.equal({
body1: '400 1rem/1.5 "Roboto", "Helvetica", "Arial", sans-serif',
body2: '400 0.875rem/1.43 "Roboto", "Helvetica", "Arial", sans-serif',
button: '500 0.875rem/1.75 "Roboto", "Helvetica", "Arial", sans-serif',
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-system/src/cssVars/prepareTypographyVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type RecordPropertyNames<T> = {

export type ExtractTypographyTokens<T> = { [K in RecordPropertyNames<T>]: string };

export default function prepareTypographyTokens<T extends Record<string, any>>(typography: T) {
export default function prepareTypographyVars<T extends Record<string, any>>(typography: T) {
const vars: Record<string, string | number> = {};
const entries = Object.entries(typography);
entries.forEach((entry) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/mui-system/src/styleFunctionSx/defaultSxConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ const defaultSxConfig = {
boxSizing: {},

// typography
font: {
themeKey: 'font',
},
fontFamily: {
themeKey: 'typography',
},
Expand Down

0 comments on commit fc8fd64

Please sign in to comment.