Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace JavaScript alert/confirmations with sweetalert2 #1035

Merged
merged 1 commit into from
Jul 21, 2017
Merged

Replace JavaScript alert/confirmations with sweetalert2 #1035

merged 1 commit into from
Jul 21, 2017

Conversation

sacr3dc0w
Copy link
Contributor

@sacr3dc0w sacr3dc0w commented Jul 2, 2017

What?

I’ve used this for a few projects and wanted to make it accessible to the community. I’ve cleaned it up and done my best to make it conform to Cornerstone stylings but maybe some additional eyes can fill in any missing blanks. SweetAlert2 is described by their webpage as:

A beautiful, responsive, customizable, accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies.

Documentation

The project’s webpage goes into deeper detail on it’s usage.

Screen Recordings

Example

Remove item from cart, desktop (all theme variations)
desktop

Remove item from cart, tablet (all theme variations)
tablet

Remove item from cart, mobile (all theme variations)
mobile

Compare page error
mobile

Compare product error on category page
mobile

Copy link
Contributor

@lord2800 lord2800 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me.

@mjschock
Copy link
Contributor

mjschock commented Jul 5, 2017

@sacr3dc0w overall it looks good to me. my concern is the file size:

screen shot 2017-07-05 at 3 47 45 pm

i see nearly 50k for this. however, it turns out sweetalert2's package main is pointing to dist/sweetalert2.js. modifying that to point to dist/sweetalert2.min.js gets a more reasonable profile:

screen shot 2017-07-05 at 3 51 46 pm

can you please add another alias in webpack.conf.js to map to the min file, like so:

sweetalert2:  path.resolve(__dirname, 'node_modules/sweetalert2/dist/sweetalert2.min.js'),

Copy link
Contributor

@mjschock mjschock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM - just need to update the webpack resolve to use the minified version

Copy link
Contributor

@davidchin davidchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sacr3dc0w, nice work. Thank you for the screenshots. The alert does look better! I just have few minor comments I want you to look at 😄

@@ -57,7 +58,10 @@ export default function (urlContext) {
const productsToCompare = $this.find('input[name="products\[\]"]:checked');

if (productsToCompare.length <= 1) {
alert('You must select at least two products to compare');
swal({
text: 'You must select at least two products to compare',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an existing problem. So you probably don't need to fix it in this PR. Just pointing it out that the warning message needs to come from the language file, not hard-coded in here.

const delta = new Date() - openTime;

// Delta workaround for Chrome's "prevent popup"
if (!result && delta > 10) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question. Do we need this hack anymore? It looks like sweetalert2 is just a regular modal. Unlike window.confirm, the browser might not interfer.

$swal-font-headingsColor: stencilColor("color-textHeading");

// Modal stylings
$swal-modal-bgColor: stencilColor("navUser-dropdown-backgroundColor") !important;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably introduce a new variable instead of using navUser-dropdown-backgroundColor? It looks like navUser-dropdown-backgroundColor is intended to used for navigation dropdowns only.

$swal-button-wrapperMargin: 25px 0 0;

// Confirm button
$swal-button-bgColor: stencilColor("button--primary-backgroundColor") !important;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to use !important here? If the styles are cascaded properly, you shouldn't need to increase their specificity.

@@ -129,7 +130,10 @@ export default function (stateElement, context = {}, options, callback) {

utils.api.country.getByName(countryName, (err, response) => {
if (err) {
alert(context.state_error);
swal({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to only execute the callback after dismissing the alert? I think sweetalert2 is non-blocking, unlike alert, which stops the execution the script until it is dismissed.

@junedkazi
Copy link
Contributor

junedkazi commented Jul 6, 2017

This is nice but I have been thinking if it makes sense to move this functionality to https://github.com/bigcommerce/stencil-utils so we can build a wrapper around it. It will make it much more easier to change the underlying library in the future & also to upgrade the lib in the future to propagate the changes across the board by just updating the package version.

@mjschock
Copy link
Contributor

mjschock commented Jul 6, 2017

i like @junedkazi 's idea of abstracting it as utils.alert from stencil.utils. i'm not sure where exactly it would fit in there though - perhaps in a components folder? it would be nice down the line to have the specifics of the theme 'swappable' so to speak. maybe we could even ultimately swap in different frameworks.

Copy link
Contributor

@davidchin davidchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junedkazi good idea! I'm not against moving the alert function in stencil-utils which internally invokes sweetalert2, if you think it will be useful for all theme developers. I'm also fine leaving it here for now and moving it out later when there's a need.

Anyway, when you create the facade, make sure it is well encapsulated so that it doesn't leak implementation details (i.e.: any references to sweetalert2 cannot be exposed, including CSS classes).

PS: A question. It seems stencil-utils doesn't really have a clear responsibility. Is it just a general purpose package?

config.json Outdated
@@ -177,6 +177,7 @@
"checkRadio-color": "#4f4f4f",
"checkRadio-backgroundColor": "#ffffff",
"checkRadio-borderColor": "#dfdfdf",
"sweetAlert-backgroundColor": "#ffffff",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a more generic config name? This name ties to a specific vendor library. Maybe just alert-backgroundColor.

// -----------------------------------------------------------------------------

// Font color, size, family
$swal-font-body: stencilFontFamily("body-font"), Arial, Helvetica, sans-serif;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these variables can probably be generalised as well. i.e.: $alert-font-body

@@ -105,7 +107,10 @@ export default class Account extends PageManager {

if (!submitForm) {
event.preventDefault();
alert('Please select one or more items to reorder.');
swal({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a intention-revealing function name? swal doesn't communicate anything.

@@ -5,6 +5,7 @@ import Wishlist from './wishlist';
import validation from './common/form-validation';
import stateCountry from './common/state-country';
import { classifyForm, Validators, insertStateHiddenField } from './common/form-utils';
import swal from 'sweetalert2';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may choose to not import the vendor library directly. Instead, create a facade so that it's easier to change its implementation later.

Copy link
Contributor

@davidchin davidchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing the changes 👍. I'm happy with the current state. And do refactoring later.

@mjschock
Copy link
Contributor

@sacr3dc0w - could you please update the changelog to include an entry for these changes in the drafts section - i'll merge it in after that

@mjschock mjschock merged commit 4e79c07 into bigcommerce:master Jul 21, 2017
@sacr3dc0w sacr3dc0w deleted the sweetalert2 branch July 21, 2017 21:51
@limonte
Copy link

limonte commented Jan 6, 2018

https://limonte.github.io/sweetalert2/

this link is outdated, here's the correct one https://sweetalert2.github.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants