Skip to content

Commit

Permalink
refactor: useThemeMode - cleanup; change LS key name to be specific
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Sutu committed Sep 25, 2023
1 parent e7b6f9c commit fc42a74
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/helpers/use-theme-mode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';

import { useEffect, useState } from 'react';
import { isClient } from './is-client';

const LS_THEME_MODE = 'theme-mode';
const LS_THEME_MODE = 'flowbite-theme-mode';

// TODO: add `system` as well
export type Mode = 'light' | 'dark';
Expand All @@ -27,8 +28,6 @@ export const useThemeMode = () => {
* Sets `mode` to a specific value (`light | dark`)
*/
const handleSetMode = (mode: Mode) => {
if (!isClient()) return;

setMode(mode);
setModeInLS(mode);
setModeOnBody(mode);
Expand All @@ -50,16 +49,10 @@ const setModeOnBody = (mode: Mode) => {
};

const prefersColorScheme: () => Mode = () => {
if (!isClient()) {
return 'light';
}

return window.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
};

const getInitialMode: () => Mode = () => {
if (!isClient()) return 'light';

const LSMode = localStorage.getItem(LS_THEME_MODE) as Mode | undefined;

return LSMode ?? prefersColorScheme();
Expand Down

0 comments on commit fc42a74

Please sign in to comment.