AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

use-appearance.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useSyncExternalStore } from 'react';
  2. export type ResolvedAppearance = 'light' | 'dark';
  3. export type Appearance = ResolvedAppearance | 'system';
  4. export type UseAppearanceReturn = {
  5. readonly appearance: Appearance;
  6. readonly resolvedAppearance: ResolvedAppearance;
  7. readonly updateAppearance: (mode: Appearance) => void;
  8. };
  9. const listeners = new Set<() => void>();
  10. let currentAppearance: Appearance = 'system';
  11. const prefersDark = (): boolean => {
  12. if (typeof window === 'undefined') {
  13. return false;
  14. }
  15. return window.matchMedia('(prefers-color-scheme: dark)').matches;
  16. };
  17. const setCookie = (name: string, value: string, days = 365): void => {
  18. if (typeof document === 'undefined') {
  19. return;
  20. }
  21. const maxAge = days * 24 * 60 * 60;
  22. document.cookie = `${name}=${value};path=/;max-age=${maxAge};SameSite=Lax`;
  23. };
  24. const getStoredAppearance = (): Appearance => {
  25. if (typeof window === 'undefined') {
  26. return 'system';
  27. }
  28. return (localStorage.getItem('appearance') as Appearance) || 'system';
  29. };
  30. const isDarkMode = (appearance: Appearance): boolean => {
  31. return appearance === 'dark' || (appearance === 'system' && prefersDark());
  32. };
  33. const applyTheme = (appearance: Appearance): void => {
  34. if (typeof document === 'undefined') {
  35. return;
  36. }
  37. const isDark = isDarkMode(appearance);
  38. document.documentElement.classList.toggle('dark', isDark);
  39. document.documentElement.style.colorScheme = isDark ? 'dark' : 'light';
  40. };
  41. const subscribe = (callback: () => void) => {
  42. listeners.add(callback);
  43. return () => listeners.delete(callback);
  44. };
  45. const notify = (): void => listeners.forEach((listener) => listener());
  46. const mediaQuery = (): MediaQueryList | null => {
  47. if (typeof window === 'undefined') {
  48. return null;
  49. }
  50. return window.matchMedia('(prefers-color-scheme: dark)');
  51. };
  52. const handleSystemThemeChange = (): void => applyTheme(currentAppearance);
  53. export function initializeTheme(): void {
  54. if (typeof window === 'undefined') {
  55. return;
  56. }
  57. if (!localStorage.getItem('appearance')) {
  58. localStorage.setItem('appearance', 'system');
  59. setCookie('appearance', 'system');
  60. }
  61. currentAppearance = getStoredAppearance();
  62. applyTheme(currentAppearance);
  63. // Set up system theme change listener
  64. mediaQuery()?.addEventListener('change', handleSystemThemeChange);
  65. }
  66. export function useAppearance(): UseAppearanceReturn {
  67. const appearance: Appearance = useSyncExternalStore(
  68. subscribe,
  69. () => currentAppearance,
  70. () => 'system',
  71. );
  72. const resolvedAppearance: ResolvedAppearance = isDarkMode(appearance)
  73. ? 'dark'
  74. : 'light';
  75. const updateAppearance = (mode: Appearance): void => {
  76. currentAppearance = mode;
  77. // Store in localStorage for client-side persistence...
  78. localStorage.setItem('appearance', mode);
  79. // Store in cookie for SSR...
  80. setCookie('appearance', mode);
  81. applyTheme(mode);
  82. notify();
  83. };
  84. return { appearance, resolvedAppearance, updateAppearance } as const;
  85. }