Comments (12)
Thanks for the input. This definitely something that should be possible, but isn't right now.
Would an API like this work for you?
// ⚠️ Draft, not an actual API
import { createCustomToaster } from 'react-hot-toast'
const { useToaster, Toaster, toast } = createCustomToaster([ "info", "custom" ])
<Toaster
toastOptions={{
info: {
icon: "💡"
}
}}
/>
toast.info("Did you know...")
from react-hot-toast.
hey guys 👋🏻
any progress on that feature or maybe simple
workaround? I have similar issue for warning
variant and it doesn't seem too intuitive to use e.g. toast('This is warning message');
by default
from react-hot-toast.
Hi, has there been any progress on this, it would be really useful just to be able to create a warning variant type and similar?
from react-hot-toast.
One thing I'd like to tack on here, is that in my current project, I have a similar need for custom variants, but I'm also having extra props being passed through the ToastOptions as well to support our Alert
component. It works pretty well, though I had to recreate the toast
function and do type casting to make typescript not complain.
from react-hot-toast.
I basically had to wrap the original toast function calls and create my own to be able to expand the type definitions to include the props we wanted.
Then I followed https://react-hot-toast.com/docs/use-toaster to some extent, although I ended up referring back to the original code in the github for the Toaster
component. I made my own Toaster
component that rendered my custom Alerts. It was a lot of work, but it's for a component library, so not much to be done about that.
// These are the statuses allowed in the Alert Component
type AlertStatus = 'error'|'success'|'info'|'warning'|'loading';
export interface Toast {
type: AlertStatus,
// other normal Toast options
duration?: number;
role: 'status' | 'alert';
ariaLive: 'assertive' | 'off' | 'polite';
//extra props meant for the Alert component we are rendering
solid?: boolean
}
export type ToastOptions = Partial<
Omit<Toast, 'height' | 'message' | 'pauseDuration' | 'visible'>
>;
export type DefaultToastOptions = ToastOptions &
{
[key in AlertStatus]?: ToastOptions;
};
export type ToastMessage = ValueOrFunction<Renderable, Toast>;
type ToastHandler = (message: ToastMessage, options?: ToastOptions) => string;
const createHandler = (type: AlertStatus) =>
((message, options) =>
// This is needed unfortunately due to types being different despite working
ogToast(
message as ValueOrFunction<Renderable, OgToast>,
{
...options,
type,
} as OriginalToastOptions,
)) as ToastHandler;
const toast = (...args: Parameters<ToastHandler>): string =>
ogToast(...(args as Parameters<typeof ogToast>));
toast.error = createHandler('error');
toast.success = createHandler('success');
toast.info = createHandler('info');
toast.warning = createHandler('warning');
toast.loading = createHandler('loading');
toast.dismiss = ogToast.dismiss;
toast.remove = ogToast.remove;
from react-hot-toast.
I think that you can do something like this:
import { toast as handler } from 'react-hot-toast'
toast.info = msg => handler(msg, {
style: {}
});
export { toast }
from react-hot-toast.
@mverissimo I'd like to achieve slightly different result. Using useToaster
hook, I'd create my own notifications with headless mode and have custom toast variants. Documentation.
from react-hot-toast.
Yep, this looks good to me! It would be great if I can just pass custom toast variant to the Toaster
as in your example, without creating completely custom component with headless mode.
from react-hot-toast.
The benefit of the proposed method is, that it also adds the toast.custom() to the toast function. Also it makes all options type-safe with auto-complete.
from react-hot-toast.
Yeah, that's awesome. I really like this method.
from react-hot-toast.
@ZachHaber do you mind expanding on how you setup a custom Alert
toast? I am looking to add one as well
from react-hot-toast.
@ZachHaber thanks for the detailed example! I think I may try to hold out for v2 and just style the default toast as an alert for now. Not sure when v2 is planned to release though
from react-hot-toast.
Related Issues (20)
- Custom / Position HOT 1
- Property 'document' doesn't exist || Cannot read property 'useToaster' of undefined. React Native HOT 2
- Toaster gives Unhandled RunTime Error on Nextjs HOT 4
- Toasts not firing (help). HOT 2
- How can i pass dynamic variable to toast.promise loading? HOT 1
- Allow customizing slide up/down animation
- Allow customizing exit animation when user manually dismisses toast HOT 2
- toast.loading() not working with nextjs 14 HOT 3
- Pause on Hover does not work with Headless UI HOT 1
- Dismiss toast by matching toastIds with regex
- Custom output direction HOT 1
- Support loading with "mask"
- Element type Error when using with nextjs and redux-toolkit
- Accessibility
- (Suggestion) Enhancing Web Accessibility
- Add delay to toast
- Feature Requests for warning and max toast
- Toast persists after page navigation HOT 1
- Support popover api
- Dismiss a headless toast? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hot-toast.