Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

Switch Material UI theme

I'm trying out the Material UI theming but has stubbled upon something I can't figure out.

I have declared two different themes, the only difference between the two is the colors:

const defaultTheme = {
    palette: {
        primary: {
            main: "#039be5"
        },
        secondary: {
            main: "#ff4081"
        }
    },
    props: {
        MuiButtonBase: {
            disableRipple: true
        },
        MuiButton: {
            disableElevation: true,
            variant: "contained"
        },
    }
};

const userTheme = {
    palette: {
        primary: {
            main: "#3fb5b5"
        },
        secondary: {
            main: "#ad976e"
        }
    },
    props: {
        MuiButtonBase: {
            disableRipple: true
        },
        MuiButton: {
            disableElevation: true,
            variant: "contained"
        },
    }
};

If the "userTheme" does not have any values the "defaultTheme" will be used:

const theme = createMuiTheme(isEmpty(userTheme) ? defaultTheme : userTheme);

function isEmpty(obj) {
    for (var key in obj) {
        if (obj.hasOwnProperty(key))
            return false;
    }
    return true;
}

Now this works as expected but when the "userTheme" is beeing used the text of the buttons somehow turns black instead of white which is the Material UI standard text color. This works as expected when using my "defaultTheme".

Example of using the defaultTheme: Default theme with white text

Example of using the userTheme: User theme with black text, why?

question from:https://stackoverflow.com/questions/65952504/switch-material-ui-theme

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...