Skip to content

Commit

Permalink
fix: tooltip overflow causing scrollbar (#1018)
Browse files Browse the repository at this point in the history
fix #1017
  • Loading branch information
luwes authored Nov 5, 2024
1 parent 0b84475 commit 181aec2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/js/media-tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
closestComposedNode,
getMediaController,
getStringAttr,
isElementVisible,
setStringAttr,
} from './utils/element-utils.js';
import { globalThis, document } from './utils/server-safe-globals.js';
Expand Down Expand Up @@ -190,6 +191,10 @@ class MediaTooltip extends globalThis.HTMLElement {
// such that it doesn't spill out of the left or right sides. Only applies
// to 'top' and 'bottom' placed tooltips.
updateXOffset = () => {
// If the tooltip is hidden don't offset the tooltip because it could be
// positioned offscreen causing scrollbars to appear.
if (!isElementVisible(this, { checkOpacity: false, checkVisibilityCSS: false })) return;

const placement = this.placement;

// we don't offset against tooltips coming out of left and right sides
Expand Down
10 changes: 5 additions & 5 deletions src/js/utils/element-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,25 @@ export function getDocumentOrShadowRoot(
*/
export function isElementVisible(
element: HTMLElement,
depth: number = 3
{ depth = 3, checkOpacity = true, checkVisibilityCSS = true } = {}
): boolean {
// Supported by Chrome and Firefox https://caniuse.com/mdn-api_element_checkvisibility
// https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
// @ts-ignore
if (element.checkVisibility) {
// @ts-ignore
return element.checkVisibility({
checkOpacity: true,
checkVisibilityCSS: true,
checkOpacity,
checkVisibilityCSS,
});
}
// Check if the element or its ancestors are hidden.
let el = element;
while (el && depth > 0) {
const style = getComputedStyle(el);
if (
style.opacity === '0' ||
style.visibility === 'hidden' ||
(checkOpacity && style.opacity === '0') ||
(checkVisibilityCSS && style.visibility === 'hidden') ||
style.display === 'none'
) {
return false;
Expand Down

0 comments on commit 181aec2

Please sign in to comment.