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: Allow limiting the number of maximum visible Snackbars #58559

Merged
merged 2 commits into from
Feb 1, 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.
Jump to
Jump to file
Failed to load files.
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 @@ -5,6 +5,7 @@
### 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)).

### Bug Fix

Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/snackbar/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ 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 @@ -76,7 +78,7 @@ export function SnackbarList( {
<div className={ className } tabIndex={ -1 } ref={ listRef }>
{ children }
<AnimatePresence>
{ notices.map( ( notice ) => {
{ visibleNotices.map( ( notice ) => {
const { content, ...restNotice } = notice;

return (
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/snackbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type SnackbarListProps = {
content: string;
}
>;
maxVisible?: number;
onRemove: ( id: string ) => void;
children?: NoticeChildren | Array< NoticeChildren >;
};
1 change: 1 addition & 0 deletions packages/edit-widgets/src/components/notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Notices() {
/>
<SnackbarList
notices={ snackbarNotices }
maxVisible={ 3 }
className="edit-widgets-notices__snackbar"
onRemove={ removeNotice }
/>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/editor-snackbars/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function EditorSnackbars() {
return (
<SnackbarList
notices={ snackbarNotices }
maxVisible={ 3 }
className="components-editor-notices__snackbar"
onRemove={ removeNotice }
/>
Expand Down