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

how to measure before animating with Transition ? #20

Closed
cusxio opened this issue Jun 16, 2017 · 2 comments
Closed

how to measure before animating with Transition ? #20

cusxio opened this issue Jun 16, 2017 · 2 comments

Comments

@cusxio
Copy link

cusxio commented Jun 16, 2017

Hello thank you for this library, been using it to create some components, working well so far.

Problem Description

I'm trying to use Transition to create a drop down menu.

To position and animate the drop down menu properly, I need to measure the width and height of the element.

How can I do this with in react-move ?

class Menu extends Component {
  constructor() {
    super();
    this.getMenuList = this.getMenuList.bind(this);
    this.getInnerDimensions = this.getInnerDimensions.bind(this);
    this.state = {
      items: [],
    };
  }

  componentWillReceiveProps(nextProps) {
    if (!this.props.open && nextProps.open) {
      this.setState({ items: [{}] });
    }
  }

  getMenuList(c) {
    this.menuList = c;
  }

  getInnerDimensions() { // where should I call getInnerDimensions? because the component is not even mounted.
    this.dimensions = {
      width: this.menuList.offsetWidth,
      height: this.menuList.offsetHeight,
    };
  }

  render() {
    const { open, children } = this.props;
    const { items } = this.state;

    return (
      <Portal open={open}>
        <Transition
          data={items}
          getKey={(_, i) => i}
          update={() => ({
            opacity: 1,
            scale: 1,
          })}
          enter={() => ({
            opacity: 0,
            scale: 0,
          })}
          leave={() => ({
            opacity: 0,
            scale: 0,
          })}
          duration={200}
          easing={bezier(0, 0, 0.2, 1)}
        >
          {interpolatedStyles => {
            if (interpolatedStyles.length === 0) {
              return null;
            }

            const { state: { opacity } } = interpolatedStyles[0];
            return (
              <MenuListWrapper>
                <MenuList
                  innerRef={this.getMenuList}
                  style={{
                    opacity,
                  }}
                >
                  {children}
                </MenuList>
              </MenuListWrapper>
            );
          }}
        </Transition>
      </Portal>
    );
  }
}
@cusxio cusxio changed the title How do I get the height/width of an element before animating in Transition ? how to measure before animating with Transition ? Jun 20, 2017
@chasedeanda
Copy link

Were you able to find a workaround for this?

@tannerlinsley
Copy link
Contributor

You would likely need to put it in a type of wrapper component that is invisible or overflow hidden, measure the dimensions of the element, then apply the transition.

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

3 participants