Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the jetpack domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/feedavenue.com/public_html/wp-includes/functions.php on line 6114
Detect System Theme Preference Change Using JavaScript - Feedavenue
Friday, January 10, 2025
HomeTechnologySoftwareDetect System Theme Preference Change Using JavaScript

Detect System Theme Preference Change Using JavaScript

Date:

Related stories

Amber Grimes Is The Best-Smelling Music Exec Around

“Because I’m from Atlanta, I’ve always been in...

16 Free Games January 2025

Happy New Year! Did you walk away from...

8 Best Teeth-Whitening Pens of 2025 for a Brighter Smile

Key ingredients: hydrogen peroxide (3.5%), fluoride (sodium monofluorophosphate,...
spot_imgspot_img

JavaScript and CSS allow users to detect the user theme preference with CSS’ prefers-color-scheme media query. It’s standard these days to use that preference to show the dark or light theme on a given website. But what if the user changes their preference while using your app?

To detect a system theme preference change using JavaScript, you need to combine matchMedia, prefers-color-scheme, and an event listener:

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

The change event of the matchMedia API notifies you when the system preference changes. You can use this event to automatically update the site’s display in real time.

I love that this API allows detecting user preference on a system level. Catering to user needs is an important part of creating a great web experience!

  • 9 More Mind-Blowing WebGL Demos
  • Chris Coyier’s Favorite CodePen Demos

    David asked me if I’d be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you…

  • New York Times-Style Text Selection Widget Using MooTools or jQuery
  • jQuery Countdown Plugin

    You’ve probably been to sites like RapidShare and MegaUpload that allow you to download files but make you wait a specified number of seconds before giving you the download link. I’ve created a similar script but my script allows you to animate the CSS font-size…



Source link

Latest stories

spot_img