11document . addEventListener ( 'DOMContentLoaded' , ( ) => {
2- // Define a set of random color palettes
32 const randomPalettes = [
43 // Palette 1
54 {
@@ -37,17 +36,18 @@ document.addEventListener('DOMContentLoaded', () => {
3736 if ( themeToggle ) {
3837 const body = document . body ;
3938 const savedTheme = localStorage . getItem ( 'theme' ) ;
40- let currentTheme = 'light' ; // Default
39+ const savedRandomIndex = localStorage . getItem ( 'randomIndex' ) ;
40+ let currentTheme = 'light' ;
4141
4242 if ( savedTheme ) {
4343 currentTheme = savedTheme ;
44- if ( currentTheme === 'random' ) {
45- applyRandomTheme ( body , randomPalettes ) ;
44+ if ( currentTheme === 'random' && savedRandomIndex !== null ) {
45+ // Use the saved random palette index
46+ applyRandomTheme ( body , randomPalettes , savedRandomIndex ) ;
4647 } else {
4748 body . classList . add ( currentTheme ) ;
4849 }
4950 } else {
50- // Fallback to the user's system preference
5151 if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
5252 currentTheme = 'dark' ;
5353 body . classList . add ( currentTheme ) ;
@@ -64,15 +64,19 @@ document.addEventListener('DOMContentLoaded', () => {
6464 body . classList . remove ( 'dark' ) ;
6565 body . classList . add ( 'light' ) ;
6666 removeRandomTheme ( body ) ;
67+ localStorage . removeItem ( 'randomIndex' ) ;
6768 } else if ( currentTheme === 'light' ) {
6869 currentTheme = 'random' ;
6970 body . classList . remove ( 'light' ) ;
70- applyRandomTheme ( body , randomPalettes ) ;
71+ const newRandomIndex = Math . floor ( Math . random ( ) * randomPalettes . length ) ;
72+ applyRandomTheme ( body , randomPalettes , newRandomIndex ) ;
73+ localStorage . setItem ( 'randomIndex' , newRandomIndex ) ;
7174 } else { // It's 'random'
7275 currentTheme = 'dark' ;
7376 body . classList . remove ( 'random' ) ;
7477 body . classList . add ( 'dark' ) ;
7578 removeRandomTheme ( body ) ;
79+ localStorage . removeItem ( 'randomIndex' ) ;
7680 }
7781 localStorage . setItem ( 'theme' , currentTheme ) ;
7882 updateToggleText ( themeToggle , currentTheme ) ;
@@ -84,27 +88,27 @@ document.addEventListener('DOMContentLoaded', () => {
8488 if ( theme === 'dark' ) {
8589 button . textContent = '☀️' ;
8690 } else if ( theme === 'light' ) {
87- button . textContent = '🎨' ; // Paint palette for random
91+ button . textContent = '🎨' ;
8892 } else {
8993 button . textContent = '🌙' ;
9094 }
9195 }
9296
93- // Helper function to apply random theme colors
94- function applyRandomTheme ( body , palettes ) {
97+ // Helper function to apply random theme colors based on index
98+ function applyRandomTheme ( body , palettes , index ) {
9599 body . classList . add ( 'random' ) ;
96- const randomPalette = palettes [ Math . floor ( Math . random ( ) * palettes . length ) ] ;
97- for ( const [ property , value ] of Object . entries ( randomPalette ) ) {
100+ const palette = palettes [ index ] ;
101+ for ( const [ property , value ] of Object . entries ( palette ) ) {
98102 body . style . setProperty ( property , value ) ;
99103 }
100104 }
101105
102106 // Helper function to remove random theme styles
103107 function removeRandomTheme ( body ) {
104108 body . classList . remove ( 'random' ) ;
105- const randomPalette = randomPalettes [ 0 ] ; // Use any palette to get property keys
109+ const randomPalette = randomPalettes [ 0 ] ;
106110 for ( const property of Object . keys ( randomPalette ) ) {
107111 body . style . removeProperty ( property ) ;
108112 }
109113 }
110- } ) ;
114+ } ) ;
0 commit comments