javascript - How to customize LinearProgress color in Material UI - Stack Overflow

I have set a theme for my codebase using createTheme like so:import { createTheme } from '@muima

I have set a theme for my codebase using createTheme like so:

import { createTheme } from '@mui/material/styles';

export default function getTheme() {
  return {
    theme: createTheme({
      palette: {
        ...,
        progress: {
          0: '#000000',
          126: '#222222',
          251: '#444444',
          376: '#666666',
          501: '#888888',
          626: '#aaaaaa',
          751: '#cccccc',
          876: '#eeeeee',
          1000: '#ffffff',
        },
        ...,
      }
    });
  }
}

It works just fine for the Typography component:

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

export default function HelloWorld() {
  return (
    <Typography color="progress.126">
      Hello World
    </Typography>
  );
}

However, it raises a TypeError: Cannot read properties of undefined (reading 'main') when I use it with LinearProgress:

import { LinearProgress } from '@mui/material';

export default function ProgressBar() {
  return (
    <LinearProgress
      variant="determinate"
      value={50}
      color="progress.501"
      sx={{
        height: 8,
        width: 120,
      }}
    />
  );
}

How can I apply these colors to the LinearProgress like I would with other Material UI components?

I have set a theme for my codebase using createTheme like so:

import { createTheme } from '@mui/material/styles';

export default function getTheme() {
  return {
    theme: createTheme({
      palette: {
        ...,
        progress: {
          0: '#000000',
          126: '#222222',
          251: '#444444',
          376: '#666666',
          501: '#888888',
          626: '#aaaaaa',
          751: '#cccccc',
          876: '#eeeeee',
          1000: '#ffffff',
        },
        ...,
      }
    });
  }
}

It works just fine for the Typography component:

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

export default function HelloWorld() {
  return (
    <Typography color="progress.126">
      Hello World
    </Typography>
  );
}

However, it raises a TypeError: Cannot read properties of undefined (reading 'main') when I use it with LinearProgress:

import { LinearProgress } from '@mui/material';

export default function ProgressBar() {
  return (
    <LinearProgress
      variant="determinate"
      value={50}
      color="progress.501"
      sx={{
        height: 8,
        width: 120,
      }}
    />
  );
}

How can I apply these colors to the LinearProgress like I would with other Material UI components?

Share asked Mar 7 at 18:55 Renan ElfoRenan Elfo 276 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Consider reading the docs on How to customize:

Overriding nested component styles

To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop... Material UI rely on class names that all follow a standard pattern: [hash]-Mui[Component name]-[name of the slot].

In your case you can find the classes names on the doc LinearProgress API page.

Putting it together, it's just a matter of finding the class and targeting in the sx prop to customize:

<LinearProgress
        variant="determinate"
        value={50}
        color="inherit"
        sx={{
          height: 8,
          width: 120,
          "& .MuiLinearProgress-bar" : {
             color: "progress.501"
          }
        }}
      />

You might have to change other classes to get to the desired result;

If you want it to be more reusable consider using styled as shown here;

I found a workarround using Box and setting the color on LinearProgress to inherit:

import { Box, LinearProgress } from '@mui/material';

export default function ProgressBar() {
  return (
    <Box color="progress.501">
      <LinearProgress
        variant="determinate"
        value={50}
        color="inherit"
        sx={{
          height: 8,
          width: 120,
        }}
      />
    </Box\>
  );
}

However, I wonder if there's a way to make it work like it does in Typography, i.e., without needing an extra Box component.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744912962a4600663.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信