|
| 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 | +}); |
0 commit comments