Skip to content

Commit

Permalink
Added: Ratio container for picture component
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo authored Aug 25, 2020
1 parent 9cbf54d commit 097493f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions templates/_components/picture.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@param {string} svgConvertClass
@param {string} alt
@param {bool} lazy
@param {bool} ratio
#}

Expand Down Expand Up @@ -42,8 +43,9 @@
{# All other image types #}

{% set production = craft.app.config.env == 'production' ? true : false %}
{% set lazy = lazy ?? true %}
{% set transform = transform ?? [] %}
{% set lazy = lazy ?? true %}
{% set ratio = ratio ?? true %}
{% set webp = craft.imager.serverSupportsWebp() and production %}

{% set pictureTransform = craft.imager.transformImage(
Expand All @@ -66,7 +68,14 @@
webp
%}

<picture{% if pictureClass is defined %} class="{{ pictureClass }}"{% endif %}>
{% if ratio is defined %}
<div
class="relative overflow-hidden w-full h-full"
style="padding-top: {{ (pictureTransform[0].height / pictureTransform[0].width) * 100 }}%;"
>
{% endif %}

<picture{% if pictureClass is defined or ratio is defined %} class="{{ ratio is defined ? 'absolute top-0 left-0 w-full h-full' }} {{ pictureClass is defined ? pictureClass }}"{% endif %}>
{% if lazy %}
{% if webp %}
<source
Expand Down Expand Up @@ -101,6 +110,10 @@
{% endif %}
</picture>

{% if ratio is defined %}
</div>
{% endif %}

{% endif %}

{% endif %}

0 comments on commit 097493f

Please sign in to comment.