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

Feature: Responsive media component #45

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 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
4 changes: 2 additions & 2 deletions hugo-modules/core/utils/asset/image/img.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Render Contentful asset (image/...). Uses data from cssg-plugin-assets if available.

@example - Go Template
{{- partial "utils/asset/image" (dict
{{- partial "utils/asset/image/img" (dict
"context" $image
"options" (dict
"sizes" "(max-width: 768px) 100vw, 768px"
Expand Down Expand Up @@ -51,7 +51,7 @@
{{- $width := .width -}}
{{- $height := .height -}}
{{/* Preload hint is added in sources.html */}}
<img src="{{- $src -}}" width="{{- $width -}}" height="{{- $height -}}" {{ $image_attributes }} alt="{{- $alt -}}"{{ with $params.preload }} fetchpriority="high"{{ end }}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we needed it because with different ratios the sizes come from the <source> elements. Maybe it is enough there. But I just noticed another problem, there is a layout shift when the sources have a media attribute. I am looking for a solution.

<img src="{{- $src -}}" {{ $image_attributes }} alt="{{- $alt -}}"{{ with $params.preload }} fetchpriority="high"{{ end }}>
{{- end -}}
{{- else -}}
{{- with $url -}}
Expand Down
4 changes: 2 additions & 2 deletions hugo-modules/core/utils/asset/image/sources.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Render Contentful asset (image/...). Uses data from cssg-plugin-assets if available.

@example - Go Template
{{- partial "utils/asset/image" (dict
{{- partial "utils/asset/image/sources" (dict
"context" $image
"options" (dict
"sizes" "(max-width: 768px) 100vw, 768px"
Expand Down Expand Up @@ -53,7 +53,7 @@
{{- range $srcsets -}}
{{ $srcset := .srcset | default "" }}
{{ $type := .type | default ""}}
<source sizes="{{- $local_sizes -}}" srcset="{{- $srcset -}}" type="{{- $type -}}"{{ with $media}}media="{{ . }}"{{ end }}>
<source sizes="{{- $local_sizes -}}" width="{{- $width -}}" height="{{- $height -}}" srcset="{{- $srcset -}}" type="{{- $type -}}"{{ with $media}}media="{{ . }}"{{ end }}>
{{- $preload_attr := (dict
"as" "image"
"imagesizes" $local_sizes
Expand Down
2 changes: 1 addition & 1 deletion hugo-modules/core/utils/asset/svg.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{- partial "utils/asset/svg" (dict
"context" $svg
"options" (dict
"inline" "true"
"inline" true
"class_name" "c-icon"
"lazy" true
)
Expand Down
24 changes: 9 additions & 15 deletions hugo-modules/core/utils/asset/video.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Render Contentful asset (video/...). Uses data from cssg-plugin-assets if available.

@example - Go Template
{{- partial "utils/asset/svg" (dict
{{- partial "utils/asset/video" (dict
"context" $video
"options" (dict
"loop" "false"
"muted" "false"
"autoplay" "false"
"loop" false
"muted" false
"autoplay" false
"class_name" "c-video"
)
) -}}
Expand All @@ -26,9 +26,10 @@
{{- $autoplay := $params.autoplay | default true -}}
{{- $playsinline := $params.playsinline | default true -}}
{{- $controls := $params.controls | default false -}}
{{- $poster := $params.poster | default false -}}
{{/* Use transparent base64, the real poster should be loaded as <picture> element */}}
{{- $poster := "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" -}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not skip the poster attribute completely?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I asked myself the same question (see TBD). The poster is not a mandatory attribute, but there was some reason... maybe the background is black when the video is not yet loaded? I don't know. I will try to find out and take it out for now.

{{- $muted := (or $autoplay $params.muted) -}}
{{- $loading := cond (eq $params.lazy true) "lazy" ($params.loading | default false) -}}
{{- $loading := cond (eq ($params.lazy | default true) true) "lazy" ($params.loading | default false) -}}
{{- $preload := $params.preload | default false -}}
{{- $media := $params.media -}}

Expand All @@ -39,18 +40,11 @@
"controls" $controls
"class" $class_name
"poster" $poster
"loading" $loading
) $params -}}

{{- if in $mime_type "video" -}}
{{- $preload_attr := (dict
"as" "image"
"href" $poster
"media" $media
) -}}
{{- if and $preload $poster (not (in ($globals.Scratch.Get "preload") $preload)) -}}
{{- $globals.Scratch.Add "preload" (slice $preload) -}}
{{- end -}}
<video {{$attributes}}>
<video {{$attributes}}{{- if eq $loading "lazy" -}} data-video{{- end -}}>
{{- if eq $loading "lazy" -}}
<source data-src="{{- $url -}}" type="{{- $mime_type -}}" />
{{- else -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
const defaultLocale = await helpers.locale.getDefaultLocale();

const cMedia = migration
.createContentType('c-media')
.name('Component: Media')
.description('Images / loop videos with predefined ratios')
.createContentType('c-responsive-media')
.name('Component: Responsive media')
.description('Image or short loop video for different screen sizes')
.displayField('internal_name');

cMedia
Expand All @@ -23,25 +23,15 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.createField('alt')
.name('Alt text')
.type('Symbol')
.localized(false)
.required(false)
.validations([])
.disabled(false)
.omitted(false);

cMedia
.createField('caption')
.name('Caption')
.type('Text')
.localized(false)
.required(false)
.localized(true)
.required(true)
.validations([])
.disabled(false)
.omitted(false);

cMedia
.createField('media')
.name('Media')
.createField('mobile_media')
.name('Mobile > Media')
.type('Link')
.localized(true)
.required(true)
Expand All @@ -56,35 +46,21 @@ module.exports = withHelpers(async (migration, _context, helpers) => {

cMedia
.createField('mobile_ratio')
.name('Mobile ratio')
.name('Mobile > Ratio')
.type('Symbol')
.localized(false)
.required(false)
.validations([
{
in: ['hd', 'rectangle', 'square', 'portrait'],
in: ['1x1', '16x9', '9x16', '4x3', '3x4'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely the better naming and more obviously 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so too. 👍 Unfortunately, there is still a bug in Sonar that I don't exactly understand yet. I'll have a look. https://sonarcloud.io/project/issues?resolved=false&types=BUG&pullRequest=45&id=jungvonmatt_create-contentful-hugo-app&open=AYNg2o7ptY5uRUoaXYTE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like sonar thinks that this is a mathematical expression and you just could write 1, I guess..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dlemm, that solved it.

},
])
.disabled(false)
.omitted(false);

cMedia
.createField('desktop_ratio')
.name('Desktop ratio')
.type('Symbol')
.localized(false)
.required(false)
.validations([
{
in: ['hd', 'rectangle', 'square', 'portrait'],
},
])
.disabled(false)
.omitted(false);

cMedia
.createField('focus_area')
.name('Focus area')
.createField('mobile_focus_area')
.name('Mobile > Focus area')
.type('Symbol')
.localized(false)
.required(false)
Expand Down Expand Up @@ -112,8 +88,8 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.omitted(false);

cMedia
.createField('media_hd')
.name('Media hd')
.createField('desktop_media')
.name('Desktop > Media')
.type('Link')
.localized(true)
.required(false)
Expand All @@ -127,49 +103,47 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.linkType('Asset');

cMedia
.createField('media_rectangle')
.name('Media rectangle')
.type('Link')
.localized(true)
.required(false)
.validations([
{
linkMimetypeGroup: ['image', 'video'],
},
])
.disabled(false)
.omitted(false)
.linkType('Asset');

cMedia
.createField('media_square')
.name('Media square')
.type('Link')
.localized(true)
.createField('desktop_ratio')
.name('Desktop > Ratio')
.type('Symbol')
.localized(false)
.required(false)
.validations([
{
linkMimetypeGroup: ['image', 'video'],
in: ['1x1', '16x9', '9x16', '4x3', '3x4'],
},
])
.disabled(false)
.omitted(false)
.linkType('Asset');
.omitted(false);

cMedia
.createField('media_portrait')
.name('Media portrait')
.type('Link')
.localized(true)
.createField('desktop_focus_area')
.name('Desktop > Focus area')
.type('Symbol')
.localized(false)
.required(false)
.validations([
{
linkMimetypeGroup: ['image', 'video'],
in: [
'center',
'top',
'bottom',
'right',
'left',
'top_right',
'top_left',
'bottom_right',
'bottom_left',
'face',
'faces',
],
},
])
.defaultValue({
[defaultLocale.code]: 'center',
})
.disabled(false)
.omitted(false)
.linkType('Asset');
.omitted(false);

cMedia
.createField('lazy')
Expand All @@ -185,49 +159,32 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.omitted(true);

cMedia.changeFieldControl('internal_name', 'builtin', 'singleLine', {
helpText: 'e.g. "Home page > Stage > Media"',
helpText: 'e.g. "Home page > Media > Responsive media"',
});

cMedia.changeFieldControl('alt', 'builtin', 'singleLine', {});

cMedia.changeFieldControl('caption', 'builtin', 'multipleLine', {});
cMedia.changeFieldControl('mobile_media', 'builtin', 'assetLinkEditor', {});

cMedia.changeFieldControl('media', 'builtin', 'assetLinkEditor', {});

cMedia.changeFieldControl('mobile_ratio', 'builtin', 'dropdown', {
helpText: 'Select the ratio for mobile. Leave empty to use original image dimensions',
});

cMedia.changeFieldControl('desktop_ratio', 'builtin', 'dropdown', {
helpText: 'Select the ratio for desktop. Leave empty to use original image dimensions',
cMedia.changeFieldControl('mobile_focus_area', 'builtin', 'dropdown', {
helpText: 'Choose a focus area that will be used for resizing. Default is "center".',
});

cMedia.changeFieldControl('focus_area', 'builtin', 'dropdown', {
helpText: 'You can choose the focus area for resizing. The default is center',
});

cMedia.changeFieldControl('media_hd', 'builtin', 'assetLinkEditor', {
helpText: 'Ratio: 16:9',
showLinkEntityAction: true,
showCreateEntityAction: true,
cMedia.changeFieldControl('mobile_ratio', 'builtin', 'dropdown', {
helpText: 'Select a ratio for mobile or leave empty to use original image dimensions.',
});

cMedia.changeFieldControl('media_rectangle', 'builtin', 'assetLinkEditor', {
helpText: 'Ratio: 4:3',
showLinkEntityAction: true,
showCreateEntityAction: true,
cMedia.changeFieldControl('desktop_media', 'builtin', 'assetLinkEditor', {
helpText:
'Can be used to use different ratios on mobile and desktop. If empty, the mobile media will be used for both.',
});

cMedia.changeFieldControl('media_square', 'builtin', 'assetLinkEditor', {
helpText: 'Ratio: 1:1',
showLinkEntityAction: true,
showCreateEntityAction: true,
cMedia.changeFieldControl('desktop_ratio', 'builtin', 'dropdown', {
helpText: 'Select a ratio for desktop. Leave empty to use original media dimensions.',
});

cMedia.changeFieldControl('media_portrait', 'builtin', 'assetLinkEditor', {
helpText: 'Ratio: 2:3',
showLinkEntityAction: true,
showCreateEntityAction: true,
cMedia.changeFieldControl('desktop_focus_area', 'builtin', 'dropdown', {
helpText: 'Choose a focus area that will be used for resizing. Default is "center".',
});

cMedia.changeFieldControl('lazy', 'builtin', 'boolean', {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = async function (migration) {
.required(false)
.validations([
{
linkContentType: ['c-image', 'c-media'],
linkContentType: ['c-responsive-media'],
},
])
.disabled(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.required(false)
.validations([
{
linkContentType: ['c-image', 'c-media'],
linkContentType: ['c-responsive-media'],
},
])
.disabled(false)
Expand Down Expand Up @@ -149,7 +149,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
type: 'Link',
validations: [
{
linkContentType: ['c-link', 'c-editorial', 'c-image', 'c-media', 'c-video'],
linkContentType: ['c-link', 'c-editorial', 'c-responsive-media'],
},
],
linkType: 'Entry',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
type: 'Link',
validations: [
{
linkContentType: ['c-editorial', 'c-image', 'c-media', 'c-video'],
linkContentType: ['c-editorial', 'c-responsive-media'],
},
],
linkType: 'Entry',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
type: 'Link',
validations: [
{
linkContentType: ['c-editorial', 'c-image', 'c-media', 'c-video'],
linkContentType: ['c-editorial', 'c-responsive-media'],
},
],
linkType: 'Entry',
Expand All @@ -116,7 +116,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
type: 'Link',
validations: [
{
linkContentType: ['c-editorial', 'c-image', 'c-media', 'c-video'],
linkContentType: ['c-editorial', 'c-responsive-media'],
},
],
linkType: 'Entry',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = withHelpers(async (migration, _context, helpers) => {
.required(false)
.validations([
{
linkContentType: ['c-image', 'c-media'],
linkContentType: ['c-responsive-media'],
},
])
.disabled(false)
Expand Down
Loading