-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Remove direct references to window/document objects #10328
Changes from 5 commits
3c72147
368b65d
5bfbb8e
68b3745
5cd6ef0
9cfc8d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
import EventListener from 'react-event-listener'; | ||
import ownerWindow from 'dom-helpers/ownerWindow'; | ||
import debounce from 'lodash/debounce'; | ||
import wrapDisplayName from 'recompose/wrapDisplayName'; | ||
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
|
@@ -35,15 +37,19 @@ const withWidth = (options = {}) => Component => { | |
}; | ||
|
||
componentDidMount() { | ||
this.updateWidth(window.innerWidth); | ||
const win = ownerWindow(ReactDOM.findDOMNode(this.mountNode)); | ||
this.updateWidth(win.innerWidth); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.handleResize.cancel(); | ||
} | ||
|
||
mountNode = null; | ||
|
||
handleResize = debounce(() => { | ||
this.updateWidth(window.innerWidth); | ||
const win = ownerWindow(ReactDOM.findDOMNode(this.mountNode)); | ||
this.updateWidth(win.innerWidth); | ||
}, resizeInterval); | ||
|
||
updateWidth(innerWidth) { | ||
|
@@ -102,7 +108,13 @@ const withWidth = (options = {}) => Component => { | |
} | ||
|
||
return ( | ||
<EventListener target="window" onResize={this.handleResize}> | ||
<EventListener | ||
target="window" | ||
onResize={this.handleResize} | ||
ref={node => { | ||
this.mountNode = node; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm second guessing this. Maybe i'm just tired. Let me double check that this works as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}} | ||
> | ||
<Component {...more} {...props} /> | ||
</EventListener> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to work better. I'm still not 100% confident in its function.
findDOMNode(this.mountNode)
always returnsnull
fromcomponentDidMount
when awidth
orinitialWidth
prop isn't provided (such as in the docs), since we rendernull
when width isn't defined yet in props or state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. The logic is broken. I'm surprised the CI is green.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can leave the withWidth file unchanged so we can merge the other changes right away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do. When findDOMNode returns
null
theownerWindow
function just defaults to returning the global window object.