|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | 3 | import useUI from '@austinserb/react-zero-ui'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | + |
| 6 | +// Component that automatically cycles through themes using useEffect |
| 7 | +function AutoThemeComponent() { |
| 8 | + const [, setAutoTheme] = useUI<'light' | 'dark'>('auto-theme', 'light'); |
| 9 | + const [isRunning, setIsRunning] = useState(false); |
| 10 | + const [cycleCount, setCycleCount] = useState(0); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (!isRunning) return; |
| 14 | + |
| 15 | + const interval = setInterval(() => { |
| 16 | + setAutoTheme(prev => { |
| 17 | + const newTheme = prev === 'light' ? 'dark' : 'light'; |
| 18 | + setCycleCount(count => count + 1); |
| 19 | + return newTheme; |
| 20 | + }); |
| 21 | + }, 2000); // Switch every 2 seconds |
| 22 | + |
| 23 | + return () => clearInterval(interval); |
| 24 | + }, [isRunning, setAutoTheme]); |
| 25 | + |
| 26 | + return ( |
| 27 | + <div |
| 28 | + className="auto-theme-light:bg-blue-50 auto-theme-dark:bg-blue-900 auto-theme-light:border-blue-200 auto-theme-dark:border-blue-700 border-2 rounded-lg p-6 transition-colors duration-500" |
| 29 | + data-testid="auto-theme-container"> |
| 30 | + <h3 className="auto-theme-light:text-blue-900 auto-theme-dark:text-blue-100 text-xl font-bold mb-4">Auto Theme Switcher (useEffect Test)</h3> |
| 31 | + |
| 32 | + <div className="space-y-4"> |
| 33 | + <button |
| 34 | + type="button" |
| 35 | + onClick={() => setIsRunning(prev => !prev)} |
| 36 | + className="auto-theme-light:bg-blue-600 auto-theme-dark:bg-blue-400 auto-theme-light:text-white auto-theme-dark:text-blue-900 auto-theme-light:hover:bg-blue-700 auto-theme-dark:hover:bg-blue-300 px-4 py-2 rounded-md font-medium transition-colors" |
| 37 | + data-testid="auto-theme-toggle"> |
| 38 | + {isRunning ? 'Stop Auto Theme' : 'Start Auto Theme'} |
| 39 | + </button> |
| 40 | + |
| 41 | + <div className="auto-theme-light:bg-white auto-theme-dark:bg-blue-800 auto-theme-light:border-blue-100 auto-theme-dark:border-blue-600 border rounded-md p-4"> |
| 42 | + <div className="space-y-2"> |
| 43 | + <div className="auto-theme-light:text-blue-800 auto-theme-dark:text-blue-200"> |
| 44 | + Current Theme: |
| 45 | + <span className="auto-theme-light:block hidden font-semibold text-blue-600"> Light Mode</span> |
| 46 | + <span className="auto-theme-dark:block hidden font-semibold text-blue-300"> Dark Mode</span> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div className="auto-theme-light:text-blue-600 auto-theme-dark:text-blue-400 text-sm">Status: {isRunning ? 'Auto-switching...' : 'Stopped'}</div> |
| 50 | + |
| 51 | + <div className="auto-theme-light:text-blue-500 auto-theme-dark:text-blue-500 text-sm">Cycle Count: {cycleCount}</div> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + |
| 55 | + <div className="grid grid-cols-2 gap-4"> |
| 56 | + <div className="auto-theme-light:bg-green-100 auto-theme-dark:bg-green-800 auto-theme-light:text-green-800 auto-theme-dark:text-green-200 p-3 rounded text-center"> |
| 57 | + <div className="auto-theme-light:block hidden">☀️ Light Theme Active</div> |
| 58 | + <div className="auto-theme-dark:block hidden">🌙 Dark Theme Active</div> |
| 59 | + </div> |
| 60 | + |
| 61 | + <div className="auto-theme-light:bg-purple-100 auto-theme-dark:bg-purple-800 auto-theme-light:text-purple-800 auto-theme-dark:text-purple-200 p-3 rounded text-center"> |
| 62 | + <div className="auto-theme-light:text-purple-600 auto-theme-dark:text-purple-300">Reactive UI</div> |
| 63 | + <div className="text-sm mt-1">No Re-renders!</div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +} |
4 | 70 |
|
5 | 71 | export default function Page() { |
6 | 72 | const [, setTheme] = useUI<'light' | 'dark'>('theme', 'light'); |
7 | 73 | const [, setTheme2] = useUI<'light' | 'dark'>('theme-2', 'light'); |
8 | 74 | const [, setThemeThree] = useUI<'light' | 'dark'>('themeThree', 'light'); |
9 | 75 | return ( |
10 | 76 | <> |
| 77 | + {/* Auto Theme Component - NEW */} |
| 78 | + <AutoThemeComponent /> |
| 79 | + |
| 80 | + <hr className="my-8" /> |
| 81 | + |
11 | 82 | <div |
12 | 83 | className="theme-light:bg-gray-100 theme-dark:bg-gray-900 text-blue-900" |
13 | 84 | data-testid="theme-container"> |
|
0 commit comments