import { Children, HTMLAttributes, ReactNode, TouchEvent, isValidElement, useState } from 'react'; import { LiveRegion, Sentiment, Typography } from '../../common'; import type { AriaLive } from '../../common'; import { GiftBox } from '@transferwise/icons'; import type { Sentiment as SurfaceSentiment } from '../../SentimentSurface'; import StatusIcon from '../../StatusIcon'; import { clsx } from 'clsx'; import Body from '../../Body'; import Link, { LinkProps } from '../../Link'; import { PrimitivePrompt, PrimitivePromptProps } from '../PrimitivePrompt'; export type InfoPromptAction = Pick & { /** * The label text for the action link */ label: string; }; export type InfoPromptMedia = { /** * The icon/image asset to display. * The asset should include its own accessibility attributes (e.g. title, aria-label) if it conveys meaning. */ asset: ReactNode; }; export type InfoPromptProps = Omit, 'title' | 'aria-live' | 'role'> & Pick & { /** * The sentiment determines the colour scheme * @default 'neutral' */ sentiment?: SurfaceSentiment; /** * Handler called when the close button is clicked. * If not provided, the close button is hidden. */ onDismiss?: () => void; /** * Custom media to override the default status icon. * Success and proposition sentiments support 2 status variations: standard checkmark & confetti. */ media?: InfoPromptMedia; /** * Action link to be displayed below the description */ action?: InfoPromptAction; /** * Title content for the prompt */ title?: string; /** * Description content for the prompt (required). Accepts plain text or rich content (ReactNode). * If the description contains an interactive element, the `action` prop must not be used simultaneously. */ description: ReactNode; className?: string; /** * Sets the ARIA live region politeness level. * - `'polite'` — announced after the current speech (default) * - `'assertive'` — interrupts the current speech immediately * - `'off'` — disables the live region entirely * @default 'polite' */ 'aria-live'?: AriaLive; }; /** * `InfoPrompt` displays important contextual messages to users within a screen. * It provides a visually distinct way to communicate information, warnings, errors, * or positive feedback with optional actions and dismissal capabilities. * * Use this component to replace the deprecated `Alert` component (run codemod to migrate: **`npx \@wise/wds-codemods@latest info-prompt`**). * * Guidance can be found in the [design system](https://docs.wise.design/components/info-prompt). */ export const InfoPrompt = ({ sentiment = 'neutral', onDismiss, media, action, title, description, className, 'aria-live': ariaLive = 'polite', 'data-testid': dataTestId, ...restProps }: InfoPromptProps) => { const [shouldFire, setShouldFire] = useState(); const announceOnChange = [ title, Children.toArray(description) .map((child) => isValidElement<{ children?: ReactNode }>(child) ? child.props.children : child, ) .join(''), action?.label, ] .filter(Boolean) .join('|'); const statusIconSentiment = sentiment === 'success' ? Sentiment.POSITIVE : (sentiment as Exclude); const handleTouchStart = (event: TouchEvent) => { const target = event.target as HTMLElement; if (target.closest('.wds-prompt__dismiss') || target.closest('.wds-info-prompt__action')) { return; } setShouldFire(true); }; const handleTouchEnd = () => { if (shouldFire && action) { (action.onClick as (() => void) | undefined)?.(); if (action.href) { if (action.target === '_blank') { window.top?.open(action.href, '_blank', 'noopener, noreferrer'); } else { window.top?.location.assign(action.href); } } } setShouldFire(false); }; const handleTouchMove = () => { setShouldFire(false); }; const renderMedia = () => { if (media) { return {media.asset}; } if (sentiment === 'proposition') { return ( ); } return ; }; // Render content directly in LiveRegion return (
{title && ( {title} )} {description} {action && ( {action.label} )}
); };