英文:
How do I use a different background for dark mode when using Material UI Themes?
问题
以下是代码部分的翻译:
export const myThemeOptions: ThemeOptions = {
    palette: {
        mode: "light" as PaletteMode,
        primary: {
            main: '#1FC8B9',
            dark: '#14A996',
        },
        secondary: {
            main: '#57ECE6',
            dark: '#0F6E60',
        },
        background: {
            default: '#074E40',
            paper: '#086A5D',
        },
    },
};
export const myThemeOptions: ThemeOptions = {
    palette: {
        mode: "light" as PaletteMode,
        primary: {
            main: '#1FC8B9',
            dark: '#14A996',
        },
        secondary: {
            main: '#57ECE6',
            dark: '#0F6E60',
        },
        background: {
            default: '#6AD6CD',
            paper: '#086A5D',
        },
        dark: {
            background: {
                default: '#074E40',
                paper: '#424242',
            },
        },
    },
};
export const ColorProvider: React.FC = ({ children }) => {
    const [colorMode, setColorMode] = useState<ColorMode>(() => {
        const savedColorMode = getCookie('colorMode');
        return savedColorMode ? (savedColorMode as ColorMode) : 'light';
    });
    const [theme, setTheme] = useState(createTheme(myThemeOptions));
    useEffect(() => {
        const updatedPaletteMode = colorMode === 'dark' ? 'dark' : 'light';
        const updatedThemeOptions = {
            ...myThemeOptions,
            palette: {
                ...myThemeOptions.palette,
                mode: updatedPaletteMode,
            },
        };
        const updatedTheme = createTheme(updatedThemeOptions);
        setTheme(updatedTheme);
        // Save color mode to cookie
        setCookie('colorMode', colorMode);
    }, [colorMode]);
    const toggleColorMode = () =>
        setColorMode(colorMode === 'light' ? 'dark' : 'light');
    const contextValue: ColorContextType = {
        colorMode,
        toggleColorMode,
    };
    return (
        <ColorContext.Provider value={contextValue}>
            <ThemeProvider theme={theme}>
                <CssBaseline enableColorScheme/>
                {children}
            </ThemeProvider>
        </ColorContext.Provider>
    );
};
如果需要更多帮助,请告诉我。
英文:
I have the following...
export const myThemeOptions: ThemeOptions = {
    palette: {
        mode: "light" as PaletteMode,
        primary: {
            main: '#1FC8B9',
            dark: '#14A996',
        },
        secondary: {
            main: '#57ECE6',
            dark: '#0F6E60',
        },
        background: {
            default: '#074E40',
            paper: '#086A5D',
        },
    },
};
I would like to change the background when I am in dark mode but I can't seem to do it. I tried...
export const myThemeOptions: ThemeOptions = {
    palette: {
        mode: "light" as PaletteMode,
        primary: {
            main: '#1FC8B9',
            dark: '#14A996',
        },
        secondary: {
            main: '#57ECE6',
            dark: '#0F6E60',
        },
        background: {
            default: '#6AD6CD',
            paper: '#086A5D',
        },
        dark: {
            background: {
                default: '#074E40',
                paper: '#424242',
            },
        },
    },
};
But that didn't work the background just stayed #6AD6CD. What am I missing how do I change this? My current provider looks like this...
export const ColorProvider: React.FC = ({ children }) => {
    const [colorMode, setColorMode] = useState<ColorMode>(() => {
        const savedColorMode = getCookie('colorMode');
        return savedColorMode ? (savedColorMode as ColorMode) : 'light';
    });
    const [theme, setTheme] = useState(createTheme(myThemeOptions));
    useEffect(() => {
        const updatedPaletteMode = colorMode === 'dark' ? 'dark' : 'light'; // Updated logic
        const updatedThemeOptions = {
            ...myThemeOptions,
            palette: {
                ...myThemeOptions.palette,
                mode: updatedPaletteMode,
            },
        };
        const updatedTheme = createTheme(updatedThemeOptions);
        setTheme(updatedTheme);
        // Save color mode to cookie
        setCookie('colorMode', colorMode);
    }, [colorMode]);
    const toggleColorMode = () =>
        setColorMode(colorMode === 'light' ? 'dark' : 'light');
    const contextValue: ColorContextType = {
        colorMode,
        toggleColorMode,
    };
    return (
        <ColorContext.Provider value={contextValue}>
            <ThemeProvider theme={theme}>
                <CssBaseline enableColorScheme/>
                {children}
            </ThemeProvider>
        </ColorContext.Provider>
    );
};
答案1
得分: 1
您很可能希望将您的 myThemeOptions 对象替换为一个函数,该函数根据传递的模式展开并返回适当的主题值,符合MUI文档中的使用自定义调色板的暗模式的示例。例如:
const myThemeOptions = (mode: PaletteMode) => ({
  palette: {
    mode,
    primary: {
      main: "#1FC8B9",
      dark: "#14A996"
    },
    secondary: {
      main: "#57ECE6",
      dark: "#0F6E60"
    },
    background: {
      default: "#6AD6CD",
      paper: "#086A5D"
    },
    ...(mode === "dark" && {
      background: {
        default: "#074E40",
        paper: "#424242"
      }
    })
  }
});
然后,您可以在提供程序内部运行/记忆化结果,和/或在状态中存储它。
我将几个MUI文档示例组合在一起,以制作这个快速的[非提供程序]演示。
工作演示: https://codesandbox.io/s/mutable-grass-qjh9ij?file=/demo.tsx:286-692
英文:
You're most likely going to want to replace your myThemeOptions object with a function that spreads in and returns the appropriate theme values for the passed mode, per the MUI documentation for Dark mode with custom palette. For example:
const myThemeOptions = (mode: PaletteMode) => ({
  palette: {
    mode,
    primary: {
      main: "#1FC8B9",
      dark: "#14A996"
    },
    secondary: {
      main: "#57ECE6",
      dark: "#0F6E60"
    },
    background: {
      default: "#6AD6CD",
      paper: "#086A5D"
    },
    ...(mode === "dark" && {
      background: {
        default: "#074E40",
        paper: "#424242"
      }
    })
  }
});
Then you'll want to either run/memoize the result from within your provider and/or store it elsewhere in state.
I stitched together a couple of the MUI doc examples to make this quick [non-Provider] demo.
Working demo: https://codesandbox.io/s/mutable-grass-qjh9ij?file=/demo.tsx:286-692
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论