Skip to content

Commit

Permalink
default to lightbox on mobile when hover selected
Browse files Browse the repository at this point in the history
  • Loading branch information
metamoni committed Oct 4, 2022
1 parent 0b0ede7 commit 9bf7ea4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 60 deletions.
18 changes: 11 additions & 7 deletions assets/magnify.js
@@ -1,13 +1,13 @@
// create a container and set the full-size image as its background
const createOverlay = (image) => {
function createOverlay(image) {
overlay = document.createElement('div');
overlay.setAttribute('class', 'image--full-size');
overlay.setAttribute('aria-hidden', 'true');
overlay.style.backgroundImage = `url('${image.src}')`;
return overlay;
};

const moveWithHover = (image, event, zoomRatio) => {
function moveWithHover(image, event, zoomRatio) {
// calculate mouse position
const ratio = image.height / image.width;
const container = event.target.getBoundingClientRect();
Expand All @@ -21,17 +21,21 @@ const moveWithHover = (image, event, zoomRatio) => {
overlay.style.backgroundSize = `${image.width * zoomRatio}px`;
};

const magnify = (image, zoomRatio) => {
function magnify(image, zoomRatio) {
// add full-size image on top of original
const overlay = createOverlay(image);
image.parentElement.insertBefore(overlay, image);

overlay.onclick = () => overlay.remove();
overlay.onmousemove = (event) => moveWithHover(image, event, zoomRatio);
overlay.onmouseleave = () => overlay.remove();
}

const images = document.querySelectorAll('.image--original');
function enableZoomOnHover() {
const images = document.querySelectorAll('.image--hover');
images && images.forEach(image => {
image.onclick = () => magnify(image, 2);
});
}

images.forEach(image => {
image.onclick = () => magnify(image, 3);
});
enableZoomOnHover();
13 changes: 12 additions & 1 deletion assets/section-main-product.css
Expand Up @@ -1145,12 +1145,23 @@ a.product__text {
.image--full-size {
cursor: zoom-out;
z-index: 1;
margin: 0;
}

.image--original {
.image--hover {
cursor: zoom-in;
}

.lightbox-button--none {
pointer-events: none;
}

@media (hover: hover) {
.lightbox-button--hover {
pointer-events: none;
}
}

.product__media-container {
overflow: hidden !important;
}
Expand Down
10 changes: 10 additions & 0 deletions assets/theme-editor.js
Expand Up @@ -18,3 +18,13 @@ document.addEventListener('shopify:block:deselect', function(event) {
const parentSlideshowComponent = event.target.closest('slideshow-component');
if (parentSlideshowComponent.autoplayButtonIsSetToPlay) parentSlideshowComponent.play();
});

document.addEventListener('shopify:section:load', () => {
const zoomOnHoverScript = document.querySelector('[id^=enableZoomOnHover]');
if (!zoomOnHoverScript) return;
if (zoomOnHoverScript) {
const newScriptTag = document.createElement('script');
newScriptTag.src = zoomOnHoverScript.src;
zoomOnHoverScript.parentNode.replaceChild(newScriptTag, zoomOnHoverScript);
}
});
105 changes: 67 additions & 38 deletions sections/featured-product.liquid
Expand Up @@ -625,52 +625,54 @@
</div>
{%- endif -%}
</div>
<product-modal id="ProductModal-{{ section.id }}" class="product-media-modal media-modal">
<div
class="product-media-modal__dialog"
role="dialog"
aria-label="{{ 'products.modal.label' | t }}"
aria-modal="true"
tabindex="-1"
>
<button
id="ModalClose-{{ section.id }}"
type="button"
class="product-media-modal__toggle"
aria-label="{{ 'accessibility.close' | t }}"
>
{% render 'icon-close' %}
</button>

{% if section.settings.image_zoom == 'lightbox' %}
<product-modal id="ProductModal-{{ section.id }}" class="product-media-modal media-modal">
<div
class="product-media-modal__content color-background-1 gradient"
role="document"
class="product-media-modal__dialog"
role="dialog"
aria-label="{{ 'products.modal.label' | t }}"
tabindex="0"
aria-modal="true"
tabindex="-1"
>
{%- liquid
if product.selected_or_first_available_variant.featured_media != null
assign media = product.selected_or_first_available_variant.featured_media
render 'product-media', media: media, loop: section.settings.enable_video_looping, variant_image: section.settings.hide_variants
endif
-%}
<button
id="ModalClose-{{ section.id }}"
type="button"
class="product-media-modal__toggle"
aria-label="{{ 'accessibility.close' | t }}"
>
{% render 'icon-close' %}
</button>

{%- for media in product.media -%}
<div
class="product-media-modal__content color-background-1 gradient"
role="document"
aria-label="{{ 'products.modal.label' | t }}"
tabindex="0"
>
{%- liquid
if section.settings.hide_variants and media_to_render contains media.id
assign variant_image = true
else
assign variant_image = false
if product.selected_or_first_available_variant.featured_media != null
assign media = product.selected_or_first_available_variant.featured_media
render 'product-media', media: media, loop: section.settings.enable_video_looping, variant_image: section.settings.hide_variants
endif

unless media.id == product.selected_or_first_available_variant.featured_media.id
render 'product-media', media: media, loop: section.settings.enable_video_looping, variant_image: variant_image
endunless
-%}
{%- endfor -%}

{%- for media in product.media -%}
{%- liquid
if section.settings.hide_variants and media_to_render contains media.id
assign variant_image = true
else
assign variant_image = false
endif

unless media.id == product.selected_or_first_available_variant.featured_media.id
render 'product-media', media: media, loop: section.settings.enable_video_looping, variant_image: variant_image
endunless
-%}
{%- endfor -%}
</div>
</div>
</div>
</product-modal>
</product-modal>
{% endif %}
</div>
</section>

Expand All @@ -681,6 +683,12 @@
</script>
<script src="{{ 'product-model.js' | asset_url }}" defer></script>
{%- endif -%}
{% if section.settings.image_zoom == 'hover' %}
<script id="enableZoomOnHover-featured" src="{{ 'magnify.js' | asset_url }}" defer="defer"></script>
{% endif %}
{%- if request.design_mode and section.settings.image_zoom != 'hover' -%}
<script src="{{ 'theme-editor.js' | asset_url }}" defer="defer"></script>
{%- endif -%}

{%- liquid
if product.selected_or_first_available_variant.featured_media
Expand Down Expand Up @@ -1603,6 +1611,27 @@
"label": "t:sections.featured-product.settings.media_position.label",
"info": "t:sections.featured-product.settings.media_position.info"
},
{
"type": "select",
"id": "image_zoom",
"options": [
{
"value": "lightbox",
"label": "t:sections.main-product.settings.image_zoom.options__1.label"
},
{
"value": "hover",
"label": "t:sections.main-product.settings.image_zoom.options__2.label"
},
{
"value": "none",
"label": "t:sections.main-product.settings.image_zoom.options__3.label"
}
],
"default": "lightbox",
"label": "t:sections.main-product.settings.image_zoom.label",
"info": "t:sections.main-product.settings.image_zoom.info"
},
{
"type": "checkbox",
"id": "hide_variants",
Expand Down
9 changes: 6 additions & 3 deletions sections/main-product.liquid
Expand Up @@ -27,6 +27,12 @@
{%- endstyle -%}

<script src="{{ 'product-form.js' | asset_url }}" defer="defer"></script>
{% if section.settings.image_zoom == 'hover' %}
<script id="enableZoomOnHover-main" src="{{ 'magnify.js' | asset_url }}" defer="defer"></script>
{% endif %}
{%- if request.design_mode and section.settings.image_zoom != 'hover' -%}
<script src="{{ 'theme-editor.js' | asset_url }}" defer="defer"></script>
{%- endif -%}

{%- assign first_3d_model = product.media | where: 'media_type', 'model' | first -%}
{%- if first_3d_model -%}
Expand Down Expand Up @@ -1146,9 +1152,6 @@
endif
-%}

{% if section.settings.image_zoom == 'hover' %}
<script src="{{ 'magnify.js' | asset_url }}" defer="defer"></script>
{% endif %}
{%- for media in product.media -%}
{%- liquid
if section.settings.hide_variants and variant_images contains media.src
Expand Down
20 changes: 9 additions & 11 deletions snippets/product-thumbnail.liquid
Expand Up @@ -38,8 +38,8 @@
assign mobile_columns = 2
endif

if section.settings.image_zoom == 'hover'
assign image_class = 'image--original'
if media.media_type == 'image'
assign image_class = 'image--' | append: section.settings.image_zoom
endif
-%}

Expand Down Expand Up @@ -74,7 +74,7 @@
</noscript>

<modal-opener class="product__modal-opener product__modal-opener--{{ media.media_type }} no-js-hidden" data-modal="#ProductModal-{{ modal_id }}">
{%- if section.settings.image_zoom == 'lightbox' %}
{% if media.media_type != 'image' or section.settings.image_zoom == 'lightbox' %}
<span class="product__media-icon motion-reduce quick-add-hidden" aria-hidden="true">
{%- liquid
case media.media_type
Expand All @@ -87,7 +87,7 @@
endcase
-%}
</span>
{%- endif %}
{% endif %}

<div class="product__media product__media-container media media--transparent gradient global-media-settings" style="padding-top: {{ 1 | divided_by: media.preview_image.aspect_ratio | times: 100 }}%;">
{{ media.preview_image | image_url: width: 1946 | image_tag:
Expand All @@ -98,13 +98,11 @@
alt: media.preview_image.alt | escape
}}
</div>
{% if section.settings.image_zoom == 'lightbox' %}
<button class="product__media-toggle quick-add-hidden" type="button" aria-haspopup="dialog" data-media-id="{{ media.id }}">
<span class="visually-hidden">
{{ 'products.product.media.open_media' | t: index: position }}
</span>
</button>
{% endif %}
<button class="product__media-toggle quick-add-hidden lightbox-button--{{ section.settings.image_zoom }}" type="button" aria-haspopup="dialog" data-media-id="{{ media.id }}">
<span class="visually-hidden">
{{ 'products.product.media.open_media' | t: index: position }}
</span>
</button>
</modal-opener>

{%- if media.media_type != 'image' -%}
Expand Down

0 comments on commit 9bf7ea4

Please sign in to comment.