Skip to content

Commit 9168643

Browse files
authored
Merge pull request #3608 from ProjectMirador/int-obs
Replace @researchgate/react-intersection-observer with react-intersection-observer
2 parents 2bfd6a1 + e4ad31e commit 9168643

5 files changed

Lines changed: 18 additions & 21 deletions

File tree

__tests__/src/components/GalleryViewThumbnail.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { shallow } from 'enzyme';
33
import { Utils } from 'manifesto.js';
44
import Chip from '@material-ui/core/Chip';
5-
import IntersectionObserver from '@researchgate/react-intersection-observer';
5+
import { InView } from 'react-intersection-observer';
66
import manifestJson from '../../fixtures/version-2/019.json';
77
import { GalleryViewThumbnail } from '../../../src/components/GalleryViewThumbnail';
88
import IIIFThumbnail from '../../../src/containers/IIIFThumbnail';
@@ -78,7 +78,7 @@ describe('GalleryView', () => {
7878
};
7979
wrapper = createWrapper({ annotationsCount: 0, canvas, requestCanvasAnnotations });
8080

81-
wrapper.find(IntersectionObserver).simulate('change', { isIntersecting: true });
81+
wrapper.find(InView).simulate('change', { isIntersecting: true });
8282
expect(requestCanvasAnnotations).toHaveBeenCalled();
8383
});
8484
it('does nothing if there is no intersection', () => {
@@ -89,7 +89,7 @@ describe('GalleryView', () => {
8989
};
9090
wrapper = createWrapper({ canvas, requestCanvasAnnotations });
9191

92-
wrapper.find(IntersectionObserver).simulate('change', { isIntersecting: false });
92+
wrapper.find(InView).simulate('change', { isIntersecting: false });
9393
expect(requestCanvasAnnotations).not.toHaveBeenCalled();
9494
});
9595
it('does nothing if there are already some annotations', () => {
@@ -100,7 +100,7 @@ describe('GalleryView', () => {
100100
};
101101
wrapper = createWrapper({ annotationsCount: 5, canvas, requestCanvasAnnotations });
102102

103-
wrapper.find(IntersectionObserver).simulate('change', { isIntersecting: true });
103+
wrapper.find(InView).simulate('change', { isIntersecting: true });
104104
expect(requestCanvasAnnotations).not.toHaveBeenCalled();
105105
});
106106
});

__tests__/src/components/IIIFThumbnail.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { shallow } from 'enzyme';
3-
import IntersectionObserver from '@researchgate/react-intersection-observer';
3+
import { InView } from 'react-intersection-observer';
44
import Typography from '@material-ui/core/Typography';
55
import { IIIFThumbnail } from '../../../src/components/IIIFThumbnail';
66

@@ -26,9 +26,9 @@ describe('IIIFThumbnail', () => {
2626
it('renders properly', () => {
2727
expect(wrapper.matchesElement(
2828
<div>
29-
<IntersectionObserver onChange={wrapper.instance().handleIntersection}>
29+
<InView onChange={wrapper.instance().handleIntersection}>
3030
<img alt="" />
31-
</IntersectionObserver>
31+
</InView>
3232
</div>,
3333
)).toBe(true);
3434
});
@@ -37,9 +37,9 @@ describe('IIIFThumbnail', () => {
3737
wrapper = createWrapper({});
3838
expect(wrapper.matchesElement(
3939
<div>
40-
<IntersectionObserver onChange={wrapper.instance().handleIntersection}>
40+
<InView onChange={wrapper.instance().handleIntersection}>
4141
<img alt="" />
42-
</IntersectionObserver>
42+
</InView>
4343
</div>,
4444
)).toBe(true);
4545
expect(wrapper.find('img').props().src).toMatch(/data:image\/png;base64/);

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@
3737
"@material-ui/core": "^4.12.3",
3838
"@material-ui/icons": "^4.9.1",
3939
"@material-ui/lab": "^4.0.0-alpha.53",
40-
"@researchgate/react-intersection-observer": "^1.0.0",
4140
"classnames": "^2.2.6",
4241
"clsx": "^1.0.4",
4342
"deepmerge": "^4.2.2",
4443
"dompurify": "^2.0.11",
4544
"i18next": "^19.5.0",
4645
"icomcom-react": "^1.0.1",
47-
"intersection-observer": "^0.10.0",
4846
"isomorphic-unfetch": "^3.0.0",
4947
"jss": "^10.3.0",
5048
"jss-rtl": "^0.3.0",
@@ -63,6 +61,7 @@
6361
"react-full-screen": "^1.1.1",
6462
"react-i18next": "^11.7.0",
6563
"react-image": "^4.0.1",
64+
"react-intersection-observer": "^9.0.0",
6665
"react-mosaic-component": "^4.0.1",
6766
"react-redux": "^7.1.0 || ^8.0.0",
6867
"react-resize-observer": "^1.1.1",

src/components/GalleryViewThumbnail.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import Chip from '@material-ui/core/Chip';
55
import AnnotationIcon from '@material-ui/icons/CommentSharp';
66
import SearchIcon from '@material-ui/icons/SearchSharp';
77
import classNames from 'classnames';
8-
import 'intersection-observer'; // polyfill needed for Safari
9-
import IntersectionObserver from '@researchgate/react-intersection-observer';
8+
import { InView } from 'react-intersection-observer';
109
import MiradorCanvas from '../lib/MiradorCanvas';
1110
import IIIFThumbnail from '../containers/IIIFThumbnail';
1211

@@ -109,7 +108,7 @@ export class GalleryViewThumbnail extends Component {
109108
const miradorCanvas = new MiradorCanvas(canvas);
110109

111110
return (
112-
<IntersectionObserver onChange={this.handleIntersection}>
111+
<InView onChange={this.handleIntersection}>
113112
<div
114113
key={canvas.index}
115114
className={
@@ -168,7 +167,7 @@ export class GalleryViewThumbnail extends Component {
168167
</div>
169168
</IIIFThumbnail>
170169
</div>
171-
</IntersectionObserver>
170+
</InView>
172171
);
173172
}
174173
}

src/components/IIIFThumbnail.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import 'intersection-observer'; // polyfill needed for Safari
43
import Typography from '@material-ui/core/Typography';
5-
import IntersectionObserver from '@researchgate/react-intersection-observer';
4+
import { InView } from 'react-intersection-observer';
65
import classNames from 'classnames';
76
import getThumbnail from '../lib/ThumbnailFactory';
87

@@ -48,10 +47,10 @@ export class IIIFThumbnail extends Component {
4847
* Handles the intersection (visibility) of a given thumbnail, by requesting
4948
* the image and then updating the state.
5049
*/
51-
handleIntersection(event) {
50+
handleIntersection(inView, _entry) {
5251
const { loaded } = this.state;
5352

54-
if (loaded || !event.isIntersecting) return;
53+
if (loaded || !inView) return;
5554

5655
this.setState(state => ({ ...state, loaded: true }));
5756
}
@@ -152,15 +151,15 @@ export class IIIFThumbnail extends Component {
152151

153152
return (
154153
<div className={classNames(classes.root, { [classes[`${variant}Root`]]: variant })}>
155-
<IntersectionObserver onChange={this.handleIntersection}>
154+
<InView as="span" onChange={this.handleIntersection}>
156155
<img
157156
alt=""
158157
role="presentation"
159158
src={src}
160159
style={this.imageStyles()}
161160
className={classes.image}
162161
/>
163-
</IntersectionObserver>
162+
</InView>
164163
{ labelled && (
165164
<div className={classNames(classes.label, { [classes[`${variant}Label`]]: variant })}>
166165
<Typography variant="caption" classes={{ root: classNames(classes.caption, { [classes[`${variant}Caption`]]: variant }) }}>

0 commit comments

Comments
 (0)