-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Implement state management for toasts #27800
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
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9e65c44
feat: Implement state management for toasts
ling1726 09e42f9
remove redundant import
ling1726 7e15b98
remove props
ling1726 0c9f61c
useToastFactory
ling1726 72d5cf1
review feedback
ling1726 aa53249
rename toast controls
ling1726 43664a5
don't create duplicate toasts
ling1726 56f4c84
make position styles vanilla
ling1726 686cac1
refactor Timer
ling1726 9c3566c
fix cruft
ling1726 815c30c
remove unnecessary callbacks
ling1726 d77f18c
stop leaking react
ling1726 ce1387c
update toaster
ling1726 8eed249
update useToaster
ling1726 6305979
update timer usage
ling1726 4a35e67
update timer usage
ling1726 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const rootMain = require('../../../../.storybook/main'); | ||
|
|
||
| module.exports = /** @type {Omit<import('../../../../.storybook/main'), 'typescript'|'babel'>} */ ({ | ||
| ...rootMain, | ||
| stories: [...rootMain.stories, '../stories/**/*.stories.mdx', '../stories/**/index.stories.@(ts|tsx)'], | ||
| addons: [...rootMain.addons], | ||
| webpackFinal: (config, options) => { | ||
| const localConfig = { ...rootMain.webpackFinal(config, options) }; | ||
|
|
||
| // add your own webpack tweaks if needed | ||
|
|
||
| return localConfig; | ||
| }, | ||
| }); |
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,7 @@ | ||
| import * as rootPreview from '../../../../.storybook/preview'; | ||
|
|
||
| /** @type {typeof rootPreview.decorators} */ | ||
| export const decorators = [...rootPreview.decorators]; | ||
|
|
||
| /** @type {typeof rootPreview.parameters} */ | ||
| export const parameters = { ...rootPreview.parameters }; |
10 changes: 10 additions & 0 deletions
10
packages/react-components/react-toast/.storybook/tsconfig.json
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,10 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "", | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "types": ["static-assets", "environment", "storybook__addons"] | ||
| }, | ||
| "include": ["../stories/**/*.stories.ts", "../stories/**/*.stories.tsx", "*.js"] | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
packages/react-components/react-toast/src/components/Timer/Timer.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,31 @@ | ||
| import * as React from 'react'; | ||
| import { useStyles } from './useTimerStyles.styles'; | ||
|
|
||
| export const Timer: React.FC<{ | ||
| running: boolean; | ||
| timeout: number; | ||
| onTimeout: () => void; | ||
| }> = props => { | ||
| const styles = useStyles(); | ||
| const { running, timeout, onTimeout } = props; | ||
| const ref = React.useRef<HTMLSpanElement>(null); | ||
|
|
||
| React.useEffect(() => { | ||
| if (ref.current) { | ||
| const timerElement = ref.current; | ||
| timerElement.addEventListener('animationend', onTimeout); | ||
| return () => timerElement.removeEventListener('animationend', onTimeout); | ||
| } | ||
| }, [onTimeout]); | ||
|
|
||
| const style: React.CSSProperties = { | ||
| animationDuration: `${timeout}ms`, | ||
| animationPlayState: running ? 'running' : 'paused', | ||
| }; | ||
|
|
||
| if (timeout < 0) { | ||
| return null; | ||
| } | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return <span ref={ref} style={style} className={styles.progress} />; | ||
| }; | ||
1 change: 1 addition & 0 deletions
1
packages/react-components/react-toast/src/components/Timer/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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './Timer'; |
14 changes: 14 additions & 0 deletions
14
packages/react-components/react-toast/src/components/Timer/useTimerStyles.styles.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,14 @@ | ||
| import { makeStyles } from '@griffel/react'; | ||
|
|
||
| export const useStyles = makeStyles({ | ||
| progress: { | ||
| animationName: { | ||
| from: { | ||
| opacity: 0, | ||
| }, | ||
| to: { | ||
| opacity: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
106 changes: 106 additions & 0 deletions
106
packages/react-components/react-toast/src/components/Toast.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,106 @@ | ||
| /** | ||
| * ⚠️ This is temporary and WILL be removed | ||
| */ | ||
|
|
||
| import * as React from 'react'; | ||
| import { Transition } from 'react-transition-group'; | ||
| import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities'; | ||
| import { makeStyles, mergeClasses, shorthands } from '@griffel/react'; | ||
| import { useToast, Toast as ToastProps } from '../state'; | ||
| import { Timer } from './Timer'; | ||
|
|
||
| const useStyles = makeStyles({ | ||
| toast: { | ||
| ...shorthands.border('2px', 'dashed', 'red'), | ||
| ...shorthands.padding('4px'), | ||
| display: 'flex', | ||
| minHeight: '40px', | ||
| maxHeight: '40px', | ||
| minWidth: '200px', | ||
| maxWidth: '200px', | ||
| alignItems: 'center', | ||
| backgroundColor: 'white', | ||
| }, | ||
|
|
||
| slide: { | ||
| animationDuration: '200ms, 400ms', | ||
| animationDelay: '0ms, 200ms', | ||
| animationName: [ | ||
| { | ||
| from: { | ||
| height: '0', | ||
| minHeight: '0', | ||
| maxHeight: '0', | ||
| opacity: 0, | ||
| }, | ||
| to: { | ||
| opacity: 0, | ||
| }, | ||
| }, | ||
| { | ||
| from: { | ||
| opacity: 0, | ||
| }, | ||
| to: { | ||
| opacity: 1, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
| fadeOut: { | ||
| animationDuration: '400ms, 200ms', | ||
| animationDelay: '0ms, 400ms', | ||
| animationName: [ | ||
| { | ||
| from: { | ||
| opacity: 1, | ||
| }, | ||
| to: { | ||
| opacity: 0, | ||
| }, | ||
| }, | ||
| { | ||
| from: { | ||
| opacity: 0, | ||
| }, | ||
| to: { | ||
| opacity: 0, | ||
| height: 0, | ||
| maxHeight: 0, | ||
| minHeight: 0, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| export const Toast: React.FC<Omit<ToastProps, 'content'> & { visible: boolean }> = props => { | ||
| const styles = useStyles(); | ||
| const { visible, children, close, remove, timeout } = props; | ||
| const { play, running } = useToast(); | ||
| const toastRef = React.useRef<HTMLDivElement>(null); | ||
|
|
||
| // start the toast once it's fully in | ||
| useIsomorphicLayoutEffect(() => { | ||
| if (toastRef.current) { | ||
| const toast = toastRef.current; | ||
| toast.addEventListener('animationend', play, { | ||
| once: true, | ||
| }); | ||
|
|
||
| return () => { | ||
| toast.removeEventListener('animationend', play); | ||
| }; | ||
| } | ||
| }, [play, toastRef]); | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <Transition in={visible} unmountOnExit mountOnEnter timeout={500} onExited={remove} nodeRef={toastRef}> | ||
| <div ref={toastRef} className={mergeClasses(styles.toast, visible && styles.slide, !visible && styles.fadeOut)}> | ||
| {children} | ||
| <Timer onTimeout={close} timeout={timeout ?? -1} running={running} /> | ||
| </div> | ||
| </Transition> | ||
| ); | ||
| }; | ||
45 changes: 45 additions & 0 deletions
45
packages/react-components/react-toast/src/components/Toaster.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,45 @@ | ||
| /** | ||
| * ⚠️ This is temporary and WILL be removed | ||
| */ | ||
|
|
||
| import * as React from 'react'; | ||
| import { Portal } from '@fluentui/react-portal'; | ||
| import { useToaster, getPositionStyles } from '../state'; | ||
| import { Toast } from './Toast'; | ||
| import { makeStyles, mergeClasses } from '@griffel/react'; | ||
|
|
||
| const useStyles = makeStyles({ | ||
| container: { | ||
| position: 'fixed', | ||
| }, | ||
| }); | ||
|
|
||
| export const Toaster: React.FC = () => { | ||
| const { getToastsToRender, isToastVisible } = useToaster(); | ||
|
|
||
| const styles = useStyles(); | ||
|
|
||
| return ( | ||
| <Portal> | ||
| <div> | ||
| {getToastsToRender((position, toasts) => { | ||
| return ( | ||
| <div key={position} style={getPositionStyles(position)} className={mergeClasses(styles.container)}> | ||
| {toasts.map(({ content, ...toastProps }) => { | ||
| return ( | ||
| <Toast | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {...toastProps} | ||
| key={`toast-${toastProps.toastId}`} | ||
ling1726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| visible={isToastVisible(toastProps.toastId)} | ||
| > | ||
| {content as React.ReactNode} | ||
| </Toast> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| })} | ||
| </div> | ||
| </Portal> | ||
| ); | ||
| }; | ||
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,2 +1,4 @@ | ||
| // TODO: replace with real exports | ||
| export {}; | ||
| export { Toaster } from './components/Toaster'; | ||
|
|
||
| export { useToastController } from './state'; | ||
| export type { ToastPosition } from './state'; |
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,3 @@ | ||
| export const EVENTS = { | ||
| show: 'fui-toast-show', | ||
| } 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './types'; | ||
| export * from './useToaster'; | ||
| export * from './useToast'; | ||
| export * from './useToastController'; | ||
| export { getPositionStyles } from './vanilla'; |
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,25 @@ | ||
| import { EVENTS } from './constants'; | ||
|
|
||
| export type ToastId = string; | ||
|
|
||
| export type ToastPosition = 'top-right' | 'top-center' | 'top-left' | 'bottom-right' | 'bottom-center' | 'bottom-left'; | ||
|
|
||
| export interface ToastOptions { | ||
| toastId?: ToastId; | ||
| position?: ToastPosition; | ||
| content?: unknown; | ||
| timeout?: number; | ||
| } | ||
|
|
||
| export interface Toast extends Required<Omit<ToastOptions, 'toasterId'>> { | ||
| close: () => void; | ||
| remove: () => void; | ||
| } | ||
|
|
||
| export interface ToastEventMap { | ||
| [EVENTS.show]: CustomEvent<ToastOptions>; | ||
| } | ||
|
|
||
| export type ToastEventListenerGeneric<K extends keyof ToastEventMap> = (e: ToastEventMap[K]) => void; | ||
| export type ToastShowEventListener = ToastEventListenerGeneric<typeof EVENTS.show>; | ||
| export type ToastEventListener = ToastShowEventListener; |
18 changes: 18 additions & 0 deletions
18
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import * as React from 'react'; | ||
| import { Toast } from './vanilla/toast'; | ||
| import { useForceUpdate } from '@fluentui/react-utilities'; | ||
|
|
||
| export function useToast() { | ||
| const forceRender = useForceUpdate(); | ||
| const [toast] = React.useState(() => { | ||
| const newToast = new Toast(); | ||
| newToast.onUpdate = forceRender; | ||
| return newToast; | ||
| }); | ||
|
|
||
| return { | ||
| play: toast.play, | ||
| pause: toast.pause, | ||
| running: toast.running, | ||
| }; | ||
| } |
21 changes: 21 additions & 0 deletions
21
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
| import { dispatchToast as dispatchToastVanilla } from './vanilla/dispatchToast'; | ||
| import * as React from 'react'; | ||
| import { ToastOptions } from './types'; | ||
|
|
||
| export function useToastController() { | ||
| const { targetDocument } = useFluent(); | ||
|
|
||
| const dispatchToast = React.useCallback( | ||
| (content: React.ReactNode, options?: ToastOptions) => { | ||
| if (targetDocument) { | ||
| dispatchToastVanilla(content, options, targetDocument); | ||
| } | ||
| }, | ||
| [targetDocument], | ||
| ); | ||
|
|
||
| return { | ||
| dispatchToast, | ||
| }; | ||
| } |
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.