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

fix: getOuterSizes returning NaN for virtual elements #711

Merged
merged 1 commit into from Nov 15, 2018

Conversation

RatoX
Copy link

@RatoX RatoX commented Nov 13, 2018

When the element is not attach at the DOM the getComputedStyle returns
a empty string making the convertion to float parseFloat(styles.marginTop)
return NaN.

  • Add tests to cover the bug
  • yarn test

When the element is not attach at the DOM the `getComputedStyle` returns
a empty string making the convertion to float `parseFloat(styles.marginTop)`
return NaN.
@FezVrasta
Copy link
Member

Thanks for the PR!
May you please explain me what's the advantage of this approach? Do we care to return some number value in cases like this?

@RatoX
Copy link
Author

RatoX commented Nov 13, 2018

There is no advantage to using this approach, we just need to return a valid number without it we end up with translate positions like that: translate3d(NaNpx, NaNpx, 0)

@RatoX
Copy link
Author

RatoX commented Nov 13, 2018

Let me show you what's going on:

export default function getOuterSizes(element) {
  const window = element.ownerDocument.defaultView;

  // When the element is no attach to the DOM, the getComputedStyle returns empty for all properties 
  const styles = window.getComputedStyle(element); 

 // parseFloat do not convert empty strings in zero it will return a NaN 
  const x = parseFloat(styles.marginTop) + parseFloat(styles.marginBottom);
  const y = parseFloat(styles.marginLeft) + parseFloat(styles.marginRight);

// Any operation with NaN always return NaN
  const result = {
    width: element.offsetWidth + y,
    height: element.offsetHeight + x,
   };

// So this function will return a object like that:
// {
//    width: NaN,
//    height: NaN
// }
   return result;
 }

This is making the rest of the lib broke on non-attached elements. :/

Some references about this bug:

By the way, nice lib!!! I was using this lib a long time ago, and I'm glad to be able to contribute to something.

@FezVrasta
Copy link
Member

Thanks for the reply.

I'd like to understand if, returning 0 rather than NaN, do you expect the library to work properly? I guess you are just going to have a popper with a wrong position, am I correct?

@RatoX
Copy link
Author

RatoX commented Nov 14, 2018

returning 0 rather than NaN, do you expect the library to work properly?

Yes, I do. I already check it, since the element is not on the DOM yet there is no margin to calc the outerSize, so this function will calc only with the offsetWidth and offsetHeight.

@FezVrasta FezVrasta merged commit 2d4400b into floating-ui:master Nov 15, 2018
@RatoX
Copy link
Author

RatoX commented Nov 16, 2018

<3

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

Successfully merging this pull request may close these issues.

None yet

2 participants