reboottime / React-Develpment

Some notes, thought, articles or source code readings aggregated here about React Web development.
0 stars 0 forks source link

[Mantine UI Series] Notification ( Systems) and Alert #24

Open reboottime opened 11 months ago

reboottime commented 11 months ago

Overview

This article talks about


Notification Vs Alert

At UI and accessibility level, the Notification and Alert components exhibit a striking similarity. However, their usage differs in the following ways:

reboottime commented 11 months ago

Notification System and its Application

Mantine UI's notification system operates as a toast solution, simplifying the process of programmatically.


Mantine UI notification system comprises three primary parts:

  1. The Notifications component, which serves as both a container for programmatically added notifications and a functional element used in notifications.
  2. The Notification component, responsible for holding notification information.
  3. A collection of helper functions are exposed to developers, offering developers the capability to manage notifications programmatically. Below, is a tailored toast utils built upon Mantine Notification system.


const toast = {
    error: (message: string, options?: NotificationProps) => {
        notifications.show({
            ...options,
            color: 'red',
            icon: <IconX size="1.1rem" />,
            message,
            title: 'Error'
        });
    },
    success: (message: string, options: NotificationProps) => {
        notifications.show({
            ...options,
            color: 'green',
            icon: <IconCheck size="1.1rem" />,
            message,
            title: 'Success'
        });
    },
    info: (message: string, options?: NotificationProps) => {
        notifications.show({
            ...options,
            color: 'cyan',
            icon: <IconInfoCircle size="1.1rem" />,
            message,
            title: 'Info'
        });
    }
};
reboottime commented 11 months ago

Learnings from Mantine UI Notification System

The Animation Effect





Manage Notifications Programmatically


A general solution

Bellow is the sample code:

export const NotificationProvider = ({children}) => {
   const [notifications, setNotifications] = useState([]);

   const value = useMemo({
     notifications,
     addNotification:  (notification) => setNotifications(prevNotifications => [prevNotifications, notification])
    }, [notifications]);

   return <NotificationProvider value={value}>{children}</NotificationProvider>
};

// How we use the Notification system

const App = () => {
  return <NotificationProvider>{/**some other code**/} </NotificationProvider>
};

While this approach is functional, the drawback are


The Novel Solution ( by Mantine UI)

  1. How Mantine Notifications System manages notifications limit in a timeframe.

Mantine UI notification system relies on use-queue to limit what notifications are should be rendered, and what notifications are waiting in the notification system's queue

const { state, queue, update, cleanQueue } = useQueue<NotificationProps>({
  initialValues: [],
  limit,
});
  1. How Mantine UI exposes its notification management helpers

Mantine Notification System employs window as the cross components communication channel for notifications management. Here are how it works under the hood


Following are some key source code screenshot



image


image


image
reboottime commented 11 months ago

References