Skip to content

Commit 88a44ac

Browse files
fix(website): clamp info-tip bubbles inside the viewport
The bubble is horizontally centered on its (i) toggle, so a toggle near the viewport edge (e.g. the I/O card's tooltip on the homepage) opened half outside the screen and clipped. max-width bounds the width, not the position. On open — hover, tap or keyboard — the bubble now shifts horizontally to stay 12px inside the viewport, and re-clamps on resize. The shift is measured from the bubble's real rect after layout and removed on close.
1 parent c352d72 commit 88a44ac

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nostream-website": patch
3+
---
4+
5+
Info-tip bubbles no longer clip at the viewport edge: the tooltip is centered on its (i) toggle, so near an edge it overflowed. On open (hover, tap or keyboard) the bubble now shifts horizontally to stay inside the viewport, and re-clamps on resize.

‎website/site/src/components/InfoTip.astro‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,42 @@ const id = `tip-${Astro.props['detail'] ? '' : ''}${Math.random().toString(36).s
124124
})
125125
// Keep aria-expanded truthful for screen readers: the toggle event fires on
126126
// the <details> element (not the summary), including programmatic closes.
127+
// While open, also clamp the bubble inside the viewport: the bubble is
128+
// centered on its (i) toggle, so near a viewport edge it would otherwise
129+
// clip (CSS max-width bounds the width, not the position).
130+
const clamp = (details: Element) => {
131+
const bubble = details.querySelector<HTMLElement>('.tip-bubble')
132+
if (!bubble) return
133+
bubble.style.transform = ''
134+
const pad = 12
135+
const rect = bubble.getBoundingClientRect()
136+
const overshootLeft = pad - rect.left
137+
const overshootRight = rect.right - (window.innerWidth - pad)
138+
if (overshootLeft > 0) {
139+
bubble.style.transform = `translateX(-50%) translateX(${overshootLeft}px)`
140+
} else if (overshootRight > 0) {
141+
bubble.style.transform = `translateX(-50%) translateX(${-overshootRight}px)`
142+
}
143+
}
127144
document.querySelectorAll('details.info-tip').forEach((d) => {
145+
// Hover path: the bubble opens via CSS without a toggle event, so clamp
146+
// when the pointer reaches the toggle (bubble rect exists before the
147+
// opacity transition finishes, so measurement is valid).
148+
d.addEventListener('pointerenter', () => clamp(d))
128149
d.addEventListener('toggle', () => {
129150
d.querySelector('summary')?.setAttribute('aria-expanded', d.hasAttribute('open') ? 'true' : 'false')
151+
if (d.hasAttribute('open')) {
152+
// The bubble lays out on the same frame the attribute flips; measure
153+
// after layout so getBoundingClientRect sees the real rect.
154+
requestAnimationFrame(() => clamp(d))
155+
} else {
156+
const bubble = d.querySelector<HTMLElement>('.tip-bubble')
157+
if (bubble) bubble.style.transform = ''
158+
}
130159
})
131160
})
161+
// Re-clamp when the viewport moves under an open bubble (rotation, resize).
162+
window.addEventListener('resize', () => {
163+
document.querySelectorAll('details.info-tip[open]').forEach(clamp)
164+
}, { passive: true })
132165
</script>

0 commit comments

Comments
 (0)