Skip to content
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

Describe how to use 2.0.0-alpha1 #62

Open
sontek opened this issue Mar 29, 2017 · 5 comments
Open

Describe how to use 2.0.0-alpha1 #62

sontek opened this issue Mar 29, 2017 · 5 comments

Comments

@sontek
Copy link

sontek commented Mar 29, 2017

I think this is related to:

#50

Basically, I'm using 1.3.0 and its working perfectly:

Dimensions()(FormContent)

but I have the warning:

Wrapper div has no height or width

which I based on github issues 2.0.0 was supposed to solve.

But now I have the problem that pretty much all my styling is 100% messed up and I'm not sure why. 1.3.0 didn't mess with my styling at all and thats what I expect from a HOC.

Can you document how to use this 2.0.0 version without having styles messed up? Right now I see it basically adds this container div with inline styles:

overflow: visible;height: 0px;width: 0px;

but those are really bad styles to be applying to a container div :)

@bagongkia
Copy link

+1

1 similar comment
@steodor
Copy link

steodor commented May 26, 2017

+1

@stephanembl
Copy link

+1

@sontek
Copy link
Author

sontek commented Feb 7, 2018

We wrote our own based on this that doesn't have any issues (and works with Chrome 64 ResizeObserver)

import React, { Children, Component } from 'react';

import { PropTypes } from 'prop-types';
import ResizeObserver from 'resize-observer-polyfill';
import debounce from 'lodash.debounce';
import invariant from 'invariant';
import omit from 'lodash.omit';
import pick from 'lodash.pick';

export default class ContainerDimensions extends Component {

    static propTypes = {
        children: PropTypes.oneOfType([PropTypes.element, PropTypes.func]).isRequired,
        onResize: PropTypes.func
    };

    static defaultProps = {
        onResize: () => {}
    }

    static pickDimensions(object) {
        return pick(object, ["top", "right", "bottom", "left", "width", "height"]);
    }

    constructor(props) {
        super(props);

        this.state = {
            width: 1280
        };
    }

    componentWillMount() {
        this.resizeObserver = new ResizeObserver(debounce(this.handleResize, 10));
    }

    componentWillUnmount() {
        if (this.resizeObserver){
            this.resizeObserver.disconnect();
        }
    }

    handleResize = (entries, observer) => {
        const entry = entries[0];
        if (this.state.width !== entry.contentRect.width) {
            this.setState({
                ...ContainerDimensions.pickDimensions(entry.contentRect)
            }, () => {
                this.props.onResize();
            });
        }
    }

    renderChildren() {
        invariant(this.props.children, 'Expected children to be one of function or React.Element');

        const childrenProps = omit(this.state, ["initiated"]);

        if (typeof this.props.children === 'function') {
            const renderedChildren = this.props.children(childrenProps);
            return renderedChildren && Children.only(renderedChildren);
        }
        return Children.only(React.cloneElement(this.props.children, childrenProps));
    }

    render() {
        return (
            <div ref={(parent) => {
                if (this.resizeObserver) {
                    if (parent) {
                        this.resizeObserver.observe(parent);
                    } else {
                        this.resizeObserver.disconnect(this.parentNode);
                    }
                }
                this.parentNode = parent;
            }}
                className={
                    this.props.getClassName
                        ? this.props.getClassName(ContainerDimensions.pickDimensions(this.state))
                        : this.props.className
                }
            >
                {this.renderChildren()}
            </div>
        );
    }
}

We haven't looked at element-resize-event yet, but are considering it. What we have works good enough for us though.

@xlthor
Copy link

xlthor commented Apr 21, 2018

Setting the dim's of the wrapper to 0px is a real bad idea, even "auto" doesn't work good. I've cloned the source and after changing to

        const wrapperStyle = {
          overflow: 'visible',
          height: '100%',
          width: '100%'
        }

it worked smoothly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants