1.0.2 • Published 9 months ago

dynamic-ajax-popup v1.0.2

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

Dynamic Ajax Popup

A flexible and customizable popup modal package that supports static content, form submission, and dynamic content loading via network requests. This package allows you to pass custom HTML for forms, handle AJAX-based form submissions, and execute callbacks on successful form submission.

Features

  • Customizable Popup Content: Define your own HTML for the popup's body, including forms, text, images, etc.
  • Form Submission via AJAX: Submit forms within the popup using AJAX without page reloads.
  • Callback Functionality: Execute a callback function on successful form submission.
  • Network Requests: Load popup content dynamically from a remote URL.
  • Custom Animations and Styling: Control the width, height, z-index, background colors, and open/close animations.

Installation

Using npm:

First, install the package via npm:

npm install dynamic-ajax-popup

Usage

Basic Popup with Static Content

To create a simple popup with static content:

import Popup from 'dynamic-ajax-popup';

// Create a new instance of Popup
const myPopup = new Popup({
  modalId: 'simplePopup',
  contentTitle: 'Hello World',
  contentText: 'This is a basic static popup.',
  width: '50%',
  height: '200px',
  zIndex: '10',
  modalBackgroundColor: 'rgba(0, 0, 0, 0.7)',
  contentBackgroundColor: '#fff',
  closeButtonColor: '#ff0000',
  openAnimation: 'fade',
});

// Trigger the popup when needed
document.getElementById('openPopupButton').onclick = () => {
  myPopup.showPopup();
};

In your HTML:

<button id="openPopupButton">Open Popup</button>

Popup with Form Submission

You can add a form to your popup and handle form submission via AJAX. A callback function can be passed to handle post-submission actions.

import Popup from 'dynamic-ajax-popup';

// Define a callback function to execute after successful form submission
function onFormSuccess(response) {
  console.log('Form submitted successfully:', response);
  alert('Thank you for submitting the form!');
}

// Create a popup with a custom form inside
const formPopup = new Popup({
  modalId: 'formPopup',
  contentTitle: 'Submit Your Details',
  formAction: 'https://your-api-url.com/submit',  // URL for form submission
  formMethod: 'POST',
  formHTML: \`
    <label for="name">Name</label>
    <input type="text" name="name" id="name" placeholder="Your Name" required>
    
    <label for="email">Email</label>
    <input type="email" name="email" id="email" placeholder="Your Email" required>
    
    <button type="submit">Submit</button>
  \`,
  onSuccess: onFormSuccess,  // Callback function after successful submission
  width: '60%',
  height: '300px',
  zIndex: '100',
  modalBackgroundColor: 'rgba(0, 0, 0, 0.7)',
  contentBackgroundColor: '#fff',
  closeButtonColor: '#ff0000',
  openAnimation: 'fade',
});

// Trigger the form popup
document.getElementById('openFormPopup').onclick = () => {
  formPopup.showPopup();
};

In your HTML:

<button id="openFormPopup">Open Form Popup</button>

How to Import CSS:

To style the popup correctly, you need to import the accompanying CSS file. You can do this either in your HTML file or in your JavaScript file (if your environment supports it).

Option 1: Import in HTML In your HTML file, include the following line in the section:

<link rel="stylesheet" href="./node_modules/dynamic-ajax-popup/src/popup.css">

Option 2: Import in JavaScript If your environment supports it (e.g., using a bundler like Webpack), you can also import the CSS directly in your JavaScript file:

import 'dynamic-ajax-popup/src/popup.css';

Popup with Network-Fetched Content

The popup can also load content dynamically from a network request.

import Popup from 'dynamic-ajax-popup';

// Create a popup that fetches content from a URL when opened
const networkPopup = new Popup({
  modalId: 'networkPopup',
  contentTitle: 'Loading...',
  contentText: 'Fetching content...',
  fetchUrl: 'https://jsonplaceholder.typicode.com/posts/1',  // Example API request
  width: '50%',
  height: '250px',
  zIndex: '10',
  modalBackgroundColor: 'rgba(0, 0, 0, 0.7)',
  contentBackgroundColor: '#fff',
  closeButtonColor: '#000',
  openAnimation: 'fade',
});

// Trigger the popup with network content
document.getElementById('openNetworkPopup').onclick = () => {
  networkPopup.showPopup();
};

In your HTML:

<button id="openNetworkPopup">Open Network Content Popup</button>

Popup Options

Common Options

OptionTypeDefaultDescription
modalIdString''The unique ID for the popup modal.
contentTitleString''The title of the popup.
contentTextString''Static content for the popup body.
formActionStringnullThe URL to which the form will be submitted.
formMethodString'POST'The HTTP method for the form submission.
formHTMLString''The custom HTML for the form inside the popup.
onSuccessFunctionnullA callback function triggered after successful form submission.
fetchUrlStringnullThe URL to fetch dynamic content from when the popup is opened.
widthString'80%'The width of the popup.
heightString'auto'The height of the popup.
zIndexString'1'The z-index of the popup modal.
modalBackgroundColorString'rgba(0, 0, 0, 0.5)'The background color of the modal overlay.
contentBackgroundColorString'#fff'The background color of the popup content.
closeButtonVisibleBooleantrueWhether the close button is visible.
closeButtonColorString'#aaa'The color of the close button.
overlayClickCloseBooleantrueWhether clicking the overlay closes the popup.
openAnimationStringnullAnimation for opening the popup (e.g., 'fade').

License

This project is licensed under the MIT License.

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago