Skip to content

Instantly share code, notes, and snippets.

@arush15june
Created August 31, 2022 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arush15june/0847b7d598ff42755b72d3399d7b7ae7 to your computer and use it in GitHub Desktop.
Save arush15june/0847b7d598ff42755b72d3399d7b7ae7 to your computer and use it in GitHub Desktop.
AntD Dark Mode with @umijs/ssr-darkreader
import {
disable as darkreaderDisable,
enable as darkreaderEnable,
setFetchMethod as setFetch,
} from '@umijs/ssr-darkreader';
import { ConfigProvider } from 'antd';
const themeConfig = {
daybreak: '#1890ff',
dust: '#F5222D',
volcano: '#FA541C',
sunset: '#FAAD14',
cyan: '#13C2C2',
green: '#52C41A',
geekblue: '#2F54EB',
purple: '#722ED1',
};
/**
* Daybreak-> #1890ff
*
* @param val
*/
export function genStringToTheme(val?: string): string {
return val && themeConfig[val] ? themeConfig[val] : val;
}
const updateTheme = async (dark: boolean, color?: string) => {
if (typeof window === 'undefined') return;
if (typeof window.MutationObserver === 'undefined') return;
if (!ConfigProvider.config) return;
ConfigProvider.config({
theme: {
primaryColor: genStringToTheme(color) || '#1890ff',
},
});
if (dark) {
const defaultTheme = {
brightness: 100,
contrast: 90,
sepia: 0,
};
const defaultFixes = {
invert: [],
css: '',
ignoreInlineStyle: ['.react-switch-handle'],
ignoreImageAnalysis: [],
disableStyleSheetsProxy: true,
};
if (window.MutationObserver && window.fetch) {
setFetch(window.fetch);
darkreaderEnable(defaultTheme, defaultFixes);
}
} else {
if (window.MutationObserver) darkreaderDisable();
}
};
export const enableDarkMode = () => {
updateTheme(true)
}
export const disableDarkMode = () => {
updateTheme(false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment