Skip to content
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

Components: Fix the Snackbar auto-dismissal timers #58604

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `Placeholder`: Fix Placeholder component padding when body text font size is changed ([#58323](https://github.com/WordPress/gutenberg/pull/58323)).
- `Placeholder`: Fix Global Styles typography settings bleeding into placeholder component ([#58303](https://github.com/WordPress/gutenberg/pull/58303)).
- `PaletteEdit`: Fix palette item accessibility in details view ([#58214](https://github.com/WordPress/gutenberg/pull/58214)).
- `Snackbar`: Fix the auto-dismissal timers ([#58604](https://github.com/WordPress/gutenberg/pull/58604)).

### Experimental

Expand Down
23 changes: 18 additions & 5 deletions packages/components/src/snackbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import { useEffect, forwardRef, renderToString } from '@wordpress/element';
import {
useEffect,
useLayoutEffect,
useRef,
forwardRef,
renderToString,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import warning from '@wordpress/warning';

Expand Down Expand Up @@ -88,17 +94,24 @@ function UnforwardedSnackbar(

useSpokenMessage( spokenMessage, politeness );

// Only set up the timeout dismiss if we're not explicitly dismissing.
// The `onDismiss/onRemove` can have unstable references,
// trigger side-effect cleanup, and reset timers.
const callbackRefs = useRef( { onDismiss, onRemove } );
useLayoutEffect( () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for useLayoutEffect instead of useEffect ? In theory useEffects should be called in order, so we should be guaranteed that the onDimiss and onRemove refs are always up to date

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing specific. This is just a common pattern; we even have a useLatestRef hook, but I don't use here because returned value isn't recognized as Ref and linter complains.

Related: https://epicreact.dev/the-latest-ref-pattern-in-react/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the link, super interesting read!

I took a look back at this article by Kent that I usually refer to when talking useEffect and useLayoutEffect, I noticed the "special case" section too!

callbackRefs.current = { onDismiss, onRemove };
} );

useEffect( () => {
// Only set up the timeout dismiss if we're not explicitly dismissing.
const timeoutHandle = setTimeout( () => {
if ( ! explicitDismiss ) {
onDismiss?.();
onRemove?.();
callbackRefs.current.onDismiss?.();
callbackRefs.current.onRemove?.();
}
}, NOTICE_TIMEOUT );

return () => clearTimeout( timeoutHandle );
}, [ onDismiss, onRemove, explicitDismiss ] );
}, [ explicitDismiss ] );

const classes = classnames( className, 'components-snackbar', {
'components-snackbar-explicit-dismiss': !! explicitDismiss,
Expand Down
Loading