This repository has been archived on 2023-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
nord-for-org/web/dart/theme.dart
Lucien Cartier-Tilet f99d45e8ab
Simplify code, remove html dependency
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.
2020-08-25 03:34:55 +02:00

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))
);
}