Last active
March 22, 2024 15:03
-
-
Save jottenlips/c96b3023f8512ba49edab440d3326b17 to your computer and use it in GitHub Desktop.
easier replacement for the deprecated mui-styles library
This file contains hidden or 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
// this code is to make it easier to replace the deprecated mui-styles library, almost drop in | |
import { SxProps } from '@mui/material' | |
import { useMemo } from 'react' | |
type TStylesFunction = ( | |
props?: any, | |
) => Record<string, React.CSSProperties | SxProps> | |
type TStyles = Record<string, React.CSSProperties | SxProps> | |
export const makeStylesHook = (styles: TStylesFunction | TStyles) => { | |
// useStyles returned like deprecated MUI makeStyles | |
const useStyles = | |
typeof styles === 'function' | |
? (props?: any) => { | |
return useMemo(() => styles(props), [props]) | |
} | |
: (_props?: any) => { | |
return useMemo(() => styles, []) | |
} | |
return useStyles | |
} | |
// use | |
// const useStyles = makeStylesHook((props) => ({ box: { backgroundColor: "green"}})) | |
//... | |
// const Component = () => { | |
// const classes = useStyles() | |
// return <Box sx={classes.box} /> | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment