-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Implement support for multiple toaster on a page #27831
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
Merged
ling1726
merged 3 commits into
microsoft:master
from
ling1726:react-toast/feat/toaster-id
May 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export const EVENTS = { | ||
| show: 'fui-toast-show', | ||
| dismiss: 'fui-toast-dismiss', | ||
| dismissAll: 'fui-toast-dismiss-all', | ||
| update: 'fui-toast-update', | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/react-components/react-toast/src/state/vanilla/dismissAllToasts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { EVENTS } from '../constants'; | ||
| import { DismissAllToastsEventDetail, ToasterId } from '../types'; | ||
|
|
||
| export function dismissAllToasts(toasterId: ToasterId | undefined = undefined, targetDocument: Document) { | ||
| const event = new CustomEvent<DismissAllToastsEventDetail>(EVENTS.dismissAll, { | ||
| bubbles: false, | ||
| cancelable: false, | ||
| detail: { toasterId }, | ||
| }); | ||
| targetDocument.dispatchEvent(event); | ||
| } |
6 changes: 3 additions & 3 deletions
6
packages/react-components/react-toast/src/state/vanilla/dismissToast.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| import { EVENTS } from '../constants'; | ||
| import { DismissToastEventDetail, ToastId } from '../types'; | ||
| import { DismissToastEventDetail, ToastId, ToasterId } from '../types'; | ||
|
|
||
| export function dismissToast(toastId: ToastId | undefined = undefined, targetDocument: Document) { | ||
| export function dismissToast(toastId: ToastId, toasterId: ToasterId | undefined = undefined, targetDocument: Document) { | ||
| const event = new CustomEvent<DismissToastEventDetail>(EVENTS.dismiss, { | ||
| bubbles: false, | ||
| cancelable: false, | ||
| detail: { toastId }, | ||
| detail: { toastId, toasterId }, | ||
| }); | ||
| targetDocument.dispatchEvent(event); | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/react-components/react-toast/src/state/vanilla/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/react-components/react-toast/stories/Toast/CustomTimeout.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/react-components/react-toast/stories/Toast/Default.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
| import { useId } from '@fluentui/react-components'; | ||
|
|
||
| export const Default = () => { | ||
| const toasterId = useId('toaster'); | ||
| const { dispatchToast } = useToastController(); | ||
| const notify = () => dispatchToast('This is a toast'); | ||
| const notify = () => dispatchToast('This is a toast', { toasterId }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <Toaster toasterId={toasterId} /> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is usage of |
||
| <button onClick={notify}>Make toast</button> | ||
| </> | ||
| ); | ||
|
|
||
6 changes: 4 additions & 2 deletions
6
packages/react-components/react-toast/stories/Toast/DefaultToastOptions.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 8 additions & 5 deletions
13
packages/react-components/react-toast/stories/Toast/DismissAll.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
| import { useId } from '@fluentui/react-components'; | ||
|
|
||
| export const DismissAll = () => { | ||
| const { dispatchToast, dismissToast } = useToastController(); | ||
| const notify = () => dispatchToast('This is a toast'); | ||
| const toasterId = useId('toaster'); | ||
| const { dispatchToast, dismissAllToasts } = useToastController(); | ||
| const notify = () => dispatchToast('This is a toast', { toasterId }); | ||
| const dismissAll = () => dismissAllToasts(toasterId); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <button onClick={() => notify()}>Make toast</button> | ||
| <button onClick={() => dismissToast()}>Dismiss all</button> | ||
| <Toaster toasterId={toasterId} /> | ||
| <button onClick={notify}>Make toast</button> | ||
| <button onClick={dismissAll}>Dismiss all</button> | ||
| </> | ||
| ); | ||
| }; |
14 changes: 9 additions & 5 deletions
14
packages/react-components/react-toast/stories/Toast/DismissToast.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController, ToastId } from '@fluentui/react-toast'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
| import { useId } from '@fluentui/react-components'; | ||
|
|
||
| export const DismissToast = () => { | ||
| const toasterId = useId('toaster'); | ||
| const toastId = useId('example'); | ||
| const { dispatchToast, dismissToast } = useToastController(); | ||
| const notify = (id: ToastId) => dispatchToast('This is a toast', { toastId: id }); | ||
| const notify = () => dispatchToast('This is a toast', { toastId, toasterId }); | ||
| const dismiss = () => dismissToast(toastId, toasterId); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <button onClick={() => notify('example')}>Make toast</button> | ||
| <button onClick={() => dismissToast('example')}>Dismiss all</button> | ||
| <Toaster toasterId={toasterId} /> | ||
| <button onClick={notify}>Make toast</button> | ||
| <button onClick={dismiss}>Dismiss toast</button> | ||
| </> | ||
| ); | ||
| }; |
20 changes: 20 additions & 0 deletions
20
packages/react-components/react-toast/stories/Toast/MultipleToasters.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
| import { useId } from '@fluentui/react-components'; | ||
|
|
||
| export const MultipeToasters = () => { | ||
| const first = useId('toaster-1'); | ||
| const second = useId('toaster-2'); | ||
| const { dispatchToast } = useToastController(); | ||
| const notifyFirst = () => dispatchToast('Toaster first', { toasterId: first }); | ||
| const notifySecond = () => dispatchToast('Toaster second', { toasterId: second }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster toasterId={first} position="bottom-right" /> | ||
| <Toaster toasterId={second} position="top-right" /> | ||
| <button onClick={notifyFirst}>Toaster first</button> | ||
| <button onClick={notifySecond}>Toaster second</button> | ||
| </> | ||
| ); | ||
| }; |
6 changes: 4 additions & 2 deletions
6
packages/react-components/react-toast/stories/Toast/PauseOnHover.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/react-components/react-toast/stories/Toast/PauseOnWindowBlur.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.