tl;dr
- some javascriptsSome JavaScript libraries relies on window innerWidth | Height
innerWidth
andinnerHeight
to do their calculations;calculations - window.innerWidth it
window.innerWidth
is not 100% reliable, which may lead to bugs;bugs - matchMedia().matches
matchMedia().matches
is 100% accurate but it returns a boolean value;value - come around: use matchMedia
matchMedia
to verify window.innerWidthwindow.innerWidth
value; if it returns false use matchMediamatchMedia
to do a binarySearchbinary search until it finds the correct value;value
I faced that issue with some libs that relies on window.innerWidthwindow.innerWidth
. In cases where I had some media queries the return value of window.innerWidthwindow.innerWidth
used by the library would be off and that library would have issues with incorrect value.
I've seen thoughthat matchMedia().matchesmatchMedia().matches
always return the correct value, but it returns a boolean value, not the width value. I've not seen a solution so far for that (maybe some of you know a better solution?), so I came up with a solution using matchMediamatchMedia
.
I created a function getCorrectDimensiongetCorrectDimension
that performs a binarySearchbinary search around the window innerWidthinnerWidth
(or HeightinnerHeight
) to find the correct value if its value is wrong as you can see below: