-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathdark-init.js
29 lines (26 loc) · 1.17 KB
/
dark-init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// This script is run blocking at the beginning of the page load to ensure that
// we don't have flashing white background before switching to dark mode.
(function () {
// Detects OS or browser-level theme preference by using media queries.
let mediaPrefersDarkScheme = false;
try {
mediaPrefersDarkScheme =
window.matchMedia('(prefers-color-scheme: dark)').matches;
} catch (e) {
// ignore errors e.g. when media query matching is not supported
}
// Detects whether the control widget was set to use a specific theme
let colorTheme = window.localStorage.getItem('colorTheme');
let lightThemeIsSet = colorTheme == 'false';
let darkThemeIsSet = colorTheme == 'true';
if (lightThemeIsSet) {
// nothing to do here, the default style is the light-theme
} else if (mediaPrefersDarkScheme || darkThemeIsSet) {
// switch to the dark theme
document.body.classList.remove('light-theme');
document.body.classList.add('dark-theme');
}
})();