I'm trying to use Container
component with styled-components using ContainerProps
but then I can't pass component
prop which belongs to OverridableComponent
interface.
Code below gives me error which tells me that I can't pass component
property. When I change <Container/>
to <MuiContainer/>
it works.
MuiContainer
has type OverridableComponent<ContainerTypeMap<{}, 'div'>>
but I can't import OverridableComponent
from @material-ui/core
How can I make passing component
property possible?
import { Container as MuiContainer, ContainerProps } from '@material-ui/core';
import React from 'react';
import styled from 'styled-components';
const Container = styled(MuiContainer)<ContainerProps>``;
export const Test = () => {
return (
<>
<Container maxWidth="lg" component="main">
content
</Container>
</>
);
};
Overriding styles with class names If you want to override a component's styles using custom classes, you can use the className prop, available on each component.
The second way to override the style of a component is to use the inline-style approach. Every component provides a style property. These properties are always applied to the root element. You don't have to worry about CSS specificity as the inline-style takes precedence over the regular CSS.
Luckily, Material-UI provides a solution for this: makeStyles . Using makeStyles , you can add CSS in JS without making your code look messy. First, you need to import makeStyles in your app. Next, pass all the CSS you need in your app as an object to makeStyles and store it inside a variable, useStyles .
First, use your browser's dev tools to identify the class for the component slot you want to override. The styles injected into the DOM by Material UI rely on class names that all follow a standard pattern : [hash]-Mui [Component name]- [name of the slot].
Learn how to customize Material UI components by taking advantage of different strategies for specific use cases. Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization
Global theme overrides Material UI provides theme tools for managing style consistency between all components across your user interface. Visit the Component theming customization page for more details. 4. Global CSS override To add global baseline styles for some of the HTML elements, use the GlobalStyles component.
The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can be used with all Material UI components. To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop.
Usage of component prop
const Container = styled(MuiContainer)<ContainerProps<'main', { component: 'main' }>>``;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With