Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
df7e805
fix(bulma-ui): show toasts and notifications raised before their cont…
allxsmith Sep 29, 2026
6364171
perf(bulma-ui): skip the extra render when a toast or notification co…
allxsmith Sep 29, 2026
2d6911b
fix(bulma-ui): open a dialog raised before DialogContainer mounts
allxsmith Sep 29, 2026
71b90a2
fix(bulma-ui): place toasts and notifications at the position they we…
allxsmith Sep 29, 2026
6401c31
fix(bulma-ui): announce polite notifications through a status region …
allxsmith Sep 29, 2026
9902cf8
Merge remote-tracking branch 'origin/main' into merge-779
allxsmith Sep 30, 2026
8fd06e4
Merge branch 'merge-779' into merge-790
allxsmith Sep 30, 2026
3d8b33f
docs(bulma-ui): say what position does on a standalone Toast and when…
allxsmith Sep 30, 2026
df0f4c7
Merge remote-tracking branch 'origin/main' into merge-779
allxsmith Sep 30, 2026
42f92d9
Merge remote-tracking branch 'origin/fix/779-show-position' into merg…
allxsmith Sep 30, 2026
684d17c
Merge remote-tracking branch 'origin/main' into merge-790
allxsmith Sep 30, 2026
9edf237
test(bulma-ui): drop the position tests the main merge duplicated
allxsmith Sep 30, 2026
1963004
fix(bulma-ui): hide the notification status region without a styleshe…
allxsmith Oct 1, 2026
0f9e880
fix(bulma-ui): keep written notification announcements for their whol…
allxsmith Oct 1, 2026
e1cd732
fix(bulma-ui): keep notification announcements whole and apart when a…
allxsmith Oct 1, 2026
930e167
fix(bulma-ui): leave inline display: none SVG out of notification ann…
allxsmith Oct 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bestax-mcp/data/components/Notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"code": "<Notification color=\"warning\">\n <Strong>Warning!</Strong> This notification contains{' '}\n <Link href=\"#\">custom content</Link>.\n</Notification>"
}
],
"accessibility": "- **Delete button:** Includes `aria-label=\"Close notification\"` for screen readers.\n- **Keyboard:** The delete button is focusable and can be activated by keyboard.\n- **Content:** Use semantic HTML within the notification for best accessibility.\n- **Announcements:** A notification shown through `notification` announces its message: as an alert for `danger` and `warning`, and as a status otherwise.\n\n:::tip\nAlways provide clear, actionable text inside notifications.\n:::",
"accessibility": "- **Delete button:** Includes `aria-label=\"Close notification\"` for screen readers.\n- **Keyboard:** The delete button is focusable and can be activated by keyboard.\n- **Content:** Use semantic HTML within the notification for best accessibility.\n- **Announcements:** A notification shown through `notification` announces its message. `danger` and `warning` announce themselves: the message carries `role=\"alert\"`, which screen readers reliably announce even though it arrives with its text. The rest are announced politely through a visually hidden `role=\"status\"` region that `NotificationContainer` keeps in the page from the moment it mounts, even with nothing showing, because a polite region that arrives already holding its text often isn't announced. A moment after one of those notifications appears, its message is written into the region, then cleared again shortly after.\n- **Testing:** A polite notification's message doesn't carry `role=\"status\"`, so `getByRole('status')` finds the container's region rather than the notification. Query the message text or `.notification` instead. While a message is in the region, the page also holds a second copy of its text, so a test that advances the clock past the announcement and then looks the message up by text can find both. Scope that query to the notification, for example with `within`.\n\n:::tip\nAlways provide clear, actionable text inside notifications.\n:::",
"related": ["Delete", "useBulmaClasses"],
"storybook": "https://bestax.io/storybook/?path=/story/elements-notification--default",
"summary": "The `Notification` component is a Bulma-styled alert/message area for providing feedback, warnings, or information to users.",
Expand Down
131 changes: 105 additions & 26 deletions bulma-ui/src/elements/Notification.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, {
useEffect,
useMemo,
useRef,
useState,
useCallback,
useSyncExternalStore,
Expand All @@ -13,6 +15,8 @@ import {
} from '../helpers/useBulmaClasses';
import { warnUnstyledColor } from '../helpers/colorDeprecations';
import { groupIntoPositionStacks } from '../helpers/positionStacks';
import { useIsHydrated } from '../helpers/useIsHydrated';
import { StatusRegion } from '../helpers/statusRegion';

/**
* Props for the Notification component.
Expand Down Expand Up @@ -175,9 +179,9 @@ const getServerNotifications = () => noNotifications;
// updates alone, so notifications raised before it mounted still appear. It is
// replaced rather than mutated, and only when listeners are notified, because
// useSyncExternalStore needs the same array back between changes. An empty
// list is `noNotifications` itself, so a container that hydrates with nothing
// to show reads the same snapshot the server did and has no reason to render
// again.
// list is `noNotifications` itself, so an empty store reads the same snapshot
// on the client as on the server, and a notify that leaves nothing showing
// when nothing was showing doesn't render the container again.
let visibleNotifications: NotificationInstance[] = noNotifications;
const getVisibleNotifications = () => visibleNotifications;

Expand Down Expand Up @@ -312,17 +316,29 @@ export const notification = {
},
};

/**
* Whether a notification of this color announces itself as an alert, rather
* than politely through NotificationContainer's status region.
*
* @function isUrgentColor
* @param {NotificationOptions['color']} color - The notification's color.
* @returns {boolean} True for `danger` and `warning`.
*/
const isUrgentColor = (color: NotificationOptions['color']): boolean =>
color === 'danger' || color === 'warning';

/**
* Single auto-dismissing notification item used by NotificationContainer.
*
* @function
* @param {{ instance: NotificationInstance; onClose: (id: string) => void }} props - Component props.
* @param {{ instance: NotificationInstance; onClose: (id: string) => void; registerMessage: (id: string, node: HTMLElement | null) => void }} props - Component props.
* @returns {JSX.Element} The rendered notification item.
*/
const NotificationItem: React.FC<{
instance: NotificationInstance;
onClose: (id: string) => void;
}> = ({ instance, onClose }) => {
registerMessage: (id: string, node: HTMLElement | null) => void;
}> = ({ instance, onClose, registerMessage }) => {
const {
message,
color,
Expand All @@ -334,12 +350,19 @@ const NotificationItem: React.FC<{
} = instance.options;

const [isPaused, setIsPaused] = useState(false);
const urgent = color === 'danger' || color === 'warning';
const urgent = isUrgentColor(color);

const handleClose = useCallback(() => {
onClose(instance.id);
}, [onClose, instance.id]);

const messageRef = useCallback(
(node: HTMLElement | null) => {
registerMessage(instance.id, node);
},
[registerMessage, instance.id]
);

// Auto-close timer
useEffect(() => {
if (indefinite || duration === 0 || isPaused) return undefined;
Expand All @@ -366,18 +389,19 @@ const NotificationItem: React.FC<{
onMouseLeave={handleMouseLeave}
style={{ pointerEvents: 'auto' }}
>
{/* It appears after the page has rendered, so its message announces
itself: as an alert for danger and warning, as a status otherwise.
The live region wraps only the message, not the close button that
Notification renders ahead of its children. */}
{/* Only the message is announced, not the close button Notification
renders ahead of its children. Danger and warning announce
themselves as an alert, which a screen reader reliably reads out
even when it arrives with its text. The rest are announced through
the container's status region, which reads their text from this
span, so it carries no role of its own that would announce them a
second time. */}
{urgent ? (
<span role="alert" aria-live="assertive">
{message}
</span>
) : (
<span role="status" aria-live="polite">
{message}
</span>
<span ref={messageRef}>{message}</span>
)}
</Notification>
);
Expand Down Expand Up @@ -433,9 +457,21 @@ const notificationStackStyle = (
* container's `position`, so the container renders a stack for each position
* in use.
*
* It keeps a visually hidden `role="status"` live region in the page from the
* moment it mounts, even while nothing is showing, and announces every
* notification other than `danger` and `warning` through it, a moment after
* the notification appears. Those notifications carry no `role="status"` of
* their own, so `getByRole('status')` finds the region, not the notification.
* The text stays in the region briefly, and while it's there the page holds a
* second copy of it. A polite notification that closes before its
* announcement is written, a moment after it appears, isn't announced at all.
* `danger` and `warning` notifications announce themselves with
* `role="alert"`. The region is hidden with inline styles, so it needs no
* stylesheet.
*
* @function
* @param {{ position?: NotificationPosition }} props - Container props.
* @returns {JSX.Element | null} The rendered notification container, or null if empty.
* @returns {JSX.Element | null} The rendered notification container, or null on the server and while hydrating.
*/
export const NotificationContainer: React.FC<{
/**
Expand All @@ -453,8 +489,47 @@ export const NotificationContainer: React.FC<{
getVisibleNotifications,
getServerNotifications
);
// A portal needs a document, and it has no server-rendered counterpart, so
// the server render and the hydrating one render nothing, and the container
// portals in from the render after hydration.
const isHydrated = useIsHydrated();

// The notifications the status region announces. Danger and warning
// announce themselves.
const politeItems = useMemo(
() => items.filter(item => !isUrgentColor(item.options.color)),
[items]
);

// Each polite notification's message element, so the text the region
// announces is the text on screen, whatever the message renders.
const messageNodesRef = useRef(new Map<string, HTMLElement>());
const registerMessage = useCallback(
(id: string, node: HTMLElement | null) => {
if (node) {
messageNodesRef.current.set(id, node);
} else {
messageNodesRef.current.delete(id);
}
},
[]
);
// StatusRegion only describes polite notifications that are on screen, and
// each of those registered its message element when it mounted. The
// fallback is for a notification announced without being rendered, should
// the container ever render fewer than it announces: a plain-text message
// still says itself, and anything else is skipped.
const describe = useCallback((item: NotificationInstance) => {
const node = messageNodesRef.current.get(item.id);
/* istanbul ignore if: every polite notification on screen registers its message element when it mounts */
if (!node) {
const { message } = item.options;
return typeof message === 'string' ? message : null;
}
return node.textContent;
Comment thread
allxsmith marked this conversation as resolved.
Resolved
}, []);

if (typeof document === 'undefined' || items.length === 0) {
if (typeof document === 'undefined' || !isHydrated) {
return null;
}

Expand All @@ -466,17 +541,21 @@ export const NotificationContainer: React.FC<{
);

return createPortal(
stacks.map(stack => (
<div key={stack.key} style={notificationStackStyle(stack.position)}>
{stack.items.map(item => (
<NotificationItem
key={item.id}
instance={item}
onClose={notification.close}
/>
))}
</div>
)),
<>
{stacks.map(stack => (
<div key={stack.key} style={notificationStackStyle(stack.position)}>
{stack.items.map(item => (
<NotificationItem
key={item.id}
instance={item}
onClose={notification.close}
registerMessage={registerMessage}
/>
))}
</div>
))}
<StatusRegion items={politeItems} describe={describe} />
</>,
document.body
);
};
7 changes: 7 additions & 0 deletions bulma-ui/src/elements/__tests__/Notification.ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import { renderToStaticMarkup } from 'react-dom/server';
import { NotificationContainer, notification } from '../Notification';

describe('NotificationContainer SSR (node environment)', () => {
it('renders nothing with nothing raised, status region included', () => {
const html = renderToStaticMarkup(
React.createElement(NotificationContainer)
);
expect(html).toBe('');
});

it('renders nothing, even with notifications raised', () => {
notification.show({ message: 'Raised on the server', duration: 0 });
try {
Expand Down
Loading
Loading