Created
September 27, 2019 14:23
-
-
Save vincentnavetat/cc803d96f6f7c27e8801f7e848faea24 to your computer and use it in GitHub Desktop.
Tooltip web component JS - responsive positioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleDropdownPosition() { | |
const screenPadding = 16; | |
const placeholderRect = this.placeholder.getBoundingClientRect(); | |
const dropdownRect = this.dropdown.getBoundingClientRect(); | |
const dropdownRightX = dropdownRect.x + dropdownRect.width; | |
const placeholderRightX = placeholderRect.x + placeholderRect.width; | |
if (dropdownRect.x < 0) { | |
this.dropdown.style.left = '0'; | |
this.dropdown.style.right = 'auto'; | |
this.dropdown.style.transform = `translateX(${-placeholderRect.x + screenPadding}px)`; | |
} else if (dropdownRightX > window.outerWidth) { | |
this.dropdown.style.left = 'auto'; | |
this.dropdown.style.right = '0'; | |
this.dropdown.style.transform = `translateX(${(window.outerWidth - placeholderRightX) - screenPadding}px)`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment