Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. function setColorScheme() {
  2. const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches
  3. const isLightMode = window.matchMedia("(prefers-color-scheme: light)").matches
  4. const isNotSpecified = window.matchMedia("(prefers-color-scheme: no-preference)").matches
  5. const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified;
  6.  
  7. window.matchMedia("(prefers-color-scheme: dark)").addListener(e => e.matches && activateDarkMode())
  8. window.matchMedia("(prefers-color-scheme: light)").addListener(e => e.matches && activateLightMode())
  9.  
  10. if(isDarkMode) activateDarkMode()
  11. if(isLightMode) activateLightMode()
  12. if(isNotSpecified || hasNoSupport) {
  13. console.log('You specified no preference for a color scheme or your browser does not support it. I Schedule dark mode during night time.')
  14. now = new Date();
  15. hour = now.getHours();
  16. if (hour < 4 || hour >= 16) {
  17. activateDarkMode();
  18. }
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement