Commit 20cfc3ca6e3 for woocommerce
commit 20cfc3ca6e38b70b24b227b31a3aef07d5c521d7
Author: Mike Jolley <mike.jolley@me.com>
Date: Thu Nov 26 12:58:23 2026 +0000
Add motion to snackbars (#70887)
* Add motion to snackbars
Snackbars rise in and drop out, and unmount when the animation ends
instead of after a fixed 500ms timeout.
* Remove a dismissed snackbar at once
A snackbar that the shopper dismisses with its button goes immediately.
The timeout still plays the exit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/add-cart-checkout-motion-snackbars b/plugins/woocommerce/changelog/add-cart-checkout-motion-snackbars
new file mode 100644
index 00000000000..4e415f10d9b
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-cart-checkout-motion-snackbars
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Animate snackbar notices in the Cart and Checkout blocks as they appear and go.
diff --git a/plugins/woocommerce/client/blocks/.stylelintrc.json b/plugins/woocommerce/client/blocks/.stylelintrc.json
index d7da814473e..667f5eceed3 100644
--- a/plugins/woocommerce/client/blocks/.stylelintrc.json
+++ b/plugins/woocommerce/client/blocks/.stylelintrc.json
@@ -30,7 +30,7 @@
{
"files": [
"assets/js/blocks/{cart,checkout,cart-checkout-shared,order-confirmation}/**/*.scss",
- "assets/js/base/components/{button,notice-banner,loading-mask,rolling-price,cart-checkout,skeleton,quantity-selector}/**/*.scss",
+ "assets/js/base/components/{button,notice-banner,loading-mask,rolling-price,cart-checkout,skeleton,quantity-selector,snackbar-list}/**/*.scss",
"packages/public-api/blocks-components/**/*.scss",
"assets/css/abstracts/_mixins.scss",
"assets/css/abstracts/_motion.scss"
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/notice-banner/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/notice-banner/index.tsx
index 7fec997e333..6b9b34d2f28 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/notice-banner/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/notice-banner/index.tsx
@@ -22,6 +22,8 @@ export interface NoticeBannerProps {
spokenMessage?: string | React.ReactNode;
status: 'success' | 'error' | 'info' | 'warning' | 'default';
summary?: string;
+ // A ref to the banner element, for example so that a transition can wait for its animations.
+ containerRef?: React.Ref< HTMLDivElement >;
}
/**
@@ -38,6 +40,7 @@ const NoticeBanner = ( {
isDismissible = true,
politeness = getDefaultPoliteness( status ),
summary,
+ containerRef,
}: NoticeBannerProps ) => {
useSpokenMessage( spokenMessage, politeness );
@@ -53,6 +56,7 @@ const NoticeBanner = ( {
return (
<div
+ ref={ containerRef }
role={
status === 'error' ||
status === 'warning' ||
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/index.tsx
index 5cf17552b75..1fb60389f9a 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/index.tsx
@@ -3,8 +3,13 @@
*/
import clsx from 'clsx';
import type { NoticeType } from '@woocommerce/types';
-import { useReducedMotion } from '@wordpress/compose';
-import { useRef } from '@wordpress/element';
+import {
+ cloneElement,
+ createRef,
+ useRef,
+ type RefObject,
+} from '@wordpress/element';
+import { whenAnimationsEnd } from '@woocommerce/base-utils';
import { CSSTransition, TransitionGroup } from 'react-transition-group';
/**
@@ -31,7 +36,13 @@ const SnackbarList = ( {
onRemove = () => void 0,
}: SnackbarListProps ): JSX.Element => {
const listRef = useRef< HTMLDivElement | null >( null );
- const isReducedMotion = useReducedMotion();
+ // One ref for each snackbar, so that its transition can find it without `findDOMNode`, which React deprecates.
+ const snackbarRefs = useRef(
+ new Map< string, RefObject< HTMLDivElement > >()
+ );
+
+ // A snackbar that the shopper dismisses goes at once. Only the timeout plays the exit.
+ const dismissed = useRef( new WeakSet< RefObject< HTMLDivElement > >() );
const removeNotice = ( notice: NoticeType ) => () =>
onRemove( notice?.id || '' );
@@ -45,42 +56,49 @@ const SnackbarList = ( {
tabIndex={ -1 }
ref={ listRef }
>
- { isReducedMotion ? (
- notices.map( ( notice ) => {
+ <TransitionGroup
+ // Applies to the snackbars that leave too, which keep the props of their last render.
+ childFactory={ ( child ) =>
+ cloneElement( child, {
+ exit: ! dismissed.current.has( child.props.nodeRef ),
+ } )
+ }
+ >
+ { notices.map( ( notice ) => {
const { content, ...restNotice } = notice;
+ const snackbarRef =
+ snackbarRefs.current.get( notice.id ) ??
+ createRef< HTMLDivElement >();
+ snackbarRefs.current.set( notice.id, snackbarRef );
return (
- <Snackbar
- { ...restNotice }
- onRemove={ removeNotice( notice ) }
- listRef={ listRef }
- key={ notice.id }
+ <CSSTransition
+ key={ 'snackbar-' + notice.id }
+ nodeRef={ snackbarRef }
+ // The stylesheet sets the timing and handles reduced motion, so the script waits for the
+ // animation to end instead of a fixed timeout.
+ addEndListener={ ( done: () => void ) =>
+ whenAnimationsEnd( snackbarRef.current, done )
+ }
+ onExited={ () =>
+ snackbarRefs.current.delete( notice.id )
+ }
+ classNames="notice-transition"
>
- { notice.content }
- </Snackbar>
- );
- } )
- ) : (
- <TransitionGroup>
- { notices.map( ( notice ) => {
- const { content, ...restNotice } = notice;
- return (
- <CSSTransition
- key={ 'snackbar-' + notice.id }
- timeout={ 500 }
- classNames="notice-transition"
+ <Snackbar
+ { ...restNotice }
+ onRemove={ removeNotice( notice ) }
+ onDismiss={ () =>
+ dismissed.current.add( snackbarRef )
+ }
+ listRef={ listRef }
+ containerRef={ snackbarRef }
>
- <Snackbar
- { ...restNotice }
- onRemove={ removeNotice( notice ) }
- listRef={ listRef }
- >
- { content }
- </Snackbar>
- </CSSTransition>
- );
- } ) }
- </TransitionGroup>
- ) }
+ { content }
+ </Snackbar>
+ </CSSTransition>
+ );
+ } ) }
+ </TransitionGroup>
</div>
);
};
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/snackbar.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/snackbar.tsx
index 7e2efcf57ac..04e143584ae 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/snackbar.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/snackbar.tsx
@@ -13,12 +13,15 @@ import { SNACKBAR_TIMEOUT } from './constants';
export interface SnackbarProps extends NoticeBannerProps {
// A ref to the list that contains the snackbar.
listRef?: React.MutableRefObject< HTMLDivElement | null >;
+ // Called before `onRemove` when the shopper dismisses the snackbar.
+ onDismiss?: () => void;
}
export const Snackbar = ( {
onRemove = () => void 0,
children,
listRef,
+ onDismiss,
className,
...notice
}: SnackbarProps ) => {
@@ -43,6 +46,7 @@ export const Snackbar = ( {
if ( listRef && listRef.current ) {
listRef.current.focus();
}
+ onDismiss?.();
onRemove();
} }
>
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/style.scss b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/style.scss
index 8cc5b70eba4..bbfe3f62b04 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/style.scss
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/style.scss
@@ -38,23 +38,32 @@
}
}
-.notice-transition-enter {
- max-height: 0;
-}
-.notice-transition-enter.notice-transition-enter-active {
- max-height: 99em;
- transition: max-height 0.5s ease-in;
-}
-.notice-transition-enter-done {
- max-height: auto;
-}
-.notice-transition-exit {
- opacity: 1;
+// Snackbars rise 8px in and drop 4px out. They only move with `translate`, so the fade comes from the motion helpers.
+@keyframes wc-snackbar-rise {
+ from {
+ translate: 0 8px;
+ }
}
-.notice-transition-exit.notice-transition-exit-active {
- opacity: 0;
- transition: opacity 0.5s ease-in;
+
+@keyframes wc-snackbar-drop {
+ to {
+ translate: 0 4px;
+ }
}
-.notice-transition-exit-done {
- opacity: 0;
+
+.wc-block-components-notice-snackbar {
+ // A new snackbar rises in over `lg` and fades in over `md`.
+ &.notice-transition-enter {
+ @include motion-enter(wc-snackbar-rise);
+ }
+
+ // It goes faster than it came, over `md`, and keeps its end state until it unmounts.
+ &.notice-transition-exit {
+ @include motion-exit(wc-snackbar-drop);
+ }
+
+ // Stays hidden between the end of the exit and the unmount.
+ &.notice-transition-exit-done {
+ opacity: 0;
+ }
}
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/test/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/test/index.tsx
new file mode 100644
index 00000000000..233c6082f3f
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/snackbar-list/test/index.tsx
@@ -0,0 +1,66 @@
+/**
+ * External dependencies
+ */
+import { fireEvent, render, screen } from '@testing-library/react';
+import type { NoticeType } from '@woocommerce/types';
+
+/**
+ * Internal dependencies
+ */
+import SnackbarList from '../index';
+
+const notice = {
+ id: 'added',
+ content: 'Item added to the cart.',
+ status: 'success',
+} as NoticeType;
+
+// An exit that never reports its end.
+const stuckExit = {
+ playState: 'running',
+ effect: { getComputedTiming: () => ( { endTime: 200 } ) },
+ finished: new Promise( () => undefined ),
+};
+
+const snackbars = ( container: HTMLElement ) =>
+ container.querySelectorAll( '.wc-block-components-notice-snackbar' );
+
+describe( 'SnackbarList', () => {
+ beforeEach( () => {
+ jest.useFakeTimers();
+ Element.prototype.getAnimations = function ( this: Element ) {
+ return ( this.classList.contains( 'notice-transition-exit' )
+ ? [ stuckExit ]
+ : [] ) as unknown as Animation[];
+ };
+ } );
+
+ afterEach( () => {
+ jest.useRealTimers();
+ // @ts-expect-error Not in jsdom.
+ delete Element.prototype.getAnimations;
+ } );
+
+ it( 'keeps a snackbar that the timeout removes until its exit ends', () => {
+ const { container, rerender } = render(
+ <SnackbarList notices={ [ notice ] } onRemove={ jest.fn() } />
+ );
+ rerender( <SnackbarList notices={ [] } onRemove={ jest.fn() } /> );
+
+ expect( snackbars( container ) ).toHaveLength( 1 );
+ } );
+
+ it( 'removes a snackbar that the shopper dismisses at once', () => {
+ const onRemove = jest.fn();
+ const { container, rerender } = render(
+ <SnackbarList notices={ [ notice ] } onRemove={ onRemove } />
+ );
+ fireEvent.click(
+ screen.getByRole( 'button', { name: 'Dismiss this notice' } )
+ );
+ expect( onRemove ).toHaveBeenCalledWith( notice.id );
+ rerender( <SnackbarList notices={ [] } onRemove={ onRemove } /> );
+
+ expect( snackbars( container ) ).toHaveLength( 0 );
+ } );
+} );