Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit 7f6a645

Browse files
committed
fix(build): conditionaly require intersection-observer
1 parent 1740f10 commit 7f6a645

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/styled/ShareBlockAside.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import 'intersection-observer';
21
import { css, cx, injectGlobal } from 'emotion';
32
import PropTypes from 'prop-types';
43
import React from 'react';
54

6-
import ShareBlock from '../ShareBlock';
75
import debounce from 'lodash.debounce';
86

7+
import ShareBlock from '../ShareBlock';
8+
9+
if (typeof window !== 'undefined') {
10+
require('intersection-observer');
11+
}
12+
913
injectGlobal`
1014
body {
1115
height: 100%;
@@ -85,10 +89,8 @@ class ShareBlockAside extends React.Component {
8589

8690
componentWillUnmount() {
8791
// unobserve the targets
88-
if (typeof window !== `undefined`) {
89-
for (var value of this.observerTargets) {
90-
this.observer.unobserve(value);
91-
}
92+
for (var value of this.observerTargets) {
93+
this.observer.unobserve(value);
9294
}
9395
}
9496

@@ -108,36 +110,32 @@ class ShareBlockAside extends React.Component {
108110
};
109111

110112
setupObserver = () => {
111-
if (typeof window !== `undefined`) {
112-
const options = { rootMargin: '0px', threshold: [0.5] };
113-
this.observer = new IntersectionObserver(this.handleIntersect, options);
113+
const options = { rootMargin: '0px', threshold: [0.5] };
114+
this.observer = new IntersectionObserver(this.handleIntersect, options);
114115

115-
// add inline buttons to targets
116-
this.observerTargets.push(document.querySelector('.rcs-buttons--inline'));
116+
// add inline buttons to targets
117+
this.observerTargets.push(document.querySelector('.rcs-buttons--inline'));
117118

118-
// add page header to targets, if selector is provided
119-
const { pageHeaderSelector } = this.props;
120-
if (this.props.pageHeaderSelector) {
121-
this.observerTargets.push(document.querySelector(pageHeaderSelector));
122-
}
119+
// add page header to targets, if selector is provided
120+
const { pageHeaderSelector } = this.props;
121+
if (this.props.pageHeaderSelector) {
122+
this.observerTargets.push(document.querySelector(pageHeaderSelector));
123+
}
123124

124-
// observe the targets
125-
for (var value of this.observerTargets) {
126-
this.observer.observe(value);
127-
}
125+
// observe the targets
126+
for (var value of this.observerTargets) {
127+
this.observer.observe(value);
128128
}
129129
};
130130

131131
handleIntersect = (entries, observer) => {
132-
if (typeof window !== `undefined`) {
133-
entries.forEach(entry => {
134-
if (this.state.fixedButtonsTranslation) {
135-
this.setState({
136-
fixedButtonsVisible: entry.intersectionRatio > 0.5 ? false : true,
137-
});
138-
}
139-
});
140-
}
132+
entries.forEach(entry => {
133+
if (this.state.fixedButtonsTranslation) {
134+
this.setState({
135+
fixedButtonsVisible: entry.intersectionRatio > 0.5 ? false : true,
136+
});
137+
}
138+
});
141139
};
142140

143141
render() {

0 commit comments

Comments
 (0)