Commit 7538d24f799 for woocommerce
commit 7538d24f7994c87ec471354b34f7f6e609d85916
Author: Mike Jolley <mike.jolley@me.com>
Date: Thu Nov 26 12:58:24 2026 +0000
Add motion to field errors (#70890)
* Add motion to field errors
Inline validation errors grow in and collapse out in the Cart and
Checkout blocks, so the content below moves instead of jumping.
* Use a logical property for the field error minimum height
* Bind the payment method error to its own validation key
The always-mounted ValidationInputError had no propertyName, so with no message it fell back to the store's '' key and could show an extension field's error (one with an empty errorId) below the payment methods.
* Show field errors at once after a key press
The error grew and collapsed whenever the page had user activation, so typing a fix or pressing Tab to leave a field moved the fields below while the shopper was using the keyboard. Track whether the last input was a key press or a pointer, and apply the change at once after a key press.
* Use the shared exiting attributes for field errors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Grow and collapse field errors after Tab or typing too
A field error follows the field, not the key. Leaving a field with Tab now looks the same as leaving it with a click, and the field that takes focus slides down instead of jumping.
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-field-errors b/plugins/woocommerce/changelog/add-cart-checkout-motion-field-errors
new file mode 100644
index 00000000000..a4d305435f9
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-cart-checkout-motion-field-errors
@@ -0,0 +1,4 @@
+Significance: patch
+Type: tweak
+
+Field errors in the Cart and Checkout blocks now grow in and collapse out smoothly, so the content below no longer jumps.
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/payment-method-required-error.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/payment-method-required-error.tsx
index b54ab6a6e38..06f462c277f 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/payment-method-required-error.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/payment-method-required-error.tsx
@@ -61,9 +61,13 @@ const PaymentMethodRequiredError = ( {
<div className={ visibleMessage ? 'has-error' : undefined }>
{ children }
</div>
- { visibleMessage && (
- <ValidationInputError errorMessage={ visibleMessage } />
- ) }
+ { /* Always there, so the message can collapse when it goes. Bound to the payment error, so it never
+ falls back to another field's error while there is no message. */ }
+ <ValidationInputError
+ errorMessage={ visibleMessage }
+ propertyName={ ERROR_ID }
+ elementId={ ERROR_ID }
+ />
</>
);
};
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/test/payment-method-required.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/test/payment-method-required.tsx
index 185b9901fcc..50510211ea4 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/test/payment-method-required.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/payment-methods/test/payment-method-required.tsx
@@ -159,6 +159,29 @@ describe( 'PaymentMethods: asking the shopper to select a payment method', () =>
).toBeNull();
} );
+ it( 'does not show an error that belongs to another field', async () => {
+ await seedStoreWithNothingSelected();
+ act( () => {
+ dispatch( paymentStore ).__internalSetActivePaymentMethod( 'bacs' );
+ // An extension field with an empty `errorId` stores its error under ''.
+ dispatch( validationStore ).setValidationErrors( {
+ '': { message: 'Another field error', hidden: false },
+ } );
+ } );
+
+ render( <PaymentMethods /> );
+ await screen.findByRole( 'radio', {
+ name: ( name ) => name.startsWith( 'bacs' ),
+ } );
+
+ expect(
+ screen.queryByText( 'Another field error' )
+ ).not.toBeInTheDocument();
+ act( () => {
+ dispatch( validationStore ).clearValidationError( '' );
+ } );
+ } );
+
it( 'does not register the error while an express method is active', async () => {
await seedStoreWithNothingSelected( { withExpress: true } );
act( () => {
diff --git a/plugins/woocommerce/client/blocks/docs/contributors/motion.md b/plugins/woocommerce/client/blocks/docs/contributors/motion.md
index 45045d02325..c97c2371cb0 100644
--- a/plugins/woocommerce/client/blocks/docs/contributors/motion.md
+++ b/plugins/woocommerce/client/blocks/docs/contributors/motion.md
@@ -14,7 +14,7 @@ This guide uses these terms:
- **Animate what the shopper does.** A click or a tap animates. Content that loads with the page appears immediately, apart from the lists in the next rule. A later change that the shopper's action causes can animate, such as a totals row, a notice, or an error.
- **Where appropriate, list items stagger on first load.** The items of a list, such as cart items, order summary items, or the order confirmation sections, can rise in 50ms apart when the page first shows them. They do not stagger again, such as after an update, a new item, or a key press. Lists of options, such as shipping or payment methods, appear immediately.
-- **Keyboard changes apply immediately.** A shopper can press keys quickly, for example to move through options. After a key press, content that shows, hides, or swaps does not animate.
+- **Keyboard changes apply immediately.** A shopper can press keys quickly, for example to move through options. After a key press, content that shows, hides, or swaps does not animate. Field errors are the exception: they follow the field, so they grow in and collapse after Tab or typing too.
- **Content moves to make space.** Content that appears grows from nothing, and the content below moves down. Content that goes collapses, and the content below moves up. Nothing jumps.
- **One change, one movement.** When one action changes many things, the content below moves one time, in one direction.
- **Motion follows the selection.** When the shopper picks another option in a row of options, such as Ship or Pickup, the selection slides to the new option. Content that the choice swaps moves the same way: the old content leaves in that direction, and the new content follows from the other side.
@@ -130,7 +130,7 @@ Do these checks by hand for each change, also with reduced motion:
- **Change and reverse.** Do the change, then do the reverse. The content below moves in one direction each time, and nothing jumps.
- **Interruption.** Reverse the change 80–150ms after it starts. The motion turns around from its current position.
-- **Keyboard.** Use Tab, Space, Enter, or the arrow keys. The change applies immediately.
+- **Keyboard.** Use Tab, Space, Enter, or the arrow keys. The change applies immediately, apart from field errors.
- **Late content.** Content that loads after the change, such as rates or totals, does not jump.
- **First time.** Open the element for the first time on the page, with the smallest input that opens it. It must stay open. Motion makes an existing flicker easy to see, so fix the cause.
- **Settled.** About 1 second after each change, the blocks have no attributes, inline styles, copies of content, or running animations that the motion added, and no console errors.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/text-input/validated-text-input.tsx b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/text-input/validated-text-input.tsx
index d92f632942d..f00ec8ce59e 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/text-input/validated-text-input.tsx
+++ b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/text-input/validated-text-input.tsx
@@ -304,9 +304,10 @@ const ValidatedTextInput = forwardRef<
}
type={ type }
feedback={
- showError && hasError ? (
+ // Without other feedback, the error stays mounted while there is none, so that it can collapse.
+ showError && ( hasError || ! feedback ) ? (
<ValidationInputError
- errorMessage={ passedErrorMessage }
+ errorMessage={ hasError ? passedErrorMessage : '' }
propertyName={ errorIdString }
elementId={ errorIdString }
/>
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/index.tsx b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/index.tsx
index 8f99c5b44a6..4d03d0adae0 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/index.tsx
+++ b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/index.tsx
@@ -3,6 +3,7 @@
*/
import { useSelect } from '@wordpress/data';
import { validationStore } from '@woocommerce/block-data';
+import { exitingProps, usePresence } from '@woocommerce/base-hooks';
import { Icon, cautionFilled } from '@wordpress/icons';
/**
@@ -40,18 +41,38 @@ export const ValidationInputError = ( {
);
if ( ! errorMessage || typeof errorMessage !== 'string' ) {
- if ( validationError?.message && ! validationError?.hidden ) {
- errorMessage = validationError.message;
- } else {
- return null;
- }
+ errorMessage =
+ validationError?.message && ! validationError?.hidden
+ ? validationError.message
+ : '';
+ }
+
+ // The error grows in and collapses out, so the content below moves with it. This includes errors after Tab or
+ // typing: they follow the field, not the key. Errors that the page shows before the shopper interacts, such as for
+ // invalid saved data, appear immediately.
+ const presence = usePresence( errorMessage || null, {
+ animate: navigator.userActivation?.hasBeenActive !== false,
+ collapse: true,
+ } );
+
+ if ( ! presence.value ) {
+ return null;
}
return (
- <div className="wc-block-components-validation-error" role="alert">
- <p id={ id || validationErrorId }>
+ <div
+ ref={ presence.ref }
+ className="wc-block-components-validation-error"
+ role="alert"
+ { ...exitingProps( presence.isExiting ) }
+ >
+ <p
+ // A new paragraph when the error comes back during its collapse, so the alert announces it again.
+ key={ presence.isExiting ? 'exiting' : 'present' }
+ id={ presence.isExiting ? undefined : id || validationErrorId }
+ >
<Icon icon={ cautionFilled } />
- <span>{ errorMessage }</span>
+ <span>{ presence.value }</span>
</p>
</div>
);
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/style.scss b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/style.scss
index 7f31b9c0283..f0b26766dd1 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/style.scss
+++ b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/style.scss
@@ -30,3 +30,13 @@
.wc-block-components-select + .wc-block-components-validation-error {
margin-bottom: $gap-large;
}
+
+// Only the blocks that use motion animate the error.
+:where(.wp-block-woocommerce-cart, .wp-block-woocommerce-checkout) .wc-block-components-validation-error {
+ @include motion-collapse;
+
+ // Lets the height reach zero.
+ &[data-wc-collapse] {
+ min-block-size: 0;
+ }
+}
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/test/index.tsx b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/test/index.tsx
index d2a1dcf6d5f..3771f390fac 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/test/index.tsx
+++ b/plugins/woocommerce/client/blocks/packages/public-api/blocks-components/validation-input-error/test/index.tsx
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
-import { act, render, screen } from '@testing-library/react';
+import { act, fireEvent, render, screen } from '@testing-library/react';
import { dispatch } from '@wordpress/data';
import { validationStore } from '@woocommerce/block-data';
@@ -11,7 +11,28 @@ import { validationStore } from '@woocommerce/block-data';
import { ValidationInputError } from '..';
describe( 'ValidationInputError', () => {
+ let finish: () => void;
+
+ // Gives every element one running animation, which ends when `finish` is called.
+ beforeEach( () => {
+ const { promise, resolve } = Promise.withResolvers< void >();
+ const animation = {
+ playState: 'running',
+ finished: promise,
+ effect: { getComputedTiming: () => ( { endTime: 200 } ) },
+ };
+ finish = () => {
+ animation.playState = 'finished';
+ resolve();
+ };
+ Element.prototype.getAnimations = () => [
+ animation as unknown as Animation,
+ ];
+ } );
+
afterEach( () => {
+ // @ts-expect-error jsdom has no Web Animations API.
+ delete Element.prototype.getAnimations;
act( () => {
dispatch( validationStore ).clearValidationErrors();
} );
@@ -56,4 +77,52 @@ describe( 'ValidationInputError', () => {
expect( container ).toBeEmptyDOMElement();
} );
+
+ it( 'keeps the last message, hidden and inert, while it collapses', async () => {
+ const { rerender } = render(
+ <ValidationInputError errorMessage="Enter a postcode" />
+ );
+ const alert = screen.getByRole( 'alert' );
+ expect( alert ).not.toHaveAttribute( 'aria-hidden' );
+
+ rerender( <ValidationInputError errorMessage="" /> );
+ expect( alert ).toHaveTextContent( 'Enter a postcode' );
+ expect( alert ).toHaveAttribute( 'aria-hidden', 'true' );
+ expect( alert ).toHaveAttribute( 'inert' );
+ expect( alert.querySelector( 'p' ) ).not.toHaveAttribute( 'id' );
+
+ await act( async () => finish() );
+ expect( alert ).not.toBeInTheDocument();
+ } );
+
+ it( 'is announced and interactive again when the message returns during the collapse', () => {
+ const { rerender } = render(
+ <ValidationInputError errorMessage="Enter a postcode" />
+ );
+ const alert = screen.getByRole( 'alert' );
+ rerender( <ValidationInputError errorMessage="" /> );
+ const exitingMessage = alert.querySelector( 'p' );
+ rerender( <ValidationInputError errorMessage="Enter a postcode" /> );
+
+ // The same element turns around, and the message inside it is new, so the alert announces it.
+ expect( screen.getByRole( 'alert' ) ).toBe( alert );
+ expect( alert.querySelector( 'p' ) ).not.toBe( exitingMessage );
+ expect( alert ).not.toHaveAttribute( 'aria-hidden' );
+ expect( alert ).not.toHaveAttribute( 'inert' );
+ } );
+
+ it( 'collapses the message after a key press too', async () => {
+ const { rerender } = render(
+ <ValidationInputError errorMessage="Enter a postcode" />
+ );
+ const alert = screen.getByRole( 'alert' );
+
+ fireEvent.keyDown( document, { key: 'a' } );
+ rerender( <ValidationInputError errorMessage="" /> );
+ expect( alert ).toHaveTextContent( 'Enter a postcode' );
+ expect( alert ).toHaveAttribute( 'aria-hidden', 'true' );
+
+ await act( async () => finish() );
+ expect( alert ).not.toBeInTheDocument();
+ } );
} );