mirror of
https://github.com/mastodon/mastodon.git
synced 2026-08-20 05:44:40 -05:00
Add ability to include inline javascript (#37459)
This commit is contained in:
23
app/javascript/inline/theme-selection.js
Normal file
23
app/javascript/inline/theme-selection.js
Normal file
@@ -0,0 +1,23 @@
|
||||
(function (element) {
|
||||
const {userTheme} = element.dataset;
|
||||
|
||||
const colorSchemeMediaWatcher = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const contrastMediaWatcher = window.matchMedia('(prefers-contrast: more)');
|
||||
|
||||
const updateColorScheme = () => {
|
||||
const useDarkMode = userTheme === 'system' ? colorSchemeMediaWatcher.matches : userTheme !== 'mastodon-light';
|
||||
element.dataset.mode = useDarkMode ? 'dark' : 'light';
|
||||
};
|
||||
|
||||
const updateContrast = () => {
|
||||
const useHighContrast = userTheme === 'contrast' || contrastMediaWatcher.matches;
|
||||
|
||||
element.dataset.contrast = useHighContrast ? 'high' : 'default';
|
||||
}
|
||||
|
||||
colorSchemeMediaWatcher.addEventListener('change', updateColorScheme);
|
||||
contrastMediaWatcher.addEventListener('change', updateContrast);
|
||||
|
||||
updateColorScheme();
|
||||
updateContrast();
|
||||
})(document.documentElement);
|
||||
Reference in New Issue
Block a user