-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Adding Toasts #17030
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
Adding Toasts #17030
Changes from all commits
fd6b892
21e7f1c
d354e47
06807c8
d53ed76
5c89a14
e4b0694
a101b4a
7f818d2
cecadda
f1f18b9
ace6350
1b2be0c
8266edd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import classNames from "classnames"; | ||
import { FC, useCallback, useEffect, useRef } from "react"; | ||
import { useId } from "../../hooks/useId"; | ||
import { ToastEntry } from "./reducer"; | ||
|
||
type Props = ToastEntry & { | ||
onRemove: (id: string) => void; | ||
}; | ||
|
||
export const Toast: FC<Props> = ({ id, message, duration = 5000, autoHide = true, onRemove }) => { | ||
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. suggestion: Can we keep the toast shown if the user hovers the mouse over it? 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. Nice idea, definitely! |
||
const elId = useId(); | ||
const hideTimeout = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
||
const handleRemove = useCallback( | ||
(e) => { | ||
e.preventDefault(); | ||
|
||
onRemove(id); | ||
}, | ||
[id, onRemove], | ||
); | ||
|
||
useEffect(() => { | ||
if (!autoHide) { | ||
return; | ||
} | ||
|
||
hideTimeout.current = setTimeout(() => { | ||
onRemove(id); | ||
}, duration); | ||
|
||
return () => { | ||
if (hideTimeout.current) { | ||
clearTimeout(hideTimeout.current); | ||
} | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const onMouseEnter = useCallback(() => { | ||
if (hideTimeout.current) { | ||
clearTimeout(hideTimeout.current); | ||
} | ||
}, []); | ||
|
||
const onMouseLeave = useCallback(() => { | ||
if (!autoHide) { | ||
return; | ||
} | ||
|
||
if (hideTimeout.current) { | ||
clearTimeout(hideTimeout.current); | ||
} | ||
|
||
hideTimeout.current = setTimeout(() => { | ||
onRemove(id); | ||
}, duration); | ||
}, [autoHide, duration, id, onRemove]); | ||
|
||
return ( | ||
<div | ||
className={classNames( | ||
"relative flex justify-between items-center", | ||
"w-full md:w-96 max-w-full", | ||
"p-4 md:rounded-md", | ||
"bg-gray-800 dark:bg-gray-100", | ||
"text-white dark:text-gray-800", | ||
"transition-transform animate-toast-in-right", | ||
)} | ||
onMouseEnter={onMouseEnter} | ||
onMouseLeave={onMouseLeave} | ||
role="alert" | ||
aria-labelledby={elId} | ||
> | ||
<p className="text-white dark:text-gray-800" id={elId}> | ||
{message} | ||
</p> | ||
<button | ||
className={classNames( | ||
"cursor-pointer p-2", | ||
"bg-transparent hover:bg-transparent", | ||
"text-white hover:text-gray-300 dark:text-gray-800 dark:hover:text-gray-600", | ||
)} | ||
onClick={handleRemove} | ||
> | ||
<svg version="1.1" width="10px" height="10px" viewBox="0 0 100 100"> | ||
<line x1="0" y1="0" x2="100" y2="100" stroke="currentColor" strokeWidth="20" /> | ||
<line x1="0" y1="100" x2="100" y2="0" stroke="currentColor" strokeWidth="20" /> | ||
</svg> | ||
</button> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||
/** | ||||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||||
* Licensed under the GNU Affero General Public License (AGPL). | ||||
* See License.AGPL.txt in the project root for license information. | ||||
*/ | ||||
|
||||
import classNames from "classnames"; | ||||
import { createContext, FC, memo, useCallback, useContext, useMemo, useReducer } from "react"; | ||||
import { Portal } from "react-portal"; | ||||
import { ToastEntry, toastReducer } from "./reducer"; | ||||
import { Toast } from "./Toast"; | ||||
|
||||
type ToastFnProps = string | (Pick<ToastEntry, "message"> & Partial<ToastEntry>); | ||||
|
||||
const ToastContext = createContext<{ | ||||
toast: (toast: ToastFnProps, opts?: Partial<ToastEntry>) => void; | ||||
}>({ | ||||
toast: () => undefined, | ||||
}); | ||||
|
||||
export const useToast = () => { | ||||
return useContext(ToastContext); | ||||
}; | ||||
|
||||
export const ToastContextProvider: FC = ({ children }) => { | ||||
const [toasts, dispatch] = useReducer(toastReducer, []); | ||||
|
||||
const removeToast = useCallback((id) => { | ||||
dispatch({ type: "remove", id }); | ||||
}, []); | ||||
|
||||
const addToast = useCallback((message: ToastFnProps, opts = {}) => { | ||||
let newToast: ToastEntry = { | ||||
...(typeof message === "string" | ||||
? { | ||||
id: `${Math.random()}`, | ||||
message, | ||||
} | ||||
: { | ||||
id: `${Math.random()}`, | ||||
...message, | ||||
}), | ||||
...opts, | ||||
}; | ||||
|
||||
dispatch({ type: "add", toast: newToast }); | ||||
}, []); | ||||
|
||||
const ctxValue = useMemo(() => ({ toast: addToast }), [addToast]); | ||||
|
||||
return ( | ||||
<ToastContext.Provider value={ctxValue}> | ||||
{children} | ||||
<ToastsList toasts={toasts} onRemove={removeToast} /> | ||||
</ToastContext.Provider> | ||||
); | ||||
}; | ||||
|
||||
type ToastsListProps = { | ||||
toasts: ToastEntry[]; | ||||
onRemove: (id: string) => void; | ||||
}; | ||||
const ToastsList: FC<ToastsListProps> = memo(({ toasts, onRemove }) => { | ||||
return ( | ||||
<Portal> | ||||
<div | ||||
className={classNames( | ||||
"fixed box-border space-y-2", | ||||
"w-full md:w-auto", | ||||
"bottom-0 md:bottom-2 right-0 md:right-2", | ||||
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. thought: Skipping the center for the toasts sounds ok. 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. question: What do you think moving the alignment to the left so that toast notifications show up in the bottom left corner matching the LTR (left-to-right) default writing direction we're using in the dashboard, avoid splitting information across the edges of the screen, etc. Thoughts?
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. I'm fine with that, no real attachment to where it's at right now. |
||||
)} | ||||
tabIndex={-1} | ||||
role="region" | ||||
aria-label="Notifications" | ||||
> | ||||
{toasts.map((toast) => { | ||||
return <Toast key={toast.id} {...toast} onRemove={onRemove} />; | ||||
})} | ||||
</div> | ||||
</Portal> | ||||
); | ||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
export type ToastEntry = { | ||
id: string; | ||
message: string; | ||
duration?: number; | ||
autoHide?: boolean; | ||
}; | ||
|
||
type ToastAction = | ||
| { | ||
type: "add"; | ||
toast: ToastEntry; | ||
} | ||
| { | ||
type: "remove"; | ||
id: string; | ||
}; | ||
export const toastReducer = (state: ToastEntry[], action: ToastAction) => { | ||
if (action.type === "add") { | ||
return [...state, action.toast]; | ||
} | ||
|
||
if (action.type === "remove") { | ||
return state.filter((toast) => toast.id !== action.id); | ||
} | ||
|
||
return state; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Excellent choice, 5 seconds. ✨