Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect dark mode using JavaScript?

Windows and macOS now have dark mode.

For CSS I can use:

@media (prefers-dark-interface) { 
  color: white; background: black 
}

But I am using the Stripe Elements API, which puts colors in JavaScript

For example:

const stripeElementStyles = {
  base: {
    color: COLORS.darkGrey,
    fontFamily: `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"`,
    fontSize: '18px',
    fontSmoothing: 'antialiased',
    '::placeholder': {
      color: COLORS.midgrey
    },
    ':-webkit-autofill': {
      color: COLORS.icyWhite
    }
  }
}

How can I detect the OS's preferred color scheme in JavaScript?

like image 455
mikemaccana Avatar asked Oct 17 '22 12:10

mikemaccana


People also ask

How do you test for dark mode in HTML?

The command line flag --force-dark-mode=true will force the Chrome browser to use the Dark appearance even if the host OS has the Light appearance set.

How does JavaScript night mode work?

Steps to Create Dark/Light mode: Create an HTML Document. Create CSS for the document file as well as for dark mode. Add a switch/toggler to toggle between light and dark mode. Add functionality to the switch/toggler to toggle between light and dark mode using javascript or jQuery code.

How do I enable Dark mode on a JavaScript window?

Create a new file named script.js and open it in your code editor. We first create a variable called dark and assign it to the window.matchMedia function. This function will return a MediaQueryList object. This object will contain a matches property that will be true if the system dark mode is enabled.

How do I check if dark mode is enabled in code?

If you want to check if dark-mode is enabled somewhere in your code, you would simply check if matchMedia exists on the browser and check if it has prefers-color-scheme set to dark.

How to detect if a website is using dark-mode in Java?

You can detect if a website is using dark-mode using the prefers-color-scheme media query and checking if its value is dark. Within the Browser, the window.matchMedia method returns a new MediaQueryList object representing the parsed results of the specified media query string.

How do I detect dark mode on matchmedia?

Here’s how we can do it. First, detect if the matchMedia object exists (otherwise the browser does not support dark mode, and you can fall back to light mode). Then, check if it’s dark mode using This will return true if dark mode is enabled. Here’s a full example, where I invert the colors of an image if it’s dark mode:


3 Answers

if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    // dark mode
}

To watch for changes:

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
    const newColorScheme = event.matches ? "dark" : "light";
});
like image 469
Mark Szabo Avatar answered Oct 21 '22 15:10

Mark Szabo


You can check the CSS Media-Queries directly with JavaScript

The window.matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string. The value of the matchMedia() method can be any of the media features of the CSS @media rule, like min-height, min-width, orientation, etc.

To check if the Media-Query is true, the matches property can be used

// Check to see if Media-Queries are supported
if (window.matchMedia) {
  // Check if the dark-mode Media-Query matches
  if(window.matchMedia('(prefers-color-scheme: dark)').matches){
    // Dark
  } else {
    // Light
  }
} else {
  // Default (when Media-Queries are not supported)
}

To update the color-scheme dynamically based on the user's preference, the following can be used:

function setColorScheme(scheme) {
  switch(scheme){
    case 'dark':
      // Dark
      break;
    case 'light':
      // Light
      break;
    default:
      // Default
      break;
  }
}

function getPreferredColorScheme() {
  if (window.matchMedia) {
    if(window.matchMedia('(prefers-color-scheme: dark)').matches){
      return 'dark';
    } else {
      return 'light';
    }
  }
  return 'light';
}

if(window.matchMedia){
  var colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
  colorSchemeQuery.addEventListener('change', setColorScheme(getPreferredColorScheme()));
}
like image 21
SanBen Avatar answered Oct 21 '22 14:10

SanBen


According to MediaQueryList - Web APIs | MDN, addListener is the correct way to listen to the change. addEventListener is not working for me on iOS 13.4.

window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
  console.log(`changed to ${e.matches ? "dark" : "light"} mode`)
});
like image 16
imgg Avatar answered Oct 21 '22 14:10

imgg