React/MUI主题在不同组件上的工作不一致。

huangapple go评论67阅读模式
英文:

React/MUI theme is not working consistently for different components

问题

I'm using React/MUI for the first time and am unable to make themes work consistently. Here's a simple example of something that doesn't work for me, a Box:

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

const myTheme = createTheme({
  typography: {
    fontSize:   30,
    fontWeight: 500
  }
})

export default function App() {
  return (
    <>
      <Box>
        This has no theme and should use the default size and weight for the font.
      </Box>
      <ThemeProvider theme={myTheme}>
        <Box>
          This box should reflect the theme but doesn't. The font is the same size and weight as the previous box.
        </Box>
      </ThemeProvider>
    </>
  )
}

But if I instead use ThemeProvider with a Table then the font size is affected by the theme but the font weight is not:

import { createTheme, ThemeProvider } from '@mui/material/styles';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableRow from '@mui/material/TableRow';
import TableCell from '@mui/material/TableCell';

const myTheme = createTheme({
  typography: {
    fontSize:   30,
    fontWeight: 800
  }
})

export default function App() {
  return (
    <>
      <Table>
        <TableBody>
          <TableRow>
            <TableCell>
              This table has a single row with a single cell
              and uses the default size and weight for the font.
            </TableCell>
          </TableRow>
        </TableBody>
      </Table>
      <ThemeProvider theme={myTheme}>
        <Table>
          <TableBody>
            <TableRow>
              <TableCell>
                This table has a single row with a single cell
                and uses the theme's font size, but not its weight.
              </TableCell>
            </TableRow>
          </TableBody>
        </Table>
      </ThemeProvider>
    </>
  )
}

I'm hopeful someone out there sees what I'm doing wrong.

英文:

I'm using React/MUI for the first time and am unable to make themes work consistently. Here's a simple example of something that doesn't work for me, a Box:

import { createTheme, ThemeProvider } from &#39;@mui/material/styles&#39;;
import Box from &#39;@mui/material/Box&#39;

const myTheme = createTheme({
  typography: {
    fontSize:   30,
    fontWeight: 500
  }
})

export default function App() {
  return (
    &lt;&gt;
      &lt;Box&gt;
        This has no theme and should use the default size and weight for the font.
      &lt;/Box&gt;
      &lt;ThemeProvider theme={myTheme}&gt;
        &lt;Box&gt;
          This box should reflect the theme but doesn&#39;t.  The font is the same size and weight as the previous box.
        &lt;/Box&gt;
      &lt;/ThemeProvider&gt;
    &lt;/&gt;
  )
}

But if I instead use ThemeProvider with a Table then the font size is affected by the theme but the font weight is not:

import { createTheme, ThemeProvider } from &#39;@mui/material/styles&#39;;
import Table from &#39;@mui/material/Table&#39;
import TableBody from &#39;@mui/material/TableBody&#39;
import TableRow from &#39;@mui/material/TableRow&#39;
import TableCell from &#39;@mui/material/TableCell&#39;

const myTheme = createTheme({
  typography: {
    fontSize:   30,
    fontWeight: 800
  }
})

export default function App() {
  return (
    &lt;&gt;
      &lt;Table&gt;
        &lt;TableBody&gt;
          &lt;TableRow&gt;
            &lt;TableCell&gt;
              This table has a single row with a single cell
              and uses the default size and weight for the font.
            &lt;/TableCell&gt;
          &lt;/TableRow&gt;
        &lt;/TableBody&gt;
      &lt;/Table&gt;
      &lt;ThemeProvider theme={myTheme}&gt;
        &lt;Table&gt;
          &lt;TableBody&gt;
            &lt;TableRow&gt;
              &lt;TableCell&gt;
                This table has a single row with a single cell
                and uses the theme&#39;s font size, but not its weight.
              &lt;/TableCell&gt;
            &lt;/TableRow&gt;
          &lt;/TableBody&gt;
        &lt;/Table&gt;
      &lt;/ThemeProvider&gt;
    &lt;/&gt;
  )
}

I'm hopeful someone out there sees what I'm doing wrong.

答案1

得分: 1

我不是专家,但我相信可以通过针对特定变体应用fontWeight样式。
通过定位body1变体,您将能够更改字重。像这样

const myTheme = createTheme({
 typography: {
  fontSize: 30,
  body1: {
   fontWeight: 100,
  },
 },
});

这肯定会起作用。

英文:

I'm not expert but I believe that fontWeight style can be applieb by trageting specific varient.
By targeting body1 varient you will able to change font weight as well. like this

const myTheme = createTheme({
 typography: {
  fontSize: 30,
  body1: {
   fontWeight: 100,
  },
 },
});

This will work for sure

答案2

得分: 0

以下是翻译好的内容:

我以答案的形式发布,而不是评论,因为尽管说明中说可以在反引号之间包含代码,但结果无法阅读。

@user16967562建议的页面是指导我的页面。这是该页面中“字体大小”下的内容:

const myTheme = createTheme({
  typography: {
    fontSize: 30,
  }
})

这对于在<Box>中使用<ThemeProvider>不起作用。<Box>内部文本的大小与默认值相同。

也许我应该注意那个建议的 MUI 页面的其他部分?

英文:

I'm posting as an answer instead of a comment because although the instructions say you can include code between backticks, the result wasn't readable.

The page @user16967562 suggested is the one that guided me. This is from that page under "Font size":

const myTheme = createTheme({
  typography: {
    fontSize:   30,
  }
})

This doesn't work for &lt;Box&gt; using &lt;ThemeProvider&gt;. The size of the text inside &lt;Box&gt; is unchanged from the default.

Maybe there's some other part of that suggested MUI page I should be paying attention to?

答案3

得分: 0

I've translated the code portion for you:

我继续进行实验认为我有了一个更完整的答案解决方案是主题必须在body1下定义fontWeight并且您的组件必须包含在<Typography>标签内如下所示

import * as React from 'react';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';

const theme = createTheme({
  typography: {
    fontSize: 50,
    body1: {
      fontWeight: 500
    }
  }
});

export default function FontSizeTheme() {
  return (
    <ThemeProvider theme={theme}>
      <Typography>
        <Box>
          Some Text
        </Box>
      </Typography>
    </ThemeProvider>
  );
}

如果您需要更多帮助,请随时提出。

英文:

I've continued to experiment and think I have a more complete answer. The solution is that the theme must define fontWeight under body1, and your component has to be included within &lt;Typography&gt; tags, like so:

import * as React from &#39;react&#39;;
import { createTheme, ThemeProvider } from &#39;@mui/material/styles&#39;;
import Typography from &#39;@mui/material/Typography&#39;;
import Box from &#39;@mui/material/Box&#39;;


const theme = createTheme({
  typography: {
    fontSize: 50,
    body1: {
      fontWeight: 500
    }
  }
});

export default function FontSizeTheme() {
  return (
    &lt;ThemeProvider theme={theme}&gt;
      &lt;Typography&gt;
        &lt;Box&gt;
          Some Text
        &lt;/Box&gt;
      &lt;/Typography&gt;
    &lt;/ThemeProvider&gt;
  );
}

Missing from the MUI documentation is any indication that &lt;Box&gt; is associated with the body1 variant. The word "body1" does not appear on the MUI documentation page for &lt;Box&gt;. The doc page for Typography lists all the variants but doesn't say which variants apply to which components. Why is the body1 variant associated with &lt;Box&gt; and the body2 variant is not? Why can I say this to get &lt;Box&gt; to use body2:

&lt;Typography variant=&#39;body2&#39;&gt;
  &lt;Box&gt;
    Some Text
  &lt;/Box&gt;
&lt;/Typography&gt;

But not this:

&lt;Typography&gt;
  &lt;Box variant=&#39;body2&#39;&gt;
    Some Text
  &lt;/Box&gt;
&lt;/Typography&gt;

What I'm seeking is a system or a pattern or a style of organization within Mui so that when I learn how to do one particular kind of thing, I can then easily figure out how to do a whole bunch of related other kinds of things. So far it just isn't fitting together for me.

huangapple
  • 本文由 发表于 2023年5月21日 03:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定