Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve hero image handling for lazy-loading.

Matthew Kennedy79fa715d1f4f
diff --git a/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss b/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss
index d47638cb39..7ed0df8d0c 100644
--- a/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss
+++ b/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss
@@ -14,8 +14,8 @@
.col-sm-6, .col-sm-5, .col-sm-4, .col-sm-3, .col-sm-2, .col-sm-1, .col,
.col-auto, .col-12, .col-11, .col-10, .col-9, .col-8, .col-7, .col-6,
.col-5, .col-4, .col-3, .col-2, .col-1 {
- padding-right: 2.5px;
- padding-left: 2.5px;
+ padding-right: 5px;
+ padding-left: 5px;
}
.cms_section_preview_image_holder {
position: relative;
diff --git a/backend/app/views/spree/admin/cms_pages/edit.html.erb b/backend/app/views/spree/admin/cms_pages/edit.html.erb
index ff123f2fdc..123172ff9e 100644
--- a/backend/app/views/spree/admin/cms_pages/edit.html.erb
+++ b/backend/app/views/spree/admin/cms_pages/edit.html.erb
@@ -44,8 +44,7 @@
<div class="row">
<div class="col-12">
<div class="alert alert-info no-objects-found m-4">
- <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::CmsSection)) %>
- <%= link_to Spree.t(:add_one), new_object_url if can?(:create, Spree::CmsSection) %>
+ <%= Spree.t('admin.cms.no_sections_found_please_add_one') %>
</div>
</div>
</div>
diff --git a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb b/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb
index 515938dfc2..616eb88871 100644
--- a/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb
+++ b/backend/app/views/spree/admin/cms_sections/types/_spree_cms_sections_hero.html.erb
@@ -12,10 +12,10 @@
<%= f.error_message_on :width %>
<% end %>
- <%= f.field_container :subtitle, class: ['form-group'] do %>
- <%= f.label :subtitle, Spree.t(:button_text) %>
- <%= f.text_field :subtitle, class: 'form-control' %>
- <%= f.error_message_on :subtitle %>
+ <%= f.field_container :button_text, class: ['form-group'] do %>
+ <%= f.label :button_text, Spree.t(:button_text) %>
+ <%= f.text_field :button_text, class: 'form-control' %>
+ <%= f.error_message_on :button_text %>
<% end %>
</div>
<div class="col-12 col-md-6">
@@ -25,13 +25,13 @@
<% unless @cms_section.icon.new_record? %>
<div id="menuItemImgContainer" class="my-3 ">
<%= image_tag(main_app.url_for(@cms_section.icon.attachment)) %>
- <%#= link_to Spree.t(:remove_image), remove_icon_admin_menu_menu_item_path(@cms_page, @cms_section), method: :delete %>
</div>
<% end %>
<div id="<% unless @cms_section.icon.new_record? %>MenuItemImageButton<% end %>">
<%= icon_field.file_field :attachment %>
</div>
<% end %>
+ <small class="form-text text-muted"><%= Spree.t('admin.cms.hero.aspect_ratio') %></small>
<%= f.field_container :icon_alt do %>
<% unless @cms_section.icon.new_record? %>
diff --git a/core/app/models/spree/cms/sections/hero.rb b/core/app/models/spree/cms/sections/hero.rb
index d0dc472325..53f4f7ed13 100644
--- a/core/app/models/spree/cms/sections/hero.rb
+++ b/core/app/models/spree/cms/sections/hero.rb
@@ -1,5 +1,8 @@
module Spree::Cms::Sections
class Hero < Spree::CmsSection
+ has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::Icon'
+ accepts_nested_attributes_for :icon, reject_if: :all_blank
+
def links_to
['Spree::Taxon', 'Spree::Product']
end
diff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rb
index c503768eaf..0346ab7234 100644
--- a/core/app/models/spree/cms_section.rb
+++ b/core/app/models/spree/cms_section.rb
@@ -9,9 +9,6 @@ module Spree
default_scope { order(position: :asc) }
- has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::Icon'
- accepts_nested_attributes_for :icon, reject_if: :all_blank
-
TYPES = ['Spree::Cms::Sections::Hero',
'Spree::Cms::Sections::Promo',
'Spree::Cms::Sections::FeaturedArticle']
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 5e2bed7452..026d5313b3 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -550,10 +550,13 @@ en:
adjustments: Adjustments
admin:
cms:
+ hero:
+ aspect_ratio: Please use an image with the aspext ratio of 12:5 (2400px x 1000px)
select_page_type: Select a page type
click_button_to_change_page_type: Click the <b>%{button_type}</b> button to load the options for this page type.
view_seo_settings: View SEO Settings
seo_settings: SEO Settings
+ no_sections_found_please_add_one: Add your first section to this page by clicking the + Add New Section button above.
click_create_to_load_new_link: 'Click the <b>Create</b> button below to save your new page section, and load the section settings.'
click_update_to_load_new_link: 'Click the <b>Update</b> button below to load the new section settings.'
settings_for: 'Available Options For %{section}'
diff --git a/frontend/app/assets/stylesheets/spree/frontend/helpers/spree/frontend_helper.scss b/frontend/app/assets/stylesheets/spree/frontend/helpers/spree/frontend_helper.scss
index e4b5e8af6b..e2321d9ea6 100644
--- a/frontend/app/assets/stylesheets/spree/frontend/helpers/spree/frontend_helper.scss
+++ b/frontend/app/assets/stylesheets/spree/frontend/helpers/spree/frontend_helper.scss
@@ -30,3 +30,32 @@
overflow: hidden;
text-overflow: ellipsis;
}
+
+
+// Aspect Ratios
+[style*="--aspect-ratio"] > :first-child {
+ width: 100%;
+}
+
+[style*="--aspect-ratio"] > img {
+ height: auto;
+}
+
+@supports (--custom:property) {
+ [style*="--aspect-ratio"] {
+ position: relative;
+ }
+
+ [style*="--aspect-ratio"]::before {
+ content: "";
+ display: block;
+ padding-bottom: calc(100% / (var(--aspect-ratio)));
+ }
+
+ [style*="--aspect-ratio"] > :first-child {
+ position: absolute;
+ top: 0;
+ left: 0;
+ height: 100%;
+ }
+}
diff --git a/frontend/app/assets/stylesheets/spree/frontend/views/spree/home/index.scss b/frontend/app/assets/stylesheets/spree/frontend/views/spree/home/index.scss
index 91c384fa14..416e7116fc 100644
--- a/frontend/app/assets/stylesheets/spree/frontend/views/spree/home/index.scss
+++ b/frontend/app/assets/stylesheets/spree/frontend/views/spree/home/index.scss
@@ -1,10 +1,9 @@
.homepage {
&-hero-image {
display: flex;
- justify-content: center;
+ justify-content: start;
flex-direction: column;
text-align: center;
- height: 204px;
width: 100%;
overflow: hidden;
@@ -17,17 +16,18 @@
}
@include media-breakpoint-up(md) {
- height: 418px;
+
}
@include media-breakpoint-up(lg) {
- height: 600px;
+
}
.hero-content {
position: absolute;
z-index: 1;
right: 10%;
+ top:25%;
}
&-text {
diff --git a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb b/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb
index 4f8bc0a58b..45d82101c5 100644
--- a/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb
+++ b/frontend/app/views/spree/shared/cms/sections/_spree_cms_sections_hero.html.erb
@@ -2,16 +2,51 @@
<div class="<%= edge_to_edge %>">
<div class="homepage-hero-image mb-3 position-relative p-0">
<% if section.icon.present? %>
- <%= image_tag main_app.url_for(section.icon.attachment), alt: section.icon.alt %>
- <% end %>
+ <!-- XS to MD -->
+ <div class="d-sm-none" style="--aspect-ratio:12/5;">
+ <img class="lazyload"
+ data-src="<%= main_app.url_for(section.icon.attachment.variant(resize: '768x320>')) %>"
+ data-srcset="<%= main_app.url_for(section.icon.attachment.variant(resize: '768x320>')) %>"
+ alt="<%= section.icon.alt %>">
+ </div>
+
+ <!-- SM to MD -->
+ <div class="d-none d-sm-block d-md-none" style="--aspect-ratio:12/5;">
+ <img class="lazyload"
+ data-src="<%= main_app.url_for(section.icon.attachment.variant(resize: '990x412>')) %>"
+ data-srcset="<%= main_app.url_for(section.icon.attachment.variant(resize: '990x412>')) %>"
+ alt="<%= section.icon.alt %>">
+ </div>
- <div class="hero-content">
- <div class="homepage-hero-image-text mb-2 mx-auto pb-3 pb-md-4">
- <%= section.title %>
+ <!-- MD to LG -->
+ <div class="d-none d-md-block d-lg-none" style="--aspect-ratio:12/5;">
+ <img class="lazyload"
+ data-src="<%= main_app.url_for(section.icon.attachment.variant(resize: '1200x500>')) %>"
+ data-srcset="<%= main_app.url_for(section.icon.attachment.variant(resize: '1200x500>')) %>"
+ alt="<%= section.icon.alt %>">
</div>
- <div>
- <%= link_to section.subtitle, section.link, class: 'btn btn-primary spree-btn px-5' %>
+
+ <!-- XL UP -->
+ <div class="d-none d-lg-block" style="--aspect-ratio:12/5;">
+ <img class="lazyload"
+ data-src="<%= main_app.url_for(section.icon.attachment.variant(resize: '2400x1000>')) %>"
+ data-srcset="<%= main_app.url_for(section.icon.attachment.variant(resize: '2400x1000>')) %>"
+ alt="<%= section.icon.alt %>">
</div>
+ <% end %>
+
+ <div class="hero-content align-self-center">
+ <% if section.title.present? %>
+ <div class="homepage-hero-image-text mb-2 mx-auto pb-3 pb-md-4">
+ <%= section.title %>
+ </div>
+ <% end %>
+
+ <% if section.link.present? && section.button_text.present? %>
+ <div>
+ <%= link_to section.button_text, section.link, class: 'btn btn-primary spree-btn px-5' %>
+ </div>
+ <% end %>
</div>
</div>
</div>