-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Update existing toasts #27827
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/update-toast
May 12, 2023
Merged
feat: Update existing toasts #27827
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export const EVENTS = { | ||
| show: 'fui-toast-show', | ||
| dismiss: 'fui-toast-dismiss', | ||
| 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
51 changes: 28 additions & 23 deletions
51
packages/react-components/react-toast/src/state/useToastController.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,31 +1,36 @@ | ||
| import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
| import { dispatchToast as dispatchToastVanilla, dismissToast as dismissToastVanilla } from './vanilla'; | ||
| import * as React from 'react'; | ||
| import { ToastId, ToastOptions } from './types'; | ||
| import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
| import { | ||
| dispatchToast as dispatchToastVanilla, | ||
| dismissToast as dismissToastVanilla, | ||
| updateToast as updateToastVanilla, | ||
| } from './vanilla'; | ||
| import { ToastId, ToastOptions, UpdateToastEventDetail } from './types'; | ||
|
|
||
| const noop = () => undefined; | ||
|
|
||
| export function useToastController() { | ||
| const { targetDocument } = useFluent(); | ||
|
|
||
| const dispatchToast = React.useCallback( | ||
| (content: React.ReactNode, options?: Partial<ToastOptions>) => { | ||
| if (targetDocument) { | ||
| dispatchToastVanilla(content, options, targetDocument); | ||
| } | ||
| }, | ||
| [targetDocument], | ||
| ); | ||
| return React.useMemo(() => { | ||
| if (!targetDocument) { | ||
| return { | ||
| dispatchToast: noop, | ||
| dismissToast: noop, | ||
| updateToast: noop, | ||
| }; | ||
| } | ||
|
|
||
| const dismissToast = React.useCallback( | ||
| (toastId?: ToastId) => { | ||
| if (targetDocument) { | ||
| return { | ||
| dispatchToast: (content: React.ReactNode, options?: Partial<ToastOptions>) => { | ||
| dispatchToastVanilla(content, options, targetDocument); | ||
| }, | ||
| dismissToast: (toastId?: Partial<ToastId>) => { | ||
| dismissToastVanilla(toastId, targetDocument); | ||
| } | ||
| }, | ||
| [targetDocument], | ||
| ); | ||
|
|
||
| return { | ||
| dispatchToast, | ||
| dismissToast, | ||
| }; | ||
| }, | ||
| updateToast: (options: UpdateToastEventDetail) => { | ||
| updateToastVanilla(options, targetDocument); | ||
| }, | ||
| }; | ||
| }, [targetDocument]); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from './dispatchToast'; | ||
| export * from './dismissToast'; | ||
| export * from './updateToast'; | ||
| export * from './toast'; | ||
| export * from './toaster'; | ||
| export * from './getPositionStyles'; |
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
12 changes: 12 additions & 0 deletions
12
packages/react-components/react-toast/src/state/vanilla/updateToast.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,12 @@ | ||
| import { UpdateToastEventDetail } from '../types'; | ||
| import { EVENTS } from '../constants'; | ||
|
|
||
| export function updateToast(options: UpdateToastEventDetail, targetDocument: Document) { | ||
| const event = new CustomEvent<UpdateToastEventDetail>(EVENTS.update, { | ||
| bubbles: false, | ||
| cancelable: false, | ||
| detail: options, | ||
| }); | ||
|
|
||
| targetDocument.dispatchEvent(event); | ||
| } |
16 changes: 16 additions & 0 deletions
16
packages/react-components/react-toast/stories/Toast/UpdateToast.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,16 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController, ToastId } from '@fluentui/react-toast'; | ||
|
|
||
| export const UpdateToast = () => { | ||
| const { dispatchToast, updateToast } = useToastController(); | ||
| const notify = (id: ToastId) => dispatchToast('This toast never closes', { toastId: id, timeout: -1 }); | ||
| const update = (id: ToastId) => updateToast({ content: 'This toast will close soon', toastId: id, timeout: 1000 }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <button onClick={() => notify('EXAMPLE_ID')}>Make toast</button> | ||
| <button onClick={() => update('EXAMPLE_ID')}>Update toast</button> | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </> | ||
| ); | ||
| }; | ||
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
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.