Detect System Theme Desire Change Utilizing JavaScript


JavaScript and CSS permit customers to detect the consumer theme desire with CSS’ prefers-color-scheme media question. It is normal nowadays to make use of that desire to indicate the darkish or gentle theme on a given web site. However what if the consumer adjustments their desire whereas utilizing your app?


To detect a system theme desire change utilizing JavaScript, it is advisable to mix matchMedia, prefers-color-scheme, and an occasion listener:

window.matchMedia('(prefers-color-scheme: darkish)')
      .addEventListener('change',({ matches }) => {
  if (matches) {
    console.log("change to darkish mode!")
  } else {
    console.log("change to gentle mode!")

The change occasion of the matchMedia API notifies you when the system desire adjustments. You should use this occasion to mechanically replace the location’s show in actual time.

I like that this API permits detecting consumer desire on a system degree. Catering to consumer wants is a crucial a part of creating a fantastic net expertise!

  • Facebook Open Graph META Tags

    It is no secret that Fb has change into a serious site visitors driver for every type of internet sites.  These days even giant companies steer shoppers towards their Fb pages as an alternative of the company web sites immediately.  And naturally there are Fb “Like” and “Advocate” widgets on each web site.  One…

  • 9 Mind-Blowing WebGL Demos

    As a lot as builders now detest Flash, we’re nonetheless enjoying a little bit of catch as much as natively duplicate the animation capabilities that Adobe’s outdated know-how supplied us.  In fact we now have canvas, an superior know-how, one which I highlighted 9 mind-blowing demos.  One other know-how out there…