Skip to content

Do not render "0" when parent height and width are both 0. #61

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const _debounce = require('lodash.debounce')
const React = require('react')
const onElementResize = require('element-resize-event')
const unbind = require('element-resize-event').unbind

const defaultContainerStyle = {
width: '100%',
height: '100%',
padding: 0,
border: 0
}

function defaultGetDimensions (element) {
return [element.clientWidth, element.clientHeight]
Expand Down Expand Up @@ -73,11 +81,12 @@ function defaultGetDimensions (element) {
* module.exports = Dimensions()(MyComponent) // Enhanced component
*
*/
module.exports = function Dimensions ({
export default function Dimensions ({
getDimensions = defaultGetDimensions,
debounce = 0,
debounceOpts = {},
elementResize = false
elementResize = false,
containerStyle = defaultContainerStyle
} = {}) {
return (ComposedComponent) => {
return class DimensionsHOC extends React.Component {
Expand Down Expand Up @@ -139,9 +148,12 @@ module.exports = function Dimensions ({
}

componentWillUnmount () {
this.getWindow().removeEventListener('resize', this.onResize)
// TODO: remote element-resize-event listener.
// pending https://github.com/KyleAMathews/element-resize-event/issues/2
this._parent = this.refs.wrapper.parentNode
if (elementResize) {
unbind(this._parent)
} else {
this.getWindow().removeEventListener('resize', this.onResize)
}
}

/**
Expand All @@ -161,20 +173,17 @@ module.exports = function Dimensions ({
// only trigger a warning about the wrapper div if we already have a reference to it
console.warn('Wrapper div has no height or width, try overriding style with `containerStyle` option')
}
const wrapperStyle = {
overflow: 'visible',
height: 0,
width: 0
}

return (
<div style={wrapperStyle} ref='wrapper'>
{(containerWidth || containerHeight) &&
<ComposedComponent
<div style={containerStyle} ref='wrapper'>
{(containerWidth || containerHeight)
? <ComposedComponent
{...this.state}
{...this.props}
updateDimensions={this.updateDimensions}
ref='wrappedInstance'
/>
/>
: null
}
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"react-dom": "^0.14.0 || ^15.0.0"
},
"dependencies": {
"element-resize-event": "^2.0.4",
"element-resize-event": "^2.0.8",
"lodash.debounce": "^4.0.6"
}
}