Skip to content

Commit

Permalink
fix(image): only observe io of element exists
Browse files Browse the repository at this point in the history
  • Loading branch information
BJvdA committed Jun 21, 2021
1 parent 3af4373 commit 3bae06a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
78 changes: 78 additions & 0 deletions example/src/pages/unmounttest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useEffect, useState } from 'react';
import { GetStaticProps } from 'next';
import { Box, Text, Flex } from 'rebass';
import SbEditable from 'storyblok-react';
import { sdk } from '~/lib/graphqlClient';
import { WithStoryProps, useStory, Image } from '@storyofams/storyblok-toolkit';

type GalleryProps = WithStoryProps;

const Gallery = ({ story: providedStory }: GalleryProps) => {
const storyProp = useStory(providedStory);

const [story, setStory] = useState(storyProp);

useEffect(() => {
setStory(null);
}, []);

return (
<Box
sx={{
maxWidth: 1228,
mx: 'auto',
px: 3,
py: 6,
}}
>
<Text mt={5} as="h1">
Gallery
</Text>
{!!story && (
<SbEditable content={story?.content}>
<Flex mx={-2} mt={4} flexWrap="wrap">
{story?.content?.images?.map((image) => (
<Box key={image?.filename} width={['100%', '50%']} px={2} mb={3}>
<Box
sx={{
borderRadius: '8px',
}}
height={['200px', '380px']}
overflow="hidden"
>
<Image
src={image?.filename}
fluid={590}
alt=""
showPlaceholder={false}
/>
</Box>
</Box>
))}
</Flex>
</SbEditable>
)}
</Box>
);
};

export default Gallery;

export const getStaticProps: GetStaticProps = async () => {
let story;
let notFound = false;

try {
story = (await sdk.galleryItem({ slug: 'gallery' })).GalleryItem;
} catch (e) {
notFound = true;
}

return {
props: {
story,
},
notFound,
// revalidate: 60,
};
};
16 changes: 16 additions & 0 deletions src/image/__tests__/createIntersectionObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,20 @@ describe('[image] createIntersectionObserver', () => {

expect(optionsMock).toHaveBeenLastCalledWith({ rootMargin: '1250px' });
});

it('should not observe if target doesnt exist', async () => {
const callbackMock = jest.fn();
const component = <div id="test" />;

act(() => {
render(component);
});

const result = await createIntersectionObserver(
document.querySelector('#notexistant'),
callbackMock,
);

expect(result).toBeNull();
});
});
4 changes: 4 additions & 0 deletions src/image/createIntersectionObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const createIntersectionObserver = async (
},
);

if (!el) {
return null;
}

// Add element to the observer
io.observe(el);

Expand Down

1 comment on commit 3bae06a

@vercel
Copy link

@vercel vercel bot commented on 3bae06a Jun 21, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.