๐จ SweetAlert2 - ์ด์ alert ๋ชจ๋ฌ์ฐฝ ์ค์น & ์ฌ์ฉ๋ฒ
SweetAlert2
์๋ฐ์คํฌ๋ฆฝํธ๋ก ์น ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค ๋ณด๋ฉด ์์ฃผ Alert ํจ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค.
Alert๋ ์ฌ์ฉ์์๊ฒ ์๋ฆผ์ ์ฃผ๊ณ ์ ํ ๋ ์ ๋ง ์๊ตฌ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ ์ ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ์ alert๋ ์์ฃผ ๊ธฐ๋ณธ์ ์ธ ๋ธ๋ผ์ฐ์ UI๋ฅผ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
ํ์ง๋ง ์ฐ๋ฆฌ๋ ์ด๋ฐ ์๋ฆผ์ฐฝ๋ณด๋ค ๋ ์์ ์๋ฆผ ์ฐฝ์ ์ํ์ฃ .
SweetAlert2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋๊ธฐ์กด์ alert ์ฐฝ๋ณด๋ค ๋ค์ํ ๋์์ธ๊ณผ ์๊ฐ์ผ๋ก ๋์์ธ์ด ๋ alert ์ฐฝ ์ ๋๋ค.
๋ํ ํ ์ข ๋ฅ๋ง ์๋๊ฒ ์๋๋ผ alert, confirm, prompt ์ ๋ ฅ์ฐฝ ์ญ์ ์ง์ํ๋ฉฐ, ์ฌ๋ฌ ์ฃผ์ ์ ๋ฐ๋ผ ๋ค์ํ ์ด์ ์๋์ฐฝ์ ๋ฐ๋ก css์์ ์์ด ๊ณ ํ๋ฆฌํฐ์ ์ ๋๋ฉ์ด์ ์ฐฝ์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
์ด๋ฒ ์๊ฐ์๋ SweetAlert2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ์ ๋ํ ํฌ์คํ ์ ์งํํฉ๋๋ค.
SweetAlert2
A beautiful, responsive, customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes
sweetalert2.github.io
SweetAlert2 ์ค์น
๋งํฌ์ ๋ค์ด๊ฐ์, ๋นจ๊ฐ ๋ฐ์ค ์ณ์ง ๋ถ๋ถ๋ง 2๊ฐ์ ์๋ฐ์คํฌ๋ฆฝํธ์ CSSํ์ผ์ ๊ฐ๊ฐ ๋ค์ด๋ฐ์ผ์๋ฉด ๋ฉ๋๋ค.
jsDelivr - A free, fast, and reliable CDN for Open Source
Supports npm, GitHub, WordPress, Deno, and more. Largest network and best performance among all CDNs. Serving more than 80 billion requests per month. Built for production use.
www.jsdelivr.com
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.10/dist/sweetalert2.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.10/dist/sweetalert2.min.js"></script>
SweetAlert2 ์ฌ์ฉ๋ฒ
See the Pen SweetAlert2 by Horly Andelo (@horly-andelo) on CodePen.
See the Pen SweetAlert2 by barzz12 (@inpaSkyrim) on CodePen.
๊ธฐ๋ณธ Alert
Swal.fire({
'Alert๊ฐ ์คํ๋์์ต๋๋ค.', // Alert ์ ๋ชฉ
'์ด๊ณณ์ ๋ด์ฉ์ด ๋ํ๋๋ ๊ณณ์
๋๋ค.', // Alert ๋ด์ฉ
'success', // Alert ํ์
});
Confirm
Confirm์ ๊ตฌํํ ๋ ๊ฐ ๋ฒํผ์ ์๊ณผ ๋ด์ฉ์ ๊ตฌ์ฑํ ์ ์๊ณ , ๊ฒฐ๊ณผ๊ฐ ๋ญ๋์ ๋ฐ๋ผ ํ์ฒ๋ฆฌ ์ด๋ฒคํธ(then)๋ฅผ ์ด์ฉํด ๋ค์ ๋์์ ์ค์ ํ ์ ์์ต๋๋ค.
Swal.fire({
title: '์ ๋ง๋ก ๊ทธ๋ ๊ฒ ํ์๊ฒ ์ต๋๊น?',
text: '๋ค์ ๋๋๋ฆด ์ ์์ต๋๋ค. ์ ์คํ์ธ์.',
icon: 'warning',
showCancelButton: true, // cancel๋ฒํผ ๋ณด์ด๊ธฐ. ๊ธฐ๋ณธ์ ์๋ ์์
confirmButtonColor: '#3085d6', // confrim ๋ฒํผ ์๊น ์ง์
cancelButtonColor: '#d33', // cancel ๋ฒํผ ์๊น ์ง์
confirmButtonText: '์น์ธ', // confirm ๋ฒํผ ํ
์คํธ ์ง์
cancelButtonText: '์ทจ์', // cancel ๋ฒํผ ํ
์คํธ ์ง์
reverseButtons: true, // ๋ฒํผ ์์ ๊ฑฐ๊พธ๋ก
}).then(result => {
// ๋ง์ฝ Promise๋ฆฌํด์ ๋ฐ์ผ๋ฉด,
if (result.isConfirmed) { // ๋ง์ฝ ๋ชจ๋ฌ์ฐฝ์์ confirm ๋ฒํผ์ ๋๋ ๋ค๋ฉด
Swal.fire('์น์ธ์ด ์๋ฃ๋์์ต๋๋ค.', 'ํ๋ํ์๋ค์~!', 'success');
}
});
HANDLING BUTTONS
์ด๊ธฐ ๋ชจ๋ฌ์ฐฝ์์ ์ด๋ค ๋ฒํผ์ ๋๋ ๋๋์ ๋ฐ๋ผ key๋ฅผ ๋ค๋ฅด๊ฒ ์ค์ ํ ์ ์์ต๋๋ค.
Swal.fire({
// ...
}).then(result => {
if (result.isConfirmed) { // ๋ง์ฝ ๋ชจ๋ฌ์ฐฝ์์ confirm ๋ฒํผ์ ๋๋ ๋ค๋ฉด
// ...์คํ
} else if (result.isDismissed) { // ๋ง์ฝ ๋ชจ๋ฌ์ฐฝ์์ cancel ๋ฒํผ์ ๋๋ ๋ค๋ฉด
// ...์คํ
}
});
Prompt
Prompt๋ ํ ์คํธ๋ฅผ ์ ๋ ฅ๋ฐ๋ ์๋ฆผ์ฐฝ์ ๋๋ค.
๋ณด์ฌ๋๋ฆฌ๋ ์์๋ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ๋ text ์์์ง๋ง, ์ด๋ฐ์ ์ฒดํฌ๋ฐ์ค, ๋ผ๋์ค ๋ฒํผ, ์ ๋ ํธ ๋ฐ์ค, ํ์ผ ๋ฑ <input> ํ๊ทธ๋ก ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒ๋ค์ prompt๋ก ๊ตฌํํ ์ ์๋ค๊ณ ๋ณด์๋ฉด ๋๊ฒ ์ต๋๋ค.
(async () => {
const { value: getName } = await Swal.fire({
title: '๋น์ ์ ์ด๋ฆ์ ์
๋ ฅํ์ธ์.',
text: '๊ทธ๋ฅ ์์์ผ ๋ฟ์
๋๋ค.',
input: 'text',
inputPlaceholder: '์ด๋ฆ์ ์
๋ ฅ..'
})
// ์ดํ ์ฒ๋ฆฌ๋๋ ๋ด์ฉ.
if (getName) {
Swal.fire(`: ${getName}`)
}
})()
Toast
const Toast = Swal.mixin({
toast: true,
position: 'center-center',
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({
icon: 'success',
title: 'toast ์๋ฆผ์ด ์ ์์ ์ผ๋ก ์คํ ๋์์ต๋๋ค.'
})
Image
Swal.fire({
title: 'Sweet!',
text: 'Modal with a custom image.',
imageUrl: 'https://unsplash.it/400/200',
imageWidth: 400,
imageHeight: 200,
imageAlt: 'Custom image',
})
Ajax
Swal.fire({
title: 'Submit your Github username',
input: 'text',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Look up',
showLoaderOnConfirm: true, // ๋ฐ์ดํฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์๋๊น์ง, ๋ฒํผ์๋ค๊ฐ ๋ก๋ฉ๋ฐ๋ฅผ ํํ
preConfirm: (login) => { // ํ์ธ ๋ฒํผ ๋๋ฅด๋ฉด ์คํ๋๋ ์ฝ๋ฐฑํจ์
return fetch(`//api.github.com/users/${login}`)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json()
})
.catch(error => {
Swal.showValidationMessage(
`Request failed: ${error}`
)
})
},
// ์คํ๋๋ ๋์ ๋ฐฐ๊ฒฝ ๋๋ฅผ๋ ๋ชจ๋ฌ์ฐฝ ์๋ซํ๋๋ก ์ค์
// isLoading() ์ฆ, ๋ก๋ฉ์ด ์งํ๋๋ ๋์ false๋ฅผ ๋ฆฌํดํ๊ฒ ํด์ ousideClick์ ์๋๊ฒ ํ๊ณ , ๋ก๋ฉ ์ํ๊ฐ ์๋๋ฉด ousideClick์ ํ์ฉํ๋ค.
allowOutsideClick: () => !Swal.isLoading()
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
title: `${result.value.login}'s avatar`,
imageUrl: result.value.avatar_url
})
}
})
์์ด์ฝ ํ์
Swal.fire({
icon: 'success', // ์ฌ๊ธฐ๋ค๊ฐ ์์ด์ฝ ์ข
๋ฅ๋ฅผ ์ฐ๋ฉด ๋ฉ๋๋ค.
title: 'Alert๊ฐ ์คํ๋์์ต๋๋ค.',
text: '์ด๊ณณ์ ๋ด์ฉ์ด ๋ํ๋๋ ๊ณณ์
๋๋ค.',
});