Adding custom styles to the Mui v5 AppBar component

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

Adding custom styles to the Mui v5 AppBar component

问题

在Typescript中为Mui v5的AppBar组件添加自定义样式时遇到了问题。

  1. import { alpha } from '@mui/material/styles';
  2. export function bgBlur(props: { color: any; blur?: any; opacity?: any; imgUrl?: any; }) {
  3. const color = props?.color || '#000000';
  4. const blur = props?.blur || 6;
  5. const opacity = props?.opacity || 0.8;
  6. const imgUrl = props?.imgUrl;
  7. if (imgUrl) {
  8. return {
  9. position: 'relative',
  10. backgroundImage: `url(${imgUrl})`,
  11. '&:before': {
  12. position: 'absolute',
  13. top: 0,
  14. left: 0,
  15. zIndex: 9,
  16. content: '""',
  17. width: '100%',
  18. height: '100%',
  19. backdropFilter: `blur(${blur}px)`,
  20. WebkitBackdropFilter: `blur(${blur}px)`,
  21. backgroundColor: alpha(color, opacity),
  22. },
  23. };
  24. }
  25. return {
  26. backdropFilter: `blur(${blur}px)`,
  27. WebkitBackdropFilter: `blur(${blur}px)`,
  28. };
  29. }

在这里调用了bgBlur函数:

  1. const StyledAppBar = styled(AppBar)(({ theme }) => ({
  2. ...bgBlur({ color: theme.palette.background.default }),
  3. boxShadow: "none",
  4. })) as typeof AppBar;

出现的错误是:

No overload matches this call. The last overload gave the following error.

英文:

I have a problem adding custom styles to the Mui v5 AppBar component in Typescript.

  1. import { alpha } from '@mui/material/styles';
  2. export function bgBlur(props: { color: any; blur?: any; opacity?: any; imgUrl?: any; }) {
  3. const color = props?.color || '#000000';
  4. const blur = props?.blur || 6;
  5. const opacity = props?.opacity || 0.8;
  6. const imgUrl = props?.imgUrl;
  7. if (imgUrl) {
  8. return {
  9. position: 'relative',
  10. backgroundImage: `url(${imgUrl})`,
  11. '&:before': {
  12. position: 'absolute',
  13. top: 0,
  14. left: 0,
  15. zIndex: 9,
  16. content: '""',
  17. width: '100%',
  18. height: '100%',
  19. backdropFilter: `blur(${blur}px)`,
  20. WebkitBackdropFilter: `blur(${blur}px)`,
  21. backgroundColor: alpha(color, opacity),
  22. },
  23. };
  24. }
  25. return {
  26. backdropFilter: `blur(${blur}px)`,
  27. WebkitBackdropFilter: `blur(${blur}px)`,
  28. };
  29. }

I call bgBlur here

  1. const StyledAppBar = styled(AppBar)(({ theme }) => ({
  2. ...bgBlur({ color: theme.palette.background.default }),
  3. boxShadow: "none",
  4. })) as typeof AppBar;

The error is

> No overload matches this call. The last overload gave the following
> error.

答案1

得分: 1

以下是要翻译的代码部分:

"It's because Typescript expect the position property to be a Property.Position (defined in cssstyle/index.d.ts), but can't infer properly that relative belongs to this union type.

Adding a as const fix the error.

  1. const bgBlur = (props: {
  2. color: any;
  3. blur?: any;
  4. opacity?: any;
  5. imgUrl?: any;
  6. }) => {
  7. const color = props?.color || "#000000";
  8. const blur = props?.blur || 6;
  9. const opacity = props?.opacity || 0.8;
  10. const imgUrl = props?.imgUrl;
  11. return imgUrl
  12. ? {
  13. position: "relative" as const, // <-- here,
  14. backgroundImage: `url(${imgUrl})`,
  15. "&:before": {
  16. position: "absolute",
  17. top: 0,
  18. left: 0,
  19. zIndex: 9,
  20. width: "100%",
  21. height: "100%",
  22. backdropFilter: `blur(${blur}px)`,
  23. WebkitBackdropFilter: `blur(${blur}px)`,
  24. },
  25. }
  26. : {
  27. backdropFilter: `blur(${blur}px)`,
  28. WebkitBackdropFilter: `blur(${blur}px)`,
  29. };
  30. };
  31. const StyledAppBar = styled(AppBar)(({ theme }) => ({
  32. ...bgBlur({ color: theme.palette.background.default }),
  33. boxShadow: "none",
  34. }));

希望这有所帮助。

英文:

It's because Typescript expect the position property to be a Property.Position (defined in cssstyle/index.d.ts), but can't infer properly that relative belongs to this union type.

Adding a as const fix the error.

  1. const bgBlur = (props: {
  2. color: any;
  3. blur?: any;
  4. opacity?: any;
  5. imgUrl?: any;
  6. }) =&gt; {
  7. const color = props?.color || &quot;#000000&quot;;
  8. const blur = props?.blur || 6;
  9. const opacity = props?.opacity || 0.8;
  10. const imgUrl = props?.imgUrl;
  11. return imgUrl
  12. ? {
  13. position: &quot;relative&quot; as const // &lt;-- here,
  14. backgroundImage: `url(${imgUrl})`,
  15. &quot;&amp;:before&quot;: {
  16. position: &quot;absolute&quot;,
  17. top: 0,
  18. left: 0,
  19. zIndex: 9,
  20. width: &quot;100%&quot;,
  21. height: &quot;100%&quot;,
  22. backdropFilter: `blur(${blur}px)`,
  23. WebkitBackdropFilter: `blur(${blur}px)`,
  24. },
  25. }
  26. : {
  27. backdropFilter: `blur(${blur}px)`,
  28. WebkitBackdropFilter: `blur(${blur}px)`,
  29. };
  30. };
  31. const StyledAppBar = styled(AppBar)(({ theme }) =&gt; ({
  32. ...bgBlur({ color: theme.palette.background.default }),
  33. boxShadow: &quot;none&quot;,
  34. }));

huangapple
  • 本文由 发表于 2023年6月29日 22:08:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581832.html
匿名

发表评论

匿名网友

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

确定