This library was made for people with this feeling : window.confirm(...)
is ungly !
If you need to add a confirm dialog to your web site / web app that is beautiful or, at least easily themable, you are on the right repo !
You can use C2 for two major use-cases :
- You want to add a confirm dialog when a link, set of links or buttons is / are clicked.
CustomConfirm('button', function (confirmed, target_element) {
if (confirmed) {
// do something when the user confirms
// use the target element if you want to... or not !
} else {
// do something (or not) when he cancels
}
});
- You need to pragmatically show the confirm dialog whenever you want.
CustomConfirm(function (confirmed) {
if (confirmed) {
// do something when the user confirms
} else {
// do something (or not) when he cancels
}
});
confirmed (Boolean)
: as the name says it, it contains whether the user confirmed or not.target_element (Element)
: represents the element that was clicked and that triggered the dialog (check the demo page).
C2 is developer-friendly, you can override the default texts by passing options.
Here's the full list of the available options :
title (String) (default: 'Confirm dialog')
: To set the Title textbody (String) (default: 'Are you sure ?')
: To set the Body textbtn_yes (String) (default: 'confirm')
: To set the Confirm button textbtn_no (String) (default: 'cancel')
: To set the Cancel button textbtn_close (String) (default: '')
: To set the Close button texthas_overlay (String) (default: true)
: To set whether to have an overlay markup or nottargets (String | Element | Array | NodeList) (default: undefined)
: To set a selector or DOM element(s)
Example
CustomConfirm({
targets: 'a',
title: 'A fancy website',
body: 'Are you sure you want to visit this link ?',
btn_yes: 'Oh yeah !',
btn_no: 'No, please cancel'
}, function (confirmed, target_element) {
if (confirmed) {
// do something when the user confirms
location.href = target_element.href;
} else {
// do something (or not) when he cancels
}
});
C2 has a simple markup with some ready-to-use classes. To customize your dialog you can use the following classes :
.c2_content
: For the main content box (the white box, check the default theme).c2_header
: For the header (contains the title and close markup).c2_title
: For the title.c2_btn-close
: For the close button.c2_body
: For the the inner content (where your confirmation message appears).c2_footer
: For the footer (contains confirm and cancel button).c2_btn-no
: For the cancel button.c2_btn-yes
: For the confirm button.c2_overlay
: For the overlay
I'm not a web designer so, i'd be really happy to have some more themes for the community. Feel free to create a theme and share it here by sending a pull request.
I'll add some animations when i have some time !
IE 9+ Chrome all versions Firefox all versions ...
Thanks !