// Everyone at this element; the first user's color draws the frame.
users: readonly UserProfile[]
children: ReactNode
align?: PresenceFrameContainerProps['align']
}
// One visible element gets a frame matching its shape; other content gets one shared frame.
export function PresenceFrame({ users, align, children, className, ...rest }: PresenceFrameProps) {
return (
{/* Keep the overlay first so showing it never changes the content's spacing. */}
{children}
)
}
const FRAME_GAP = 4
const FRAME_RADIUS = 6
const CORNERS = [
'borderTopLeftRadius',
'borderTopRightRadius',
'borderBottomRightRadius',
'borderBottomLeftRadius'
] as const
type FrameOutlineProps = {
names: string
color: UserColor | undefined
align?: PresenceFrameContainerProps['align']
}
function FrameOutline({ names, color, align = 'end' }: FrameOutlineProps) {
const node = useRef(null)
// Refit after rendering and whenever the content resizes.
useLayoutEffect(() => {
const outline = node.current
const frame = outline?.parentElement
if (!names || !outline || !frame) return
const fit = () => {
// Ignore the overlay, hidden elements, and helpers outside normal flow.
const inside = [...frame.children].filter(child => {
if (child === outline) return false
const { display, position } = getComputedStyle(child)
return display !== 'none' && position !== 'absolute' && position !== 'fixed'
})
const bareText = [...frame.childNodes].some(
child => child.nodeType === Node.TEXT_NODE && child.textContent?.trim()
)
const only = inside.length === 1 && !bareText ? inside[0] : null
const subject = only instanceof HTMLElement ? only : null
const target = subject ?? frame
const bounds = target.getBoundingClientRect()
const origin = frame.getBoundingClientRect()
const box = outline.style
// Rectangles preserve fractions; absolute positions start inside the wrapper's border.
box.left = `${bounds.left - origin.left - frame.clientLeft + frame.scrollLeft - FRAME_GAP}px`
box.top = `${bounds.top - origin.top - frame.clientTop + frame.scrollTop - FRAME_GAP}px`
box.width = `${bounds.width + FRAME_GAP * 2}px`
box.height = `${bounds.height + FRAME_GAP * 2}px`
box.setProperty('--presence-target-width', `${bounds.width}px`)
// Expand one element's corners by the gap; otherwise use the default radius.
const shape = subject ? getComputedStyle(subject) : null
for (const corner of CORNERS) {
box[corner] = shape ? `calc(${shape[corner]} + ${FRAME_GAP}px)` : `${FRAME_RADIUS}px`
}
}
fit()
const observer = new ResizeObserver(fit)
observer.observe(frame)
for (const child of frame.children) if (child !== outline) observer.observe(child)
return () => observer.disconnect()
})
return (
{names}
)
}