Skip to content

Commit

Permalink
fix: bug where _inPicture would be set on <img>'s that were children …
Browse files Browse the repository at this point in the history
…of <Picture>
  • Loading branch information
frederickfogerty committed Feb 28, 2022
1 parent 060e353 commit 4d57c1f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/HOFs/propMerger.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export const mergeProps = (src, destination) => {
* @param {React.Element <typeof Component} Component - with defined `props`.
* @returns Component with merged `props`.
*/
export const mergeComponentPropsHOF = (Component) => (props) => {
export const mergeComponentPropsHOF = (Component) => function mergeComponentPropsHOFInner(props) {
const contextProps = useImgixContext();
if (contextProps == null) {
return <Component {...props} />
return <Component {...props} />;
}

const childProps = mergeProps(contextProps, props);
Expand Down
14 changes: 11 additions & 3 deletions src/react-imgix.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,18 @@ class PictureImpl extends Component {
// make sure all of our children have key set, otherwise we get react warnings
let _children =
React.Children.map(children, (child, idx) =>
React.cloneElement(child, {
{
const childIsReactImgix = child.type?.name === 'mergeComponentPropsHOFInner';
return React.cloneElement(child, Object.assign({
key: buildKey(idx),
_inPicture: true,
})
},
// This prevents props._inPicture being set on other children if
// they're passed, such as an <img> component, which will cause a
// React error
childIsReactImgix && {
_inPicture: true,
}));
}
) || [];

/*
Expand Down
10 changes: 10 additions & 0 deletions test/unit/react-imgix.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ describe("When in picture mode", () => {
const onMountArg = onMountedSpy.lastCall.args[0];
expect(onMountArg).toBeInstanceOf(HTMLPictureElement);
});

it('should not pass _inPicture to a <img> element', () => {
const sut = mount(
<Picture >
<img />
</Picture>
);

expect(sut.find('img').props()._inPicture).toBe(undefined)
})
});

describe("When in background mode", () => {
Expand Down

0 comments on commit 4d57c1f

Please sign in to comment.