Skip to content

Commit

Permalink
Limit tooltip width in gatsby-remark-shiki-twoslash
Browse files Browse the repository at this point in the history
  • Loading branch information
pveyes committed May 25, 2020
1 parent 5bfa51c commit 19d8e8c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/gatsby-remark-shiki-twoslash/src/dom.ts
Expand Up @@ -28,6 +28,14 @@ const findOrCreateTooltip = () => {
return globalPopover
}

const getRootRect = (element: HTMLElement): DOMRect => {
if (element.nodeName.toLowerCase() === 'pre') {
return element.getBoundingClientRect();
}

return getRootRect(element.parentElement!);
}

/**
* Creates the main mouse over popup for LSP info using the DOM API.
* It is expected to be run inside a `useEffect` block inside your main
Expand Down Expand Up @@ -71,6 +79,11 @@ export const setupTwoslashHovers = () => {
tooltip.style.display = 'block'
tooltip.style.top = `${position.top + yOffset}px`
tooltip.style.left = `${position.left}px`

// limit the width of the tooltip to the outer container (pre)
const rootRect = getRootRect(hovered);
const relativeLeft = position.left - rootRect.x;
tooltip.style.maxWidth = `${rootRect.width - relativeLeft}px`;
}

twoslashes.forEach((codeblock) => {
Expand Down

0 comments on commit 19d8e8c

Please sign in to comment.