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

Editor: Limit visible Snackbars from the consumers #58598

Merged
merged 1 commit 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: 0 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
### Enhancements

- `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)).

### Bug Fix
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
Loading