@@ -72,6 +72,7 @@ class ReadChapter extends React.Component {
7272 htmlContent,
7373 isMobile : false ,
7474 hideHeader : false ,
75+ darkTheme : true ,
7576 } ;
7677 }
7778
@@ -83,6 +84,16 @@ class ReadChapter extends React.Component {
8384 if ( this . state . isMobile !== isMobile ) {
8485 this . setState ( { isMobile } ) ; // eslint-disable-line
8586 }
87+
88+ if ( typeof localStorage !== 'undefined' ) {
89+ if ( localStorage . getItem ( 'darkTheme' ) === 'true' ) {
90+ this . loadDarkTheme ( ) ;
91+ this . setState ( { darkTheme : true } ) ;
92+ } else if ( localStorage . getItem ( 'darkTheme' ) === 'false' ) {
93+ this . loadLightTheme ( ) ;
94+ this . setState ( { darkTheme : false } ) ;
95+ }
96+ }
8697 }
8798
8899 componentWillReceiveProps ( nextProps ) {
@@ -175,13 +186,107 @@ class ReadChapter extends React.Component {
175186 } ) ;
176187 } ;
177188
189+ changeThemeType = ( ) => {
190+ const { darkTheme } = this . state ;
191+
192+ if ( darkTheme === true ) {
193+ this . loadLightTheme ( ) ;
194+ localStorage . setItem ( 'darkTheme' , false ) ;
195+ this . setState ( { darkTheme : false } ) ;
196+ } else if ( darkTheme === false ) {
197+ this . loadDarkTheme ( ) ;
198+ localStorage . setItem ( 'darkTheme' , true ) ;
199+ this . setState ( { darkTheme : true } ) ;
200+ }
201+ } ;
202+
203+ loadLightTheme = ( ) => {
204+ const $ = document . querySelectorAll . bind ( document ) ;
205+
206+ const changeColors = ( color , background ) => ( e ) => {
207+ e . style . backgroundColor = background ;
208+ e . style . color = color ;
209+ } ;
210+
211+ const elements = [
212+ $ ( 'body' ) ,
213+ $ ( 'ol li a' ) ,
214+ $ ( 'h1' ) ,
215+ $ ( 'h2' ) ,
216+ $ ( 'h3' ) ,
217+ $ ( 'h4' ) ,
218+ $ ( 'h5' ) ,
219+ $ ( 'h6' ) ,
220+ $ ( 'i.material-icons' ) ,
221+ $ ( 'p code' ) ,
222+ $ ( '#__next div' ) ,
223+ ] ;
224+
225+ const black = 'black' ;
226+ const white = 'white' ;
227+ const blue = '#2289d1' ;
228+
229+ elements . forEach ( ( e ) => e . forEach ( changeColors ( black , white ) ) ) ;
230+ $ ( 'p a' ) . forEach ( changeColors ( blue , white ) ) ;
231+
232+ const style = document . createElement ( 'style' ) ;
233+ const link = document . createElement ( 'link' ) ;
234+
235+ link . setAttribute ( 'href' , 'https://fonts.googleapis.com/css?family=Roboto+Mono:400,400i' ) ;
236+ link . setAttribute ( 'rel' , 'stylesheet' ) ;
237+
238+ document . head . appendChild ( style ) ;
239+ document . head . appendChild ( link ) ;
240+ } ;
241+
242+ loadDarkTheme = ( ) => {
243+ const $ = document . querySelectorAll . bind ( document ) ;
244+
245+ const changeColors = ( color , background ) => ( e ) => {
246+ e . style . backgroundColor = background ;
247+ e . style . color = color ;
248+ } ;
249+
250+ const elements = [
251+ $ ( 'body' ) ,
252+ $ ( 'ol li a' ) ,
253+ $ ( 'h1' ) ,
254+ $ ( 'h2' ) ,
255+ $ ( 'h3' ) ,
256+ $ ( 'h4' ) ,
257+ $ ( 'h5' ) ,
258+ $ ( 'h6' ) ,
259+ $ ( 'i.material-icons' ) ,
260+ $ ( 'p code' ) ,
261+ $ ( '#__next div' ) ,
262+ ] ;
263+
264+ const black = 'black' ;
265+ const white = 'white' ;
266+ const blue = '#2289d1' ;
267+
268+ elements . forEach ( ( e ) => e . forEach ( changeColors ( white , black ) ) ) ;
269+ $ ( 'p a' ) . forEach ( changeColors ( blue , white ) ) ;
270+
271+ const style = document . createElement ( 'style' ) ;
272+ const link = document . createElement ( 'link' ) ;
273+
274+ link . setAttribute ( 'href' , 'https://fonts.googleapis.com/css?family=Roboto+Mono:400,400i' ) ;
275+ link . setAttribute ( 'rel' , 'stylesheet' ) ;
276+
277+ document . head . appendChild ( style ) ;
278+ document . head . appendChild ( link ) ;
279+ } ;
280+
178281 closeTocWhenMobile = ( ) => {
179282 this . setState ( { showTOC : ! this . state . isMobile } ) ;
180283 } ;
181284
182285 renderMainContent ( ) {
183286 const { user, showStripeModal } = this . props ;
184- const { chapter, htmlContent, isMobile, showTOC } = this . state ;
287+ const { chapter, htmlContent, isMobile, showTOC, darkTheme } = this . state ;
288+
289+ console . log ( 'rendered' , darkTheme ) ;
185290
186291 let padding = '20px 20%' ;
187292 if ( ! isMobile && showTOC ) {
@@ -343,15 +448,15 @@ class ReadChapter extends React.Component {
343448 left : '15px' ,
344449 } }
345450 >
346- < i //eslint-disable-line
347- className = "material-icons"
348- style = { styleIcon }
349- onClick = { this . toggleChapterList }
350- onKeyPress = { this . toggleChapterList }
351- role = "button"
352- >
451+ < i //eslint-disable-line
452+ className = "material-icons"
453+ style = { styleIcon }
454+ onClick = { this . toggleChapterList }
455+ onKeyPress = { this . toggleChapterList }
456+ role = "button"
457+ >
353458 format_list_bulleted
354- </ i >
459+ </ i >
355460
356461 { book . supportURL ? (
357462 < div >
@@ -376,6 +481,29 @@ class ReadChapter extends React.Component {
376481 activeSection = { this . state . activeSection }
377482 />
378483 ) : null }
484+ < div >
485+ { this . state . darkTheme ? (
486+ < i
487+ className = "material-icons"
488+ style = { { opacity : '0.75' , fontSize : '24px' , cursor : 'pointer' , color : 'white' } }
489+ onClick = { this . changeThemeType }
490+ onKeyPress = { this . changeThemeType }
491+ role = "none"
492+ >
493+ lens
494+ </ i >
495+ ) : (
496+ < i
497+ className = "material-icons"
498+ style = { { opacity : '0.75' , fontSize : '24px' , cursor : 'pointer' , color : 'black' } }
499+ onClick = { this . changeThemeType }
500+ onKeyPress = { this . changeThemeType }
501+ role = "none"
502+ >
503+ lens
504+ </ i >
505+ ) }
506+ </ div >
379507 </ div >
380508 </ div >
381509 ) ;
0 commit comments