30秒学会 JavaScript 片段 · 2022年3月15日

30秒学会 JavaScript 片段 – Detect user color scheme preference in JavaScript

The recently introduced prefers-color-scheme media query allows us to check if the user prefers a light or dark color scheme. This can be a useful tool when implementing a dark mode for your website or web application.

Check if user prefers a dark color scheme

Combining the media query with the Window.matchMedia() method allows us to check if the user has selected a dark color scheme ('dark') in their operating system settings.

代码实现

const prefersDarkColorScheme = () =>
  window &&
  window.matchMedia &&
  window.matchMedia('(prefers-color-scheme: dark)').matches;

Check if user prefers a light color scheme

The same technique can be used for checking if the user prefers a light color scheme. Bear in mind, however, that 'light' is the default value for the prefers-color-scheme media query. This means that it might also mean that the user has not expressed a preference.

使用样例

const prefersLightColorScheme = () =>
  window &&
  window.matchMedia &&
  window.matchMedia('(prefers-color-scheme: light)').matches;

翻译自:https://www.30secondsofcode.org/js/s/prefers-color-scheme