如何在使用Material UI主题时为深色模式使用不同的背景?

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

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: &quot;light&quot; as PaletteMode,
        primary: {
            main: &#39;#1FC8B9&#39;,
            dark: &#39;#14A996&#39;,
        },
        secondary: {
            main: &#39;#57ECE6&#39;,
            dark: &#39;#0F6E60&#39;,
        },
        background: {
            default: &#39;#074E40&#39;,
            paper: &#39;#086A5D&#39;,
        },
    },
};

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: &quot;light&quot; as PaletteMode,
        primary: {
            main: &#39;#1FC8B9&#39;,
            dark: &#39;#14A996&#39;,
        },
        secondary: {
            main: &#39;#57ECE6&#39;,
            dark: &#39;#0F6E60&#39;,
        },
        background: {
            default: &#39;#6AD6CD&#39;,
            paper: &#39;#086A5D&#39;,
        },
        dark: {
            background: {
                default: &#39;#074E40&#39;,
                paper: &#39;#424242&#39;,
            },
        },
    },
};

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 }) =&gt; {
    const [colorMode, setColorMode] = useState&lt;ColorMode&gt;(() =&gt; {
        const savedColorMode = getCookie(&#39;colorMode&#39;);
        return savedColorMode ? (savedColorMode as ColorMode) : &#39;light&#39;;
    });

    const [theme, setTheme] = useState(createTheme(myThemeOptions));

    useEffect(() =&gt; {
        const updatedPaletteMode = colorMode === &#39;dark&#39; ? &#39;dark&#39; : &#39;light&#39;; // Updated logic
        const updatedThemeOptions = {
            ...myThemeOptions,
            palette: {
                ...myThemeOptions.palette,
                mode: updatedPaletteMode,
            },
        };
        const updatedTheme = createTheme(updatedThemeOptions);
        setTheme(updatedTheme);
        // Save color mode to cookie
        setCookie(&#39;colorMode&#39;, colorMode);
    }, [colorMode]);

    const toggleColorMode = () =&gt;
        setColorMode(colorMode === &#39;light&#39; ? &#39;dark&#39; : &#39;light&#39;);

    const contextValue: ColorContextType = {
        colorMode,
        toggleColorMode,
    };

    return (
        &lt;ColorContext.Provider value={contextValue}&gt;
            &lt;ThemeProvider theme={theme}&gt;
                &lt;CssBaseline enableColorScheme/&gt;
                {children}
            &lt;/ThemeProvider&gt;
        &lt;/ColorContext.Provider&gt;
    );
};

答案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) =&gt; ({
  palette: {
    mode,
    primary: {
      main: &quot;#1FC8B9&quot;,
      dark: &quot;#14A996&quot;
    },
    secondary: {
      main: &quot;#57ECE6&quot;,
      dark: &quot;#0F6E60&quot;
    },
    background: {
      default: &quot;#6AD6CD&quot;,
      paper: &quot;#086A5D&quot;
    },
    ...(mode === &quot;dark&quot; &amp;&amp; {
      background: {
        default: &quot;#074E40&quot;,
        paper: &quot;#424242&quot;
      }
    })
  }
});

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.

如何在使用Material UI主题时为深色模式使用不同的背景?
如何在使用Material UI主题时为深色模式使用不同的背景?

Working demo: https://codesandbox.io/s/mutable-grass-qjh9ij?file=/demo.tsx:286-692

huangapple
  • 本文由 发表于 2023年6月1日 09:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定