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

Set thumbnail when selected variant featured media changes #3505

Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions assets/media-gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (!customElements.get('media-gallery')) {
this.elements.thumbnails.querySelectorAll('[data-target]').forEach((mediaToSwitch) => {
mediaToSwitch
.querySelector('button')
.addEventListener('click', this.setActiveMedia.bind(this, mediaToSwitch.dataset.target));
.addEventListener('click', this.setActiveMedia.bind(this, mediaToSwitch.dataset.target, false));
});
if (this.dataset.desktopLayout.includes('thumbnail') && this.mql.matches) this.removeListSemantic();
}
Expand All @@ -28,7 +28,7 @@ if (!customElements.get('media-gallery')) {
this.setActiveThumbnail(thumbnail);
}

setActiveMedia(mediaId) {
setActiveMedia(mediaId, prepend) {
const activeMedia =
this.elements.viewer.querySelector(`[data-media-id="${mediaId}"]`) ||
this.elements.viewer.querySelector('[data-media-id]');
Expand All @@ -40,6 +40,15 @@ if (!customElements.get('media-gallery')) {
});
activeMedia?.classList?.add('is-active');

if (prepend) {
activeMedia.parentElement.prepend(activeMedia);
if (this.elements.thumbnails) {
const activeThumbnail = this.elements.thumbnails.querySelector(`[data-target="${mediaId}"]`);
activeThumbnail.parentElement.prepend(activeThumbnail);
}
if (this.elements.viewer.slider) this.elements.viewer.resetPages();
}

this.preventStickyHeader();
window.setTimeout(() => {
if (!this.mql.matches || this.elements.thumbnails) {
Expand Down
2 changes: 1 addition & 1 deletion assets/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ if (!customElements.get('product-info')) {
// set featured media as active in the media gallery
this.querySelector(`media-gallery`)?.setActiveMedia?.(
`${this.dataset.section}-${variantFeaturedMediaId}`,
false
true
);

// update media modal
Expand Down
Loading