Skip to content

Commit

Permalink
Allow responsive images in hero and card component
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Oct 24, 2023
1 parent f0056d3 commit 02a023c
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Chips can be made plain with `tna-chip--plain`
- Visual regression tests can now be run in the Storyobok UI
- Allow right/left margins to be added to columns
- Hero components can have different image sources with `<source>` elements

### Changed

Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Font paths fixed for prototype kit, stylesheets and JavaScript loading
- Better alignment of site name next to logo in header
- Fixed right/left padding of logo and hamburger on small devices
- Card image type is now variable when using sources

### Security

Expand Down
5 changes: 4 additions & 1 deletion src/nationalarchives/components/card/card.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const argTypes = {
imageAlt: { control: "text" },
imageWidth: { control: { type: "number", min: 1 } },
imageHeight: { control: { type: "number", min: 1 } },
imageType: { control: "text" },
imageSources: { control: "object" },
label: { control: "text" },
meta: { control: "object" },
Expand Down Expand Up @@ -47,6 +48,7 @@ const Template = ({
imageAlt,
imageWidth,
imageHeight,
imageType,
imageSources,
label,
meta,
Expand All @@ -70,6 +72,7 @@ const Template = ({
imageAlt,
imageWidth,
imageHeight,
imageType,
imageSources,
label,
meta,
Expand Down Expand Up @@ -270,7 +273,7 @@ Sources.args = {
imageHeight: 404,
imageSources: [
{
imageSrc: "https://www.gstatic.com/webp/gallery/2.webp",
src: "https://www.gstatic.com/webp/gallery/2.webp",
type: "image/webp",
},
],
Expand Down
6 changes: 6 additions & 0 deletions src/nationalarchives/components/card/macro-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"required": false,
"description": "The height of the image in pixels."
},
{
"name": "imageType",
"type": "string",
"required": false,
"description": ""
},
{
"name": "imageSources",
"type": "array",
Expand Down
6 changes: 3 additions & 3 deletions src/nationalarchives/components/card/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
{%- endif -%}
<picture class="tna-card__image">
{%- if params.imageSources -%}
{%- for source in params.imageSources -%}
{%- for source in params.imageSources %}
<source srcset="{{ source.src }}" type="{{ source.type }}" width="{{ source.width if source.width else params.imageWidth }}" height="{{ source.height if source.height else params.imageHeight }}">
{%- endfor -%}
<source srcset="{{ params.imageSrc }}" type="image/jpeg" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
{%- endfor %}
<source srcset="{{ params.imageSrc }}" type="{{ params.imageType if params.imageType else 'image/jpeg' }}" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
{%- endif -%}
<img src="{{ params.imageSrc }}" alt="{{ params.imageAlt }}" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
</picture>
Expand Down
95 changes: 88 additions & 7 deletions src/nationalarchives/components/hero/fixtures.json

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/nationalarchives/components/hero/hero.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

@include colour.invert;

// @include typography.relative-font-size(16);
@include typography.relative-font-size(16);
}

&__image {
Expand All @@ -94,7 +94,12 @@
inset: 0;
z-index: 1;

object-fit: cover;
img {
width: 100%;
height: 100%;

object-fit: cover;
}
}

&__details[open] &__details-summary {
Expand Down Expand Up @@ -234,6 +239,10 @@
position: static;

order: 1;

img {
height: auto;
}
}

&__inner {
Expand Down
30 changes: 27 additions & 3 deletions src/nationalarchives/components/hero/hero.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ const argTypes = {
heading: { control: "text" },
body: { control: "text" },
text: { control: "text" },

imageSrc: { control: { type: "file", accept: ".jpg" } },
imageAlt: { control: "text" },
imageWidth: { control: { type: "number", min: 1 } },
imageHeight: { control: { type: "number", min: 1 } },

imageType: { control: "text" },
imageSources: { control: "object" },
imageCaption: { control: "text" },

classes: { control: "text" },
attributes: { control: "object" },
};
Expand All @@ -39,6 +38,8 @@ const Template = ({
imageAlt,
imageWidth,
imageHeight,
imageType,
imageSources,
imageCaption,
classes,
attributes,
Expand All @@ -52,6 +53,8 @@ const Template = ({
imageAlt,
imageWidth,
imageHeight,
imageType,
imageSources,
imageCaption,
classes,
attributes,
Expand Down Expand Up @@ -120,6 +123,27 @@ CaptionWithNoHeading.args = {
classes: "tna-hero--demo",
};

export const Sources = Template.bind({});
Sources.args = {
heading: "Title",
body: "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>",
imageSrc:
"https://www.nationalarchives.gov.uk/wp-content/uploads/sites/24/2023/07/tna-building-compress.jpg",
imageAlt: "The National Archives office",
imageWidth: 499,
imageHeight: 333,
imageSources: [
{
src: "https://www.gstatic.com/webp/gallery/2.webp",
type: "image/webp",
media: "(max-width: 48em)",
width: 550,
height: 404,
},
],
classes: "tna-hero--demo",
};

export const Mobile = Template.bind({});
Mobile.parameters = {
viewport: {
Expand Down
44 changes: 44 additions & 0 deletions src/nationalarchives/components/hero/macro-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@
"required": true,
"description": ""
},
{
"name": "imageType",
"type": "string",
"required": false,
"description": ""
},
{
"name": "imageSources",
"type": "array",
"required": true,
"description": "Alternative sources of the image which can include WebP files.",
"params": [
{
"name": "src",
"type": "string",
"required": true,
"description": ""
},
{
"name": "type",
"type": "string",
"required": true,
"description": ""
},
{
"name": "media",
"type": "string",
"required": false,
"description": ""
},
{
"name": "width",
"type": "number",
"required": false,
"description": ""
},
{
"name": "height",
"type": "number",
"required": false,
"description": ""
}
]
},
{
"name": "imageCaption",
"type": "string",
Expand Down
10 changes: 9 additions & 1 deletion src/nationalarchives/components/hero/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
</details>
</figcaption>
{%- endif -%}
<img src="{{ params.imageSrc }}" alt="{{ params.imageAlt }}" class="tna-hero__image" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
<picture class="tna-hero__image">
{%- if params.imageSources -%}
{%- for source in params.imageSources %}
<source srcset="{{ source.src }}" type="{{ source.type }}" width="{{ source.width if source.width else params.imageWidth }}" height="{{ source.height if source.height else params.imageHeight }}"{% if source.media %} media="{{ source.media }}"{% endif %}>
{%- endfor %}
<source srcset="{{ params.imageSrc }}" type="{{ params.imageType if params.imageType else 'image/jpeg' }}" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
{%- endif -%}
<img src="{{ params.imageSrc }}" alt="{{ params.imageAlt }}" width="{{ params.imageWidth }}" height="{{ params.imageHeight }}">
</picture>
{%- if params.heading -%}
<div class="tna-container tna-hero__inner">
<div class="tna-column tna-column--width-2-3 tna-column--full-small tna-column--full-tiny tna-hero__content">
Expand Down

0 comments on commit 02a023c

Please sign in to comment.