-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a theme selector to the backend UI
- Save the user preference alongside the system preference so that when the system switches the UI will follow along. - Use the session to store preferences so that we start the page with the correct theme(s). - Keep the current theme in the select tag up to date at page load and whenever the system changes. Co-Authored-By: piyushswain <[email protected]> Co-Authored-By: Elia Schito <[email protected]> Co-Authored-By: Massimiliano Lattanzio <[email protected]>
- Loading branch information
1 parent
88d534e
commit 92bfed4
Showing
9 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
backend/app/assets/javascripts/spree/backend/theme_selection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Spree.ready(() => { | ||
const themeSelect = document.querySelector(".js-theme-selection") | ||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)") | ||
const lightTheme = themeSelect.dataset.selectedLightTheme | ||
const darkTheme = themeSelect.dataset.selectedDarkTheme | ||
const defaultTheme = themeSelect.dataset.defaultTheme | ||
|
||
const updateSelection = () => { | ||
console.log({ | ||
themeSelect, | ||
prefersDark, | ||
lightTheme, | ||
darkTheme, | ||
defaultTheme, | ||
}) | ||
themeSelect.value = (prefersDark.matches ? darkTheme : lightTheme) || defaultTheme | ||
} | ||
|
||
const setTheme = () => { | ||
Spree.ajax({ | ||
type: "PUT", | ||
dataType: "json", | ||
url: Spree.pathFor("admin/theme/set"), | ||
data: { | ||
switch_to_theme: themeSelect.value, | ||
system_theme: prefersDark.matches ? "dark" : "light", | ||
}, | ||
success: (data) => document.location.reload(), | ||
}) | ||
} | ||
|
||
updateSelection() | ||
|
||
prefersDark.addEventListener("change", updateSelection) | ||
themeSelect.addEventListener("change", setTheme) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Admin | ||
class ThemeController < Spree::Admin::BaseController | ||
skip_before_action :authorize_admin, only: [:set] | ||
|
||
def set | ||
requested_theme = params[:switch_to_theme].presence | ||
|
||
# Avoid interpolating user content into the session key | ||
system_theme = params[:system_theme].presence == "dark" ? "dark" : "light" | ||
session_key = :"admin_#{system_theme}_theme" | ||
|
||
if theme_is_available?(requested_theme) | ||
session[session_key] = requested_theme | ||
head :ok | ||
else | ||
head :not_found | ||
end | ||
end | ||
|
||
private | ||
|
||
def theme_is_available?(theme) | ||
theme && Spree::Backend::Config.themes.key?(theme.to_sym) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
backend/app/views/spree/admin/shared/_theme_selection.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<label class="admin-navbar-selection"> | ||
<i class="fa fa-paint-brush fa-fw" title="<%= I18n.t('spree.choose_dashboard_theme') %>"></i> | ||
<select | ||
class="js-theme-selection custom-select fullwidth" | ||
data-selected-light-theme="<%= session[:admin_light_theme] %>" | ||
data-selected-dark-theme="<%= session[:admin_dark_theme] %>" | ||
data-default-theme="<%= Spree::Backend::Config.default_theme %>" | ||
> | ||
<%= options_for_select( | ||
Spree::Backend::Config.themes.keys.map { |theme| [theme.to_s.humanize, theme] }.sort, | ||
) %> | ||
</select> | ||
</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/spec/controllers/spree/admin/theme_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Spree::Admin::ThemeController, type: :controller do | ||
stub_authorization! | ||
|
||
it 'sets the theme in a different session key for each system theme' do | ||
stub_spree_preferences(Spree::Backend::Config, themes: { foo: 'foo-path', bar: 'bar-path' }) | ||
|
||
get :set, params: { switch_to_theme: 'foo', system_theme: 'light', format: :json } | ||
|
||
expect(session[:admin_light_theme]).to eq('foo') | ||
expect(session[:admin_dark_theme]).to eq(nil) | ||
expect(response).to have_http_status(:ok) | ||
|
||
get :set, params: { switch_to_theme: 'bar', system_theme: 'dark', format: :json } | ||
expect(session[:admin_light_theme]).to eq('foo') | ||
expect(session[:admin_dark_theme]).to eq('bar') | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'responds with "not found" for a missing theme' do | ||
stub_spree_preferences(Spree::Backend::Config, themes: { foo: 'foo-path' }) | ||
|
||
get :set, params: { switch_to_theme: 'bar', system_theme: 'dark', format: :json } | ||
|
||
expect(session[:admin_light_theme]).to eq(nil) | ||
expect(session[:admin_dark_theme]).to eq(nil) | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters