Skip to main content

Lesson 7 of 8

Mini Project: Design System Component Library

Build a mini design system using compound components, polymorphic types, and Storybook documentation. Includes a Card component (compound), a polymorphic Text component, a Toast notification system, and a Storybook story for each.

60 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Toast Notification System - Compound Compound Pattern

Toast Notification System - Compound Compound Pattern
import { createContext, useContext, useState, useCallback, useId, type ReactNode } from 'react';

// ---- Types ----
type ToastType = 'success' | 'error' | 'warning' | 'info';
interface Toast { id: string; message: string; type: ToastType; duration: number; }

// ---- Context ----
interface ToastContextValue {
  toasts: Toast[];
  show: (message: string, type?: ToastType, duration?: number) => string;
  dismiss: (id: string) => void;
}

const ToastContext = createContext<ToastContextValue | null>(null);

function useToast() {
  const ctx = useContext(ToastContext);
  if (!ctx) throw new Error('useToast must be inside <ToastProvider>');
  return ctx;
}

// ---- Provider ----
function ToastProvider({ children }: { children: ReactNode }) {
  const [toasts, setToasts] = useState<Toast[]>([]);

  const show = useCallback((message: string, type: ToastType = 'info', duration = 4000): string => {
    const id = crypto.randomUUID();
    setToasts(prev => [...prev, { id, message, type, duration }]);

    if (duration > 0) {
      setTimeout(() => dismiss(id), duration);
    }
    return id;
  }, []);

  const dismiss = useCallback((id: string) => {
    setToasts(prev => prev.filter(t => t.id !== id));
  }, []);

  return (
    <ToastContext.Provider value={{ toasts, show, dismiss }}>
      {children}
      <ToastContainer />
    </ToastContext.Provider>
  );
}

// ---- Toast Container - renders all active toasts ----
const TOAST_ICONS: Record<ToastType, string> = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' };
const TOAST_COLORS: Record<ToastType, string> = {
  success: '#064e3b', error: '#450a0a', warning: '#451a03', info: '#1e3a5f',
};
const TOAST_BORDER: Record<ToastType, string> = {
  success: '#10b981', error: '#ef4444', warning: '#f59e0b', info: '#229c6b',
};

function ToastContainer() {
  const { toasts, dismiss } = useToast();
  return (
    <div
      aria-live="polite"
      aria-label="Notifications"
      style={{ position: 'fixed', bottom: 24, right: 24, display: 'flex', flexDirection: 'column', gap: 8, zIndex: 9999, maxWidth: 380 }}
    >
      {toasts.map(toast => (
        <div
          key={toast.id}
          role="alert"
          style={{
            display: 'flex', alignItems: 'center', gap: 12, padding: '12px 16px',
            borderRadius: 10, background: TOAST_COLORS[toast.type],
            border: `1px solid ${TOAST_BORDER[toast.type]}`,
            color: 'white', fontSize: 14, fontFamily: 'system-ui',
            animation: 'slideIn 0.2s ease',
            boxShadow: '0 4px 12px rgba(0,0,0,0.3)',
          }}
        >
          <span>{TOAST_ICONS[toast.type]}</span>
          <span style={{ flex: 1 }}>{toast.message}</span>
          <button
            onClick={() => dismiss(toast.id)}
            aria-label="Dismiss"
            style={{ background: 'transparent', border: 'none', color: 'rgba(255,255,255,0.6)', cursor: 'pointer', fontSize: 16, padding: 0 }}
          >
            ✕
          </button>
        </div>
      ))}
    </div>
  );
}

// ---- Demo usage ----
function ToastDemo() {
  const toast = useToast();
  return (
    <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', fontFamily: 'system-ui' }}>
      {(['success', 'error', 'warning', 'info'] as ToastType[]).map(type => (
        <button
          key={type}
          onClick={() => toast.show(`This is a ${type} notification!`, type)}
          style={{
            padding: '10px 20px', borderRadius: 8, border: 'none',
            background: TOAST_BORDER[type], color: 'white',
            cursor: 'pointer', fontWeight: 600, textTransform: 'capitalize',
          }}
        >
          {type}
        </button>
      ))}
      <button
        onClick={() => {
          const id = toast.show('This one stays until dismissed', 'info', 0);
        }}
        style={{ padding: '10px 20px', borderRadius: 8, border: '1px solid #334155', background: 'transparent', color: 'white', cursor: 'pointer', fontWeight: 600 }}
      >
        Persistent
      </button>
    </div>
  );
}

export { ToastProvider, useToast, ToastDemo };

Tip

Tip

Practice Mini Project Design System Component Library in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Mini Project: Design System Component Library

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Design System Component Library from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Design System Component Library is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done