Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate AccentColorSelector logic in conjunction with it's BaseLayout script #509

Open
AVGVSTVS96 opened this issue Jul 20, 2024 — with Linear · 1 comment
Assignees
Labels
bug Something isn't working refactor Update code structure

Comments

Copy link
Owner

AVGVSTVS96 commented Jul 20, 2024

export const AccentColorSelector = () => {
  const isBrowser = typeof window !== 'undefined' && window.localStorage;
  const [selectedTheme, setSelectedTheme] = useState(() => {
    const storedTheme =
      isBrowser && window.localStorage.getItem('themeSwitcher');
    return storedTheme;
  });

  useEffect(() => {
    return () => {
      window.removeEventListener('storage', handleStorageChange);
    };
  }, []);

  const handleStorageChange = () => {
    const storedTheme = localStorage.getItem('themeSwitcher') || 'sky';

    if (isBrowser && storedTheme !== selectedTheme) {
      setSelectedTheme(storedTheme);
      document.documentElement.setAttribute('data-theme', storedTheme);
    }
  };

  isBrowser && window.addEventListener('storage', handleStorageChange);

  const handleThemeChange = (href: string) => {
    const theme = href.substring(1);
    setSelectedTheme(theme);
    localStorage.setItem('themeSwitcher', theme);
    document.documentElement.setAttribute('data-theme', theme);
  };
 </x-turndown>
  const storedThemeColor = localStorage.getItem('themeSwitcher');
  const theme = storedThemeColor || 'sky';
  document.documentElement.setAttribute('data-theme', theme);
@AVGVSTVS96 AVGVSTVS96 self-assigned this Jul 20, 2024
@AVGVSTVS96 AVGVSTVS96 added refactor Update code structure Improvement labels Jul 20, 2024 — with Linear
@AVGVSTVS96 AVGVSTVS96 added the bug Something isn't working label Jul 20, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working refactor Update code structure
Projects
None yet
Development

No branches or pull requests

1 participant