-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Implement Toast pause #27811
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 5 commits into
microsoft:master
from
ling1726:react-toast/feat/pause-toast
May 11, 2023
Merged
feat: Implement Toast pause #27811
Changes from 1 commit
Commits
Show all changes
5 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
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
43 changes: 35 additions & 8 deletions
43
packages/react-components/react-toast/src/state/useToast.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,18 +1,45 @@ | ||
| import * as React from 'react'; | ||
| import { Toast } from './vanilla/toast'; | ||
| import { useForceUpdate } from '@fluentui/react-utilities'; | ||
| import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
| import { Toast } from './vanilla/toast'; | ||
| import { ValidatedToastOptions } from './types'; | ||
|
|
||
| export function useToast() { | ||
| export function useToast(options: ValidatedToastOptions) { | ||
| const forceRender = useForceUpdate(); | ||
| const { targetDocument } = useFluent(); | ||
| const [toast] = React.useState(() => { | ||
| const newToast = new Toast(); | ||
| newToast.onUpdate = forceRender; | ||
| return newToast; | ||
| if (targetDocument) { | ||
| const newToast = new Toast(targetDocument, options); | ||
| newToast.onUpdate = forceRender; | ||
| return newToast; | ||
| } | ||
| }); | ||
|
|
||
| const toastRef = React.useCallback( | ||
| (el: HTMLElement | null) => { | ||
| if (el && toast) { | ||
| toast.setToastElement(el); | ||
| } | ||
| }, | ||
| [toast], | ||
| ); | ||
|
|
||
| const play = React.useCallback(() => { | ||
| if (toast) { | ||
| toast.play(); | ||
| } | ||
| }, [toast]); | ||
|
|
||
| const pause = React.useCallback(() => { | ||
| if (toast) { | ||
| toast.pause(); | ||
| } | ||
| }, [toast]); | ||
|
|
||
| return { | ||
| play: toast.play, | ||
| pause: toast.pause, | ||
| running: toast.running, | ||
| toastRef, | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| play, | ||
| pause, | ||
| running: toast?.running ?? false, | ||
| }; | ||
ling1726 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
57 changes: 51 additions & 6 deletions
57
packages/react-components/react-toast/src/state/vanilla/toast.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,13 +1,58 @@ | ||
| import { ValidatedToastOptions } from '../types'; | ||
|
|
||
| // TODO convert to closure | ||
| export class Toast { | ||
| public running = false; | ||
| public onUpdate: () => void = () => null; | ||
| public running: boolean; | ||
| public onUpdate: () => void; | ||
| private targetDocument: Document; | ||
| private toastElement?: HTMLElement; | ||
| private pauseOnWindowBlur: ValidatedToastOptions['pauseOnWindowBlur']; | ||
| private pauseOnHover: ValidatedToastOptions['pauseOnHover']; | ||
|
|
||
| public play() { | ||
| this.running = true; | ||
| constructor(targetDocument: Document, options: ValidatedToastOptions) { | ||
| this.running = false; | ||
| this.onUpdate = () => null; | ||
| this.targetDocument = targetDocument; | ||
|
|
||
| const { pauseOnHover, pauseOnWindowBlur } = options; | ||
| this.pauseOnHover = pauseOnHover; | ||
| this.pauseOnWindowBlur = pauseOnWindowBlur; | ||
|
|
||
| if (this.pauseOnWindowBlur) { | ||
| this.targetDocument.defaultView?.addEventListener('focus', this.play); | ||
| this.targetDocument.defaultView?.addEventListener('blur', this.pause); | ||
| } | ||
ling1726 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public pause() { | ||
| this.running = false; | ||
| public dispose() { | ||
| if (this.pauseOnWindowBlur) { | ||
| this.targetDocument.defaultView?.removeEventListener('focus', this.play); | ||
| this.targetDocument.defaultView?.removeEventListener('blur', this.pause); | ||
| } | ||
|
|
||
| if (this.toastElement && this.pauseOnHover) { | ||
| this.toastElement.addEventListener('mouseenter', this.pause); | ||
| this.toastElement.addEventListener('mouseleave', this.play); | ||
| } | ||
|
|
||
| this.toastElement = undefined; | ||
| } | ||
|
|
||
| public setToastElement = (element: HTMLElement) => { | ||
| this.toastElement = element; | ||
| if (this.pauseOnHover) { | ||
| this.toastElement.addEventListener('mouseenter', this.pause); | ||
| this.toastElement.addEventListener('mouseleave', this.play); | ||
| } | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| public play = () => { | ||
| this.running = true; | ||
| this.onUpdate(); | ||
| }; | ||
|
|
||
| public pause = () => { | ||
| this.running = false; | ||
| this.onUpdate(); | ||
| }; | ||
| } | ||
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
14 changes: 14 additions & 0 deletions
14
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
|
|
||
| export const PauseOnHover = () => { | ||
| const { dispatchToast } = useToastController(); | ||
| const notify = () => dispatchToast('Hover me!', { pauseOnHover: true }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <button onClick={notify}>Make toast</button> | ||
| </> | ||
| ); | ||
| }; |
14 changes: 14 additions & 0 deletions
14
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import * as React from 'react'; | ||
| import { Toaster, useToastController } from '@fluentui/react-toast'; | ||
|
|
||
| export const PauseOnWindowBlur = () => { | ||
| const { dispatchToast } = useToastController(); | ||
| const notify = () => dispatchToast('Click on another window!', { pauseOnWindowBlur: true }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Toaster /> | ||
| <button onClick={notify}>Make toast</button> | ||
| </> | ||
| ); | ||
| }; |
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.