import { ElementType, ReactNode, useId, useState } from 'react'; import { clsx } from 'clsx'; import Body from '../../Body'; import Button from '../../Button'; import Container from '../../Container'; import { Breakpoint, LiveRegion, Typography } from '../../common'; import { ButtonProps } from '../../Button/Button.types'; import { PrimitivePrompt, PrimitivePromptProps } from '../PrimitivePrompt'; import { renderPromptMedia, PromptMedia } from '../helpers/promptMedia'; import { ExpanderToggle } from '../common/Expander/Expander'; import { buildAnnouncementString } from './helpers'; import { useContainerSize } from '../../common/hooks/useContainerSize'; export type CriticalBannerProps = { title?: ReactNode; description: ReactNode; media?: PromptMedia; action?: Pick & { label: ButtonProps['children']; }; actionSecondary?: Pick & { label: ButtonProps['children']; }; sentiment?: Exclude; /** * Controls whether the banner plays its entry animation 500ms after mounting. * Expand and collapse animations are unaffected. */ animateEntry?: boolean; /** * Controls whether the description and actions are visible, as a controlled component. * When collapsed with a title, only the title is shown. * When collapsed without a title, the description is trimmed to 2 lines. * * Note: On desktop (container width >= 768px), the banner is always expanded * regardless of this prop value. */ expanded?: boolean; /** * Called when the user clicks the chevron toggle. * If not provided, the component will manage expanded state internally. */ onToggle?: () => void; } & Pick; /** * A full-width, non-dismissible banner for critical messages such as account blocks or * time-sensitive actions that require immediate user attention. * @see {@link https://docs.wise.design/components/critical-banner Design Spec} */ export const CriticalBanner = ({ sentiment = 'negative', title, description, media = {}, action, actionSecondary, animateEntry = true, expanded: expandedProp, onToggle, id, className, 'data-testid': testId, }: CriticalBannerProps) => { const [containerRef, isDesktop] = useContainerSize(Breakpoint.MEDIUM); const isControlled = expandedProp !== undefined && onToggle !== undefined; const [internalExpanded, setInternalExpanded] = useState(true); const resolvedExpanded = isDesktop ? true : isControlled ? expandedProp : internalExpanded; const handleToggle = isControlled ? onToggle : () => setInternalExpanded((previousExpanded) => !previousExpanded); const hasActions = action ?? actionSecondary; const mediaId = useId(); const titleId = useId(); const descId = useId(); const ariaLabelledByIds = [ media['aria-hidden'] ? undefined : mediaId, !title ? undefined : titleId, ] .filter(Boolean) .join(' '); const bannerSurface = ( {actionSecondary && ( // @ts-expect-error onClick type mismatch )} {action && ( // @ts-expect-error onClick type mismatch )} ) : undefined } role="region" aria-labelledby={ariaLabelledByIds || undefined} aria-describedby={description ? descId : undefined} >
{title && ( {title} )} {description && ( {description} )}
); return (
{bannerSurface}
); }; export default CriticalBanner;