Created
October 5, 2022 22:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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