Skip to content

Commit fac57b8

Browse files
authored
ROOS-303: Make image required and enable the props: fetchPriority, wi… (#1470)
* ROOS-303: Make image required and enable the props: fetchPriority, width, height, loading, sizes and srcSet. * ROOS-303: Make alt required. * ROOS-303: Add missing alt tags.
1 parent 961dc70 commit fac57b8

File tree

12 files changed

+71
-30
lines changed

12 files changed

+71
-30
lines changed

.changeset/polite-cycles-vanish.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@nl-rvo/css-hero": minor
3+
"@nl-rvo/component-library-react": minor
4+
---
5+
6+
Make image required and enable the props: fetchPriority, width, height, loading, sizes and srcSet.

components/hero/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Het Hero component wordt altijd bovenaan een pagina geplaatst. De afbeelding die je wilt tonen kun je doen door middel van een object van `{src: string, alt: string}` of door een html `<img />` item mee te sturen.
1+
Het Hero component wordt altijd bovenaan een pagina geplaatst.
2+
De afbeelding is verplicht en kan worden aangeleverd als een afbeeldingsobject `{ src, alt }` of als een HTML`<img />` element.
3+
Bij gebruik van een afbeeldingsobject kunnen optioneel extra instellingen worden meegegeven, zoals `loading`, `decoding`, `fetchPriority`, `width` en `height`.

components/hero/hero.stories.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const argTypes = {
66
image: {
77
control: { type: 'object' },
88
},
9-
imageAlt: {
10-
control: { type: 'text' },
11-
},
129
title: {
1310
control: 'text',
1411
},
@@ -68,7 +65,7 @@ export const Default: Story = {
6865
export const CustomImageHero: Story = {
6966
args: {
7067
...defaultArgs,
71-
image: <img src="images/www/nieuwsbrief.webp" />,
68+
image: <img src="images/www/nieuwsbrief.webp" alt="newsletter" />,
7269
},
7370
name: 'Hero - Custom image',
7471
};

components/hero/src/presets/0-default.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export default (
55
uxpId="hero"
66
title="Rijksdienst voor Ondernemend Nederland"
77
subtitle="Wij helpen u graag vooruit!"
8-
image={{ src: 'https://placehold.co/600x400', alt: '' }}
9-
imageAlt="Alt text"
8+
image={{ src: 'https://placehold.co/600x400', alt: 'placeholder' }}
109
/>
1110
);

components/hero/src/template.tsx

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,79 @@ import Heading from '../../heading/src/template';
88
import MaxWidthLayout from '../../max-width-layout/src/template';
99
import './index.scss';
1010
import parseContentMarkup from '../../utils/parseContentMarkup';
11-
import { isOfType } from '../../utils/typeUtils';
11+
12+
type HeroImage =
13+
| {
14+
src: string;
15+
alt: string;
16+
loading?: 'eager' | 'lazy';
17+
fetchPriority?: 'high' | 'low' | 'auto';
18+
decoding?: 'async' | 'sync' | 'auto';
19+
width?: number;
20+
height?: number;
21+
srcSet?: string;
22+
sizes?: string;
23+
}
24+
| React.ReactElement<React.ImgHTMLAttributes<HTMLImageElement>>;
1225

1326
export interface IHeroProps {
14-
image?: Pick<HTMLImageElement, 'src' | 'alt'> | React.JSX.Element;
27+
image: HeroImage;
1528
title?: string;
1629
subtitle?: string;
1730
size?: 'sm' | 'md' | 'lg';
1831
children?: ReactNode | undefined;
1932
}
2033

21-
export const Hero: React.FC<IHeroProps & React.HTMLAttributes<HTMLDivElement>> = (props) => {
34+
type HeroProps = IHeroProps & React.HTMLAttributes<HTMLDivElement>;
35+
36+
export const Hero: React.FC<HeroProps> = (props) => {
2237
const { image, title, subtitle, size, className, children, ...rootElementProps } = props;
2338

2439
// Parse content markup (either a string, HTML string, React node or children)
2540
const contentMarkup: string | ReactNode = parseContentMarkup(children);
26-
2741
const renderImage = () => {
2842
if (React.isValidElement(image)) {
2943
return <span className="rvo-hero__custom-image-wrapper">{image}</span>;
3044
}
3145

32-
if (isOfType(image, 'src') && image.src) return <img src={image.src} className="rvo-hero__image" alt={image.alt} />;
46+
if (!image.src.trim()) throw new Error('Hero: image.src must be a non-empty string.');
47+
48+
if (!image.alt.trim()) throw new Error('Hero: image.alt must be a non-empty string.');
49+
50+
const loading = image.loading ?? 'eager';
51+
const fetchPriority = image.fetchPriority ?? (loading === 'eager' ? 'high' : 'auto');
52+
const decoding = image.decoding ?? 'async';
3353

34-
return null;
54+
return (
55+
<img
56+
src={image.src}
57+
alt={image.alt}
58+
className="rvo-hero__image"
59+
loading={loading}
60+
fetchPriority={fetchPriority}
61+
decoding={decoding}
62+
width={image.width}
63+
height={image.height}
64+
srcSet={image.srcSet}
65+
sizes={image.sizes}
66+
/>
67+
);
3568
};
3669

3770
return (
3871
<MaxWidthLayout size={size} className={clsx('rvo-hero', className)} {...rootElementProps}>
3972
<div className="rvo-hero__image-container">{renderImage()}</div>
40-
<div className="rvo-hero__content">
41-
<Heading type="h1" className="rvo-hero__title" noMargins={true}>
42-
{title}
43-
</Heading>
44-
<span className="rvo-hero__subtitle">{subtitle}</span>
45-
{contentMarkup}
46-
</div>
73+
{(title || subtitle || contentMarkup) && (
74+
<div className="rvo-hero__content">
75+
{title && (
76+
<Heading type="h1" className="rvo-hero__title" noMargins={true}>
77+
{title}
78+
</Heading>
79+
)}
80+
{subtitle && <span className="rvo-hero__subtitle">{subtitle}</span>}
81+
{contentMarkup}
82+
</div>
83+
)}
4784
</MaxWidthLayout>
4885
);
4986
};

components/image/image.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const Decorative: Story = {
167167
name: 'Decoratief (lage prioriteit)',
168168
args: {
169169
...defaultArgs,
170-
alt: '',
170+
alt: 'deco',
171171
loading: 'lazy',
172172
fetchPriority: 'low',
173173
},

documentation/demopages/experimenteel/www/ImageLines.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const Home = () => {
5656
<Hero
5757
size="lg"
5858
title="Rijksdienst voor Ondernemend Nederland"
59-
image={{ src: 'images/www/home.jpg', alt: '' }}
59+
image={{ src: 'images/www/home.jpg', alt: 'home' }}
6060
/>
6161
</div>
6262
<main className="rvo-max-width-layout rvo-max-width-layout--lg rvo-padding-block-end--3xl rvo-padding-inline-end--sm rvo-padding-inline-start--sm">
@@ -542,7 +542,7 @@ const Home = () => {
542542
</LayoutFlow>
543543
</div> */}
544544
{/* <div className="rvo-feedback">
545-
545+
546546
</div> */}
547547
</main>
548548
{/* <div className="rvo-section rvo-section--www rvo-section--coloured-bg rvo-section--grijs-3">

documentation/demopages/templates/Content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Content = () => {
3636
/>
3737
<LayoutFlow gap="3xl">
3838
<LayoutFlow gap="xl">
39-
<Hero title="Heading" image={{ src: 'images/www/home.jpg', alt: '' }} />
39+
<Hero title="Heading" image={{ src: 'images/www/home.jpg', alt: 'home' }} />
4040
<main className="rvo-max-width-layout rvo-max-width-layout--sm rvo-max-width-layout-inline-padding--md">
4141
<Heading type="h1">H1 heading</Heading>
4242
<p className="rvo-text--lg">

documentation/demopages/voorbeelden/nieuwsbrieven/Nieuwsbrief.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const Nieuwsbrief = () => {
6767
size="lg"
6868
image={{
6969
src: 'https://www.rvo.nl/_next/image?url=%2Ffiles%2Ffile%2Fstyles%2Fcontent%2Fpublic%2F2024-05%2FParticulier01.png%3Fitok%3D445OIaw-&w=640&q=85',
70-
alt: '',
70+
alt: 'next',
7171
}}
7272
title="Duurzaam ondernemen"
7373
subtitle=""

documentation/demopages/voorbeelden/www/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const Home = () => {
5555
<div className="rvo-padding-inline-end--sm rvo-padding-inline-start--sm">
5656
<Hero
5757
size="lg"
58-
image={{ src: 'images/www/home.jpg', alt: '' }}
58+
image={{ src: 'images/www/home.jpg', alt: 'home' }}
5959
title="Rijksdienst voor Ondernemend Nederland"
6060
subtitle="Wij helpen u graag vooruit!"
6161
/>

0 commit comments

Comments
 (0)