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

Load picture srcset from partial #4

Merged
merged 7 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions resources/views/components/_picture.antlers.html
Expand Up @@ -5,6 +5,7 @@
@param* sizes The sizes attribute. Something like `(min-width: 768px) 55vw, 90vw` for example.
@param aspect_ratio Pass in an aspect ratio to crop the image in a certain way. `16/9` for example or specify a second ratio for larger screens: `1/1 large:1/2`.
@param skip_ratio_steps Integer. Skip 1, 2 or 3 ratio steps to force small screens rendering big images to use mobile cropping instead of `large` cropping.
@param srcset The path to a partial with an alternative srcsets variable array. Something like `snippets/picture/srcsets/full_width` for example.
marcorieser marked this conversation as resolved.
Show resolved Hide resolved
@param class Add optional CSS classes.
@param cover Boolean. Whether the image should cover the parent. Uses the focus position.
@param bg String. Sets a background color for transparent images.
Expand Down Expand Up @@ -35,18 +36,11 @@
{{ /aspect_ratio }}
{{ original_ratio = height / width }}

{{# Initialize srcset variable in current scope. #}}
{{ srcsets = null }}

{{# Set srcset sizes. #}}
{{ srcsets = [
[ 'width' => 320, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 480, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 640, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 768, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 1024, 'ratio' => '{ ((skip_ratio_steps != 1 && skip_ratio_steps != 2 && skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1280, 'ratio' => '{ ((skip_ratio_steps != 2 && skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1440, 'ratio' => '{ ((skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1536, 'ratio' => '{ ratio_large ?? ratio ?? original_ratio }' ],
[ 'width' => 1680, 'ratio' => '{ ratio_large ?? ratio ?? original_ratio }' ]
] }}
{{ partial src="{ srcset ?: 'statamic-peak-tools::snippets/picture/srcsets/default' }" }}

<picture>
{{ if extension == 'svg' || extension == 'gif' }}
Expand Down
11 changes: 11 additions & 0 deletions resources/views/snippets/picture/srcsets/_default.antlers.html
robdekort marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,11 @@
{{ srcsets = [
robdekort marked this conversation as resolved.
Show resolved Hide resolved
[ 'width' => 320, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 480, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 640, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 768, 'ratio' => '{ ratio ?? original_ratio }' ],
[ 'width' => 1024, 'ratio' => '{ ((skip_ratio_steps != 1 && skip_ratio_steps != 2 && skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1280, 'ratio' => '{ ((skip_ratio_steps != 2 && skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1440, 'ratio' => '{ ((skip_ratio_steps != 3) ?= ratio_large) ?? ratio ?? original_ratio }' ],
[ 'width' => 1536, 'ratio' => '{ ratio_large ?? ratio ?? original_ratio }' ],
[ 'width' => 1680, 'ratio' => '{ ratio_large ?? ratio ?? original_ratio }' ]
] }}