Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { PlaceholderProps } from "./placeholder"
import { MainImageProps } from "./main-image"
import { Layout } from "../image-utils"
import { getSizer } from "./layout-wrapper"

// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface GatsbyImageProps
Expand Down Expand Up @@ -194,6 +195,8 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
props,
])

const sizer = getSizer(layout, width, height)

return (
<Type
{...wrapperProps}
Expand All @@ -204,7 +207,9 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
}}
className={`${wClass}${className ? ` ${className}` : ``}`}
ref={root}
dangerouslySetInnerHTML={{ __html: `` }}
dangerouslySetInnerHTML={{
__html: sizer,
}}
suppressHydrationWarning
/>
)
Expand Down
18 changes: 17 additions & 1 deletion packages/gatsby-plugin-image/src/components/layout-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ if (hasNativeLazyLoadSupport) {
/>
)

export function getSizer(
layout: Layout,
width: number,
height: number
): string {
let sizer: string | null = null
if (layout === `fluid`) {
sizer = `<div aria-hidden="true" style="padding-top: ${
(height / width) * 100
}%;"></div>`
}
if (layout === `constrained`) {
sizer = `<div style="max-width: ${width}px; display: block;"><img alt="" role="presentation" aria-hidden="true" src="data:image/svg+xml;charset=utf-8,%3Csvg height='${height}' width='${width}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="max-width: 100%; display: block; position: static;"></div>`
}
return sizer
}

export const LayoutWrapper: FunctionComponent<ILayoutWrapperProps> = function LayoutWrapper({
layout,
width,
Expand Down Expand Up @@ -68,7 +85,6 @@ export const LayoutWrapper: FunctionComponent<ILayoutWrapperProps> = function La
</div>
)
}

return (
<Fragment>
{sizer}
Expand Down