You can usedrift bossJavaScript to detect the position of the tooltip relative to the viewport and adjust its position if it’s close to the edge.
const tooltip = document.querySelector('.tooltip');
const rect = tooltip.getBoundingClientRect();
if (rect.right > window.innerWidth) {
tooltip.style.left = `${window.innerWidth - rect.width}px`;
}
if (rect.bottom > window.innerHeight) {
tooltip.style.top = `${window.innerHeight - rect.height}px`;
}