Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUI styled cannot pass component prop to Typography in typescript

Using MUI with typescript and want to use styled from MUI as well. When passing the component prop to the styled component I get an error from the typescript sandbox below, maybe anyone knows a workaround.

https://codesandbox.io/s/material-demo-forked-lxdrj?file=/demo.tsx

like image 931
Zygimantas Avatar asked Feb 26 '21 15:02

Zygimantas


People also ask

How do I pass props to Mui V5 styles?

Here's 2 full working examples of how to pass props to MUI v5 styles. Either using css or javascript object syntax. Note that we define MyStyledComponent within MyComponent, making the scoped props available to use in the template string of the styled () function.

What is the minimum version of typescript required for Mui?

MUI requires a minimum version of TypeScript 3.5. Have a look at the Create React App with TypeScript example. For types to work, it's recommended that you have at least the following options enabled in your tsconfig.json:

How do I style Mui components in Mui?

All the MUI components are styled with this styled () utility. This utility is built on top of the styled () module of @mui/styled-engine and provides additional features. You can use the utility coming from the @mui/system package, or if you are using @mui/material, you can import it from @mui/material/styles .

What is shouldforwardprop in Mui?

It adds support for the the sx prop (can be skipped). It adds by default the shouldForwardProp option (that can be overridden), taking into account all props used internally in the MUI components: ownerState, theme, sx, and as. Component: The component that will be wrapped.


2 Answers

You have to manually add the 'component' prop.

From https://material-ui.com/guides/typescript/#usage-of-component-prop

import React from "react";
import type { TypographyProps } from "@material-ui/core";
import { styled } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";

type MyT = React.ComponentType<TypographyProps<"span", { component: "span" }>>;

const MyTypography: MyT = styled(Typography)({
  background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
  border: 0,
  borderRadius: 3,
  boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
  color: "white",
  height: 48,
  padding: "0 30px"
});

export default function StyledComponents() {
  return (
    <MyTypography component="span">Styled with styled-components API</MyTypography>
  );
}
like image 66
GitGitBoom Avatar answered Sep 28 '22 02:09

GitGitBoom


You can use generic type parameter in HOC created by styled:

import { styled } from '@mui/material/styles';
import Typography from '@mui/material/Typography';

type ExtraProps = {
  component: React.ElementType;
};
const MyTypography = styled(Typography)<ExtraProps>({
  // ...
});

Live Demo

Codesandbox Demo

like image 32
NearHuscarl Avatar answered Sep 28 '22 01:09

NearHuscarl