aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/themes.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers/themes.ts')
-rw-r--r--src/utils/helpers/themes.ts18
1 files changed, 18 insertions, 0 deletions
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';
+};