Skip to content

Instantly share code, notes, and snippets.

@vincentnavetat
Created September 27, 2019 14:23
Show Gist options
  • Save vincentnavetat/cc803d96f6f7c27e8801f7e848faea24 to your computer and use it in GitHub Desktop.
Save vincentnavetat/cc803d96f6f7c27e8801f7e848faea24 to your computer and use it in GitHub Desktop.
Tooltip web component JS - responsive positioning
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