Created
August 31, 2022 08:08
-
-
Save arush15june/0847b7d598ff42755b72d3399d7b7ae7 to your computer and use it in GitHub Desktop.
AntD Dark Mode with @umijs/ssr-darkreader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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