Skip to content

Commit b4b8c4e

Browse files
committed
validate scrollContainer prop for server side
1 parent 919c88b commit b4b8c4e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/components/ParallaxProvider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import ParallaxContext from '../helpers/ParallaxContext';
44
import ParallaxController from '../classes/ParallaxController';
55
import { VERTICAL, HORIZONTAL } from '../constants';
6+
import validHTMLElement from '../utils/validHTMLElement';
67

78
const createController = options => {
89
// Don't initialize on the server
@@ -23,7 +24,7 @@ export default class ParallaxProvider extends Component {
2324
static propTypes = {
2425
children: PropTypes.node.isRequired,
2526
scrollAxis: PropTypes.oneOf([VERTICAL, HORIZONTAL]),
26-
scrollContainer: PropTypes.instanceOf(Element),
27+
scrollContainer: validHTMLElement,
2728
};
2829

2930
constructor(props) {

src/utils/validHTMLElement.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default function validHTMLElement(
2+
props,
3+
propName,
4+
componentName = 'ANONYMOUS'
5+
) {
6+
if (typeof window === 'undefined') {
7+
return null;
8+
}
9+
10+
if (props[propName]) {
11+
const value = props[propName];
12+
const isValid = value instanceof window.Element;
13+
14+
if (!isValid) {
15+
return new Error(
16+
`Prop name "${propName}" in <${componentName}> must be an HTML DOM element.`
17+
);
18+
}
19+
}
20+
21+
return null;
22+
}

0 commit comments

Comments
 (0)