Skip to content

Commit ad560ad

Browse files
author
Daniel Morse
committed
feat: pass placeholder values, ratio data via web component props
1 parent f7e9e93 commit ad560ad

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

packages/components/bolt-image/image.schema.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ properties:
2626
enum:
2727
- true
2828
- false
29+
placeholder_color:
30+
type: string
31+
description: A valid CSS background color property shown while image loads.
32+
default: hsl(233, 33%, 97%)
33+
placeholder_image:
34+
type: string
35+
description: Image path or image data shown while image loads.
36+
default: data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
2937
srcset:
3038
type: string
3139
description: A comma seperated string of image urls and image widths, used for optimizing image loading performance.
@@ -40,13 +48,10 @@ properties:
4048
enum:
4149
- true
4250
- false
43-
no_ratio:
44-
type: boolean
45-
description: Override the default useAspectRatio behavior. Used only on the web component, where the presence of a boolean property always equates to `true`.
46-
default: false
47-
enum:
48-
- true
49-
- false
51+
ratio:
52+
type: string
53+
description: Set the aspect ratio for the image via colon-separated width and height values, e.g. 4:3. Currently required for aspect ratio to be applied properly. Set to "none" to opt out of aspect ratio. Used only on the web component.
54+
default: auto
5055
width:
5156
oneOf:
5257
- type: number

packages/components/bolt-image/src/image.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class BoltImage extends withLitHtml() {
2929
noLazy: props.boolean,
3030
srcset: props.string,
3131
sizes: props.string,
32-
noRatio: props.boolean,
32+
ratio: props.string,
3333
width: props.string,
3434
height: props.string,
35+
placeholderColor: props.string,
36+
placeholderImage: props.string,
3537
zoom: props.boolean,
3638
};
3739

@@ -56,18 +58,6 @@ class BoltImage extends withLitHtml() {
5658
return modifiedSchema;
5759
}
5860

59-
getImageData(imagePath) {
60-
// TODO: Get real image data
61-
return {
62-
height: 480,
63-
width: 640,
64-
base64:
65-
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
66-
color: 'hsl(233, 33%, 97%)',
67-
isAbsolute: false,
68-
};
69-
}
70-
7161
lazyloadImage(image) {
7262
// console.log('@lazyloadImage');
7363
if (!this.isLoaded) {
@@ -93,23 +83,43 @@ class BoltImage extends withLitHtml() {
9383
noLazy,
9484
srcset,
9585
sizes,
96-
noRatio,
86+
ratio,
9787
width,
9888
height,
89+
placeholderColor,
90+
placeholderImage,
9991
zoom,
10092
} = this.validateProps(this.props);
10193

10294
// negate and rename variables for readability
10395
const lazyload = !noLazy;
104-
const useAspectRatio = !noRatio;
96+
97+
// use ratio by default, still depends upon width and height being set
98+
let useRatio = true;
99+
let ratioW;
100+
let ratioH;
101+
102+
if (ratio) {
103+
switch (ratio) {
104+
case 'none':
105+
useRatio = false;
106+
break;
107+
case 'auto':
108+
useRatio = true;
109+
break;
110+
default:
111+
useRatio = true;
112+
if (ratio.indexOf(':') > -1) {
113+
const ratioArr = ratio.split(':');
114+
ratioW = ratioArr[0];
115+
ratioH = ratioArr[1];
116+
}
117+
}
118+
}
119+
120+
const canUseRatio = ratioW > 0 && ratioH > 0 && useRatio;
105121

106122
const imageExt = path.extname(src);
107-
const imageData = this.getImageData(src);
108-
const placeholderColor = imageData.color;
109-
const placeholderSrc = imageData.base64;
110-
const imageWidth = width || imageData.width;
111-
const imageHeight = height || imageData.height;
112-
const canUseAspectRatio = imageWidth && imageHeight && useAspectRatio;
113123

114124
const classes = cx('c-bolt-image__image', {
115125
'c-bolt-image__lazyload': lazyload,
@@ -123,7 +133,7 @@ class BoltImage extends withLitHtml() {
123133
return html`
124134
<img
125135
class="${classes}"
126-
src="${lazyload ? placeholderSrc : src}"
136+
src="${lazyload ? placeholderImage : src}"
127137
alt="${ifDefined(alt ? alt : undefined)}"
128138
srcset="${ifDefined(
129139
!lazyload || this.isLoaded ? srcset || src : undefined,
@@ -138,11 +148,11 @@ class BoltImage extends withLitHtml() {
138148
};
139149

140150
const placeholderImageElement = () => {
141-
if (useAspectRatio && imageExt === '.jpg') {
151+
if (canUseRatio && imageExt === '.jpg') {
142152
return html`
143153
<img
144154
class="${cx('c-bolt-image__image-placeholder')}"
145-
src="${placeholderSrc}"
155+
src="${placeholderImage}"
146156
alt="${ifDefined(alt ? alt : undefined)}"
147157
/>
148158
`;
@@ -173,16 +183,15 @@ class BoltImage extends withLitHtml() {
173183
const ratioTemplate = children => {
174184
return html`
175185
<bolt-ratio
176-
attributes=""
177-
aspect-ratio-width="${imageWidth * 1}"
178-
aspect-ratio-height="${imageHeight * 1}"
186+
aspect-ratio-width="${ratioW * 1}"
187+
aspect-ratio-height="${ratioH * 1}"
179188
>
180189
${children}
181190
</bolt-ratio>
182191
`;
183192
};
184193

185-
if (placeholderColor && canUseAspectRatio && imageExt === '.jpg') {
194+
if (canUseRatio && imageExt === '.jpg' && placeholderColor) {
186195
this.style.backgroundColor = placeholderColor;
187196
}
188197

packages/components/bolt-image/src/image.twig

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
{% set image_data_twig = getImageData(src) %} {# Data from processing image file #}
99

1010
{% set ext = src|split('.')|last %}
11+
{% set display = display in display_options ? display : schema.properties.display.default %}
1112
{% set height = height|default(image_data_twig.height) %}
1213
{% set width = width|default(image_data_twig.width) %}
1314
{% set lazyload = lazyload ?? true %}
14-
{% set placeholder_dummy_src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" %}
15-
{% set placeholder_color = image_data_twig.color|default('hsl(233, 33%, 97%)') %}
16-
{% set placeholder_image = image_data_twig.base64|default(placeholder_dummy_src) %}
17-
{% set sizes = sizes | default("auto") %}
15+
{% set sizes = sizes|default("auto") %}
1816
{% set srcset = srcset|default(src) %}
17+
1918
{% if image_data_bolt.srcset %}
2019
{% set srcset = image_data_bolt.srcset %}
2120
{% endif %}
2221

22+
{# Set alt to null if missing, prevents empty alt tags #}
2323
{% set alt = alt ? alt : null %}
2424

25-
{% if (ratio is defined and ratio == false) or no_ratio %}
26-
{% set useAspectRatio = false %}
27-
{% endif %}
25+
{# Temp: prefix "ph" to preserve original value for web component, better pattern in the works #}
26+
{% set ph_color = placeholder_color|default(image_data_twig.color|default(schema.properties.placeholder_color.default)) %}
27+
{% set ph_image = placeholder_image|default(image_data_twig.base64|default(schema.properties.placeholder_image.default)) %}
28+
2829
{% set useAspectRatio = useAspectRatio ?? true %}
30+
{% set can_use_ratio = width > 0 and height > 0 and useAspectRatio is sameas(true) %}
2931

3032
{#--------------------#}
3133
{# STOP!!!!!!!!!#}
@@ -66,7 +68,6 @@
6668
.setAttribute(srcset ? "srcset" : "", srcset ? srcset : null)
6769
%}
6870

69-
7071
{% set image_tag %}
7172
<img {{ image_attributes }} />
7273

@@ -82,7 +83,7 @@
8283
.removeAttribute("src")
8384
.removeAttribute("data-sizes")
8485
.removeAttribute("srcset")
85-
.setAttribute("src", placeholder_image)
86+
.setAttribute("src", ph_image)
8687
}} />
8788
{% endset %}
8889

@@ -94,13 +95,15 @@
9495
{% if sizes %}sizes="{{ sizes }}"{% endif %}
9596
{% if width %}width="{{ width }}"{% endif %}
9697
{% if height %}height="{{ height }}"{% endif %}
97-
{% if not useAspectRatio %}no-ratio{% endif %}
98+
{% if not useAspectRatio %}ratio="none"{% elseif can_use_ratio %}ratio="{{ width }}:{{ height }}"{% endif %}
99+
{% if placeholder_color %}placeholder-color="{{ placeholder_color }}"{% endif %}
100+
{% if placeholder_image %}placeholder-image="{{ placeholder_image }}"{% endif %}
98101
{% if zoom %}zoom{% endif %}
99102
{% if not lazyload %}no-lazy{% endif %}
100-
{% if placeholder_color and ext == "jpg" %} style="background-color: {{ placeholder_color }};" {% endif %}
103+
{% if can_use_ratio and ext == "jpg" %} style="background-color: {{ ph_color }};" {% endif %}
101104
>
102105
{% block image_content %}
103-
{% if width > 0 and height > 0 and useAspectRatio == true %}
106+
{% if can_use_ratio %}
104107
{% include "@bolt-components-ratio/ratio.twig" with {
105108
attributes: attributes,
106109
aspectRatioHeight: height * 1,
@@ -111,7 +114,6 @@
111114
] | join("")
112115
} only %}
113116
{% else %}
114-
{{ ext == "jpg" ? image_placeholder : "" }}
115117
{{ image_tag }}
116118
{% endif %}
117119
{% endblock %}

0 commit comments

Comments
 (0)