@atlaskit/navigation-system
Version:
Page layout and navigation components for Atlassian apps.
286 lines (284 loc) • 15.3 kB
JavaScript
/* chat-panel.tsx generated by @compiled/babel-plugin v3.0.4 */
import _extends from "@babel/runtime/helpers/extends";
import "./chat-panel.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { bind } from 'bind-event-listener';
import mergeRefs from '@atlaskit/ds-lib/merge-refs';
import { useLayoutEffect } from '@atlaskit/ds-lib/use-layout-effect';
import { useMotion } from '@atlaskit/motion/entering/use-motion';
import MotionPresenceBoundary from '@atlaskit/motion/motion-presence-boundary';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { useSkipLinkInternal } from '../../context/skip-links/use-skip-link-internal';
import { ChatPanelLayoutContext } from './chat-panel-layout-context';
import { ChatPanelResponsiveStyles } from './chat-panel-responsive-styles';
import { chatPanelVar, defaultMainMinWidth, mainMinimumWidthVar } from './constants';
import { DangerouslyHoistSlotSizes } from './hoist-slot-sizes-context';
import { chatPanelPanelSplitterId } from './panel-splitter/chat-panel-panel-splitter-id';
import { convertResizeBoundToPixels } from './panel-splitter/convert-resize-bound-to-pixels';
import { PanelSplitterProvider } from './panel-splitter/provider';
import { resolveLayoutWidth } from './resolve-layout-width';
import { gridRootFitId, gridRootId } from './root';
import { useChatPanelSize } from './use-chat-panel-size';
import { useLayoutAreaSizing } from './use-layout-area-sizing';
import { useLayoutId } from './use-layout-id';
import { usePanelContentEnter } from './use-panel-content-enter';
import { useResizingWidthCssVarOnRootElement } from './use-resizing-width-css-var-on-root-element';
import { useSafeDefaultWidth } from './use-safe-default-width';
const panelSplitterResizingVar = '--n_cPnlRsz';
const chatPanelOverlayWidthVar = '--n_cPnlOverlayW';
const fallbackDefaultWidth = 400;
const fallbackCompactDefaultWidth = 320;
const inlineMediaQuery = '(min-width: 40rem)';
// Compiled needs a static value, so name the local copy and check it against the shared scale.
const chatPanelOverlayLayer = 5;
function getPixelCustomProperty(element, propertyName, fallback) {
const raw = getComputedStyle(element).getPropertyValue(propertyName).trim();
const value = Number.parseFloat(raw);
return Number.isFinite(value) ? resolveLayoutWidth(raw.endsWith('vw') ? `${value}vw` : value, window.innerWidth) : fallback;
}
// Inline chat and TopNav both change layout on each frame. A composited transform
// can advance while the header is still painting its preceding width.
const inlineEnter = null;
const inlineExit = null;
const styles = {
responsiveOverlay: "_12xtquub _qo37qfx8 _16hg1bhr _w1ap16sz",
root: "_nd5lquub _10fph9n0 _vchhusvi _p12f1osq _1pbyqfx8 _4t3i56xv _kqsw1if8 _152t1nws _bfhk1bhr _1bsb1ky5 _qhdr1ym0 _m5h51wug _1ttbvuon _1mxh8rg1",
refinedOverlay: "_p12fglyw",
managedInline: "_12xt1ym0 _qo371wug _16hgvuon _w1ap8rg1",
managedOverlay: "_12xtquub _qo37qfx8 _16hg1bhr _w1ap1ky5",
entering: "_8rc92rbg _k1afx3bs _105az3zp",
exiting: "_8rc93z33 _12xtquub _w1ap6wvj _9qarglyw _qo37qfx8 _k1afry0p _105a97o5",
inlineMotion: "_hi7k1erq",
contentEntering: "_k1afi8w4",
scrollContainer: "_1reo1wug _18m91wug _4t3i1osq",
border: "_191wglyw _16qs130s _19dmia51 _1ao7glyw",
managedInlineBorder: "_i8luia51 _so1eglyw",
managedOverlayBorder: "_i8luglyw _so1e130s"
};
/**
* The ChatPanel layout area is rendered below the banner and to the right (inline end) of the app.
*
* Below 40rem it overlays the app. At 40rem and above it is rendered as the final
* full-height column in the page layout grid.
*
* You can optionally render a `PanelSplitter` as a child to make the chat panel resizable.
* With chat layout enabled, Root retains separate inline and overlay resize preferences
* when ChatPanel is closed and remounted during the same layout session.
* Wrap conditional ChatPanel rendering in ExitingPersistence to retain it through the exit slide.
* Entrance, exit and opening content motion use the panel motion gate and respect reduced motion.
*/
export function ChatPanel({
children,
defaultWidth: defaultWidthProp,
compactDefaultWidth: compactDefaultWidthProp,
minWidth = '320px',
maxWidth,
label = 'Chat panel',
skipLinkLabel = label,
testId,
id: providedId,
xcss,
hasBorder = true,
onClose
}) {
var _managedSizing$mode, _managedSizing$width, _managedSizing$mode2;
const isMotionEnabled = fg('platform-dst-motion-uplift-panel');
const {
state: motionState,
ref: motionRef
} = useMotion();
const isExiting = isMotionEnabled && motionState === 'exiting';
const shouldAnimateContentEnter = usePanelContentEnter(motionState);
const id = useLayoutId({
providedId
});
const defaultWidth = useSafeDefaultWidth({
defaultWidthProp: defaultWidthProp !== null && defaultWidthProp !== void 0 ? defaultWidthProp : fallbackDefaultWidth,
fallbackDefaultWidth,
slotName: 'ChatPanel'
});
const compactFallbackWidth = defaultWidthProp === undefined ? fallbackCompactDefaultWidth : defaultWidth;
const compactDefaultWidth = useSafeDefaultWidth({
defaultWidthProp: fg('platform-dst-chat-panel-layout') ? compactDefaultWidthProp !== null && compactDefaultWidthProp !== void 0 ? compactDefaultWidthProp : compactFallbackWidth : compactFallbackWidth,
fallbackDefaultWidth: compactFallbackWidth,
slotName: 'ChatPanel compactDefaultWidth'
});
useSkipLinkInternal({
id,
label: skipLinkLabel,
isHidden: isExiting || defaultWidth === 0 || fg('platform_dst_nav4_skip_link_a11y_1')
});
const ref = useRef(null);
const dangerouslyHoistSlotSizes = useContext(DangerouslyHoistSlotSizes);
const layoutContext = useContext(ChatPanelLayoutContext);
const hoistWidth = dangerouslyHoistSlotSizes && fg('platform-dst-chat-panel-layout');
const mergedRef = useMemo(() => isMotionEnabled ? mergeRefs([ref, motionRef]) : ref, [isMotionEnabled, motionRef]);
const [inlineWidth, setInlineWidth] = useState(defaultWidth);
const [hasInlinePreference, setHasInlinePreference] = useState(false);
const [hasOverlayPreference, setHasOverlayPreference] = useState(false);
const [overlayWidth, setOverlayWidth] = useState(compactDefaultWidth);
const [isInline, setIsInline] = useState(false);
const {
isManaged,
prepareResize,
state: managedSizing,
getResizeBounds: getManagedResizeBounds,
startResize,
resize,
completeResize
} = useLayoutAreaSizing({
area: 'chat-panel',
config: {
defaultWidth,
compactDefaultWidth,
minWidth,
maxWidth,
isOpen: !isExiting,
onRequestClose: onClose
}
});
useEffect(() => {
const mediaQuery = window.matchMedia(inlineMediaQuery);
setIsInline(mediaQuery.matches);
const onChange = event => {
setIsInline(event.matches);
};
return bind(mediaQuery, {
type: 'change',
listener: onChange
});
}, []);
const mode = (_managedSizing$mode = managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.mode) !== null && _managedSizing$mode !== void 0 ? _managedSizing$mode : isInline ? 'inline' : 'overlay';
const renderedWidth = (_managedSizing$width = managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.width) !== null && _managedSizing$width !== void 0 ? _managedSizing$width : mode === 'inline' ? inlineWidth : overlayWidth;
useResizingWidthCssVarOnRootElement({
isEnabled: !managedSizing,
cssVar: panelSplitterResizingVar,
panelId: chatPanelPanelSplitterId
});
const getResizeBounds = useCallback(() => {
const managedBounds = getManagedResizeBounds();
if (managedBounds) {
return managedBounds;
}
const chatPanel = ref.current;
const layoutRoot = layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.rootRef.current;
const main = layoutRoot === null || layoutRoot === void 0 ? void 0 : layoutRoot.querySelector(':scope > [data-layout-slot][role="main"]');
if (!chatPanel || !main || getComputedStyle(chatPanel).gridArea !== 'chat-panel') {
return {
min: minWidth,
max: maxWidth !== null && maxWidth !== void 0 ? maxWidth : '90vw'
};
}
const chatPanelWidth = chatPanel.getBoundingClientRect().width;
const mainWidth = main.getBoundingClientRect().width;
if (chatPanelWidth === 0 || mainWidth === 0) {
return {
min: minWidth,
max: maxWidth !== null && maxWidth !== void 0 ? maxWidth : '90vw'
};
}
const localPanel = main.querySelector('[data-layout-with-panel-slot]');
const isLocalPanelInline = localPanel && getComputedStyle(localPanel).gridArea === 'panel';
const localPanelWidth = isLocalPanelInline ? localPanel.getBoundingClientRect().width : 0;
const mainMinimumWidth = getPixelCustomProperty(main, mainMinimumWidthVar, defaultMainMinWidth);
const mainContentWidth = mainWidth - localPanelWidth;
const availableWidth = Math.max(0, mainContentWidth - mainMinimumWidth);
const availableInlineWidth = Math.floor(chatPanelWidth + availableWidth);
const constrainedMaxWidth = maxWidth ? Math.min(convertResizeBoundToPixels(maxWidth), availableInlineWidth) : availableInlineWidth;
return {
min: minWidth,
max: `${Math.max(convertResizeBoundToPixels(minWidth), constrainedMaxWidth)}px`
};
}, [getManagedResizeBounds, layoutContext, maxWidth, minWidth]);
const configuredMaxWidth = maxWidth !== null && maxWidth !== void 0 ? maxWidth : '100vw';
const chatPanelVariableWidth = managedSizing ? `${renderedWidth}px` : `clamp(${minWidth}, ${inlineWidth}px, ${configuredMaxWidth})`;
const overlayDefault = `var(--n_chatDefaultW, ${compactDefaultWidth}px)`;
const overlayMaximum = `max(${minWidth}, min(${configuredMaxWidth}, var(--n_layoutW, 100vw), max(${overlayDefault}, calc(var(--n_layoutW, 100vw) * 0.9))))`;
const chatPanelOverlayVariableWidth = managedSizing ? `${renderedWidth}px` : fg('platform-dst-chat-panel-layout') ? `clamp(${minWidth}, ${hasOverlayPreference ? `${overlayWidth}px` : overlayDefault}, ${overlayMaximum})` : `clamp(${minWidth}, ${overlayWidth}px, min(90vw, ${configuredMaxWidth}))`;
const onCompleteResize = useCallback(finalWidth => {
if (mode === 'inline') {
setInlineWidth(finalWidth);
setHasInlinePreference(true);
} else {
setOverlayWidth(finalWidth);
setHasOverlayPreference(true);
}
completeResize(mode, finalWidth);
}, [completeResize, mode]);
const onResizeStartInternal = useCallback(() => {
startResize(mode);
}, [mode, startResize]);
const onResizeInternal = useCallback(width => resize(mode, width), [mode, resize]);
const layout = (_managedSizing$mode2 = managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.mode) !== null && _managedSizing$mode2 !== void 0 ? _managedSizing$mode2 : 'responsive';
const lastSize = useChatPanelSize({
panelRef: ref,
layout,
hoistWidth,
rememberSize: isMotionEnabled && !isExiting
});
const motionPhase = isMotionEnabled && (motionState === 'entering' || isExiting) ? motionState : undefined;
const exitSize = isExiting ? lastSize.current : undefined;
const layoutMode = isExiting ? 'exiting' : layout;
const exitWidth = exitSize === null || exitSize === void 0 ? void 0 : exitSize.inlineWidth;
const managedWidth = managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.width;
useLayoutEffect(() => {
if (!layoutContext) {
return;
}
const reported = {
mode: layoutMode,
phase: motionPhase,
managedWidth,
exitWidth
};
layoutContext.setLayout(reported);
return () => layoutContext.clearLayout(reported);
}, [layoutContext, layoutMode, motionPhase, managedWidth, exitWidth]);
return /*#__PURE__*/React.createElement("section", _extends({
id: id,
"data-layout-slot": true,
"aria-label": label
// `inert` already removes the exiting panel from the accessibility tree. Adding
// `aria-hidden` on top triggers "blocked aria-hidden" warnings when focus is inside.
}, isExiting ? {
inert: ''
} : {}, {
className: ax([styles.root, !managedSizing && !isExiting && styles.responsiveOverlay, hasBorder && styles.border, (managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.mode) === 'inline' && styles.managedInline, (managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.mode) === 'overlay' && styles.managedOverlay, fg('platform-dst-chat-panel-layout') && layout !== 'inline' && styles.refinedOverlay, hasBorder && (managedSizing === null || managedSizing === void 0 ? void 0 : managedSizing.mode) === 'inline' && styles.managedInlineBorder, hasBorder && (layout === 'overlay' || layout === 'responsive') && styles.managedOverlayBorder, isMotionEnabled && motionState === 'entering' && styles.entering, isExiting && styles.exiting, (isExiting ? exitWidth !== '0px' : layout === 'inline') && styles.inlineMotion, xcss]),
style: {
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop, @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/ui-styling-standard/no-imported-style-values -- Shared layout CSS variable name.
[chatPanelVar]: chatPanelVariableWidth,
'--n_chatMotionW': exitSize === null || exitSize === void 0 ? void 0 : exitSize.width,
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop, @atlaskit/ui-styling-standard/no-unsafe-values
[chatPanelOverlayWidthVar]: chatPanelOverlayVariableWidth,
'--n_chatResponsiveOverlayW': fg('platform-dst-chat-panel-layout') ? chatPanelOverlayVariableWidth : `min(calc(var(--n_layoutW) * 0.9), ${chatPanelOverlayVariableWidth})`
},
"data-testid": testId,
"data-layout-chat-motion": motionPhase,
"data-layout-chat-panel": isExiting ? undefined : '',
ref: mergedRef
}), !managedSizing && !isExiting && /*#__PURE__*/React.createElement(ChatPanelResponsiveStyles, {
gridRootId: gridRootId,
gridRootFitId: gridRootFitId,
compactDefaultWidth: compactDefaultWidth,
defaultWidth: defaultWidth,
compactPreferredWidth: fg('platform-dst-chat-panel-layout') && !hasInlinePreference ? `clamp(${minWidth}, ${compactDefaultWidth}px, ${configuredMaxWidth})` : chatPanelVariableWidth,
preferredWidth: chatPanelVariableWidth,
minWidth: minWidth,
hasBorder: hasBorder
}), /*#__PURE__*/React.createElement(PanelSplitterProvider, {
panelId: chatPanelPanelSplitterId,
panelRef: ref,
panelWidth: renderedWidth,
onCompleteResize: onCompleteResize,
prepareResize: prepareResize,
onResizeStartInternal: isManaged ? onResizeStartInternal : undefined,
onResizeInternal: isManaged ? onResizeInternal : undefined,
getResizeBounds: getResizeBounds,
resizingCssVar: panelSplitterResizingVar,
position: "start"
}, /*#__PURE__*/React.createElement("div", {
className: ax([styles.scrollContainer, isMotionEnabled && shouldAnimateContentEnter && styles.contentEntering])
}, isMotionEnabled ? /*#__PURE__*/React.createElement(MotionPresenceBoundary, null, children) : children)));
}