Hi @catWT good work here thanks for sharing with the community for any future merchants struggling to DIY this feature without an app.
Here’s some notes to go further to enhance the feature:
Eventing, Latest versions of dawn uses a pubsub architecture to subscribe to events:
variantChangesubscriber = subscribe(PUB_SUB_EVENTS.variantChange, (event) => {
console.log(event);
console.log(event.data.variant);
});
The available events are in constants.js; cartUpdate , quantityUpdate , variantChange , cartError
https://github.com/Shopify/dawn/blob/main/assets/constants.js#L3
Reloads, try to avoid page reloads for just images, look into the section rendering api to pull in parts of a page https://shopify.dev/api/section-rendering . That can be used with vanilla javascript so your not loading yet another javascript library for one subfeature.
A catch for images though is if a gallery/slidehow is involved it may need to be reinitialized or have the images programmatically added to it.
Common conventional approaches, Look at the methods that use images alt-text to associate any product image to variants.
If you sanely name images and alt-text then it’s also a benefit to the sites accessibility and SEO.
Same process if using metafields on variants, instead of images in the product.media ; or if instead of alt-text you use data attributes.
Look at the Impact theme even in trial mode you can go over their docs to understand such a system. Example https://homelero.com/products/fawna ; note this is an advanced implementation that was customized to work with more than 1 option.
The general idea is just built a filter to toggle hiding images not related to the currently selected variant, and unhiding images that are related or not related to other variants (i.e. product featured image) . Either applying a css class, or using the hidden , aria-hidden attributes
Good Hunting.