-
Notifications
You must be signed in to change notification settings - Fork 35
Add notification banner component #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
colinrotherham
merged 13 commits into
NHSDigital:rollup-directives
from
robkerrybjss:add-notification-banner-component
Oct 13, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a00d653
Add the Notification Banner introduced in NHS Frontend v10 (#283)
robkerrybjss 161cb57
Fix circular dependency
robkerrybjss 737c284
Address review comments
robkerrybjss 5c38de7
Address review comments (sonar)
robkerrybjss 4808877
Move notification banner header bar to main component
colinrotherham dcbd029
Export notification banner child components
colinrotherham 8f12383
Allow NHS.UK frontend errors to be caught
colinrotherham e0ae00f
Add fallback `className` to component guards
colinrotherham 1c43ecd
Reduce component filters
colinrotherham 6ebc628
Address further review comments
robkerrybjss 081b895
Address further review comments
robkerrybjss 7eb6bf4
Address further review comments
robkerrybjss abc9b8f
Formatting
colinrotherham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/components/content-presentation/notification-banner/NotificationBanner.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| Children, | ||
| createRef, | ||
| forwardRef, | ||
| useEffect, | ||
| useState, | ||
| type ComponentPropsWithoutRef, | ||
| } from 'react'; | ||
| import classNames from 'classnames'; | ||
| import { | ||
| NotificationBannerHeading, | ||
| NotificationBannerLink, | ||
| NotificationBannerTitle, | ||
| } from './components/index.js'; | ||
| import { type NotificationBanner as NotificationBannerModule } from 'nhsuk-frontend'; | ||
| import { childIsOfComponentType } from '#util/types/TypeGuards.js'; | ||
|
|
||
| export interface NotificationBannerProps extends ComponentPropsWithoutRef<'div'> { | ||
| success?: boolean; | ||
| disableAutoFocus?: boolean; | ||
| titleId?: string; | ||
| } | ||
|
|
||
| const NotificationBannerComponent = forwardRef<HTMLDivElement, NotificationBannerProps>( | ||
| (props, forwardedRef) => { | ||
| const { children, className, title, titleId, success, role, disableAutoFocus, ...rest } = props; | ||
|
|
||
| const [moduleRef] = useState(() => forwardedRef || createRef<HTMLDivElement>()); | ||
colinrotherham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const [instanceError, setInstanceError] = useState<Error>(); | ||
| const [instance, setInstance] = useState<NotificationBannerModule>(); | ||
|
|
||
| useEffect(() => { | ||
| if (!('current' in moduleRef) || !moduleRef.current || instance) { | ||
| return; | ||
| } | ||
|
|
||
| import('nhsuk-frontend') | ||
| .then(({ NotificationBanner }) => setInstance(new NotificationBanner(moduleRef.current))) | ||
| .catch(setInstanceError); | ||
| }, [moduleRef, instance]); | ||
colinrotherham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const items = Children.toArray(children); | ||
|
|
||
| const titleElement = items.find((child) => | ||
| childIsOfComponentType(child, NotificationBannerTitle, { | ||
| className: 'nhsuk-notification-banner__title', | ||
| }), | ||
| ); | ||
colinrotherham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const titleElementId = titleElement?.props.id || titleId || 'nhsuk-notification-banner-title'; | ||
|
|
||
| const contentItems = items.filter((child) => child !== titleElement); | ||
|
|
||
| if (instanceError) { | ||
| throw instanceError; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className={classNames( | ||
| 'nhsuk-notification-banner', | ||
| { 'nhsuk-notification-banner--success': success }, | ||
| className, | ||
| )} | ||
| aria-labelledby={titleElementId} | ||
| data-module="nhsuk-notification-banner" | ||
| data-disable-auto-focus={disableAutoFocus} | ||
| ref={moduleRef} | ||
| role={role || (success ? 'alert' : 'region')} | ||
| {...rest} | ||
| > | ||
| <div className="nhsuk-notification-banner__header"> | ||
| {titleElement ? ( | ||
| <>{titleElement}</> | ||
| ) : ( | ||
| <NotificationBannerTitle id={titleElementId} success={success}> | ||
| {title} | ||
| </NotificationBannerTitle> | ||
| )} | ||
| </div> | ||
| <div className="nhsuk-notification-banner__content">{contentItems}</div> | ||
| </div> | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| NotificationBannerComponent.displayName = 'NotificationBanner'; | ||
|
|
||
| export const NotificationBanner = Object.assign(NotificationBannerComponent, { | ||
| Title: NotificationBannerTitle, | ||
| Heading: NotificationBannerHeading, | ||
| Link: NotificationBannerLink, | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting this!