Published on

The Ninja Guide To How To Create A Antd Notification With Tailwind CSS Better

antd notification

As a FrontEnd technology blogger, you must be familiar with Ant Design, a popular UI library for React applications. Ant Design provides a wide range of UI components, including the Notification component. However, the default styling of the Ant Design Notification component may not always fit your project's design requirements. This is where Tailwind CSS comes in handy. In this article, we will explore how to create a customized Ant Design Notification component with Tailwind CSS.

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined classes to style your HTML elements. Unlike other CSS frameworks, Tailwind CSS does not come with pre-designed components. Instead, it provides a set of low-level utility classes that you can use to create your own custom components. Tailwind CSS is highly customizable, and you can configure it to match your project's design requirements.

The description of antd notification ui component

The Ant Design Notification component is a UI element that displays a message to the user. The Notification component can be used to display success messages, error messages, or any other type of message that you want to communicate to the user. The Ant Design Notification component is highly customizable, and you can configure it to match your project's design requirements.

Why use Tailwind CSS to create a antd notification ui component?

Tailwind CSS provides a set of pre-defined utility classes that you can use to style your Ant Design Notification component. Using Tailwind CSS, you can create a customized Notification component that matches your project's design requirements. Tailwind CSS is highly customizable, and you can configure it to match your project's color scheme, typography, and other design elements.

The preview of antd notification ui component.

To give you an idea of what a customized Ant Design Notification component looks like, here is a preview:

Free download of the antd notification's source code

The source code of antd notification ui component.

Here is the source code for the customized Ant Design Notification component:

<!-- notificatoin box 1 -->
<div class="notification-box m-8 mx-auto rounded-lg bg-white p-4 shadow-lg">
  <div class="pb-2 text-sm">
    Notification Title
    <span class="float-right">
      <svg
        class="fill-current text-gray-600"
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 24 24"
        width="22"
        height="22"
      >
        <path
          class="heroicon-ui"
          d="M16.24 14.83a1 1 0 0 1-1.41 1.41L12 13.41l-2.83 2.83a1 1 0 0 1-1.41-1.41L10.59 12 7.76 9.17a1 1 0 0 1 1.41-1.41L12 10.59l2.83-2.83a1 1 0 0 1 1.41 1.41L13.41 12l2.83 2.83z"
        />
      </svg>
    </span>
  </div>
  <div class="text-sm tracking-tight  text-gray-600 ">
    I will never close automatically. This is a purposely very very long description that has many
    many characters and words.
  </div>
</div>
<!-- notificatoin box 2 -->
<div class="notification-box m-8 mx-auto flex rounded-lg bg-white p-4 shadow-lg">
  <div class="pr-2">
    <svg
      class="fill-current text-green-600"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      width="22"
      height="22"
    >
      <path
        class="heroicon-ui"
        d="M12 22a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm-3.54-4.46a1 1 0 0 1 1.42-1.42 3 3 0 0 0 4.24 0 1 1 0 0 1 1.42 1.42 5 5 0 0 1-7.08 0zM9 11a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm6 0a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"
      />
    </svg>
  </div>
  <div>
    <div class="pb-2 text-sm">
      Notification Title
      <span class="float-right">
        <svg
          class="fill-current text-gray-600"
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 24 24"
          width="22"
          height="22"
        >
          <path
            class="heroicon-ui"
            d="M16.24 14.83a1 1 0 0 1-1.41 1.41L12 13.41l-2.83 2.83a1 1 0 0 1-1.41-1.41L10.59 12 7.76 9.17a1 1 0 0 1 1.41-1.41L12 10.59l2.83-2.83a1 1 0 0 1 1.41 1.41L13.41 12l2.83 2.83z"
          />
        </svg>
      </span>
    </div>
    <div class="text-sm tracking-tight  text-gray-600 ">
      I will never close automatically. This is a purposely very very long description that has many
      many characters and words.
    </div>
  </div>
</div>
<!-- notificatoin box 3 -->
<div class="notification-box m-8 mx-auto flex rounded-lg bg-white p-4 shadow-lg">
  <div class="pr-2">
    <svg
      class="fill-current text-red-600"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      width="24"
      height="24"
    >
      <path
        class="heroicon-ui"
        d="M12 22a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm-3.54-4.54a5 5 0 0 1 7.08 0 1 1 0 0 1-1.42 1.42 3 3 0 0 0-4.24 0 1 1 0 0 1-1.42-1.42zM9 11a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm6 0a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"
      />
    </svg>
  </div>
  <div>
    <div class="pb-2 text-sm">
      Notification Title
      <span class="float-right">
        <svg
          class="fill-current text-gray-600"
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 24 24"
          width="22"
          height="22"
        >
          <path
            class="heroicon-ui"
            d="M16.24 14.83a1 1 0 0 1-1.41 1.41L12 13.41l-2.83 2.83a1 1 0 0 1-1.41-1.41L10.59 12 7.76 9.17a1 1 0 0 1 1.41-1.41L12 10.59l2.83-2.83a1 1 0 0 1 1.41 1.41L13.41 12l2.83 2.83z"
          />
        </svg>
      </span>
    </div>
    <div class="text-sm tracking-tight  text-gray-600 ">
      I will never close automatically. This is a purposely very very long description that has many
      many characters and words.
    </div>
  </div>
</div>
<!-- notificatoin box 4 -->
<div class="notification-box m-8 mx-auto flex rounded-lg bg-white p-4 shadow-lg">
  <div class="pr-2">
    <svg
      class="fill-current text-orange-600"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      width="24"
      height="24"
    >
      <path
        class="heroicon-ui"
        d="M12 2a10 10 0 1 1 0 20 10 10 0 0 1 0-20zm0 2a8 8 0 1 0 0 16 8 8 0 0 0 0-16zm0 9a1 1 0 0 1-1-1V8a1 1 0 0 1 2 0v4a1 1 0 0 1-1 1zm0 4a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"
      />
    </svg>
  </div>
  <div>
    <div class="pb-2 text-sm">
      Notification Title
      <span class="float-right">
        <svg
          class="fill-current text-gray-600"
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 24 24"
          width="22"
          height="22"
        >
          <path
            class="heroicon-ui"
            d="M16.24 14.83a1 1 0 0 1-1.41 1.41L12 13.41l-2.83 2.83a1 1 0 0 1-1.41-1.41L10.59 12 7.76 9.17a1 1 0 0 1 1.41-1.41L12 10.59l2.83-2.83a1 1 0 0 1 1.41 1.41L13.41 12l2.83 2.83z"
          />
        </svg>
      </span>
    </div>
    <div class="text-sm tracking-tight  text-gray-600 ">
      I will never close automatically. This is a purposely very very long description that has many
      many characters and words.
    </div>
  </div>
</div>

<style>
  .notification-box {
    width: 20rem;
  }
</style>

How to create a antd notification with Tailwind CSS?

To create a customized Ant Design Notification component with Tailwind CSS, follow these steps:

  1. Install Ant Design and Tailwind CSS in your project:
npm install antd tailwindcss
  1. Import the Notification component from Ant Design:
import { notification } from 'antd';
  1. Define your custom styles using Tailwind CSS utility classes:
const customStyles = {
  success: 'bg-green-500 text-white',
  error: 'bg-red-500 text-white',
  warning: 'bg-yellow-500 text-white',
  info: 'bg-blue-500 text-white',
};

In this example, we define four custom styles for the Notification component: success, error, warning, and info. Each style is defined using Tailwind CSS utility classes.

  1. Define a function to display the Notification component:
const showNotification = (type, message) => {
  notification[type]({
    message: message,
    className: customStyles[type],
  });
};

This function takes two parameters: type and message. The type parameter specifies the type of message to display (success, error, warning, or info). The message parameter specifies the message to display.

  1. Call the showNotification function to display the Notification component:
showNotification('success', 'Your changes have been saved successfully.');

In this example, we call the showNotification function with the success type and a message.

Conclusion

Creating a customized Ant Design Notification component with Tailwind CSS is easy and straightforward. Tailwind CSS provides a set of pre-defined utility classes that you can use to style your Notification component. By following the steps outlined in this article, you can create a customized Notification component that matches your project's design requirements.