Created
September 16, 2022 06:14
-
-
Save pveen2/11964364b60ebe1d2599520eb810cee6 to your computer and use it in GitHub Desktop.
Shopify image component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- comment -%} | |
Expected behaviour: | |
Renders picture element | |
About: | |
Render a responsive image | |
Accepts: | |
- img: {Object} image | |
OPTIONAL: | |
- width: {Integer} (Default: 1920) The maximum size this image should render in, this can optimise the site speed. | |
- eager: {Boolean} (Default: false) In case you would like to disable the lazy loading | |
- preload: {Boolean} (Default: true) In case you would like to disable the preloading | |
- class: {String} Class to add to the picture element | |
Usage: | |
{%- render 'component.image', | |
img: s.image, | |
width: 2560, | |
placeholder: true, | |
class: 'w-full' | |
-%} | |
{%- endcomment -%} | |
{%- liquid | |
assign width = width | default: 1940 | plus: 1 | |
assign fallback_widths = '390, 512, 768, 1280, 1440, 1920, 2140, 2560' | |
assign img = img | default: fallback_img | |
if aspect_ratio | |
assign height = width | times: aspect_ratio | |
endif | |
assign width_string = width | append: 'px' | |
assign sizes = '(max-width: [width]) 100vw, [width]' | replace: '[width]', width_string | |
capture loading | |
if eager == true | |
echo 'eager' | |
else | |
echo 'lazy' | |
endif | |
endcapture | |
echo img | image_url: width: width, height: height | image_tag: loading: loading, preload: preload, class: class, sizes: sizes, alt: img.alt, widths: fallback_widths | |
-%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment