简体   繁体   English

使用 Material-UI 从 UI 库中导出自定义主题

[英]Exporting a custom theme from a UI library using Material-UI

I am creating a custom UI library for work that uses Material-UI.我正在为使用 Material-UI 的工作创建一个自定义 UI 库。 The UI library has a custom theme where I added to the palette object with custom company colors. UI 库有一个自定义主题,我在其中添加了自定义公司颜色的调色板对象。 The theme lives in the UI library, and for elements that live there, I can use the custom colors in makeStyles, but when I try to use the exported theme in the main codebase, the custom colors throw errors in makeStyles.主题位于 UI 库中,对于位于那里的元素,我可以在 makeStyles 中使用自定义颜色,但是当我尝试在主代码库中使用导出的主题时,自定义颜色会在 makeStyles 中引发错误。

I believe the issue is I'm not exporting the theme's custom module.我相信问题是我没有导出主题的自定义模块。 I am unsure how to export and import this correctly into the main codebase.我不确定如何将其正确导出和导入到主代码库中。 Currently, I am only exporting the theme file and importing it into the main codebase.目前,我只导出主题文件并将其导入主代码库。

Main Codebase :主要代码库

import { theme } from 'customUILibrary';
...
<MuiThemeProvider theme={theme}>
    ...
</MuiThemeProvider>

CustomUILibrary :自定义UI库

index.ts:索引.ts:

export { theme } from 'theme/theme';

theme/theme.ts主题/theme.ts

import {
    createTheme,
    responsiveFontSizes,
} from '@material-ui/core/styles';
import './extendPalette';

export const theme = responsiveFontSizes(createTheme({

    palette: {
        gradients: {
            primary: 'linear-gradient(270deg, #35C7E1 -10.76%, #1A92BD 121.8%)',
            secondary: 'linear-gradient(270deg, #194E94 0%, #317CA6 100%)',
        },
    },
 
}));

extendPalette.ts扩展调色板

    declare module '@material-ui/core/styles/createPalette' {

    export interface PaletteOptions {
        gradients: {
            primary: string
            secondary: string,
        },
    }
    export interface Palette {
        gradients: {
            primary: string
            secondary: string,
        },
    }

}

As suggested here , you can do something like this:正如此处所建议的,您可以执行以下操作:

declare module '@mui/material/styles' {
    interface Theme {
        gradients: typeof customData.gradient,
    }
    interface ThemeOptions {
        gradients: typeof customData.gradient,
    }
}

I never tried to override Palette but i think you can do it in the same place.我从来没有尝试过覆盖Palette但我认为你可以在同一个地方做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM