Skip to content

Commit

Permalink
Ensure getDimensionProps does not return nullable width/height
Browse files Browse the repository at this point in the history
  • Loading branch information
mobeigi committed Oct 25, 2024
1 parent 65239ff commit 84c76f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const MediaBlockVideo = ({ mediaDark, mediaLight, widthOverride, heightOv
return <ThemedVideo dark={darkVideo} light={lightVideo} />;
} else {
// Dark is our default style
return <video src={mediaDarkData.url!} aria-label={mediaDarkData.alt} controls></video>;
return (
<video src={mediaDarkData.url!} aria-label={mediaDarkData.alt} controls style={darkDimensions.style}></video>
);
}
};
31 changes: 17 additions & 14 deletions app/src/payload/blocks/MediaBlock/Component/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Media } from '@/payload-types';
import { GetDimensionPropsResponse } from './utils.types';

/**
* Compute the final dimensions and style.
Expand All @@ -11,32 +12,34 @@ export const getDimensionProps = (
media: Media,
widthOverride: number | null | undefined,
heightOverride: number | null | undefined,
) => {
): GetDimensionPropsResponse => {
// Get the original dimensions of the media
const originalWidth = media.width!;
const originalHeight = media.height!;
const originalWidth = media.width;
const originalHeight = media.height;

let width = widthOverride || originalWidth;
let height = heightOverride || originalHeight;

// If only the width override is provided, calculate the height based on the original aspect ratio
if (widthOverride && !heightOverride) {
height = (widthOverride / originalWidth) * originalHeight; // Preserve aspect ratio
}
// If only the height override is provided, calculate the width based on the original aspect ratio
else if (heightOverride && !widthOverride) {
width = (heightOverride / originalHeight) * originalWidth; // Preserve aspect ratio
if (originalWidth && originalHeight) {
// If only the width override is provided, calculate the height based on the original aspect ratio
if (widthOverride && !heightOverride) {
height = (widthOverride / originalWidth) * originalHeight; // Preserve aspect ratio
}
// If only the height override is provided, calculate the width based on the original aspect ratio
else if (heightOverride && !widthOverride) {
width = (heightOverride / originalHeight) * originalWidth; // Preserve aspect ratio
}
}

// Create a style object that preserves space for lazy-loaded images
const style = {
width: `${width}px`, // Always set the width to avoid layout shifts for lazy-loaded images
height: heightOverride ? `${height}px` : 'auto', // Set height to auto if not overridden to maintain aspect ratio
...(width && { width: `${width}px` }), // Always set the width to avoid layout shifts for lazy-loaded images
...(height && { height: heightOverride ? `${height}px` : 'auto' }), // Set height to auto if not overridden to maintain aspect ratio
};

return {
width,
height,
width: width ?? undefined,
height: height ?? undefined,
style,
};
};
5 changes: 5 additions & 0 deletions app/src/payload/blocks/MediaBlock/Component/utils.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GetDimensionPropsResponse = {
width: number | undefined;
height: number | undefined;
style: React.CSSProperties;
};

0 comments on commit 84c76f3

Please sign in to comment.