-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add slot name to amplify-toast
#6479
Comments
@nkint If you are open to getting a PR out I can work with you to get it merged in. |
@nkint In your case, I think adding |
Thanks @ashika01 for the answer Toast notification are really important part of the ui: feedback against user actions. Due to its importance is something really hard to just incorporate in a css variables and override it. Moreover, notifications can go throughout all my application (not only where amplify is involved). What about using some other library (in #2834 they told about react-notification-alert)? I have access to the error anyway, let's take React as example: React.useEffect(() => {
Auth.currentAuthenticatedUser()
.then((user) => updateUser(user))
.catch(() => console.log('No signed in user.'));
Hub.listen('auth', (data) => {
console.log({ data }); // <------------ here I have error payload anyway
switch (data.payload.event) {
case 'signIn':
return updateUser(data.payload.data);
case 'signOut':
return updateUser(null);
default:
return null;
}
});
}, []); And I can override |
@nkint Definitely a great use case I totally don't deny that. I can open up a slot for toast as a presentational component. But wanted to see if you have considered/understood the implication. Since overriding slot would mean you wont get the default ones anymore. I am looking into this. Any feedback on CSS based style improvements? (since not everyone would want to write their own notification component) I can try to bake those in a PR too. |
+1 |
+1. I am trying to figure out how to customize the toast. It looks like the Update: |
Two options seem appropriate:
|
Here's a very ugly workaround until a better solution is proposed. The idea is to catch error messages and display them ourselves in our own custom Toast.
Here's the css in case anyone's interested:
N.B. If your goal is NOT to create your own Toast but rather to just overwrite the error messages, then this is the way to go:
|
@hrhosni |
@1443658 Note that I ended up going for the My objective was not to create my own toast but rather to overwrite the error messages. |
I have solved the problem. It should listen to Here is the code :
On hiding the default Toast, I've changed to use interval to loop on it instead of just once, so that it is still safe to set a shorter waiting time (Not sure whether it has significant improvement though).
|
so hacky but so far it's the best way I guess. |
I found setting the message to undefined prevents the default Toast from showing:
|
You can now prevent the default See this simple example from #7129 for how to implement your own notification/alert component in react (typescript): import React, { useState, useEffect } from 'react';
import { Hub, HubCallback } from '@aws-amplify/core';
import {
AmplifyAuthenticator,
} from '@aws-amplify/ui-react';
import {
UI_AUTH_CHANNEL, TOAST_AUTH_ERROR_EVENT
} from '@aws-amplify/ui-components';
const MyAuth: React.FC = ({ children }) => {
const [alertMessage, setAlertMessage] = useState('');
const handleToastErrors: HubCallback = ({ payload }) => {
if (payload.event === TOAST_AUTH_ERROR_EVENT && payload.message) {
setAlertMessage(payload.message);
}
};
useEffect(() => {
Hub.listen(UI_AUTH_CHANNEL, handleToastErrors);
return () => Hub.remove(UI_AUTH_CHANNEL, handleToastErrors);
});
return (
<>
{alertMessage && (<div>{alertMessage}</div>)}
<AmplifyAuthenticator hideToast>
// ...........
</AmplifyAuthenticator>
</>
);
} You can just replace For anyone wondering why this ended up as a |
Closing as #7129 resolves this issue. Please feel free to ask any questions. Thanks again, @Luke-Davies! |
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
Is your feature request related to a problem? Please describe.
I'd like to add custom style to toast notification. I've seen other issue about this, like #5604 #6167 #2834
Describe the solution you'd like
Fully customize toast notification via
slot
as other ui components do.I think it's matter of few lines in this file: https://github.com/aws-amplify/amplify-js/blob/3642e6af455ee1848c1cc743a7a503e96baefa41/packages/amplify-ui-components/src/components/amplify-toast/amplify-toast.tsx
Could you consider a PR?
The text was updated successfully, but these errors were encountered: