Skip to content

Style images inside documentation #992

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

Merged
merged 1 commit into from
Sep 23, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/common/imageUrlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export const imageUrlBuilder = unconfiguredImageUrlBuilder()
.dpr(window.devicePixelRatio ?? 1)
.quality(defaultQuality);

export const getAspectRatio = (imageRef: string): number | undefined => {
export const getAspectRatio = (imageRef: string): string | undefined => {
const dimensionsArr = imageRef.match(/\d+x\d+/g);
if (!dimensionsArr) {
return undefined;
}
const dimensions = dimensionsArr.join().split("x");
const [width, height] = dimensions.map((n: string) => Number(n));
return width / height;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This number was being interpreted as a pixel value which is invalid CSS. Returning a string fixes this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd noticed this wasn't working well but hadn't looked again at the code. Thanks.

return (width / height).toString();
};
7 changes: 6 additions & 1 deletion src/documentation/common/DocumentationContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ const serializers = {
.url()}
alt={props.node.alt}
width={300}
sx={{ aspectRatio: getAspectRatio(props.node.asset._ref) }}
borderRadius="lg"
border="solid 1px"
borderColor="gray.300"
sx={{
aspectRatio: getAspectRatio(props.node.asset._ref),
}}
/>
);
},
Expand Down