import {
closestComposedNode,
getMediaController,
getStringAttr,
isElementVisible,
setStringAttr
} from "./utils/element-utils.js";
import { globalThis, document } from "./utils/server-safe-globals.js";
const Attributes = {
PLACEMENT: "placement",
BOUNDS: "bounds"
};
const template = document.createElement("template");
template.innerHTML = /*html*/
`
`;
class MediaTooltip extends globalThis.HTMLElement {
constructor() {
super();
// Adjusts tooltip position relative to the closest specified container
// such that it doesn't spill out of the left or right sides. Only applies
// to 'top' and 'bottom' placed tooltips.
this.updateXOffset = () => {
var _a;
if (!isElementVisible(this, { checkOpacity: false, checkVisibilityCSS: false }))
return;
const placement = this.placement;
if (placement === "left" || placement === "right") {
this.style.removeProperty("--media-tooltip-offset-x");
return;
}
const tooltipStyle = getComputedStyle(this);
const containingEl = (_a = closestComposedNode(this, "#" + this.bounds)) != null ? _a : getMediaController(this);
if (!containingEl)
return;
const { x: containerX, width: containerWidth } = containingEl.getBoundingClientRect();
const { x: tooltipX, width: tooltipWidth } = this.getBoundingClientRect();
const tooltipRight = tooltipX + tooltipWidth;
const containerRight = containerX + containerWidth;
const offsetXVal = tooltipStyle.getPropertyValue(
"--media-tooltip-offset-x"
);
const currOffsetX = offsetXVal ? parseFloat(offsetXVal.replace("px", "")) : 0;
const marginVal = tooltipStyle.getPropertyValue(
"--media-tooltip-container-margin"
);
const currMargin = marginVal ? parseFloat(marginVal.replace("px", "")) : 0;
const leftDiff = tooltipX - containerX + currOffsetX - currMargin;
const rightDiff = tooltipRight - containerRight + currOffsetX + currMargin;
if (leftDiff < 0) {
this.style.setProperty("--media-tooltip-offset-x", `${leftDiff}px`);
return;
}
if (rightDiff > 0) {
this.style.setProperty("--media-tooltip-offset-x", `${rightDiff}px`);
return;
}
this.style.removeProperty("--media-tooltip-offset-x");
};
if (!this.shadowRoot) {
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
this.arrowEl = this.shadowRoot.querySelector("#arrow");
if (Object.prototype.hasOwnProperty.call(this, "placement")) {
const placement = this.placement;
delete this.placement;
this.placement = placement;
}
}
static get observedAttributes() {
return [Attributes.PLACEMENT, Attributes.BOUNDS];
}
/**
* Get or set tooltip placement
*/
get placement() {
return getStringAttr(this, Attributes.PLACEMENT);
}
set placement(value) {
setStringAttr(this, Attributes.PLACEMENT, value);
}
/**
* Get or set tooltip container ID selector that will constrain the tooltips
* horizontal position.
*/
get bounds() {
return getStringAttr(this, Attributes.BOUNDS);
}
set bounds(value) {
setStringAttr(this, Attributes.BOUNDS, value);
}
}
if (!globalThis.customElements.get("media-tooltip")) {
globalThis.customElements.define("media-tooltip", MediaTooltip);
}
var media_tooltip_default = MediaTooltip;
export {
Attributes,
media_tooltip_default as default
};