Toaster

The Toaster is a wrapper for your app that provides a way to trigger popup messages using the toast function.

Importation

Import the component from pol-ui to use the Toaster element:

import { Toaster } from "pol-ui";

Default Toaster

Default Toaster

import { Toaster, toast, Button } from "pol-ui";
export const ToastComponent = () => {
  const createToast = () => {
    toast({
      title: "This is a toast",
      duration: 500000,
      action: {
        label: "Undo",
        onClick: () => {
          alert("Undo");
        },
      },
    });
  };
  return (
    <div className="flex flex-col gap-3">
      <p>Click the button to show a toast</p>
      <Button onClick={createToast}>Show toaster</Button>
      <Toaster {...args} />
    </div>
  );
};

Props

Toaster

Prop nameTypeDefaultDescription
invertbooleanfalseInverts the color of the toast.
theme'light' | 'dark' | 'system'undefinedSpecifies the theme for the toaster.
positionPositionundefinedSpecifies the position of the toaster on the screen.
hotkeystring[]undefinedSpecifies the hotkey(s) to trigger the toaster.
richColorsbooleanundefinedEnables rich colors for the toasts.
expandbooleanundefinedEnables expanding behavior for the toasts.
durationnumberundefinedSpecifies the duration for which the toasts will be displayed.
gapnumberundefinedSpecifies the gap between toasts.
visibleToastsnumberundefinedSpecifies the maximum number of visible toasts.
closeButtonbooleanundefinedEnables the close button for each toast.
toastOptionsToastOptionsundefinedSpecifies additional options for the toasts.
classNamestringundefinedSpecifies additional class names for the toaster.
styleCSSPropertiesundefinedSpecifies additional styles for the toaster.
offsetstring | numberundefinedSpecifies the offset for the toaster.
dir'rtl' | 'ltr' | 'auto'undefinedSpecifies the text direction for the toaster.
loadingIconReactNodeundefinedSpecifies the loading icon for the toaster.
containerAriaLabelstringundefinedSpecifies the ARIA label for the toaster container.
pauseWhenPageIsHiddenbooleanundefinedPauses toasts when the page is hidden.

Toast

Prop nameTypeDefaultDescription
themeToastThemeundefinedSpecifies the theme for the toast.
idnumber | stringundefinedUnique identifier for the toast.
titlestring | ReactNodeundefinedTitle of the toast.
typeToastTypesundefinedType of the toast.
iconReactNodeundefinedIcon to be displayed in the toast.
jsxReactNodeundefinedJSX content for the toast.
invertbooleanundefinedInverts the color of the toast.
closeButtonbooleanundefinedEnables the close button for the toast.
dismissiblebooleanundefinedAllows dismissing the toast manually.
descriptionReactNodeundefinedDescription content for the toast.
durationnumberundefinedDuration for which the toast will be displayed.
deletebooleanundefinedEnables deleting the toast.
importantbooleanundefinedMarks the toast as important.
action { label: ReactNode, onClick: (e) => void } undefinedAction button for the toast.
cancel{ label: ReactNode, onClick?: (event) => void }undefinedCancel button for the toast.
onDismiss(toast) => voidundefinedCallback when the toast is dismissed.
onAutoClose(toast) => voidundefinedCallback when the toast auto-closes.
promisePromiseTundefinedPromise associated with the toast.
cancelButtonStyleCSSPropertiesundefinedStyles for the cancel button.
actionButtonStyleCSSPropertiesundefinedStyles for the action button.
styleCSSPropertiesundefinedStyles for the toast.
classNamestringundefinedAdditional class names for the toast.
classNamesToastClassnamesundefinedClass names for different parts of the toast.
descriptionClassNamestringundefinedClass name for the toast description.
positionPositionundefinedPosition of the toast on the screen.

Examples

Some Toaster examples

Toaster Examples

import { Toaster, toast, Button } from "pol-ui";
export const ToastComponent = () => {
  return (
    <div className="flex gap-3">
      <Button
        onClick={() => {
          toast({
            title: "This is a toast",
          });
        }}
      >
        Normal Toast
      </Button>
      <Button
        onClick={() =>
          toast({
            title: "This is a toast",
            action: {
              label: "Undo",
              onClick: () => {
                alert("Undo");
              },
            },
          })
        }
      >
        Action Toast
      </Button>
      <Button
        color="success"
        onClick={() =>
          toast({ title: "This is a success toast", type: "success" })
        }
      >
        Success Toast
      </Button>
      <Button
        color="error"
        onClick={() => toast({ title: "This is a error toast", type: "error" })}
      >
        error Toast
      </Button>
      <Button
        color="info"
        onClick={() => toast({ title: "This is a info toast", type: "info" })}
      >
        info Toast
      </Button>
      <Button
        color="warning"
        onClick={() =>
          toast({ title: "This is a warning toast", type: "warning" })
        }
      >
        warning Toast
      </Button>
      <Button
        color="secondary"
        onClick={() =>
          toast({ title: "This toast is loading", type: "loading" })
        }
      >
        Loading Toast
      </Button>
      <Button
        color="secondary"
        onClick={() =>
          toast({
            title: "This toast is loading",
            description: "I am the description of the toast",
          })
        }
      >
        Description Toast
      </Button>
      <Button
        color="secondary"
        onClick={() => toast({ title: "Martian toast", icon: <TbAlien /> })}
      >
        Custom icon <TbAlien />
      </Button>
      <Button
        color="secondary"
        onClick={() =>
          toast({
            title: "I will autoclose in 5 seconds",
            onDismiss: (t) => {
              alert("You dismissed " + t.title);
            },
            onAutoClose: (t) => {
              alert("autoclosed " + t.title);
            },
          })
        }
      >
        Action on close Toast
      </Button>
 
      <Toaster {...args} />
    </div>
  );
};

Theme

To learn more about how to customize the appearance of components, please see the Theme docs.

interface ToastTheme {
  base: string;
  title: string;
  description: string;
  success: string;
  error: string;
  info: string;
  warning: string;
  loading: string;
  default: string;
}

Since March 1, 2024

Proudly Open Source
  • Npm

  • Github

  • Creator

  • Made with love and Pol-ui by Pol Gubau Amores