Skip to content

Commit ab53a4c

Browse files
authored
feat: useToastController should handle toasterId (#27902)
toasterId can no longer be used from the individual control functions such as `dispatchToast`. Instead, the useToastController should be called with the correct toasterId to return controls for a specific toaster. This should promote modular use of the API and encapsulate toaster behaviour better.
1 parent 1e46d2c commit ab53a4c

14 files changed

+38
-37
lines changed

packages/react-components/react-toast/etc/react-toast.api.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export type ToastOffset = Partial<Record<ToastPosition, ToastOffsetObject>> | To
1919
export type ToastPosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
2020

2121
// @public (undocumented)
22-
export function useToastController(): {
23-
dispatchToast: (content: React_2.ReactNode, options?: Partial<ToastOptions> | undefined) => void;
24-
dismissToast: (toastId: ToastId, toasterId?: string | undefined) => void;
25-
dismissAllToasts: (toasterId?: string | undefined) => void;
22+
export function useToastController(toasterId?: ToasterId): {
23+
dispatchToast: (content: React_2.ReactNode, options?: Partial<Omit<ToastOptions, "toasterId">> | undefined) => void;
24+
dismissToast: (toastId: ToastId) => void;
25+
dismissAllToasts: () => void;
2626
updateToast: (options: UpdateToastEventDetail) => void;
2727
};
2828

packages/react-components/react-toast/src/state/useToastController.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ToastId, ToastOptions, ToasterId, UpdateToastEventDetail } from './type
1010

1111
const noop = () => undefined;
1212

13-
export function useToastController() {
13+
export function useToastController(toasterId?: ToasterId) {
1414
const { targetDocument } = useFluent();
1515

1616
return React.useMemo(() => {
@@ -24,18 +24,18 @@ export function useToastController() {
2424
}
2525

2626
return {
27-
dispatchToast: (content: React.ReactNode, options?: Partial<ToastOptions>) => {
27+
dispatchToast: (content: React.ReactNode, options?: Partial<Omit<ToastOptions, 'toasterId'>>) => {
2828
dispatchToastVanilla(content, options, targetDocument);
2929
},
30-
dismissToast: (toastId: ToastId, toasterId?: ToasterId) => {
30+
dismissToast: (toastId: ToastId) => {
3131
dismissToastVanilla(toastId, toasterId, targetDocument);
3232
},
33-
dismissAllToasts: (toasterId?: ToasterId) => {
33+
dismissAllToasts: () => {
3434
dismissAllToastsVanilla(toasterId, targetDocument);
3535
},
3636
updateToast: (options: UpdateToastEventDetail) => {
3737
updateToastVanilla(options, targetDocument);
3838
},
3939
};
40-
}, [targetDocument]);
40+
}, [targetDocument, toasterId]);
4141
}

packages/react-components/react-toast/stories/Toast/CustomTimeout.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const CustomTimeout = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('This is a toast', { timeout: 1000, toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('This is a toast', { timeout: 1000 });
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/Default.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const Default = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('This is a toast', { toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('This is a toast');
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/DefaultToastOptions.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const DefaultToastOptions = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('This is a toast', { toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('This is a toast');
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/DismissAll.stories.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { useId } from '@fluentui/react-components';
44

55
export const DismissAll = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast, dismissAllToasts } = useToastController();
8-
const notify = () => dispatchToast('This is a toast', { toasterId });
9-
const dismissAll = () => dismissAllToasts(toasterId);
7+
const { dispatchToast, dismissAllToasts } = useToastController(toasterId);
8+
const notify = () => dispatchToast('This is a toast');
9+
const dismissAll = () => dismissAllToasts();
1010

1111
return (
1212
<>

packages/react-components/react-toast/stories/Toast/DismissToast.stories.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { useId } from '@fluentui/react-components';
55
export const DismissToast = () => {
66
const toasterId = useId('toaster');
77
const toastId = useId('example');
8-
const { dispatchToast, dismissToast } = useToastController();
9-
const notify = () => dispatchToast('This is a toast', { toastId, toasterId });
10-
const dismiss = () => dismissToast(toastId, toasterId);
8+
const { dispatchToast, dismissToast } = useToastController(toasterId);
9+
const notify = () => dispatchToast('This is a toast', { toastId });
10+
const dismiss = () => dismissToast(toastId);
1111

1212
return (
1313
<>

packages/react-components/react-toast/stories/Toast/MultipleToasters.stories.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { useId } from '@fluentui/react-components';
55
export const MultipeToasters = () => {
66
const first = useId('toaster-1');
77
const second = useId('toaster-2');
8-
const { dispatchToast } = useToastController();
9-
const notifyFirst = () => dispatchToast('Toaster first', { toasterId: first });
10-
const notifySecond = () => dispatchToast('Toaster second', { toasterId: second });
8+
const { dispatchToast: dispatchFirstToast } = useToastController(first);
9+
const { dispatchToast: dispatchSecondToast } = useToastController(second);
10+
const notifyFirst = () => dispatchFirstToast('Toaster first');
11+
const notifySecond = () => dispatchSecondToast('Toaster second');
1112

1213
return (
1314
<>

packages/react-components/react-toast/stories/Toast/Offset.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const Offset = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = (position: ToastPosition) => dispatchToast('This is a toast', { position, toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = (position: ToastPosition) => dispatchToast('This is a toast', { position });
99
const [horizontal, setHorizontal] = React.useState(10);
1010
const [vertical, setVertical] = React.useState(10);
1111

packages/react-components/react-toast/stories/Toast/PauseOnHover.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const PauseOnHover = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('Hover me!', { pauseOnHover: true, toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('Hover me!', { pauseOnHover: true });
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/PauseOnWindowBlur.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const PauseOnWindowBlur = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('Click on another window!', { pauseOnWindowBlur: true, toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('Click on another window!', { pauseOnWindowBlur: true });
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/ToastPositions.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const ToastPositions = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = (position: ToastPosition) => dispatchToast('This is a toast', { position, toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = (position: ToastPosition) => dispatchToast('This is a toast', { position });
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/ToasterLimit.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useId } from '@fluentui/react-components';
44

55
export const ToasterLimit = () => {
66
const toasterId = useId('toaster');
7-
const { dispatchToast } = useToastController();
8-
const notify = () => dispatchToast('This is a toast', { toasterId });
7+
const { dispatchToast } = useToastController(toasterId);
8+
const notify = () => dispatchToast('This is a toast');
99

1010
return (
1111
<>

packages/react-components/react-toast/stories/Toast/UpdateToast.stories.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { useId } from '@fluentui/react-components';
55
export const UpdateToast = () => {
66
const toasterId = useId('toaster');
77
const toastId = useId('example');
8-
const { dispatchToast, updateToast } = useToastController();
9-
const notify = () => dispatchToast('This toast never closes', { toastId, timeout: -1, toasterId });
10-
const update = () => updateToast({ content: 'This toast will close soon', toastId, timeout: 1000, toasterId });
8+
const { dispatchToast, updateToast } = useToastController(toasterId);
9+
const notify = () => dispatchToast('This toast never closes', { toastId, timeout: -1 });
10+
const update = () => updateToast({ content: 'This toast will close soon', toastId, timeout: 1000 });
1111

1212
return (
1313
<>

0 commit comments

Comments
 (0)