@@ -13,17 +13,27 @@ const changeFavicon = (src) => {
1313const { mode } = window . __perfect_dark_mode__
1414
1515// Get some elements we will use.
16- const toggleElement = document . getElementById ( ' toggle')
16+ const toggleElements = document . querySelectorAll ( '. toggle')
1717
1818// Listen to the color mode and update the UI.
1919mode . subscribe ( ( v ) => {
20- toggleElement . textContent = v === 'dark' ? '🌚 Dark' : '🌝 Light'
20+ toggleElements . forEach (
21+ ( el ) => ( el . textContent = v === 'dark' ? '🌚 Dark' : '🌝 Light' ) ,
22+ )
2123 changeFavicon ( v === 'dark' ? 'moon.png' : 'sun.png' )
2224} )
2325
2426// At this point our callback will have been called,
2527// so the checkbox state and text will be correct and we can show them.
26- toggleElement . style . visibility = 'unset'
28+ toggleElements . forEach ( ( el ) => ( el . style . visibility = 'unset' ) )
29+
30+ // When the checkbox is clicked we update the mode.
31+ // Our listener above will do the rest.
32+ toggleElements . forEach ( ( el ) => {
33+ el . addEventListener ( 'click' , ( e ) =>
34+ mode . update ( ( v ) => ( v === 'light' ? 'dark' : 'light' ) ) ,
35+ )
36+ } )
2737
2838// We want to make sure that if the OS mode is 'light'
2939// and the saved mode is 'dark' that we do not transition
@@ -37,12 +47,6 @@ requestAnimationFrame(() =>
3747 ) ,
3848)
3949
40- // When the checkbox is clicked we update the mode.
41- // Our listener above will do the rest.
42- toggleElement . addEventListener ( 'click' , ( e ) =>
43- mode . update ( ( v ) => ( v === 'light' ? 'dark' : 'light' ) ) ,
44- )
45-
4650const button = document . getElementById ( 'button' )
4751
4852button . addEventListener ( 'click' , ( ) => mode . set ( undefined ) )
0 commit comments