diff --git a/assets/javascripts/app/settings.coffee b/assets/javascripts/app/settings.coffee index bfe8ae73..99fd5d13 100644 --- a/assets/javascripts/app/settings.coffee +++ b/assets/javascripts/app/settings.coffee @@ -32,10 +32,16 @@ class app.Settings manualUpdate: false schema: 1 analyticsConsent: false + theme: 'auto' constructor: -> @store = new CookiesStore @cache = {} + @autoSupported = window.matchMedia('(prefers-color-scheme)').media != 'not all' + if @autoSupported + @darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)') + @darkModeQuery.onchange = -> @setTheme(@get('theme')) + get: (key) -> return @cache[key] if @cache.hasOwnProperty(key) @@ -124,11 +130,15 @@ class app.Settings return setTheme: (theme) -> - if not theme - theme = if window.matchMedia('(prefers-color-scheme: dark)').matches then 'dark' else 'default' + if theme is 'auto' + theme = if @darkModeQuery.matches then 'dark' else 'default' classList = document.documentElement.classList classList.remove('_theme-default', '_theme-dark') classList.add('_theme-' + theme) + @updateColorMeta() + return + + updateColorMeta: -> color = getComputedStyle(document.documentElement).getPropertyValue('--headerBackground').trim() $('meta[name=theme-color]').setAttribute('content', color) return diff --git a/assets/javascripts/templates/pages/settings_tmpl.coffee b/assets/javascripts/templates/pages/settings_tmpl.coffee index 07bfc65a..6d227069 100644 --- a/assets/javascripts/templates/pages/settings_tmpl.coffee +++ b/assets/javascripts/templates/pages/settings_tmpl.coffee @@ -1,16 +1,22 @@ +themeOption = ({ label, value }, settings) -> """ + +""" + app.templates.settingsPage = (settings) -> """

Preferences

Theme:

- + #{if settings.autoSupported + themeOption label: "Automatic Matches system setting", value: "auto", settings + else + ""} + #{themeOption label: "Light", value: "default", settings} + #{themeOption label: "Dark", value: "dark", settings}
diff --git a/assets/javascripts/views/content/settings_page.coffee b/assets/javascripts/views/content/settings_page.coffee index fd747438..2db7c6bc 100644 --- a/assets/javascripts/views/content/settings_page.coffee +++ b/assets/javascripts/views/content/settings_page.coffee @@ -16,6 +16,7 @@ class app.views.SettingsPage extends app.View settings.arrowScroll = app.settings.get('arrowScroll') settings.autoInstall = app.settings.get('autoInstall') settings.analyticsConsent = app.settings.get('analyticsConsent') + settings.autoSupported = app.settings.autoSupported settings[layout] = app.settings.hasLayout(layout) for layout in app.settings.LAYOUTS settings diff --git a/assets/stylesheets/components/_settings.scss b/assets/stylesheets/components/_settings.scss index 86bbf005..807f5500 100644 --- a/assets/stylesheets/components/_settings.scss +++ b/assets/stylesheets/components/_settings.scss @@ -48,15 +48,21 @@ } ._settings-label { - margin: 0 0 .375rem; + &:not(._theme-label) { + margin: 0 0 .375rem; + } > small { display: block; color: var(--textColorLight); margin-left: 1.75rem; } + &._theme-label > small { + display: inline-block; + margin-left: 0.75rem; + } - input[type=checkbox] { + input[type=checkbox], input[type=radio] { vertical-align: top; margin: .25rem .375rem; }