diff --git a/packages/mui-joy/src/styles/CssVarsProvider.tsx b/packages/mui-joy/src/styles/CssVarsProvider.tsx index 562f20cc9371c9..48d405cd6bbc41 100644 --- a/packages/mui-joy/src/styles/CssVarsProvider.tsx +++ b/packages/mui-joy/src/styles/CssVarsProvider.tsx @@ -1,13 +1,12 @@ import { unstable_createCssVarsProvider as createCssVarsProvider } from '@mui/system'; import extendTheme from './extendTheme'; -import type { Theme, DefaultColorScheme, ExtendedColorScheme } from './types'; +import type { DefaultColorScheme, ExtendedColorScheme } from './types'; const shouldSkipGeneratingVar = (keys: string[]) => !!keys[0].match(/(typography|variants|focus|breakpoints)/); const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssVarsProvider< - DefaultColorScheme | ExtendedColorScheme, - Theme + DefaultColorScheme | ExtendedColorScheme >({ theme: extendTheme(), attribute: 'data-joy-color-scheme', diff --git a/packages/mui-joy/src/styles/ThemeProvider.tsx b/packages/mui-joy/src/styles/ThemeProvider.tsx index 56d2a78241c950..76df36c94ff756 100644 --- a/packages/mui-joy/src/styles/ThemeProvider.tsx +++ b/packages/mui-joy/src/styles/ThemeProvider.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { ThemeProvider as SystemThemeProvider, useTheme as useSystemTheme } from '@mui/system'; import defaultTheme, { getThemeWithVars } from './defaultTheme'; -import type { ThemeInput } from './extendTheme'; +import type { CssVarsThemeOptions } from './extendTheme'; export const useTheme = () => { return useSystemTheme(defaultTheme); @@ -11,7 +11,7 @@ export default function ThemeProvider({ children, theme: themeInput, }: React.PropsWithChildren<{ - theme?: ThemeInput; + theme?: CssVarsThemeOptions; }>) { return ( diff --git a/packages/mui-joy/src/styles/defaultTheme.ts b/packages/mui-joy/src/styles/defaultTheme.ts index 522c9d76c435b2..5f162c2b539d71 100644 --- a/packages/mui-joy/src/styles/defaultTheme.ts +++ b/packages/mui-joy/src/styles/defaultTheme.ts @@ -1,10 +1,10 @@ import { deepmerge } from '@mui/utils'; import extendTheme from './extendTheme'; -import type { ThemeInput, ColorSystemInput } from './extendTheme'; +import type { CssVarsThemeOptions, ColorSystemOptions } from './extendTheme'; import type { Theme, RuntimeColorSystem } from './types'; export const getThemeWithVars = ( - themeInput?: Omit & ColorSystemInput, + themeInput?: Omit & ColorSystemOptions, ) => { const { colorSchemes, diff --git a/packages/mui-joy/src/styles/extendTheme.ts b/packages/mui-joy/src/styles/extendTheme.ts index b9891e83b2e514..21b12a2cec51dc 100644 --- a/packages/mui-joy/src/styles/extendTheme.ts +++ b/packages/mui-joy/src/styles/extendTheme.ts @@ -36,10 +36,10 @@ type Partial3Level = { }; }; -export interface ColorSystemInput extends Partial3Level {} +export interface ColorSystemOptions extends Partial3Level {} // Use Partial2Level instead of PartialDeep because nested value type is CSSObject which does not work with PartialDeep. -export interface ThemeInput extends Partial2Level { +export interface CssVarsThemeOptions extends Partial2Level { /** * Prefix of the generated CSS variables * @default 'joy' @@ -57,13 +57,13 @@ export interface ThemeInput extends Partial2Level { breakpoints?: BreakpointsOptions; spacing?: SpacingOptions; components?: Components; - colorSchemes?: Partial>; + colorSchemes?: Partial>; } export const createGetCssVar = (cssVarPrefix = 'joy') => systemCreateGetCssVar(cssVarPrefix); -export default function extendTheme(themeInput?: ThemeInput): Theme { +export default function extendTheme(themeOptions?: CssVarsThemeOptions): Theme { const { cssVarPrefix = 'joy', breakpoints, @@ -71,7 +71,7 @@ export default function extendTheme(themeInput?: ThemeInput): Theme { components: componentsInput, variants: variantsInput, ...scalesInput - } = themeInput || {}; + } = themeOptions || {}; const getCssVar = createGetCssVar(cssVarPrefix); const createLightModeVariantVariables = (color: ColorPaletteProp) => ({ diff --git a/packages/mui-joy/src/styles/index.ts b/packages/mui-joy/src/styles/index.ts index afdfd662e7a20b..eaffa35986948e 100644 --- a/packages/mui-joy/src/styles/index.ts +++ b/packages/mui-joy/src/styles/index.ts @@ -63,5 +63,5 @@ export * from './ThemeProvider'; export { default as useThemeProps } from './useThemeProps'; export { sx as experimental_sx } from './styleFunctionSx'; export { default as extendTheme, createGetCssVar } from './extendTheme'; -export type { ThemeInput } from './extendTheme'; +export type { CssVarsThemeOptions } from './extendTheme'; export { default as StyledEngineProvider } from './StyledEngineProvider'; diff --git a/packages/mui-material/src/styles/CssVarsProvider.tsx b/packages/mui-material/src/styles/CssVarsProvider.tsx index 02dd6108470b57..c2b10da4b4894b 100644 --- a/packages/mui-material/src/styles/CssVarsProvider.tsx +++ b/packages/mui-material/src/styles/CssVarsProvider.tsx @@ -1,8 +1,5 @@ import { unstable_createCssVarsProvider as createCssVarsProvider } from '@mui/system'; -import experimental_extendTheme, { - SupportedColorScheme, - CssVarsTheme, -} from './experimental_extendTheme'; +import experimental_extendTheme, { SupportedColorScheme } from './experimental_extendTheme'; import createTypography from './createTypography'; const shouldSkipGeneratingVar = (keys: string[]) => @@ -11,28 +8,26 @@ const shouldSkipGeneratingVar = (keys: string[]) => const defaultTheme = experimental_extendTheme(); -const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = createCssVarsProvider< - SupportedColorScheme, - CssVarsTheme ->({ - theme: defaultTheme, - attribute: 'data-mui-color-scheme', - modeStorageKey: 'mui-mode', - colorSchemeStorageKey: 'mui-color-scheme', - defaultColorScheme: { - light: 'light', - dark: 'dark', - }, - resolveTheme: (theme) => { - const newTheme = { - ...theme, - typography: createTypography(theme.palette, theme.typography), - }; +const { CssVarsProvider, useColorScheme, getInitColorSchemeScript } = + createCssVarsProvider({ + theme: defaultTheme, + attribute: 'data-mui-color-scheme', + modeStorageKey: 'mui-mode', + colorSchemeStorageKey: 'mui-color-scheme', + defaultColorScheme: { + light: 'light', + dark: 'dark', + }, + resolveTheme: (theme) => { + const newTheme = { + ...theme, + typography: createTypography(theme.palette, theme.typography), + }; - return newTheme; - }, - shouldSkipGeneratingVar, -}); + return newTheme; + }, + shouldSkipGeneratingVar, + }); export { useColorScheme, diff --git a/packages/mui-material/src/styles/experimental_extendTheme.d.ts b/packages/mui-material/src/styles/experimental_extendTheme.d.ts index cdb7937625492a..57de1a649f69ee 100644 --- a/packages/mui-material/src/styles/experimental_extendTheme.d.ts +++ b/packages/mui-material/src/styles/experimental_extendTheme.d.ts @@ -157,6 +157,7 @@ export interface PaletteSlider { export interface PaletteSnackbarContent { bg: string; + color: string; } export interface PaletteSpeedDialAction { @@ -222,35 +223,36 @@ export interface ColorSystemOptions { overlays?: Overlays; } -// The Palette should be sync with `../themeCssVarsAugmentation/index.d.ts` +export interface CssVarsPalette { + colorScheme: SupportedColorScheme; + common: PaletteCommonChannel; + primary: PaletteColorChannel; + secondary: PaletteColorChannel; + error: PaletteColorChannel; + info: PaletteColorChannel; + success: PaletteColorChannel; + text: PaletteTextChannel; + dividerChannel: string; + action: PaletteActionChannel; + Alert: PaletteAlert; + AppBar: PaletteAppBar; + Avatar: PaletteAvatar; + Chip: PaletteChip; + FilledInput: PaletteFilledInput; + LinearProgress: PaletteLinearProgress; + Skeleton: PaletteSkeleton; + Slider: PaletteSlider; + SnackbarContent: PaletteSnackbarContent; + SpeedDialAction: PaletteSpeedDialAction; + StepConnector: PaletteStepConnector; + StepContent: PaletteStepContent; + Switch: PaletteSwitch; + TableCell: PaletteTableCell; + Tooltip: PaletteTooltip; +} + export interface ColorSystem { - palette: Palette & { - colorScheme: SupportedColorScheme; - common: PaletteCommonChannel; - primary: PaletteColorChannel; - secondary: PaletteColorChannel; - error: PaletteColorChannel; - info: PaletteColorChannel; - success: PaletteColorChannel; - text: PaletteTextChannel; - dividerChannel: string; - action: PaletteActionChannel; - Alert: PaletteAlert; - AppBar: PaletteAppBar; - Avatar: PaletteAvatar; - Chip: PaletteChip; - FilledInput: PaletteFilledInput; - LinearProgress: PaletteLinearProgress; - Skeleton: PaletteSkeleton; - Slider: PaletteSlider; - SnackbarContent: PaletteSnackbarContent; - SpeedDialAction: PaletteSpeedDialAction; - StepConnector: PaletteStepConnector; - StepContent: PaletteStepContent; - Switch: PaletteSwitch; - TableCell: PaletteTableCell; - Tooltip: PaletteTooltip; - }; + palette: Palette & CssVarsPalette; opacity: Opacity; overlays: Overlays; } @@ -261,7 +263,13 @@ export interface CssVarsThemeOptions extends Omit>; + /** + * Theme components + */ + components?: Components & CssVarsTheme>; + /** + * Color schemes configuration + */ colorSchemes?: Partial>; } @@ -286,8 +294,10 @@ interface ThemeVars { // shut off automatic exporting for the `ThemeVars` above export {}; -export interface CssVarsTheme extends Omit, ColorSystem { - components?: Components>; +/** + * Theme properties generated by extendTheme and CssVarsProvider + */ +export interface CssVarsTheme extends ColorSystem { colorSchemes: Record; cssVarPrefix: string; vars: ThemeVars; @@ -307,4 +317,4 @@ export interface CssVarsTheme extends Omit, Col export default function experimental_extendTheme( options?: CssVarsThemeOptions, ...args: object[] -): CssVarsTheme; +): Omit & CssVarsTheme; diff --git a/packages/mui-material/src/themeCssVarsAugmentation/index.d.ts b/packages/mui-material/src/themeCssVarsAugmentation/index.d.ts index 6941f947756341..9d7bfe596ee70b 100644 --- a/packages/mui-material/src/themeCssVarsAugmentation/index.d.ts +++ b/packages/mui-material/src/themeCssVarsAugmentation/index.d.ts @@ -1,22 +1,10 @@ import type { CssVarsTheme, - ColorSystem, + CssVarsPalette, PaletteCommonChannel, PaletteColorChannel, PaletteTextChannel, PaletteActionChannel, - PaletteAppBar, - PaletteAvatar, - PaletteChip, - PaletteFilledInput, - PaletteLinearProgress, - PaletteSlider, - PaletteSnackbarContent, - PaletteStepConnector, - PaletteStepContent, - PaletteSwitch, - PaletteTableCell, - PaletteTooltip, } from '../styles/experimental_extendTheme'; /** @@ -25,29 +13,10 @@ import type { */ declare module '@mui/material/styles' { // The palette must be extended in each node. - interface Theme extends Omit { - prefix: string; - vars: CssVarsTheme['vars']; - getCssVar: CssVarsTheme['getCssVar']; - getColorSchemeSelector: CssVarsTheme['getColorSchemeSelector']; - } + interface Theme extends Omit {} // The extended Palette should be in sync with `extendTheme` - interface Palette { - dividerChannel: string; - AppBar: PaletteAppBar; - Avatar: PaletteAvatar; - Chip: PaletteChip; - FilledInput: PaletteFilledInput; - LinearProgress: PaletteLinearProgress; - Slider: PaletteSlider; - SnackbarContent: PaletteSnackbarContent; - StepConnector: PaletteStepConnector; - StepContent: PaletteStepContent; - Switch: PaletteSwitch; - TableCell: PaletteTableCell; - Tooltip: PaletteTooltip; - } + interface Palette extends CssVarsPalette {} } declare module '@mui/material/styles/createPalette' { diff --git a/packages/mui-system/src/cssVars/createCssVarsProvider.d.ts b/packages/mui-system/src/cssVars/createCssVarsProvider.d.ts index 83162cc2398832..af76711d05982e 100644 --- a/packages/mui-system/src/cssVars/createCssVarsProvider.d.ts +++ b/packages/mui-system/src/cssVars/createCssVarsProvider.d.ts @@ -54,11 +54,14 @@ export interface CssVarsProviderConfig { shouldSkipGeneratingVar?: (keys: string[], value: string | number) => boolean; } -export interface CreateCssVarsProviderResult { +export interface CreateCssVarsProviderResult { CssVarsProvider: ( props: React.PropsWithChildren< Partial> & { - theme?: ThemeInput; + theme?: { + cssVarPrefix?: string; + colorSchemes: Record>; + }; /** * The document used to perform `disableTransitionOnChange` feature * @default document @@ -86,10 +89,7 @@ export interface CreateCssVarsProviderResult> }, ->( +export default function createCssVarsProvider( options: CssVarsProviderConfig & { /** * Design system default theme @@ -125,7 +125,7 @@ export default function createCssVarsProvider< */ resolveTheme?: (theme: any) => any; // the type is any because it depends on the design system. }, -): CreateCssVarsProviderResult; +): CreateCssVarsProviderResult; // disable automatic export export {}; diff --git a/packages/mui-system/src/cssVars/createCssVarsProvider.spec.tsx b/packages/mui-system/src/cssVars/createCssVarsProvider.spec.tsx index 16d622c9fc5d77..f4e964ed0bfeed 100644 --- a/packages/mui-system/src/cssVars/createCssVarsProvider.spec.tsx +++ b/packages/mui-system/src/cssVars/createCssVarsProvider.spec.tsx @@ -9,12 +9,8 @@ type JoyExtendedColorScheme = OverridableStringUnion - >; + colorSchemes?: Partial>; + fontSize?: { md?: string }; } // Simulate color scheme extending, same as module augmentation in real application @@ -23,7 +19,19 @@ interface JoyColorSchemeOverrides { trueDark: true; } -const { CssVarsProvider, useColorScheme } = createCssVarsProvider({ +const extendTheme = (themeInput: JoyThemeInput) => + ({ + colorSchemes: { + light: { palette: { primary: { main: '' } } }, + dark: { palette: { primary: { main: '' } } }, + }, + fontSize: { md: '' }, + } as { + colorSchemes: Record; + fontSize: { md: string }; + }); + +const { CssVarsProvider, useColorScheme } = createCssVarsProvider({ defaultColorScheme: 'light', theme: { fontSize: { @@ -56,14 +64,14 @@ function Content() { function App() { return ( ); } @@ -71,14 +79,14 @@ function App() { function App2() { return (