Skip to content

Commit

Permalink
Merge branch 'tonyxiao-master'
Browse files Browse the repository at this point in the history
fix(bug): Resize Detector is now lazily used in order to prevent #6.
  Thanks @tonyxiao

element-resize-detector package looks to be doing some interfacing with the DOM
on import.  This caused issues for users where the DOM was not ready yet.

closes #6
  • Loading branch information
ctrlplusb committed May 21, 2016
2 parents e311f46 + 53f02b2 commit d09bf5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/SizeMe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import React, { Children, Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import invariant from 'invariant';
import { throttle } from 'lodash';
import resizeDetector from './resizeDetector';

// Lazily require to not cause bug
// https://github.com/ctrlplusb/react-sizeme/issues/6
// import resizeDetector from './resizeDetector';
const resizeDetector = () => require(`./resizeDetector`).default;

const defaultConfig = {
monitorWidth: true,
Expand Down Expand Up @@ -148,7 +152,7 @@ function SizeMe(config = defaultConfig) {
this.checkIfSizeChanged = () => undefined;

if (this.domEl) {
resizeDetector.removeAllListeners(this.domEl);
resizeDetector().removeAllListeners(this.domEl);
this.domEl = null;
}
}
Expand All @@ -161,18 +165,18 @@ function SizeMe(config = defaultConfig) {
if (!found) {
// This is for special cases where the element may be null.
if (this.domEl) {
resizeDetector.removeAllListeners(this.domEl);
resizeDetector().removeAllListeners(this.domEl);
this.domEl = null;
}
return;
}

if (this.domEl) {
resizeDetector.removeAllListeners(this.domEl);
resizeDetector().removeAllListeners(this.domEl);
}

this.domEl = found;
resizeDetector.listenTo(this.domEl, this.checkIfSizeChanged);
resizeDetector().listenTo(this.domEl, this.checkIfSizeChanged);
}

refCallback = (element) => {
Expand Down
4 changes: 3 additions & 1 deletion test/SizeMe.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-underscore-dangle,global-require */

import React from 'react';
import { expect } from 'chai';
import { describeWithDOM } from './jsdom';
Expand Down Expand Up @@ -38,7 +40,7 @@ describeWithDOM(`Given the SizeMe library`, () => {
// :: (domeEl, callback) -> void
listenTo: sinon.spy()
};
SizeMeRewireAPI.__Rewire__(`resizeDetector`, resizeDetectorMock);
SizeMeRewireAPI.__Rewire__(`resizeDetector`, () => resizeDetectorMock);
});

describe(`When providing a configuration object`, () => {
Expand Down

0 comments on commit d09bf5e

Please sign in to comment.