Lucien Cartier-Tilet
f99d45e8ab
This commit removes the `html' package dependency, speeding up the compile time of the Dart code. It also simplifies the code and removes some unused code. For instance the sun, moon, and lightbulb icons is removed, the `Theme' class is removed, and the `switchTheme' function is now merged into the `setTheme' function. The `makeThemeItem' function has also had its second argument removed.
24 lines
634 B
Dart
24 lines
634 B
Dart
import 'dart:html';
|
|
|
|
final localStorage = window.localStorage;
|
|
|
|
Future<void> setTheme([String theme]) async {
|
|
final currentTheme = theme ?? localStorage['theme'] ??
|
|
() {
|
|
final devicePrefersDark =
|
|
window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
return devicePrefersDark ? 'dark' : 'light';
|
|
}();
|
|
localStorage['theme'] = currentTheme;
|
|
querySelector('body')
|
|
..classes.clear()
|
|
..classes.add(currentTheme);
|
|
}
|
|
|
|
void enableThemeChanger() {
|
|
final themes = <String>['light', 'dark', 'black'];
|
|
themes.forEach((theme) =>
|
|
querySelector('#${theme}Btn').onClick.listen((_) => setTheme(theme))
|
|
);
|
|
}
|