|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + Children, |
| 5 | + createRef, |
| 6 | + forwardRef, |
| 7 | + useEffect, |
| 8 | + useState, |
| 9 | + type ComponentPropsWithoutRef, |
| 10 | +} from 'react'; |
| 11 | +import classNames from 'classnames'; |
| 12 | +import { |
| 13 | + NotificationBannerHeading, |
| 14 | + NotificationBannerLink, |
| 15 | + NotificationBannerTitle, |
| 16 | +} from './components/index.js'; |
| 17 | +import { type NotificationBanner as NotificationBannerModule } from 'nhsuk-frontend'; |
| 18 | +import { childIsOfComponentType } from '#util/types/TypeGuards.js'; |
| 19 | + |
| 20 | +export interface NotificationBannerProps extends ComponentPropsWithoutRef<'div'> { |
| 21 | + success?: boolean; |
| 22 | + disableAutoFocus?: boolean; |
| 23 | + titleId?: string; |
| 24 | +} |
| 25 | + |
| 26 | +const NotificationBannerComponent = forwardRef<HTMLDivElement, NotificationBannerProps>( |
| 27 | + (props, forwardedRef) => { |
| 28 | + const { children, className, title, titleId, success, role, disableAutoFocus, ...rest } = props; |
| 29 | + |
| 30 | + const [moduleRef] = useState(() => forwardedRef || createRef<HTMLDivElement>()); |
| 31 | + const [instanceError, setInstanceError] = useState<Error>(); |
| 32 | + const [instance, setInstance] = useState<NotificationBannerModule>(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (!('current' in moduleRef) || !moduleRef.current || instance) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + import('nhsuk-frontend') |
| 40 | + .then(({ NotificationBanner }) => setInstance(new NotificationBanner(moduleRef.current))) |
| 41 | + .catch(setInstanceError); |
| 42 | + }, [moduleRef, instance]); |
| 43 | + |
| 44 | + const items = Children.toArray(children); |
| 45 | + |
| 46 | + const titleElement = items.find((child) => |
| 47 | + childIsOfComponentType(child, NotificationBannerTitle, { |
| 48 | + className: 'nhsuk-notification-banner__title', |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + const titleElementId = titleElement?.props.id || titleId || 'nhsuk-notification-banner-title'; |
| 53 | + |
| 54 | + const contentItems = items.filter((child) => child !== titleElement); |
| 55 | + |
| 56 | + if (instanceError) { |
| 57 | + throw instanceError; |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <div |
| 62 | + className={classNames( |
| 63 | + 'nhsuk-notification-banner', |
| 64 | + { 'nhsuk-notification-banner--success': success }, |
| 65 | + className, |
| 66 | + )} |
| 67 | + aria-labelledby={titleElementId} |
| 68 | + data-module="nhsuk-notification-banner" |
| 69 | + data-disable-auto-focus={disableAutoFocus} |
| 70 | + ref={moduleRef} |
| 71 | + role={role || (success ? 'alert' : 'region')} |
| 72 | + {...rest} |
| 73 | + > |
| 74 | + <div className="nhsuk-notification-banner__header"> |
| 75 | + {titleElement ? ( |
| 76 | + <>{titleElement}</> |
| 77 | + ) : ( |
| 78 | + <NotificationBannerTitle id={titleElementId} success={success}> |
| 79 | + {title} |
| 80 | + </NotificationBannerTitle> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + <div className="nhsuk-notification-banner__content">{contentItems}</div> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + }, |
| 87 | +); |
| 88 | + |
| 89 | +NotificationBannerComponent.displayName = 'NotificationBanner'; |
| 90 | + |
| 91 | +export const NotificationBanner = Object.assign(NotificationBannerComponent, { |
| 92 | + Title: NotificationBannerTitle, |
| 93 | + Heading: NotificationBannerHeading, |
| 94 | + Link: NotificationBannerLink, |
| 95 | +}); |
0 commit comments