Skip to content
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

Designed a Toast Component #70

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

ritwik-69
Copy link
Contributor

@ritwik-69 ritwik-69 commented Jun 5, 2024

This commit creates the toast component with customizable position,duration and color.

fixes:#68

Description

Created the toast component with customizable position,duration and color.To use you need use the custom hook useToast()
and then toast.open(message,duration,position,color) to get the toast at that desired position.

Motivation and Context

[Explain the motivation behind these changes and provide any relevant context.]

How to Test

[Describe the steps to test the changes made in this pull request.]

Related Issues

[If applicable, mention any related issues or pull requests.]

Checklist

  • I have tested these changes locally.
  • I have reviewed the code and ensured it follows the project's coding guidelines.
  • I have updated the documentation, if necessary.
  • I have assigned reviewers to this pull request.

Screenshots (if applicable)

[If your changes include any visual updates, provide screenshots here.]

toast img

toast.vid.1.mp4

Copy link

vercel bot commented Jun 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
multiplayer-uno ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 6:56am

Copy link
Collaborator

@kuv2707 kuv2707 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Left some suggestions.
Also appreciate the formatted commit message.
(But do mention that the App component is wrapped by the toast provider)

import './toast.css';

function useTimeout(callback: () => void, duration: number) {
const savedCallback = useRef(callback);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using useCallback would simplify the code and would make lexical sense. It would also help get rid of the useEffect.

In most cases though, there won't be any benefit of caching the callbacks since they will mostly contain state setters, which update on every re-render, thus re-rendering the entire toast component. But we'll cache nonetheless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 i did not implement this suggestion as i didn't understand how we could have avoided the use useEffect effectively and i was concerned that it might break the code . Nonetheless this still works and its implementation can be done in the future

return () => {
clearTimeout(functionId);
};
}, []);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please populate the array to get rid of the eslint error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 done with this

close: (id: number) => void;
};

export const ToastContext = createContext<ToastContextValue | null>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The | null causes the return value of useToast to be ToastContextValue | null, thus making us check for the null case wherever we use it. We can just use empty functions as default values for open and close.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 done with this too


function closeToast(id: number) {
setToasts((prevToasts) =>
prevToasts.filter((toast) => toast.id !== id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes the toast to abruptly disappear. We'd want a fade-out animation or sth of that sort. You can put the filter part inside a useTimeout, and just add a class for fade-out animation. This will cause the component to fade out, and after that the timeout kicks in, removing it entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 checkout the closing animation video

const contextValue = useMemo(
() => ({
open: openToast,
close: closeToast,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to have some default values, like INFO, ERROR etc. for the toast, so that we don't always have to pass in the colors etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 the color parameter in the open function accepts only 4 values : info,error,warning and success not any particular color.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed it now, looking closely at the screenshot.
In that case you can change the type of color to be a union of these 4 strings.
That way, it would be apparent by looking at the type itself, and we most probably won't want to customize the color anyway.

To further improve usability, you can take the arguments in an object, so that when using the open function, we only specify the values we wish to change without being concerned about the order, and use the defaults for the rest.(You can choose the defaults to be {"",5 seconds, center, info}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuv2707 done with this too

@kuv2707 kuv2707 added area: Frontend (structure) Issues related to the react frontend area: Frontend (design) under review There have been some rounds of review on the code changes in the PR. labels Jun 6, 2024
@kuv2707
Copy link
Collaborator

kuv2707 commented Jun 6, 2024

@ritwik-69 This can be merged once the minor changes suggested are applied.

This commit creates the toast component with customizable position,duration
and color.
In main.tsx wrapped the app component in toastcontextprovider so that
toast can be used everywhere in the app.

fixes:shivansh-bhatnagar18#68
@ritwik-69
Copy link
Contributor Author

closing animation

closing.animation.1.1.mp4

@kuv2707
Copy link
Collaborator

kuv2707 commented Jun 7, 2024

There are a few polishing issues with the implementation, but I'll fix them myself.
Merging, thanks for all the work @ritwik-69 .

@kuv2707 kuv2707 merged commit 8dc5449 into shivansh-bhatnagar18:master Jun 7, 2024
8 checks passed
@kuv2707 kuv2707 linked an issue Jun 7, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Frontend (design) area: Frontend (structure) Issues related to the react frontend under review There have been some rounds of review on the code changes in the PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Design a Toast Component.
2 participants