Skip to content

Commit

Permalink
Editor: Limit visible Snackbars from the consumers (#58598)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent 05d5475 commit edb8779
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

- `Button`: Update secondary variant to show the border even in a disabled state. ([#58606](https://github.com/WordPress/gutenberg/pull/58606)).
- `ConfirmDialog`: Add `__next40pxDefaultSize` to buttons ([#58421](https://github.com/WordPress/gutenberg/pull/58421)).
- `SnackbarList`: Allow limiting the number of maximum visible Snackbars ([#58559](https://github.com/WordPress/gutenberg/pull/58559)).
- `Snackbar`: Update the warning message ([#58591](https://github.com/WordPress/gutenberg/pull/58591)).
- `Composite`: Implementing `useCompositeState` with Ariakit ([#57304](https://github.com/WordPress/gutenberg/pull/57304))

Expand Down
4 changes: 1 addition & 3 deletions packages/components/src/snackbar/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ const SNACKBAR_VARIANTS = {
*/
export function SnackbarList( {
notices,
maxVisible,
className,
children,
onRemove,
}: WordPressComponentProps< SnackbarListProps, 'div' > ) {
const listRef = useRef< HTMLDivElement | null >( null );
const isReducedMotion = useReducedMotion();
const visibleNotices = maxVisible ? notices.slice( -maxVisible ) : notices;
className = classnames( 'components-snackbar-list', className );
const removeNotice =
( notice: SnackbarListProps[ 'notices' ][ number ] ) => () =>
Expand All @@ -78,7 +76,7 @@ export function SnackbarList( {
<div className={ className } tabIndex={ -1 } ref={ listRef }>
{ children }
<AnimatePresence>
{ visibleNotices.map( ( notice ) => {
{ notices.map( ( notice ) => {
const { content, ...restNotice } = notice;

return (
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/snackbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export type SnackbarListProps = {
content: string;
}
>;
maxVisible?: number;
onRemove: ( id: string ) => void;
children?: NoticeChildren | Array< NoticeChildren >;
};
10 changes: 6 additions & 4 deletions packages/edit-widgets/src/components/notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { NoticeList, SnackbarList } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

// Last three notices. Slices from the tail end of the list.
const MAX_VISIBLE_NOTICES = -3;

function Notices() {
const { removeNotice } = useDispatch( noticesStore );
const { notices } = useSelect( ( select ) => {
Expand All @@ -19,9 +22,9 @@ function Notices() {
const nonDismissibleNotices = notices.filter(
( { isDismissible, type } ) => ! isDismissible && type === 'default'
);
const snackbarNotices = notices.filter(
( { type } ) => type === 'snackbar'
);
const snackbarNotices = notices
.filter( ( { type } ) => type === 'snackbar' )
.slice( MAX_VISIBLE_NOTICES );

return (
<>
Expand All @@ -36,7 +39,6 @@ function Notices() {
/>
<SnackbarList
notices={ snackbarNotices }
maxVisible={ 3 }
className="edit-widgets-notices__snackbar"
onRemove={ removeNotice }
/>
Expand Down
10 changes: 6 additions & 4 deletions packages/editor/src/components/editor-snackbars/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import { SnackbarList } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

// Last three notices. Slices from the tail end of the list.
const MAX_VISIBLE_NOTICES = -3;

export default function EditorSnackbars() {
const notices = useSelect(
( select ) => select( noticesStore ).getNotices(),
[]
);
const { removeNotice } = useDispatch( noticesStore );
const snackbarNotices = notices.filter(
( { type } ) => type === 'snackbar'
);
const snackbarNotices = notices
.filter( ( { type } ) => type === 'snackbar' )
.slice( MAX_VISIBLE_NOTICES );

return (
<SnackbarList
notices={ snackbarNotices }
maxVisible={ 3 }
className="components-editor-notices__snackbar"
onRemove={ removeNotice }
/>
Expand Down

0 comments on commit edb8779

Please sign in to comment.