Skip to content
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

[Next.js][Image] Exclude width/height when fill prop is provided #1336

Merged
merged 3 commits into from
Feb 13, 2023
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
@@ -1,4 +1,9 @@
import { NextImage, ImageField, withDatasourceCheck } from '@sitecore-jss/sitecore-jss-nextjs';
import {
NextImage,
ImageField,
withDatasourceCheck,
getFieldValue,
} from '@sitecore-jss/sitecore-jss-nextjs';
import StyleguideSpecimen from 'components/styleguide/Styleguide-Specimen';
import { ComponentProps } from 'lib/component-props';
import { StyleguideSpecimenFields } from 'lib/component-props/styleguide';
Expand Down Expand Up @@ -51,12 +56,18 @@ const StyleguideFieldUsageImage = (props: StyleguideFieldUsageImageProps): JSX.E
IMPORTANT: The generated sizes should match your Sitecore server-side allowlist. See /sitecore/config/*.config (search for 'allowedMediaParams')
*/}
<p>Srcset responsive image</p>
<div style={{ position: 'relative', height: 160, width: 300 }}>
<div
style={{
position: 'relative',
height: +getFieldValue(props.fields, 'sample2').height || 160,
width: +getFieldValue(props.fields, 'sample2').width || 300,
}}
>
<NextImage
field={props.fields.sample2}
sizes="(min-width: 960px) 300px, 100px"
fill
priority
fill={true}
priority={true}
/>
</div>
</StyleguideSpecimen>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"incremental": true
"incremental": true,
"forceConsistentCasingInFileNames": false
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
19 changes: 19 additions & 0 deletions packages/sitecore-jss-nextjs/src/components/NextImage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ describe('<NextImage />', () => {
it('should render image with className prop', () => {
expect(rendered.prop('className')).to.eql(props.className);
});

it('should render image without width/height when "fill" prop is provided', () => {
const field = {
value: { src: '/assets/img/test0.png', alt: 'my image', width: 200, height: 400 },
};
const rendered = mount(<NextImage loader={mockLoader} {...props} field={field} fill />).find(
'img'
);

expect(rendered).to.have.length(1);
expect(rendered.prop('src')).to.equal(`${HOSTNAME}${props.field.value.src}?w=${props.width}`);
expect(rendered.prop('sizes')).to.equal('(min-width: 960px) 300px, 100px');
expect(rendered.prop('height')).to.equal(undefined);
expect(rendered.prop('width')).to.equal(undefined);
expect(mockLoader.called).to.be.true;
expect(mockLoader).to.have.been.calledWith(
match({ src: props.field.value.src, width: props.width })
);
});
});

describe('with "value" property value', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/sitecore-jss-nextjs/src/components/NextImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const NextImage: React.FC<NextImageProps> = ({
src: mediaApi.replaceMediaUrlPrefix(attrs.src, mediaUrlPrefix as RegExp),
};

// Exclude `width`, `height` in case image is responsive, `fill` is used
if (imageProps.fill) {
delete imageProps.width;
delete imageProps.height;
}

const loader = (otherProps.loader ? otherProps.loader : sitecoreLoader) as ImageLoader;

if (attrs) {
Expand Down