Skip to content

Fixes for image sizes #74

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"concurrently": "^6.5.1",
"css-loader": "^6.7.1",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.10.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-concat": "^2.6.1",
Expand Down
18 changes: 15 additions & 3 deletions src/components/Media/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import type { UploadcareImage } from '@prezly/uploadcare';
import classNames from 'classnames';
import type { CSSProperties } from 'react';

import { convertSizesToWidths } from './lib';

import './Media.scss';

interface Props {
className?: string;
image: UploadcareImage;
style?: CSSProperties;
title?: string;
sizes?: string;
}

export function Media({ className, image, style, title }: Props) {
export function Media({
className,
image,
style,
title,
sizes = '(max-width: 992px) 800px, (max-width: 576px) 400px, 1200px',
Copy link
Contributor

@e1himself e1himself Dec 27, 2023

Choose a reason for hiding this comment

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

Is the order correct here? Should it not go from the lowest to the highest max-width values?

Suggested change
sizes = '(max-width: 992px) 800px, (max-width: 576px) 400px, 1200px',
sizes = '(max-width: 576px) 400px, (max-width: 992px) 800px, 1200px',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if the order actually matters here, couldn't find anything pointing to that 🤔

Copy link
Contributor

@e1himself e1himself Dec 27, 2023

Choose a reason for hiding this comment

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

I've tested this in JSFiddle, and it seems they have to follow the lowest-to-highest order. Otherwise the smaller breakpoint following a larger one will be ignored.

Compare these two:

}: Props) {
const computedClassName = classNames('prezly-slate-media', className, {
'prezly-slate-media--image': !image.isGif(),
'prezly-slate-media--video': image.isGif(),
Expand Down Expand Up @@ -49,10 +58,13 @@ export function Media({ className, image, style, title }: Props) {
alt={title}
className={computedClassName}
src={image.format().cdnUrl}
srcSet={[image.srcSet(1200), image.srcSet(800), image.srcSet(400)]
srcSet={convertSizesToWidths(sizes)
.map((width) => image.srcSet(width))
.filter(Boolean)
.join(', ')}
sizes={`(max-width: 992px) 800px, (max-width: 576px) 400px, 1200px`}
sizes={sizes}
width={image.originalWidth}
height={image.originalHeight}
style={style}
title={title}
/>
Expand Down
22 changes: 22 additions & 0 deletions src/components/Media/lib/convertSizesToWidths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { convertSizesToWidths } from './convertSizesToWidths';

describe('convertSizesToWidths', () => {
it('Returns empty array when `sizes` does not contain valid size defintions', () => {
expect(convertSizesToWidths('')).toEqual([]);
expect(convertSizesToWidths('test string 123')).toEqual([]);
expect(convertSizesToWidths('test, 123')).toEqual([]);
Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it maybe throw instead? Just to not cover possible human mistakes?

Copy link
Contributor Author

@riffbyte riffbyte Dec 27, 2023

Choose a reason for hiding this comment

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

Yeah, I agree it makes sense to throw 👍🏻

});

it('Returns array of width numbers when `sizes` is a valid size definition', () => {
expect(
convertSizesToWidths('(max-width: 992px) 800px, (max-width: 576px) 400px, 1200px'),
).toEqual([800, 400, 1200]);
expect(
convertSizesToWidths('(max-width: 992px) 800w, (max-width: 576px) 400w, 100vw'),
).toEqual([800, 400]);
});

it('Returns array of width numbers when `sizes` has just one size definition', () => {
expect(convertSizesToWidths('1200px')).toEqual([1200]);
});
});
7 changes: 7 additions & 0 deletions src/components/Media/lib/convertSizesToWidths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const SIZE_DEFINITION_REGEX = /(?:\((?:min|max)-width: \d+px\) ?)?(\d+)(?:px|w)/g;

export function convertSizesToWidths(sizesString: string): number[] {
const matches = Array.from(sizesString.matchAll(SIZE_DEFINITION_REGEX));

return matches.map((match) => parseInt(match[1], 10));
}
1 change: 1 addition & 0 deletions src/components/Media/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { convertSizesToWidths } from './convertSizesToWidths';
25 changes: 22 additions & 3 deletions src/elements/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import './Image.scss';

interface Props extends HTMLAttributes<HTMLElement> {
node: ImageNode;
sizes?: string;
onDownload?: (image: UploadcareImage) => void;
onPreviewOpen?: (image: UploadcareImage) => void;
}
Expand All @@ -37,7 +38,15 @@ const NEW_TAB_ATTRIBUTES: Partial<AnchorHTMLAttributes<HTMLAnchorElement>> = {
rel: 'noopener noreferrer',
};

export function Image({ children, className, node, onDownload, onPreviewOpen, ...props }: Props) {
export function Image({
children,
className,
node,
sizes,
onDownload,
onPreviewOpen,
...props
}: Props) {
const { file, href, align, layout } = node;

const isNewTab = node.new_tab ?? true;
Expand Down Expand Up @@ -75,7 +84,12 @@ export function Image({ children, className, node, onDownload, onPreviewOpen, ..
{...(isNewTab ? NEW_TAB_ATTRIBUTES : {})}
style={containerStyle}
>
<Media className="prezly-slate-image__media" image={image} title={title} />
<Media
className="prezly-slate-image__media"
image={image}
title={title}
sizes={sizes}
/>
</a>
)}

Expand All @@ -87,7 +101,12 @@ export function Image({ children, className, node, onDownload, onPreviewOpen, ..
onClick={handleRolloverClick}
style={containerStyle}
>
<Media className="prezly-slate-image__media" image={image} title={title} />
<Media
className="prezly-slate-image__media"
image={image}
title={title}
sizes={sizes}
/>
</Rollover>
)}

Expand Down