react-hot-toast

snippet: react
2023.01.27
1๋ถ„

โœจ react-hot-toast

๐Ÿ”ฅ Hot by default
๐Ÿ”ฉ Easily Customizable
โณ Promise API - Automatic loader from a promise
๐Ÿ•Š Lightweight - less than 5kb including styles
โœ… Accessible
๐Ÿคฏ Headless Hooks


๊ธฐ๋ณธ ํ™œ์šฉ๋ฒ•

yarn add react-hot-toast
_app.tsx
import { AppProps } from 'next/app';
import { Toaster } from 'react-hot-toast';

export default function App({ Component, pageProps }: AppProps) {
  return (
    <div>
      <Component {...pageProps} />
      <Toaster
        toastOptions={{
          className: 'bg-gray-50 text-gray-700 dark:bg-gray-800 dark:text-gray-300',
          position: 'bottom-right',
        }}
      />
    </div>
  );
}
toast.success('successful');
toast.error('failed');

๋ฌด์ฒ™ ๊ฐ„ํŽธํ•˜๋‹ค!


์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ๋กœ undo ๊ธฐ๋Šฅ ์ถ”๊ฐ€

toast((action) => (
  <Toast
    title="Dark theme enabled"
    buttonText="Undo"
    action={() => {
      actionImpl.command?.history?.undo();
      toast.dismiss(action.id);

      toast((undoAction) => (
        <Toast
          title="Dark theme undone"
          buttonText="Redo"
          action={() => {
            actionImpl.command?.history?.redo();
            toast.dismiss(undoAction.id);
          }}
        />
      ));
    }}
  />
));
function Toast({
  title,
  buttonText,
  action,
}: {
  title: string;
  buttonText: string;
  action: () => void;
}) {
  return (
    <div className="flex items-center gap-4 text-sm">
      <span>{title}</span>
      <button
        onClick={action}
        className={$(
          'cursor-pointer rounded py-1 px-2',
          'bg-gray-150 active:bg-gray-200 dark:bg-gray-700 dark:active:bg-gray-600',
        )}
      >
        {buttonText}
      </button>
    </div>
  );
}