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

Update theme.tsx #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update theme.tsx #27

wants to merge 1 commit into from

Conversation

ollobrains
Copy link

import React from 'react';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';

import Moon from './icons/moon';
import Sun from './icons/sun';

/**

  • A simple theme toggle that switches between 'light' and 'dark' mode.
  • If process.env.theme isn't 'both', the toggle remains hidden. */ export default function ThemeToggle() {
    const { theme, setTheme, systemTheme } = useTheme();
    const [mounted, setMounted] = useState(false);

// Prevents hydration errors by ensuring the component
// doesn't render icons until the client is mounted.
useEffect(() => {
setMounted(true);
}, []);

// Return null if environment is not configured for theme toggling
if (process.env.theme !== 'both') return null;

// If we haven't mounted yet, do not render icons to avoid SSR mismatches
if (!mounted) return null;

// If you want "system" theme to behave as 'light' or 'dark',
// use a condition like (theme === 'system' && systemTheme === 'light').
const isLight = theme === 'light' || (theme === 'system' && systemTheme === 'light');

return isLight ? (
<Moon
width="2em"
height="2em"
style={{ cursor: 'pointer' }}
onClick={() => setTheme('dark')}
/>
) : (
<Sun
width="2em"
height="2em"
style={{ cursor: 'pointer' }}
onClick={() => setTheme('light')}
/>
);
}

import React from 'react';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';

import Moon from './icons/moon';
import Sun from './icons/sun';

/**
 * A simple theme toggle that switches between 'light' and 'dark' mode.
 * If process.env.theme isn't 'both', the toggle remains hidden.
 */
export default function ThemeToggle() {
  const { theme, setTheme, systemTheme } = useTheme();
  const [mounted, setMounted] = useState(false);

  // Prevents hydration errors by ensuring the component
  // doesn't render icons until the client is mounted.
  useEffect(() => {
    setMounted(true);
  }, []);

  // Return null if environment is not configured for theme toggling
  if (process.env.theme !== 'both') return null;

  // If we haven't mounted yet, do not render icons to avoid SSR mismatches
  if (!mounted) return null;

  // If you want "system" theme to behave as 'light' or 'dark', 
  // use a condition like (theme === 'system' && systemTheme === 'light').
  const isLight = theme === 'light' || (theme === 'system' && systemTheme === 'light');

  return isLight ? (
    <Moon
      width="2em"
      height="2em"
      style={{ cursor: 'pointer' }}
      onClick={() => setTheme('dark')}
    />
  ) : (
    <Sun
      width="2em"
      height="2em"
      style={{ cursor: 'pointer' }}
      onClick={() => setTheme('light')}
    />
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant