Skip to content

Commit e3da4a6

Browse files
committed
WIP Color theme page
1 parent bf213b7 commit e3da4a6

File tree

8 files changed

+124
-1
lines changed

8 files changed

+124
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ColorThemesController < ApplicationController
2+
def show
3+
@color_scale = params[:id] ? ColorScale.find(params[:id]) : ColorScale.find_or_create_default
4+
curated_color_scales = Rails.cache.fetch("curated_color_scales") { ColorScale.curated }
5+
6+
render ColorThemes::ShowView.new(
7+
color_scale: color_scale,
8+
curated_color_scales: curated_color_scales
9+
)
10+
end
11+
end

app/javascript/css/application.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
@import './components/newsletter-banner.css';
3333
@import './components/anchors.scss';
3434
@import './components/callout.css';
35+
@import './components/color-theme.css';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.color-theme {
2+
color: var(--joy-background-clean);
3+
margin-bottom: var(--space-3xl);
4+
display: flex;
5+
flex-direction: row;
6+
border-radius: 0.5rem;
7+
}
8+
9+
.color-swatch {
10+
width: 100%;
11+
padding: 1rem;
12+
margin-right: 1px;
13+
height: 6rem;
14+
border-radius: 0.5rem;
15+
justify-content: center;
16+
flex-direction: column;
17+
text-transform: uppercase;
18+
border: 1px solid var(--joy-border-subtle);
19+
font-size: var(--step--2);
20+
text-align: center;
21+
}
22+
23+
.color-swatch__weight\:50,
24+
.color-swatch__weight\:100,
25+
.color-swatch__weight\:200,
26+
.color-swatch__weight\:300,
27+
.color-swatch__weight\:400 {
28+
color: var(--joy-dark);
29+
}
30+
31+
.color-swatch__weight {
32+
font-weight: 700;
33+
}

app/javascript/css/utilities/custom.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
.mb-gap {
2+
margin-bottom: var(--grid-gutter);
3+
}
4+
5+
.mb-gap {
6+
margin-bottom: var(--grid-gutter);
7+
}
8+
19
.py-gap {
210
padding-top: var(--grid-gutter);
311
padding-bottom: var(--grid-gutter);

app/models/color_scale.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def self.curated
2525
where(name: names)
2626
end
2727

28+
def self.cached_curated
29+
cached_ids = Rails.cache.read("curated_color_scale_ids")
30+
31+
return where(id: cached_ids) if cached_ids
32+
33+
curated.tap do |collection|
34+
Rails.cache.write("curated_color_scale_ids", collection.map(&:id))
35+
end
36+
end
37+
2838
def self.find_or_create_default
2939
find_or_create_by(name: APP_DEFAULT[:name]) do |cs|
3040
APP_DEFAULT[:weights].each do |weight, value|
@@ -41,7 +51,11 @@ def set_weight(weight, value)
4151

4252
def weights
4353
VALID_WEIGHTS.each_with_object({}) do |weight, hash|
44-
hash[weight] = send(:"weight_#{weight}")
54+
hash[weight] = ColorConversion::Color.new(send(:"weight_#{weight}"))
4555
end
4656
end
57+
58+
def display_name
59+
name.gsub("Custom ", "")
60+
end
4761
end

app/views/color_themes/show_view.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
class ColorThemes::ShowView < ApplicationView
4+
include Phlex::Rails::Helpers::FormWith
5+
6+
def initialize(color_scale:, curated_color_scales: [])
7+
@color_scale = color_scale
8+
@curated_color_scales = curated_color_scales
9+
end
10+
11+
def view_template
12+
render Pages::Header.new(title: "Settings: Color Theme")
13+
div(class: "section-content container py-gap") do
14+
p { "You can preview and save your desired color theme for this site. We have curated some options for you below." }
15+
16+
h3 { @color_scale.display_name }
17+
div(class: "color-theme color-theme:#{@color_scale.name.parameterize}") do
18+
@color_scale.weights.each do |weight, color|
19+
div(class: "color-swatch color-swatch__weight:#{weight}", style: "background-color: #{color.hex}") do
20+
div(class: "color-swatch__weight") { weight }
21+
div(class: "color-swatch__color") { color.hex.delete("#").upcase }
22+
end
23+
end
24+
end
25+
26+
h3 { "Want to try a different color theme?" }
27+
form_with(url: color_theme_path, method: :get) do |f|
28+
fieldset do
29+
f.select :id, @curated_color_scales.map { |cs| [cs.display_name, cs.id] }, include_blank: "Pick one!"
30+
end
31+
fieldset do
32+
f.submit "Show me!", class: "button primary"
33+
end
34+
end
35+
end
36+
end
37+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
end
1717
resources :examples, only: [:index, :show]
1818

19+
resource :color_theme, only: [:show, :update]
20+
1921
namespace :pwa do
2022
resource :installation_instructions, only: [:show]
2123
resources :web_pushes, only: [:create]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe "Customize Color Themes", type: :system do
6+
it "user can selected a curated color scale" do
7+
visit color_theme_path
8+
9+
select "Thunderbird", from: "Choose a color scheme"
10+
11+
click_button "Use this color scheme"
12+
13+
expect(page).to have_content("You are now using the Cerulean Blue color scheme")
14+
15+
expect(page).to have_css("color-theme:custom-thunderbird")
16+
end
17+
end

0 commit comments

Comments
 (0)