2.0.2 • Published 4 months ago

react-confirmy v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

React Confirmy

A beautiful, customizable, and accessible confirmation dialog component for React applications with built-in support for Tailwind CSS and Bootstrap.

Confirmation Dialog Preview

⚠️ Important Version Notice

Version 2.0.0 introduces significant improvements and breaking changes. We strongly recommend upgrading to version 2.0.0 as version 1.x.x will no longer receive updates or support.

Why Upgrade?

  • Improved styling system with better customization
  • Enhanced dark mode support
  • Better TypeScript support
  • New features including custom icons and form fields
  • Improved positioning system
  • Better framework compatibility (Tailwind, etc.)

Features

  • 🎨 Beautiful, modern design with dark mode support
  • 🎯 Fully typed with TypeScript
  • 🎭 Multiple themes (Tailwind CSS and Bootstrap)
  • 📱 Responsive and mobile-friendly
  • 🎪 Smart positioning with Popper.js
  • ⌨️ Keyboard accessible with focus trap
  • 🎨 Highly customizable styles and animations
  • 🔧 Framework agnostic styling system
  • 🌙 Dark mode support
  • 📏 Multiple size variants (sm, md, lg)
  • 🎯 Custom positioning (top, bottom, left, right)
  • 🖼️ Built-in SVG icons with custom icon support
  • ✨ Multiple animation types (fade, scale, slide)
  • 📝 Form support with validation
  • ⏳ Async operations with loading states
  • 🔄 Dialog queuing system
  • 📦 Nested dialogs support
  • 🎯 Smart positioning with arrow indicators
  • 🔒 TypeScript strict mode support
  • 🎨 Custom animation keyframes support
  • 🌐 Framework-agnostic option

Installation

npm install react-confirmy

Quick Start

import { useRef } from 'react';
import { Confirmy, DialogProvider } from 'react-confirmy';

function App() {
  const buttonRef = useRef(null);
  const [isOpen, setIsOpen] = useState(false);

  return (
    <DialogProvider>
      <button ref={buttonRef} onClick={() => setIsOpen(true)}>
        Delete Item
      </button>

      <Confirmy
        isOpen={isOpen}
        onClose={() => setIsOpen(false)}
        onConfirm={() => {
          // Handle confirmation
          console.log('Confirmed!');
        }}
        triggerRef={buttonRef}
        type="danger"
        title="Delete Item"
        message="Are you sure you want to delete this item? This action cannot be undone."
      />
    </DialogProvider>
  );
}

Dependencies

Core Dependencies

  • react (^18.0.0): Core React library
  • react-dom (^18.0.0): React DOM rendering
  • @popperjs/core (^2.11.8): Handles positioning of the dialog

Framework Support

The library supports multiple CSS frameworks out of the box:

  • Tailwind CSS (default)
  • Bootstrap
  • Framework-agnostic option ('none')

Select your preferred framework using the framework prop:

<Confirmy framework="tailwind" /> // or "bootstrap" or "none"

Props

PropTypeDefaultDescription
isOpenbooleanrequiredControls the visibility of the dialog
onClose() => voidrequiredFunction called when the dialog should close
onConfirm(formData?: Record<string, any>) => void | PromiserequiredFunction called when the user confirms the action
triggerRefReact.RefObjectrequiredReference to the trigger element
titlestring'Confirm Action'Dialog title
messagestring'Are you sure you want to proceed?'Dialog message
confirmTextstring'Confirm'Text for the confirm button
cancelTextstring'Cancel'Text for the cancel button
typeDialogType'warning'Sets the dialog type ('danger' | 'warning' | 'info')
sizeDialogSize'md'Controls the dialog size ('sm' | 'md' | 'lg')
positionDialogPosition'top'Sets the dialog position ('top' | 'bottom' | 'left' | 'right')
frameworkDialogFramework'tailwind'CSS framework to use ('tailwind' | 'bootstrap' | 'none')
stylesPartial{}Custom styles override
classNamestring''Additional CSS classes
darkModebooleanfalseEnables dark mode
customIconReact.ComponentTypeundefinedCustom icon component
animationDialogAnimationConfig{ type: 'scale', duration: 200, timing: 'ease-out' }Animation configuration
zIndexnumber50Sets the z-index of the dialog
formFieldsDialogFormField[][]Form fields configuration
asyncOptionsDialogAsyncOptionsundefinedAsync operation configuration
stackOrdernumber0Order in the dialog stack
nestedbooleanfalseWhether this is a nested dialog
parentIdstringundefinedID of the parent dialog

Advanced Features

Animations

Configure custom animations with the animation prop:

<Confirmy
  animation={{
    type: 'fade', // 'fade' | 'scale' | 'slide' | 'none'
    duration: 200,
    timing: 'ease-out',
    customKeyframes: '' // Optional custom animation
  }}
  // ... other props
/>

Form Support

Add form fields to your dialog with validation:

const formFields = [
  {
    type: 'text',
    name: 'username',
    label: 'Username',
    required: true,
    placeholder: 'Enter username',
    validation: (value) => 
      value.length < 3 ? 'Username must be at least 3 characters' : undefined
  },
  {
    type: 'select',
    name: 'role',
    label: 'Role',
    options: [
      { label: 'Admin', value: 'admin' },
      { label: 'User', value: 'user' }
    ]
  },
  {
    type: 'checkbox',
    name: 'terms',
    label: 'Accept Terms',
    required: true
  }
];

<Confirmy
  formFields={formFields}
  onConfirm={(data) => {
    console.log('Form data:', data);
  }}
  // ... other props
/>

Async Operations

Handle async operations with loading and status states:

<Confirmy
  onConfirm={async () => {
    await someAsyncOperation();
  }}
  asyncOptions={{
    loadingText: 'Processing...',
    successText: 'Success!',
    errorText: 'Error occurred',
    showLoadingSpinner: true
  }}
  // ... other props
/>

Custom Icons

Use your own icons or the built-in SVG icons:

// Using built-in icons (default)
<Confirmy type="warning" />

// Using custom icon
const CustomIcon = ({ width = 24, height = 24, color = 'currentColor' }) => (
  <svg width={width} height={height} fill={color}>
    {/* Your SVG path */}
  </svg>
);

<Confirmy customIcon={CustomIcon} />

Dialog Queuing

Handle multiple dialogs with the queuing system:

import { useDialog } from 'react-confirmy';

function MyComponent() {
  const { addDialog } = useDialog();

  const showDialogs = () => {
    // First dialog
    addDialog({
      title: 'First Action',
      message: 'Confirm first action?',
      onConfirm: () => {
        // Second dialog will show after first is confirmed
        addDialog({
          title: 'Second Action',
          message: 'Proceed with second action?',
          type: 'warning'
        });
      }
    });
  };

  return <button onClick={showDialogs}>Start Process</button>;
}

TypeScript Support

The library is written in TypeScript and includes comprehensive type definitions. All components, hooks, and utilities are fully typed.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT