Skip to content

Commit

Permalink
Watch prefers-color-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong committed May 14, 2024
1 parent ca13929 commit bd46f94
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/components/Layout/Dashboard/Header/HeaderTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import Cookies from 'js-cookie'
import React, { useCallback, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import {
Dropdown, DropdownItem, DropdownMenu, DropdownToggle, NavLink,
Expand Down Expand Up @@ -45,21 +45,21 @@ export default function HeaderTheme({ currentPreferredTheme }: { currentPreferre
router.refresh()
}, [router])

useMediaQuery(
const isDarkMode = useMediaQuery(
{
query: '(prefers-color-scheme: dark)',
},
undefined,
(prefersDark) => {
if (preferredTheme !== Theme.Auto) {
return
}

Cookies.set('theme', prefersDark ? Theme.Dark : Theme.Light)
router.refresh()
},
)

useEffect(() => {
if (preferredTheme !== Theme.Auto) {
return
}

Cookies.set('theme', isDarkMode ? Theme.Dark : Theme.Light)
router.refresh()
}, [isDarkMode, preferredTheme, router])

return (
<Dropdown>
<DropdownToggle className="px-2 mx-1 px-sm-3 mx-sm-0" as={NavLink} bsPrefix="hide-caret" id="dropdown-theme">
Expand Down

0 comments on commit bd46f94

Please sign in to comment.