Skip to content

Commit

Permalink
optimize toast code
Browse files Browse the repository at this point in the history
  • Loading branch information
vineethasok committed Nov 13, 2023
1 parent e8a048a commit 6ed8eb8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/components/Toast/Toast.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Button, useToast, ToastProps } from "@/components";
import { Button, useCUIToast, ToastProps } from "@/components";
const ToastTrigger = (props: ToastProps) => {
const { createToast } = useToast();
const { createCUIToast } = useCUIToast();
return (
<Button
onClick={() => {
createToast(props);
createCUIToast(props);
}}
>
Create Toast
Expand Down
8 changes: 4 additions & 4 deletions src/components/Toast/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as RadixUIToast from "@radix-ui/react-toast";
import { Button, ButtonProps, Icon, IconButton, IconName } from "@/components";
import styled, { keyframes } from "styled-components";

interface ToastContextProps {
createToast: (toast: ToastProps) => void;
export interface ToastContextProps {
createCUIToast: (toast: ToastProps) => void;
}
export const ToastContext = createContext<ToastContextProps>({
createToast: () => null,
createCUIToast: () => null,
});

type ToastType = "danger" | "warning" | "default" | "success";
Expand Down Expand Up @@ -202,7 +202,7 @@ export const ToastProvider = ({ children, ...props }: RadixUIToast.ToastProps) =
}
};
const value = {
createToast: (toast: ToastProps) => {
createCUIToast: (toast: ToastProps): void => {
setToasts(currentToasts => {
const newMap = new Map(currentToasts);
newMap.set(toast?.id ?? String(Date.now()), toast);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext } from "react";
import { ToastContext } from "./Toast";
export const useToast = () => {
import { ToastContext, ToastContextProps } from "./Toast";

export const useCUIToast = (): ToastContextProps => {
const result = useContext(ToastContext);
if (!result) {
throw new Error("Context used outside of its Provider!");
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ export { TextField } from "./Input/TextField";
export { Title } from "./Typography/Title/Title";
export { Tooltip } from "./Tooltip/Tooltip";
export { default as ClickUIProvider } from "./ClickUIProvider/ClickUIProvider";
export { useToast } from "./Toast/useToast";
export { useCUIToast } from "./Toast/useCUIToast";
export { UserIcon as ProfileIcon } from "./icons/UserIcon";

0 comments on commit 6ed8eb8

Please sign in to comment.