Skip to content

Instantly share code, notes, and snippets.

@jottenlips
Last active March 22, 2024 15:03
Show Gist options
  • Save jottenlips/c96b3023f8512ba49edab440d3326b17 to your computer and use it in GitHub Desktop.
Save jottenlips/c96b3023f8512ba49edab440d3326b17 to your computer and use it in GitHub Desktop.
easier replacement for the deprecated mui-styles library
// 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