Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend OverridableComponent interface in Material-UI

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>
    </>
  );
};
like image 330
Dooomel Avatar asked Aug 22 '20 22:08

Dooomel


People also ask

How do you override mui styles?

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.

How do you override CSS in material UI?

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.

How do you apply styles in material UI?

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 .

How do I override a component slot in material UI?

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].

How do I customize material UI components?

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

What is global theme override in material UI?

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.

How do I add style overrides to a 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.


Video Answer


1 Answers

Usage of component prop

const Container = styled(MuiContainer)<ContainerProps<'main', { component: 'main' }>>``;

Edit component-prop

like image 174
Hamidreza Avatar answered Oct 18 '22 03:10

Hamidreza