Skip to content

Instantly share code, notes, and snippets.

@nautilytics
Created October 5, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nautilytics/b0485d2d6454235001d94846e2b60286 to your computer and use it in GitHub Desktop.
Save nautilytics/b0485d2d6454235001d94846e2b60286 to your computer and use it in GitHub Desktop.
An example of extending the MUI style utility functions and a custom theme
import {createStyled} from '@mui/system';
import {useTheme} from '@mui/material';
import type { MuiCustomThemeT } from './types';
export const muiStyled = createStyled<MuiCustomThemeT>();
export const muiUseTheme = useTheme<MuiCustomThemeT>();
import type { MuiCustomThemeT, TokenCornerRadiusT } from './types';
const tokens = {
"corner-radius": {
default: "8px"
},
};
const reduceTokens = <T>(tokens: Record<string, string>): T => {
return {
...(Object.entries(tokens).reduce(
(acc, [k, v]) => ({
...acc,
[k]: v
}),
{}
) as T)
};
};
const getTokensBase = (): Pick<MuiCustomThemeT, 'cornerRadius'> => {
return {
cornerRadius: reduceTokens<TokenCornerRadiusT>(tokens['corner-radius'])
};
};
export const MuiTheme = createTheme({
palette: {
mode: 'light'
},
...getTokensBase(),
colors: getLightColors()
});
import type { Theme as MuiTheme } from '@mui/material';
export type TokenCornerRadiusT = {
default: string;
};
export type MuiCustomThemeT = MuiTheme & {
cornerRadius: TokenCornerRadiusT;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment