跳转到内容

类名生成器

在构建时配置类名生成

This API is introduced in @mui/material (v5.0.5) as a replacement of deprecated createGenerateClassName.

Setup

By default, Material UI generates a global class name for each component slot. For example, <Button>Text</Button> generates html as:

<button
  class="MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeMedium MuiButton-textSizeMedium MuiButtonBase-root css-1ujsas3"
>
  Text
</button>

To customize all the class names generated by Material UI components, create a separate javascript file to use the ClassNameGenerator API.

// create a new file called `MuiClassNameSetup.js` at the root or src folder.
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className';

ClassNameGenerator.configure(
  // Do something with the componentName
  (componentName) => componentName,
);

and then import the file at the root of the index before any @mui/* imports.

import './MuiClassNameSetup';
import Button from '@mui/material/Button';
// ...other component imports

function App() {
  return <Button>Text</Button>;
}

Here are some configuration examples:

Change class name prefix

// MuiClassNameSetup.js
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className';

ClassNameGenerator.configure((componentName) => `foo-bar-${componentName}`);

As a result, the HTML result changes to the following:

<button
  class="foo-bar-MuiButton-root foo-bar-MuiButton-text foo-bar-MuiButton-textPrimary foo-bar-MuiButton-sizeMedium foo-bar-MuiButton-textSizeMedium foo-bar-MuiButtonBase-root css-1ujsas3"
>
  Button
</button>

Rename component class name

Every Material UI component has ${componentName}-${slot} classname format. For example, the component name of Chip is MuiChip, which is used as a global class name for every <Chip /> element. You can remove/change the Mui prefix as follows:

// MuiClassNameSetup.js
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className';

ClassNameGenerator.configure((componentName) => componentName.replace('Mui', ''));

Now, the Mui class is gone.

<div
  class="Chip-root Chip-filled Chip-sizeMedium Chip-colorDefault Chip-filledDefault css-mttbc0"
>
  Chip
</div>

Caveat

  • ClassNameGenerator.configure must be called before any Material UI components import.

  • 您应该始终使用 [component]类 进行主题化/自定义以获得正确的生成类名称。

    +import { outlinedInputClasses } from '@mui/material/OutlinedInput';
    
     const theme = createTheme({
       components: {
         MuiOutlinedInput: {
           styleOverrides: {
             root: {
    -          '& .MuiOutlinedInput-notchedOutline': {
    +          // the result will contain the prefix.
    +          [`& .${outlinedInputClasses.notchedOutline}`]: {
                 borderWidth: 1,
               }
             }
           }
         }
       }
     });
    
  • 此 API 只能在构建时使用。

  • 该配置应用于整个程序的所有组件。 您不能针对应用程序的特定部分。

Framework examples

Always create an intializer file to hoist the ClassNameGenerator call to the top.

// create a new file called `MuiClassNameSetup.js` at the root or src folder.
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className';

ClassNameGenerator.configure(
  // Do something with the componentName
  (componentName) => componentName,
);

Then import the file in the main javascript source based on the framework.

Next.js

Import the initializer in /pages/_app.js.

+import './MuiClassNameSetup';
 import * as React from 'react';
 import PropTypes from 'prop-types';
 import Head from 'next/head';

 export default function MyApp(props) {
   const { Component, pageProps } = props;

   return (
     <Component {...pageProps} />
   );
 }

Create React App

Import the initializer in /src/index.js.

+import './MuiClassNameSetup';
 import * as React from 'react';
 import ReactDOM from 'react-dom';
 import App from './App';

 ReactDOM.render(<App />);

Gatsby

Import the initializer in gatsby-ssr.js at the root folder.

+import './MuiClassNameSetup';

 export const wrapPageElement = ({ element }) => {
   return element;
 };