import { ComponentProps, Fragment, ReactNode, useEffect, useRef } from 'react'; import PrimaryAction from './PrimaryAction'; import { useRequestAnimationFrame } from '../animation/RequestAnimationFrameContext'; import Box from '../Box'; import Button from '../Button'; import { useDefaultLabelContext } from '../contexts/DefaultLabelProvider'; import Flex from '../Flex'; import Heading from '../Heading'; import IconButton from '../IconButton'; import Link from '../Link'; import InternalDismissButton from '../sharedSubcomponents/InternalDismissButton'; import TapArea from '../TapArea'; import Text from '../Text'; type OnClickType = (arg1: { event: | React.MouseEvent | React.KeyboardEvent | React.MouseEvent | React.KeyboardEvent; onDismissStart: () => void; }) => void; type Props = { accessibilityLabel?: string; align: 'start' | 'center'; backIconButton: | { accessibilityLabel: string; onClick: OnClickType; } | null | undefined; forwardIconButton: | { accessibilityLabel: string; onClick: OnClickType; } | null | undefined; heading: ReactNode; id: string; onDismiss?: () => void; primaryAction: | { accessibilityLabel: string; href?: string; label: string; onClick: OnClickType; rel?: ComponentProps['rel']; size?: ComponentProps['size']; target?: ComponentProps['target']; } | null | undefined; showDismissButton: boolean | null | undefined; showGrabber?: boolean; subHeading: string | null | undefined; }; export default function Header({ backIconButton, id, showDismissButton, heading, subHeading, forwardIconButton, onDismiss, primaryAction, showGrabber, align, }: Props) { const { accessibilityDismissButtonLabel, accessibilityGrabberLabel } = useDefaultLabelContext('SheetMobile'); const { onExternalDismiss } = useRequestAnimationFrame(); const dismissButtonRef = useRef(null); const grabberRef = useRef(null); useEffect(() => { if (!showGrabber && dismissButtonRef.current) { dismissButtonRef.current.focus(); } if (grabberRef.current) { grabberRef.current.focus(); } }, [dismissButtonRef, showGrabber]); return ( {showGrabber ? ( ' is not assignable to type 'LegacyRef | undefined'. ref={grabberRef} accessibilityLabel={accessibilityGrabberLabel} fullWidth={false} rounding={7} > ) : null} {backIconButton ? ( backIconButton?.onClick({ event, onDismissStart: onExternalDismiss, }) } size="lg" /> ) : null} {!backIconButton && showDismissButton ? ( ' is not assignable to type 'LegacyRef | undefined'. ref={dismissButtonRef} accessibilityControls={id} accessibilityLabel={accessibilityDismissButtonLabel} onClick={onDismiss ?? onExternalDismiss} size="lg" /> ) : null} {/* Flex.Item must wrap the conditional to prevent the DismissButton from being centered if there's no heading */} {heading ? ( {typeof heading === 'string' ? ( {heading} ) : ( heading )} {subHeading && ( {subHeading} )} ) : null} {forwardIconButton && !primaryAction ? ( forwardIconButton?.onClick({ event, onDismissStart: onExternalDismiss, }) } size="lg" /> ) : null} {primaryAction ? ( // Allow button text to wrap on mobile {primaryAction.href ? ( primaryAction?.onClick({ event, onDismissStart: onExternalDismiss, }) } rel={primaryAction.rel} role="link" target={primaryAction?.target} /> ) : ( primaryAction?.onClick({ event, onDismissStart: onExternalDismiss, }) } role="button" /> )} ) : null} ); }