-
-
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 with dark and dimmed variants
- 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. Add dark and dimmed theme variants dark: Full black background with no alteration on images. dimmed: Dark gray background with dimmed images, easier on the eyes but less accurate colors. 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
3881323
commit e31870d
Showing
19 changed files
with
226 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
//= link_tree ../images | ||
//= link_tree "../stylesheets/spree/backend/themes" .css | ||
|
||
//= link spree/backend/all.js | ||
//= link spree/backend/all.css | ||
|
||
//= link solidus_admin/select2_locales | ||
|
||
//= link spree/backend/themes/classic.css | ||
//= link spree/backend/themes/solidus_admin.css |
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
16 changes: 16 additions & 0 deletions
16
backend/app/assets/stylesheets/spree/backend/themes/classic_dark.css.scss
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,16 @@ | ||
@import 'spree/backend/themes/classic'; | ||
|
||
html { | ||
background-color: #fff; | ||
color: #fff; | ||
-webkit-filter: invert(100%); | ||
filter: invert(100%) hue-rotate(180deg); | ||
|
||
img { | ||
filter: invert(100%) hue-rotate(-180deg); | ||
} | ||
|
||
.brand-link img { | ||
filter: invert(0%) hue-rotate(0deg); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/app/assets/stylesheets/spree/backend/themes/classic_dimmed.css.scss
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,16 @@ | ||
@import 'spree/backend/themes/classic'; | ||
|
||
html { | ||
background-color: #fff; | ||
color: #fff; | ||
-webkit-filter: invert(85%); | ||
filter: invert(85%) hue-rotate(180deg); | ||
|
||
img { | ||
filter: invert(100%) hue-rotate(-180deg); | ||
} | ||
|
||
.brand-link img { | ||
filter: invert(0%) hue-rotate(0deg); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
backend/app/assets/stylesheets/spree/backend/themes/solidus_admin.css.scss
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
2 changes: 2 additions & 0 deletions
2
backend/app/assets/stylesheets/spree/backend/themes/solidus_admin/_tables.scss
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
@import "./colors"; | ||
|
||
table.index { | ||
// Borders | ||
border-collapse: separate; | ||
|
16 changes: 16 additions & 0 deletions
16
backend/app/assets/stylesheets/spree/backend/themes/solidus_admin_dark.css.scss
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,16 @@ | ||
@import "./solidus_admin"; | ||
|
||
html { | ||
background-color: #fff; | ||
color: #fff; | ||
-webkit-filter: invert(100%); | ||
filter: invert(100%) hue-rotate(180deg); | ||
|
||
img { | ||
filter: invert(100%) hue-rotate(-180deg); | ||
} | ||
|
||
.brand-link img { | ||
filter: invert(0%) hue-rotate(0deg); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/app/assets/stylesheets/spree/backend/themes/solidus_admin_dimmed.css.scss
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,16 @@ | ||
@import "./solidus_admin"; | ||
|
||
html { | ||
background-color: #fff; | ||
color: #fff; | ||
-webkit-filter: invert(85%); | ||
filter: invert(85%) hue-rotate(180deg); | ||
|
||
img { | ||
filter: invert(100%) hue-rotate(-180deg); | ||
} | ||
|
||
.brand-link img { | ||
filter: invert(0%) hue-rotate(0deg); | ||
} | ||
} |
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 | ||
redirect_back_or_to spree.admin_url, notice: t('spree.theme_changed') | ||
else | ||
redirect_back_or_to spree.admin_url, error: t('spree.error') | ||
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
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
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
<% theme_options_for_select = Spree::Backend::Config.themes.keys.map { |theme| [theme.to_s.humanize, theme] }.sort %> | ||
<%= form_tag(admin_set_theme_path(format: :html), method: :put, style: "width: 100%;", class: "light-only") do %> | ||
<%= hidden_field_tag :system_theme, :light %> | ||
<label class="admin-navbar-selection"> | ||
<i class="fa fa-sun-o fa-fw" title="<%= I18n.t('spree.choose_dashboard_theme') %>"></i> | ||
<select name="switch_to_theme" class="custom-select fullwidth" onchange="this.form.requestSubmit()"> | ||
<%= options_for_select(theme_options_for_select, session[:admin_light_theme] || Spree::Backend::Config.theme) %> | ||
</select> | ||
</label> | ||
<% end %> | ||
<%= form_tag(admin_set_theme_path(format: :html), method: :put, style: "width: 100%;", class: "dark-only") do %> | ||
<%= hidden_field_tag :system_theme, :dark %> | ||
<label class="admin-navbar-selection"> | ||
<i class="fa fa-moon-o fa-fw" title="<%= I18n.t('spree.choose_dashboard_theme') %>"></i> | ||
<select name="switch_to_theme" class="custom-select fullwidth" onchange="this.form.requestSubmit()"> | ||
<%= options_for_select(theme_options_for_select, session[:admin_dark_theme] || Spree::Backend::Config.dark_theme) %> | ||
</select> | ||
</label> | ||
<% end %> |
25 changes: 25 additions & 0 deletions
25
backend/app/views/spree/admin/shared/_theme_selection_solidus_admin.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,25 @@ | ||
<% theme_options_for_select = Spree::Backend::Config.themes.keys.map { |theme| [theme.to_s.humanize, theme] }.sort %> | ||
|
||
<li> | ||
<%= form_tag(admin_set_theme_path(format: :html), method: :put, style: "width: 100%;", class: "light-only") do %> | ||
<%= hidden_field_tag :system_theme, :light %> | ||
<label> | ||
<svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-sun-line"></use></svg> | ||
<select name="switch_to_theme" onchange="this.form.requestSubmit()"> | ||
<%= options_for_select(theme_options_for_select, session[:admin_light_theme] || Spree::Backend::Config.theme) %> | ||
</select> | ||
<svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-expand-up-down-line"></use></svg> | ||
</label> | ||
<% end %> | ||
<%= form_tag(admin_set_theme_path(format: :html), method: :put, style: "width: 100%;", class: "dark-only") do %> | ||
<%= hidden_field_tag :system_theme, :dark %> | ||
<label> | ||
<svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-moon-line"></use></svg> | ||
<select name="switch_to_theme" onchange="this.form.requestSubmit()"> | ||
<%= options_for_select(theme_options_for_select, session[:admin_dark_theme] || Spree::Backend::Config.dark_theme) %> | ||
</select> | ||
<svg aria-hidden="true"><use xlink:href="<%= image_path('spree/backend/themes/solidus_admin/remixicon.symbol.svg') %>#ri-expand-up-down-line"></use></svg> | ||
</label> | ||
<% end %> | ||
</li> |
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
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