### Install via npm Source: https://sweetalert2.github.io/ Command to install SweetAlert2 using npm. ```bash $ npm install sweetalert2 ``` -------------------------------- ### AJAX request example Source: https://sweetalert2.github.io/ Shows how to use preConfirm to fetch data from an API and display the result. ```javascript Swal.fire({ title: "Submit your GitHub username", input: "text", inputAttributes: { autocapitalize: "off" }, showCancelButton: true, confirmButtonText: "Look up", showLoaderOnConfirm: true, preConfirm: async (login) => { try { const githubUrl = ` https://api.github.com/users/${login} `; const response = await fetch(githubUrl); if (!response.ok) return Swal.showValidationMessage(` ${JSON.stringify(await response.json())} `); return response.json(); } catch (error) { Swal.showValidationMessage(` Request failed: ${error} `); } }, allowOutsideClick: () => !Swal.isLoading() }).then((result) => { if (result.isConfirmed) Swal.fire({ title: `${result.value.login}'s avatar`, imageUrl: result.value.avatar_url }); }); ``` -------------------------------- ### Install SweetAlert2 Laravel package via Composer Source: https://sweetalert2.github.io/recipe-gallery/sweetalert2-laravel.html Run this command in your Laravel project root to install the sweetalert2/laravel package. ```bash $ composer require sweetalert2/laravel ``` -------------------------------- ### Swal.getPopup() Source: https://sweetalert2.github.io/ Gets the popup element. ```APIDOC ## Swal.getPopup() ### Description Get the popup. ### Method Swal.getPopup() ### Parameters None ### Returns DOM element - The popup element. ``` -------------------------------- ### SweetAlert2 multiple inputs example Source: https://sweetalert2.github.io/ Demonstrates how to create multiple inputs using the html parameter and preConfirm to return the values as an array. ```javascript const { value: formValues } = await Swal.fire({ title: "Multiple inputs", html: ` `, focusConfirm: false, preConfirm: () => { return [document.getElementById("swal-input1").value, document.getElementById("swal-input2").value]; } }); if (formValues) Swal.fire(JSON.stringify(formValues)); ``` -------------------------------- ### SweetAlert2 file input example Source: https://sweetalert2.github.io/ Prompts for an image file, reads it as a data URL, and displays the uploaded picture. ```javascript const { value: file } = await Swal.fire({ title: "Select image", input: "file", inputAttributes: { "accept": "image/*", "aria-label": "Upload your profile picture" } }); if (file) { const reader = new FileReader(); reader.onload = (e) => { Swal.fire({ title: "Your uploaded picture", imageUrl: e.target.result, imageAlt: "The uploaded picture" }); }; reader.readAsDataURL(file); } ``` -------------------------------- ### SweetAlert2 url input example Source: https://sweetalert2.github.io/ Prompts for a URL with a placeholder and displays the entered URL. ```javascript const { value: url } = await Swal.fire({ input: "url", inputLabel: "URL address", inputPlaceholder: "Enter the URL" }); if (url) Swal.fire(`Entered URL: ${url}`); ``` -------------------------------- ### Swal.getImage() Source: https://sweetalert2.github.io/ Gets the image element. ```APIDOC ## Swal.getImage() ### Description Get the image. ### Method Swal.getImage() ### Parameters None ### Returns DOM element - The image element. ``` -------------------------------- ### Swal.getIcon() Source: https://sweetalert2.github.io/ Gets the icon element. ```APIDOC ## Swal.getIcon() ### Description Get the icon. ### Method Swal.getIcon() ### Parameters None ### Returns DOM element - The icon element. ``` -------------------------------- ### Swal.mixin example for reusable toast configuration Source: https://sweetalert2.github.io/ Creates a reusable SweetAlert2 instance with toast options, including a timer and progress bar, and fires a success toast. The didOpen hook attaches mouseenter/mouseleave handlers to pause and resume the timer. ```javascript Swal.mixin({ toast: true, position: "top-end", showConfirmButton: false, timer: 3000, timerProgressBar: true, didOpen: (toast) => { toast.onmouseenter = Swal.stopTimer; toast.onmouseleave = Swal.resumeTimer; } }).fire({ icon: "success", title: "Signed in successfully" }); ``` -------------------------------- ### Swal.fire() - Theme Usage Source: https://sweetalert2.github.io/ Examples of using the theme parameter with Swal.fire() for different themes. ```APIDOC ## Swal.fire() - Theme Usage ### Description Demonstrates how to set the theme parameter in Swal.fire() to use different themes. ### Method Swal.fire() ### Parameters - **theme** (string) - Required - The theme to use. Possible values: 'dark', 'auto', 'bootstrap-5', 'bootstrap-4', 'material-ui', 'bulma'. ### Request Example ```javascript Swal.fire({ title: 'Dark theme', theme: 'dark' }) ``` ### Response No response body; the popup is displayed. ``` -------------------------------- ### SweetAlert2 text input example Source: https://sweetalert2.github.io/ Shows how to prompt for a text input with a default value fetched from an API and validate that the user enters something. ```javascript const inputValue = (await (await fetch("//api.ipify.org?format=json")).json()).ip; const { value: ipAddress } = await Swal.fire({ title: "Enter your IP address", input: "text", inputLabel: "Your IP address", inputValue, showCancelButton: true, inputValidator: (value) => { if (!value) return "You need to write something!"; } }); if (ipAddress) Swal.fire(`Your IP address is ${ipAddress}`); ``` -------------------------------- ### SweetAlert2 email input example Source: https://sweetalert2.github.io/ Prompts for an email address with a placeholder and displays the entered email. ```javascript const { value: email } = await Swal.fire({ title: "Input email address", input: "email", inputLabel: "Your email address", inputPlaceholder: "Enter your email address" }); if (email) Swal.fire(`Entered email: ${email}`); ``` -------------------------------- ### SweetAlert2 date input example Source: https://sweetalert2.github.io/ Prompts for a departure date, setting the minimum date to today using the didOpen callback. ```javascript const { value: date } = await Swal.fire({ title: "select departure date", input: "date", didOpen: () => { const today = (new Date()).toISOString(); Swal.getInput().min = today.split("T")[0]; } }); if (date) Swal.fire("Departure date", date); ``` -------------------------------- ### Declarative template example Source: https://sweetalert2.github.io/ Defines a popup using a