Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide variant images setting #158

Merged
merged 18 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,17 @@ class SliderComponent extends HTMLElement {
}

initPages() {
if (!this.sliderItems.length === 0) return;
this.slidesPerPage = Math.floor(this.slider.clientWidth / this.sliderItems[0].clientWidth);
this.totalPages = this.sliderItems.length - this.slidesPerPage + 1;
const sliderItemsToShow = Array.from(this.sliderItems).filter(element => element.clientWidth > 0);
this.sliderLastItem = sliderItemsToShow[sliderItemsToShow.length - 1];
if (sliderItemsToShow.length === 0) return;
this.slidesPerPage = Math.floor(this.slider.clientWidth / sliderItemsToShow[0].clientWidth);
this.totalPages = sliderItemsToShow.length - this.slidesPerPage + 1;
this.update();
}

update() {
if (!this.pageCount || !this.pageTotal) return;
this.currentPage = Math.round(this.slider.scrollLeft / this.sliderItems[0].clientWidth) + 1;
this.currentPage = Math.round(this.slider.scrollLeft / this.sliderLastItem.clientWidth) + 1;

if (this.currentPage === 1) {
this.prevButton.setAttribute('disabled', true);
Expand All @@ -484,7 +486,11 @@ class SliderComponent extends HTMLElement {

onButtonClick(event) {
event.preventDefault();
const slideScrollPosition = event.currentTarget.name === 'next' ? this.slider.scrollLeft + this.sliderItems[0].clientWidth : this.slider.scrollLeft - this.sliderItems[0].clientWidth;
const slideScrollPosition = event.currentTarget.name === 'next' ? this.slider.scrollLeft + this.sliderLastItem.clientWidth : this.slider.scrollLeft - this.sliderLastItem.clientWidth;
this.slider.scrollTo({
left: slideScrollPosition
});

this.slider.scrollTo({
left: slideScrollPosition
});
Expand Down Expand Up @@ -533,10 +539,17 @@ class VariantSelects extends HTMLElement {
const newMedia = document.querySelector(
`[data-media-id="${this.dataset.section}-${this.currentVariant.featured_media.id}"]`
);

if (!newMedia) return;
const modalContent = document.querySelector(`#ProductModal-${this.dataset.section}`);
const newMediaModal = modalContent.querySelector( `[data-media-id="${this.currentVariant.featured_media.id}"]`);
const parent = newMedia.parentElement;
if (parent.firstChild == newMedia) return;
modalContent.prepend(newMediaModal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this modalContent element is not the right one.

#ProductModal-${this.dataset.section} points to the external modal, not its content wrapper.

image

This is causing the element to be prepended to the wrong container:
image

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯

parent.prepend(newMedia);
window.setTimeout(() => { parent.scroll(0, 0) });
this.stickyHeader = this.stickyHeader || document.querySelector('sticky-header');
this.stickyHeader.dispatchEvent(new Event('preventHeaderReveal'));
window.setTimeout(() => { parent.querySelector('li.product__media-item').scrollIntoView({behavior: "smooth"}); });
}

updateURL() {
Expand Down
10 changes: 10 additions & 0 deletions assets/section-main-product.css
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ a.product__text {
}
}

.product__media-item--variant,
.product-media-modal__content > .product__media-item--variant.product__media-item--variant {
display: none;
}

.product__media-item--variant:first-child,
.product-media-modal__content > .product__media-item--variant:first-child {
display: block;
}

.product__media-icon .icon {
width: 1.2rem;
height: 1.4rem;
Expand Down
3 changes: 3 additions & 0 deletions locales/en.default.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,9 @@
"enable_sticky_info": {
"label": "Enable sticky product information on large screens"
},
"hide_variants": {
"label": "Hide other variants’ media after selecting a variant"
},
"enable_video_looping": {
"label": "Enable video looping"
}
Expand Down
24 changes: 19 additions & 5 deletions sections/header.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,19 @@
this.header = document.getElementById('shopify-section-header');
this.headerBounds = {};
this.currentScrollTop = 0;
this.preventReveal = false;

this.onScrollHandler = this.onScroll.bind(this);
this.hideHeaderOnScrollUp = () => this.preventReveal = true;

this.addEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
window.addEventListener('scroll', this.onScrollHandler, false);

this.createObserver();
}

disconnectedCallback() {
this.removeEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
window.removeEventListener('scroll', this.onScrollHandler);
}

Expand All @@ -461,12 +465,22 @@
onScroll() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;

if (scrollTop > this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
if (this.preventReveal) {
window.clearTimeout(this.isScrolling);

this.isScrolling = setTimeout(() => {
this.preventReveal = false;
}, 66);

requestAnimationFrame(this.hide.bind(this));
} else if (scrollTop < this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.reveal.bind(this));
} else if (scrollTop <= this.headerBounds.top) {
requestAnimationFrame(this.reset.bind(this));
} else {
if (scrollTop > this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.hide.bind(this));
} else if (scrollTop < this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.reveal.bind(this));
} else if (scrollTop <= this.headerBounds.top) {
requestAnimationFrame(this.reset.bind(this));
}
}

this.currentScrollTop = scrollTop;
Expand Down
14 changes: 11 additions & 3 deletions sections/main-product.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
{{ "accessibility.skip_to_product_info" | t }}
</a>
<ul class="product__media-list grid grid--peek list-unstyled slider slider--mobile" role="list">
{%- assign variant_images = product.images | where: 'attached_to_variant?', true | map: 'src' -%}
{%- if product.selected_or_first_available_variant.featured_media != null -%}
{%- assign media = product.selected_or_first_available_variant.featured_media -%}
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if section.settings.hide_variants and variant_images contains media.src %} product__media-item--variant{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
{% render 'product-thumbnail', media: media, position: 'featured', loop: section.settings.enable_video_looping, modal_id: section.id %}
</li>
{%- endif -%}
{%- for media in product.media -%}
{%- unless media.id == product.selected_or_first_available_variant.featured_media.id -%}
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if section.settings.hide_variants and variant_images contains media.src %} product__media-item--variant{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
{% render 'product-thumbnail', media: media, position: forloop.index, loop: section.settings.enable_video_looping, modal_id: section.id %}
</li>
{%- endunless -%}
Expand All @@ -44,7 +45,7 @@
<span class="slider-counter--current">1</span>
<span aria-hidden="true"> / </span>
<span class="visually-hidden">{{ 'accessibility.of' | t }}</span>
<span class="slider-counter--total">{{ product.media.size }}</span>
<span class="slider-counter--total">{% if section.settings.hide_variants %}{{ product.media.size | minus: variant_images.size | plus: 1 }}{% else %}{{ product.media.size }}{% endif %}</span>
</div>
<button type="button" class="slider-button slider-button--prev" name="previous" aria-label="{{ 'accessibility.previous_slide' | t }}">{% render 'icon-caret' %}</button>
<button type="button" class="slider-button slider-button--next" name="next" aria-label="{{ 'accessibility.next_slide' | t }}">{% render 'icon-caret' %}</button>
Expand Down Expand Up @@ -332,6 +333,7 @@
width="1100"
height="{{ 1100 | divided_by: media.preview_image.aspect_ratio | ceil }}"
data-media-id="{{ media.id }}"
{% if section.settings.hide_variants and variant_images contains media.src %}class="product__media-item--variant"{% endif %}
>
{%- else -%}
{%- if media.media_type == 'model' -%}
Expand Down Expand Up @@ -739,6 +741,12 @@
"default": true,
"label": "t:sections.main-product.settings.enable_sticky_info.label"
},
{
"type": "checkbox",
"id": "hide_variants",
"default": false,
"label": "t:sections.main-product.settings.hide_variants.label"
},
{
"type": "checkbox",
"id": "enable_video_looping",
Expand Down