From 05f1dfc6896d3affa7c494a1b955f230d836a4b7 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 27 Oct 2023 18:07:45 +0200 Subject: feat: replace next-themes with a custom ThemeProvider To be honest, next-themes was working fine. However since I use a theme provider for Prism code blocks, some code is duplicated between this app and the library. So I prefer to use a custom Provider without the options I don't need. --- src/utils/helpers/themes.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/utils/helpers/themes.ts (limited to 'src/utils/helpers/themes.ts') diff --git a/src/utils/helpers/themes.ts b/src/utils/helpers/themes.ts new file mode 100644 index 0000000..8ef3a19 --- /dev/null +++ b/src/utils/helpers/themes.ts @@ -0,0 +1,18 @@ +/** + * Check if the user prefers dark color scheme. + * + * @returns {boolean|undefined} True if `prefers-color-scheme` is set to `dark`. + */ +export const prefersDarkScheme = (): boolean | undefined => { + if (typeof window === 'undefined') return undefined; + + return window.matchMedia('(prefers-color-scheme: dark)').matches; +}; + +/** + * Retrieve the theme to use depending on the user system theme. + */ +export const getThemeFromSystem = () => { + if (prefersDarkScheme()) return 'dark'; + return 'light'; +}; -- cgit v1.2.3