To define the type of prop used in the styled component of material UI, you can use TypeScript (if it is already set up in your project) and define the prop type in the component interface. Here's an example:
import { styled } from '@mui/material/styles';
interface ButtonProps {
variant: 'contained' | 'outlined';
color?: 'primary' | 'secondary';
size?: 'small' | 'medium' | 'large';
}
const Button = styled('button')<ButtonProps>(
({ theme, variant, color, size }) => ({
color: theme.palette[color || 'primary'].contrastText,
backgroundColor: theme.palette[color || 'primary'][variant],
padding: theme.spacing(1, 2),
fontSize: size === 'small' ? 14 : (size === 'medium' ? 16 : 18),
// additional styles ...
})
);
export default Button;
In the above example, we define the ButtonProps
interface which has variant
, color
, and size
properties, each with a specific set of possible values. Then, we pass this interface as a type parameter to the styled
method and use it in the component function as a parameter. This allows us to access the properties in the theme object and apply different styles based on their values.
Asked: 2023-02-17 11:00:00 +0000
Seen: 14 times
Last updated: Nov 29 '22