mattrguerra/cortexmainPrivate on GitHub, published here in full

theme.ts

Language TypeScript1.3 KB28 lines

src/lib/theme.ts

  1. import { useEffect } from 'react'
  2. import type { Settings } from '../ipc/gen/Settings'
  3. /** Resolve the theme and transparency settings onto <html>. */
  4. export function useTheme(settings: Settings | null, vibrancy: boolean) {
  5. useEffect(() => {
  6. const root = document.documentElement
  7. const media = window.matchMedia('(prefers-color-scheme: dark)')
  8. const apply = () => {
  9. const pref = settings?.theme ?? 'system'
  10. root.dataset.theme = pref === 'system' ? (media.matches ? 'dark' : 'light') : pref
  11. root.dataset.vibrancy = vibrancy ? 'native' : 'none'
  12. root.dataset.reduceTransparency = String(!!settings?.reduce_transparency)
  13. root.style.setProperty('--tint', String((settings?.glass_tint ?? 100) / 100))
  14. }
  15. apply()
  16. media.addEventListener('change', apply)
  17. return () => media.removeEventListener('change', apply)
  18. }, [settings?.theme, settings?.reduce_transparency, settings?.glass_tint, vibrancy])
  19. }
  20. export const isMac = typeof navigator !== 'undefined' && /Mac/i.test(navigator.platform || navigator.userAgent)
  21. export const mod = isMac ? '⌘' : 'Ctrl+'
  22. /** The quick window's height with no answer, and the most it grows to: `QUICK_HEIGHT` and `QUICK_MAX` in windows.rs. */
  23. export const QUICK_HEIGHT = 76
  24. export const QUICK_MAX = 560