Adding custom styles to the Mui v5 AppBar component

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

Adding custom styles to the Mui v5 AppBar component

问题

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

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

export function bgBlur(props: { color: any; blur?: any; opacity?: any; imgUrl?: any; }) {
  const color = props?.color || '#000000';
  const blur = props?.blur || 6;
  const opacity = props?.opacity || 0.8;
  const imgUrl = props?.imgUrl;

  if (imgUrl) {
    return {
      position: 'relative',
      backgroundImage: `url(${imgUrl})`,
      '&:before': {
        position: 'absolute',
        top: 0,
        left: 0,
        zIndex: 9,
        content: '""',
        width: '100%',
        height: '100%',
        backdropFilter: `blur(${blur}px)`,
        WebkitBackdropFilter: `blur(${blur}px)`,
        backgroundColor: alpha(color, opacity),
      },
    };
  }

  return {
    backdropFilter: `blur(${blur}px)`,
    WebkitBackdropFilter: `blur(${blur}px)`,
  };
}

在这里调用了bgBlur函数:

const StyledAppBar = styled(AppBar)(({ theme }) => ({
  ...bgBlur({ color: theme.palette.background.default }),
  boxShadow: "none",
})) 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.

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

export function bgBlur(props: { color: any; blur?: any; opacity?: any; imgUrl?: any; }) {
  const color = props?.color || '#000000';
  const blur = props?.blur || 6;
  const opacity = props?.opacity || 0.8;
  const imgUrl = props?.imgUrl;

  if (imgUrl) {
    return {
      position: 'relative',
      backgroundImage: `url(${imgUrl})`,
      '&:before': {
        position: 'absolute',
        top: 0,
        left: 0,
        zIndex: 9,
        content: '""',
        width: '100%',
        height: '100%',
        backdropFilter: `blur(${blur}px)`,
        WebkitBackdropFilter: `blur(${blur}px)`,
        backgroundColor: alpha(color, opacity),
      },
    };
  }

  return {
    backdropFilter: `blur(${blur}px)`,
    WebkitBackdropFilter: `blur(${blur}px)`,
  };
}

I call bgBlur here

const StyledAppBar = styled(AppBar)(({ theme }) => ({
  ...bgBlur({ color: theme.palette.background.default }),
  boxShadow: "none",
}))  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.

const bgBlur = (props: {
  color: any;
  blur?: any;
  opacity?: any;
  imgUrl?: any;
}) => {
  const color = props?.color || "#000000";
  const blur = props?.blur || 6;
  const opacity = props?.opacity || 0.8;
  const imgUrl = props?.imgUrl;

  return imgUrl
    ? {
        position: "relative" as const, // <-- here,
        backgroundImage: `url(${imgUrl})`,
        "&:before": {
          position: "absolute",
          top: 0,
          left: 0,
          zIndex: 9,
          width: "100%",
          height: "100%",
          backdropFilter: `blur(${blur}px)`,
          WebkitBackdropFilter: `blur(${blur}px)`,
        },
      }
    : {
        backdropFilter: `blur(${blur}px)`,
        WebkitBackdropFilter: `blur(${blur}px)`,
      };
};

const StyledAppBar = styled(AppBar)(({ theme }) => ({
  ...bgBlur({ color: theme.palette.background.default }),
  boxShadow: "none",
}));

希望这有所帮助。

英文:

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.

const bgBlur = (props: {
  color: any;
  blur?: any;
  opacity?: any;
  imgUrl?: any;
}) =&gt; {
  const color = props?.color || &quot;#000000&quot;;
  const blur = props?.blur || 6;
  const opacity = props?.opacity || 0.8;
  const imgUrl = props?.imgUrl;

  return imgUrl
    ? {
        position: &quot;relative&quot; as const // &lt;-- here,
        backgroundImage: `url(${imgUrl})`,
        &quot;&amp;:before&quot;: {
          position: &quot;absolute&quot;,
          top: 0,
          left: 0,
          zIndex: 9,
          width: &quot;100%&quot;,
          height: &quot;100%&quot;,
          backdropFilter: `blur(${blur}px)`,
          WebkitBackdropFilter: `blur(${blur}px)`,
        },
      }
    : {
          backdropFilter: `blur(${blur}px)`,
          WebkitBackdropFilter: `blur(${blur}px)`,
      };
};

const StyledAppBar = styled(AppBar)(({ theme }) =&gt; ({
  ...bgBlur({ color: theme.palette.background.default }),
  boxShadow: &quot;none&quot;,
}));

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:

确定