import { useEffect, useState } from 'react' import { CodeExample } from './CodeExample' import { CodeWithCopy } from './CodeWithCopy' import { IconExternalLink } from './UiIcons' export function Installation() { return ( <>

Download & install

          
            $ npm install sweetalert2
          
        

Or grab from{' '} jsDelivr CDN

`} language="xml" withoutCodepen />

Usage

1. Import the plugin:

It's also possible to import JS and CSS separately, e.g. if you need to customize styles:

2. Call the sweetAlert2-function after the page has loaded

As an old-school alternative, you can initialize the plugin by referencing the necessary files:

`} withoutCodepen />

Or with the stylesheet separately if desired:

`} language="xml" withoutCodepen />

Integrations with major frameworks

React
Vue
Angular
Laravel
) } function JsdelivrInfo() { const [jsdelivrRank, setJsdelivrRank] = useState('') const [jsdelivrTotal, setJsdelivrTotal] = useState('') function ordinalSuffix(i: string) { const n = parseInt(i) const j = n % 10 const k = n % 100 if (j === 1 && k !== 11) { return `${i}st` } if (j === 2 && k !== 12) { return `${i}nd` } if (j === 3 && k !== 13) { return `${i}rd` } return `${i}th` } useEffect(() => { fetch('https://data.jsdelivr.com/v1/package/npm/sweetalert2/stats/month') .then((response) => response.json()) .then((response) => { if (response.rank && response.total) { setJsdelivrRank(response.rank) setJsdelivrTotal(response.total) } }) .catch(() => { // ignore }) }, []) if (jsdelivrRank && jsdelivrTotal) { return (

sweetalert2 is the {ordinalSuffix(jsdelivrRank)} most popular package on jsDelivr, with{' '} {parseInt(jsdelivrTotal).toLocaleString()} CDN hits in the last month

) } }