UNPKG

@atlaskit/navigation-system

Version:

Page layout and navigation components for Atlassian apps.

55 lines • 2.22 kB
import { useRef } from 'react'; import { useLayoutEffect } from '@atlaskit/ds-lib/use-layout-effect'; import { chatPanelWidthVar } from './constants'; /** * Observes ChatPanel's rendered width for the two consumers that cannot read it from CSS: * legacy hoisted slot sizes, and the exit slide, which starts after the grid releases the track. */ export function useChatPanelSize({ panelRef, layout, hoistWidth, rememberSize }) { const lastSize = useRef({ width: '0px', inlineWidth: '0px' }); useLayoutEffect(() => { const panel = panelRef.current; if (!panel || !hoistWidth && !rememberSize) { return; } let publishedWidth; const measure = width => { const pixels = `${width}px`; if (hoistWidth && width !== publishedWidth) { publishedWidth = width; panel.ownerDocument.documentElement.style.setProperty(chatPanelWidthVar, pixels); } if (rememberSize) { // Responsive mode is resolved by CSS, so ask the browser which area it chose. const isInline = layout === 'inline' || layout === 'responsive' && getComputedStyle(panel).gridArea === 'chat-panel'; lastSize.current = { width: pixels, inlineWidth: isInline ? pixels : '0px' }; } }; measure(panel.getBoundingClientRect().width); const observer = new ResizeObserver(([entry]) => { var _entry$borderBoxSize$, _entry$borderBoxSize, _entry$borderBoxSize$2; measure((_entry$borderBoxSize$ = entry === null || entry === void 0 ? void 0 : (_entry$borderBoxSize = entry.borderBoxSize) === null || _entry$borderBoxSize === void 0 ? void 0 : (_entry$borderBoxSize$2 = _entry$borderBoxSize[0]) === null || _entry$borderBoxSize$2 === void 0 ? void 0 : _entry$borderBoxSize$2.inlineSize) !== null && _entry$borderBoxSize$ !== void 0 ? _entry$borderBoxSize$ : panel.getBoundingClientRect().width); }); observer.observe(panel, { box: 'border-box' }); return () => { observer.disconnect(); if (hoistWidth) { panel.ownerDocument.documentElement.style.removeProperty(chatPanelWidthVar); } }; }, [hoistWidth, layout, panelRef, rememberSize]); return lastSize; }