无法在TypeScript中自定义材质UI主题的调色板类型

de90aj5v  于 2022-12-19  发布在  TypeScript
关注(0)|答案(5)|浏览(124)

我创建了一个类型接口来向组件面板添加自定义属性,如下所示:

declare module @material-ui/core/styles/createMuiTheme {
  interface PaletteColor {
    transparent?: string;
    gradient?: PaletteColor;
  }
  interface Palette {
    gradient?: PaletteColor;
    transparent?: PaletteColor;
    secondary?: {
      transparent?: string;
    }
  }

  interface PaletteColorOptions {
    main?:string;
    dark?:string;
    light?:string;
    transparent?: string;
    gradient?: string;
    test?: string
  }
}

我正在尝试很多接口来让它工作。
然后我在我的主题中使用这些类型,如下所示:

export default function createMyTheme(options: ThemeOptions) {
    return createMuiTheme({
      ...options,
    })
  }

我使用该函数通过导入并调用它来创建我的主主题:

const theme = createMyTheme({});

然后我设置我的组件样式如下:背景:主题.调色板.渐变.主,
它告诉我:

Property 'gradient' does not exist on type 'Palette'.

环境:"@材料-用户界面/核心":"^4.9.2","React":"^16.12.0"," typescript ":"^3.7.5"
下面是我的完整主题:

const theme = createMyTheme({
  palette: {
    primary: {
      main: '#5FB9A6',
      dark: 'linear-gradient(-68deg, #151E27 , #335850)',
      gradient: 'linear-gradient(-68deg, #151E27 , #335850)'
    },
    secondary: {
      main: '#C68A77',
      transparent: 'rgba(198, 138, 119, 0.7)'
    },
    error: {
      light: '#e5a0a0',
      main: '#CD5C5C',
      dark: '#992c2c',
    },
    text: {
      primary:'#20383C',
      secondary: '#151E27',
      hint: 'rgba(32, 56, 60, 0.7)'
    },
    background: {
      paper: '#fff'
    },
    common: {
      white: "#FFF"
    }
  },
  typography: {
     fontFamily: '"Work Sans"'
  }

任何帮助将不胜感激!谢谢!

yhuiod9q

yhuiod9q1#

根据文档,您需要module augmentation才能将自定义特性添加到选项板。
我所做的是:
在根目录(与App.tsx所在的目录相同)中创建文件expanded-theme.ts

import '@material-ui/core/styles';

declare module '@material-ui/core/styles/createPalette' {
  interface Palette {
    myCustomColor?: Palette['primary'];
  }
  interface PaletteOptions {
    myCustomColor?: PaletteOptions['primary'];
  }
}

接下来,您可以在主题中定义自定义属性(我不需要导入expanded-theme.ts

import React from 'react';
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core';

// ...

const theme = createMuiTheme({
  palette: {
    myCustomColor: {
      main: 'blue'
    }
  }
});

// ...

<MuiThemeProvider theme={theme}>

//...

现在您可以在样式中使用myCustomColor:)。

gcuhipw9

gcuhipw92#

如果我想要一个新的自定义调色板neutral,那么这对我很有效,:
在项目根中的mui.d.ts

import "@mui/material/styles/createPalette";
declare module "@mui/material/styles/createPalette" {
  interface PaletteOptions {
    neutral: PaletteColorOptions;
  }
}

并将其放入tsconfig.json的包含中

...
"include": [... , mui.d.ts]
...
vptzau2j

vptzau2j3#

这似乎为我工作与MUI v5,增加了额外的自定义颜色“第三”

import {
  createTheme,
  responsiveFontSizes,
} from "@mui/material/styles";

    
declare module "@mui/material/styles" {
  interface BreakpointOverrides {
    xs: true;
    sm: true;
    md: true;
    lg: true;
    xl: true;
    mobile: true;
    tablet: true;
    laptop: true;
    desktop: true;
  }
  interface Palette {
    tertiary: Palette["primary"];
  }
  interface PaletteOptions {
    tertiary?: PaletteOptions["primary"];
  }
  interface PaletteColor {
    lighter?: string;
    darker?: string;
  }
  interface SimplePaletteColorOptions {
    lighter?: string;
    darker?: string;
  }
}

let theme = createTheme({
  palette: {
    primary: {
      lighter: "#F2D383",
      light: "#6E5B54",
      main: "#60504a",
      dark: "#614941",
      darker: "#473630",
    },
    secondary: {
      lighter: "#F5F5F5",
      light: "#f7f7f7",
      main: "#e7e7e7",
      dark: "#787878",
      darker: "#2e2e2e",
    },
    tertiary: {
      main: "#ff0000",
      contrastText: "#787878",
    },
    background: {
      default: "#e7e7e7",
    },
    text: {
      primary: "#5F5049",
    },
  },
  breakpoints: {
    values: {
      xs: 0,
      sm: 600,
      md: 900,
      lg: 1200,
      xl: 1536,
      mobile: 0,
      tablet: 640,
      laptop: 1200,
      desktop: 1600,
    },
  },
});

theme = createTheme(theme, {
  components: {
    MuiAppBar: {
      styleOverrides: {
        root: {
          backgroundColor: theme.palette.tertiary,main,
        },
      },
    },
  },
});

theme = responsiveFontSizes(theme);

export default theme;
qij5mzcb

qij5mzcb4#

我也在尝试让它工作,但我遗漏了一些非常基本的东西。declare module '@material-ui/core/styles/createMuiTheme'的代码实际上去了哪里?我尝试创建一个types.d.ts文件,然后在我的条目index.js文件中,我将其包括在内,如下所示:import './config/types.d';
......但我仍然会遇到与您相同的类型错误。就上面的示例而言,看起来MUI的示例在'@material-ui/core/styles/createMuiTheme'周围有引号,但您的代码没有。此外,他们的示例实际上显示了从MUI导入模块,然后进行修改。最后,你的代码看起来是自引用的(例如,接口PaletteColor中的渐变定义也是PaletteColor)。不过,我不知道这些东西是否真的有问题。
我的types.d.ts文件看起来像这样:

import { Palette } from '@material-ui/core/styles/createMuiTheme';
//https://material-ui.com/guides/typescript/#customization-of-theme
declare module '@material-ui/core/styles/createMuiTheme' {
  interface Palette {
    background: {
      appSubHead: string;
    };
  }

  interface PaletteOptions {
    background?: {
      appSubHead?: string;
    };
  }
}
e0bqpujr

e0bqpujr5#

我在create-react-app和typescript中使用了material-ui,导入调色板对我很有效。

import { Palette } from '@material-ui/core/styles/createPalette'

相关问题