Skip to content

Commit

Permalink
update Snackbar to import types from Notice
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 committed Jan 17, 2023
1 parent 60d30f5 commit eb5fad8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 96 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/notice/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type NoticeActionWithOnClick = CommonNoticeActionProps & {

export type NoticeAction = NoticeActionWithURL | NoticeActionWithOnClick;

type NoticeChildren = string | JSX.Element;
export type NoticeChildren = string | JSX.Element;

export type NoticeProps = {
/**
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/snackbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import warning from '@wordpress/warning';
* Internal dependencies
*/
import Button from '../button';
import type { NoticeAction, SnackbarProps } from './types';
import type { SnackbarProps } from './types';
import type { NoticeAction } from '../notice/types';
import type { WordPressComponentProps } from '../ui/context';

const NOTICE_TIMEOUT = 10000;
Expand Down
6 changes: 4 additions & 2 deletions packages/components/src/snackbar/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
} from '../animation';
import type { Notice, SnackbarListProps } from './types';
import type { SnackbarListProps } from './types';
import type { WordPressComponentProps } from '../ui/context';

const SNACKBAR_VARIANTS = {
Expand Down Expand Up @@ -61,7 +61,9 @@ export function SnackbarList( {
const listRef = useRef< HTMLDivElement | null >( null );
const isReducedMotion = useReducedMotion();
className = classnames( 'components-snackbar-list', className );
const removeNotice = ( notice: Notice ) => () => onRemove?.( notice.id );
const removeNotice =
( notice: SnackbarListProps[ 'notices' ][ number ] ) => () =>
onRemove?.( notice.id );
return (
<div className={ className } tabIndex={ -1 } ref={ listRef }>
{ children }
Expand Down
110 changes: 18 additions & 92 deletions packages/components/src/snackbar/types.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,42 @@
/**
* External dependencies
*/
import type { MutableRefObject, ReactNode, SyntheticEvent } from 'react';
import type { MutableRefObject, ReactNode } from 'react';

export type NoticeActionWithURL = {
label: string;
url: string;
onClick?: ( event: SyntheticEvent ) => void;
};

type NoticeActionWithOnClick = {
label: string;
url?: string;
onClick: ( event: SyntheticEvent ) => void;
};

// TODO: move this type to the Notice component once it gets typed.
export type NoticeAction = NoticeActionWithURL | NoticeActionWithOnClick;

export type Notice = {
id: string;
spokenMessage: string;
actions: NoticeAction[];
icon?: ReactNode;
onDismiss?: () => void;
content: string;
isDismissible: boolean;
explicitDismiss: boolean;
};
/**
* Internal dependencies
*/
import type { NoticeProps, NoticeChildren } from '../notice/types';

export type SnackbarProps = {
/**
* The displayed message of a notice.
*
* Also used as the spoken message for assistive technology,
* unless `spokenMessage` is provided as an alternative message.
*/
children: string;
/**
* Used to provide a custom spoken message.
*
* @default children
*/
spokenMessage?: Notice[ 'spokenMessage' ];
/**
* A politeness level for the notice's spoken message. Should be provided as
* one of the valid options for an `aria-live` attribute value. Note that this
* value should be considered a suggestion; assistive technologies may
* override it based on internal heuristics.
*
* A value of `'assertive'` is to be used for important, and usually
* time-sensitive, information. It will interrupt anything else the screen
* reader is announcing in that moment.
* A value of `'polite'` is to be used for advisory information. It should
* not interrupt what the screen reader is announcing in that moment
* (the "speech queue") or interrupt the current task.
*
* @see https://www.w3.org/TR/wai-aria-1.1/#aria-live
*
* @default 'polite'
*/
politeness?: 'polite' | 'assertive';
/**
* An array of action objects.
*
* Each member object should contain
* a `label` and either a `url` link string or `onClick` callback function.
*
* @default []
*/
actions?: Notice[ 'actions' ];
/**
* Called to remove the snackbar from the UI.
*/
onRemove?: () => void;
type SnackbarOnlyProps = {
/**
* The icon to render in the snackbar.
*
* @default null
*/
icon?: Notice[ 'icon' ];
icon?: ReactNode;
/**
* Whether to require user action to dismiss the snackbar.
* By default, this is dismissed on a timeout, without user interaction.
*
* @default false
*/
explicitDismiss?: Notice[ 'explicitDismiss' ];
/**
* A callback executed when the snackbar is dismissed.
*
* It is distinct from onRemove, which _looks_ like a callback but is
* actually the function to call to remove the snackbar from the UI.
*/
onDismiss?: Notice[ 'onDismiss' ];
explicitDismiss?: boolean;
/**
* A ref to the list that contains the snackbar.
*/
listRef?: MutableRefObject< HTMLDivElement | null >;
};

export type SnackbarProps = NoticeProps & SnackbarOnlyProps;

export type SnackbarListProps = {
/**
* Array of notices to render.
*/
notices: Notice[];
/**
* Children to be rendered inside the notice list.
*/
children?: ReactNode;
/**
* Function called when a notice should be removed / dismissed.
*/
onRemove?: ( id: Notice[ 'id' ] ) => void;
notices: Array<
Omit< SnackbarProps, 'children' > & {
id: string;
content: string;
}
>;
onRemove: ( id: string ) => void;
children?: NoticeChildren | Array< NoticeChildren >;
};

0 comments on commit eb5fad8

Please sign in to comment.