Skip to content

Commit c66e95e

Browse files
committed
fix(react): keep sidebar tooltips visible
Signed-off-by: aswanth-07 <a.aswanth0707@gmail.com>
1 parent 38eb678 commit c66e95e

6 files changed

Lines changed: 180 additions & 14 deletions

File tree

‎.changeset/calm-tooltips-float.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@doc-kit/generator-react': patch
3+
---
4+
5+
Render sidebar stability tooltips in a portal so that the table of contents
6+
scroll container does not clip them.

‎e2e/stability-tooltip.spec.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test('renders stability tooltips outside the MetaBar scroll container', async ({
4+
page,
5+
}) => {
6+
await page.goto('/assert.html');
7+
8+
const badge = page.getByLabel('Stability: Legacy').first();
9+
await badge.scrollIntoViewIfNeeded();
10+
await expect(badge.locator('xpath=ancestor::is-land[1]')).toHaveAttribute(
11+
'ready',
12+
''
13+
);
14+
await badge.hover();
15+
16+
const tooltip = page.getByRole('tooltip').filter({ hasText: 'Legacy' });
17+
await expect(tooltip).toBeVisible();
18+
19+
expect(await tooltip.evaluate(element => element.closest('aside dl'))).toBe(
20+
null
21+
);
22+
23+
const box = await tooltip.boundingBox();
24+
const viewport = page.viewportSize();
25+
26+
expect(box.x).toBeGreaterThanOrEqual(0);
27+
expect(box.x + box.width).toBeLessThanOrEqual(viewport.width);
28+
29+
await page.mouse.move(0, 0);
30+
await expect(tooltip).not.toBeVisible();
31+
await badge.focus();
32+
await expect(tooltip).toBeVisible();
33+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import Badge from '@node-core/ui-components/Common/Badge';
2+
import { createPortal } from 'preact/compat';
3+
import { useCallback, useLayoutEffect, useRef, useState } from 'preact/hooks';
4+
5+
import styles from './index.module.css';
6+
import withIsland from '../../islands/withIsland.jsx';
7+
8+
const VIEWPORT_MARGIN = 8;
9+
10+
/**
11+
* Renders a stability badge with a tooltip that is portalled outside the
12+
* MetaBar's scroll container.
13+
* @param {{ label: string, tooltip: string, ariaLabel: string, kind: string }} props
14+
*/
15+
const StabilityBadge = ({ label, tooltip, ariaLabel, kind }) => {
16+
const triggerRef = useRef(null);
17+
const tooltipRef = useRef(null);
18+
const [open, setOpen] = useState(false);
19+
const [position, setPosition] = useState(null);
20+
21+
const updatePosition = useCallback(() => {
22+
const trigger = triggerRef.current;
23+
const content = tooltipRef.current;
24+
25+
if (!trigger || !content) {
26+
return;
27+
}
28+
29+
const triggerBox = trigger.getBoundingClientRect();
30+
const contentBox = content.getBoundingClientRect();
31+
const centeredLeft =
32+
triggerBox.left + triggerBox.width / 2 - contentBox.width / 2;
33+
const left = Math.min(
34+
Math.max(centeredLeft, VIEWPORT_MARGIN),
35+
window.innerWidth - contentBox.width - VIEWPORT_MARGIN
36+
);
37+
const below = triggerBox.bottom + VIEWPORT_MARGIN;
38+
const fitsBelow =
39+
below + contentBox.height <= window.innerHeight - VIEWPORT_MARGIN;
40+
41+
setPosition({
42+
left,
43+
top: fitsBelow
44+
? below
45+
: triggerBox.top - contentBox.height - VIEWPORT_MARGIN,
46+
});
47+
}, []);
48+
49+
useLayoutEffect(() => {
50+
if (!open) {
51+
return undefined;
52+
}
53+
54+
const frame = window.requestAnimationFrame(updatePosition);
55+
window.addEventListener('resize', updatePosition);
56+
window.addEventListener('scroll', updatePosition, true);
57+
58+
return () => {
59+
window.cancelAnimationFrame(frame);
60+
window.removeEventListener('resize', updatePosition);
61+
window.removeEventListener('scroll', updatePosition, true);
62+
};
63+
}, [open, updatePosition]);
64+
65+
const closeTooltip = () => {
66+
setOpen(false);
67+
setPosition(null);
68+
};
69+
70+
return (
71+
<>
72+
<span
73+
ref={triggerRef}
74+
className={styles.tooltipTrigger}
75+
aria-label={ariaLabel}
76+
tabIndex={0}
77+
onMouseEnter={() => setOpen(true)}
78+
onMouseLeave={closeTooltip}
79+
onFocus={() => setOpen(true)}
80+
onBlur={closeTooltip}
81+
>
82+
<Badge size="small" kind={kind}>
83+
{label}
84+
</Badge>
85+
</span>
86+
87+
{open &&
88+
createPortal(
89+
<span
90+
ref={tooltipRef}
91+
role="tooltip"
92+
className={styles.tooltipContent}
93+
style={position ?? { visibility: 'hidden' }}
94+
>
95+
{tooltip}
96+
</span>,
97+
document.body
98+
)}
99+
</>
100+
);
101+
};
102+
103+
export default withIsland(StabilityBadge, {
104+
name: 'StabilityBadge',
105+
on: { idle: true },
106+
});

‎packages/react/src/html/ui/components/MetaBar/index.jsx‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CodeBracketIcon, DocumentIcon } from '@heroicons/react/24/outline';
2-
import Badge from '@node-core/ui-components/Common/Badge';
32
import MetaBar from '@node-core/ui-components/Containers/MetaBar';
43
import GitHubIcon from '@node-core/ui-components/Icons/Social/GitHub';
54

65
import styles from './index.module.css';
6+
import StabilityBadge from './StabilityBadge.jsx';
77

88
import { editURL } from '#theme/config';
99

@@ -25,24 +25,19 @@ const HeadingValue = ({ value, stability }) => {
2525
return value;
2626
}
2727

28-
const ariaLabel = STABILITY_TOOLTIPS[stability]
29-
? `Stability: ${STABILITY_TOOLTIPS[stability]}`
30-
: undefined;
28+
const tooltip = STABILITY_TOOLTIPS[stability];
29+
const ariaLabel = tooltip ? `Stability: ${tooltip}` : undefined;
3130

3231
return (
3332
<>
3433
{value}
3534

36-
<Badge
37-
size="small"
38-
className={styles.badge}
35+
<StabilityBadge
3936
kind={STABILITY_KINDS[stability]}
40-
data-tooltip={STABILITY_TOOLTIPS[stability]}
41-
aria-label={ariaLabel}
42-
tabIndex={0}
43-
>
44-
{STABILITY_LABELS[stability]}
45-
</Badge>
37+
label={STABILITY_LABELS[stability]}
38+
tooltip={tooltip}
39+
ariaLabel={ariaLabel}
40+
/>
4641
</>
4742
);
4843
};

‎packages/react/src/html/ui/components/MetaBar/index.module.css‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,32 @@
55
margin-right: 0.25rem;
66
}
77

8-
.badge {
8+
.tooltipTrigger {
99
display: inline-block;
1010
margin-left: 0.25rem;
1111
}
12+
13+
.tooltipContent {
14+
position: fixed;
15+
z-index: 50;
16+
width: max-content;
17+
max-width: 16rem;
18+
padding: 0.375rem 0.625rem;
19+
color: var(--color-neutral-900);
20+
font-size: 0.875rem;
21+
font-weight: 500;
22+
line-height: 1.25rem;
23+
pointer-events: none;
24+
background-color: var(--color-white);
25+
border: 1px solid var(--color-neutral-200);
26+
border-radius: 0.375rem;
27+
box-shadow:
28+
0 4px 6px -2px rgb(16 24 40 / 3%),
29+
0 12px 16px -4px rgb(16 24 40 / 8%);
30+
}
31+
32+
:global([data-theme='dark']) .tooltipContent {
33+
color: var(--color-white);
34+
background-color: var(--color-neutral-950);
35+
border-color: var(--color-neutral-900);
36+
}

‎packages/react/src/html/ui/islands/loaders.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export default {
88
ThemeToggle: () => import('../components/ThemeToggle.jsx'),
99
SearchBox: () => import('../components/SearchBox'),
1010
SideBar: () => import('#theme/Sidebar'),
11+
StabilityBadge: () => import('../components/MetaBar/StabilityBadge.jsx'),
1112
};

0 commit comments

Comments
 (0)