Skip to content

Commit

Permalink
feat(storefront): BCTHEME-476 Scale focus trap for all modals
Browse files Browse the repository at this point in the history
  • Loading branch information
yurytut1993 committed May 6, 2021
1 parent 31ac15a commit ed400e7
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Draft
- Scale focus trap for all modals. [#2049](https://github.com/bigcommerce/cornerstone/pull/2049)
- Added settings for payment banners. [#2021](https://github.com/bigcommerce/cornerstone/pull/2021)
- Use https:// for schema markup. [#2039](https://github.com/bigcommerce/cornerstone/pull/2039)
- Update focus tooltip styles contrast to achieve accessibility AA Complaince. [#2047](https://github.com/bigcommerce/cornerstone/pull/2047)
Expand Down
3 changes: 3 additions & 0 deletions assets/js/test-unit/theme/global/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('Modal', () => {
beforeEach(() => {
$element = attachHtml(`
<div id="modal" class="modal" data-reveal>
<button class="modal-close" type="button">
<span aria-hidden="true">&#215;</span>
</button>
<div class="modal-content"></div>
<div class="loadingOverlay"></div>
</div>
Expand Down
2 changes: 0 additions & 2 deletions assets/js/theme/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ export default class Cart extends PageManager {
this.productDetails = new CartItemDetails(this.$modal, context);

this.bindGiftWrappingForm();

modal.setupFocusTrap();
});

utils.hooks.on('product-option-change', (event, currentTarget) => {
Expand Down
11 changes: 1 addition & 10 deletions assets/js/theme/common/faceted-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hooks, api } from '@bigcommerce/stencil-utils';
import _ from 'lodash';
import Url from 'url';
import urlUtils from './utils/url-utils';
import modalFactory, { ModalEvents } from '../global/modal';
import modalFactory from '../global/modal';
import collapsibleFactory from './collapsible';
import { Validators } from './utils/form-utils';
import nod from './nod';
Expand Down Expand Up @@ -57,15 +57,6 @@ class FacetedSearch {
this.collapsedFacets = [];
this.collapsedFacetItems = [];

if (this.options.modal) {
this.options.modal.$modal.on(ModalEvents.opened, event => {
const $filterItems = $(event.target).find('#facetedSearch-filterItems');
if ($filterItems.length) {
this.options.modal.setupFocusTrap();
}
});
}

// Init collapsibles
collapsibleFactory();

Expand Down
22 changes: 19 additions & 3 deletions assets/js/theme/common/product-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ProductDetailsBase, { optionChangeDecorator } from './product-details-bas
import 'foundation-sites/js/foundation/foundation';
import 'foundation-sites/js/foundation/foundation.reveal';
import ImageGallery from '../product/image-gallery';
import modalFactory, { showAlertModal } from '../global/modal';
import modalFactory, { alertModal, showAlertModal } from '../global/modal';
import { isEmpty, isPlainObject } from 'lodash';
import { normalizeFormData } from './utils/api';
import { isBrowserIE, convertIntoArray } from './utils/ie-helpers';
Expand Down Expand Up @@ -211,6 +211,11 @@ export default class ProductDetails extends ProductDetailsBase {
this.updateProductAttributes(productAttributesData);
this.updateView(productAttributesData, productAttributesContent);
bannerUtils.dispatchProductBannerEvent(productAttributesData);

if (!this.checkIsQuickViewChild($form)) {
const $context = $form.parents('.productView').find('.productView-info');
modalFactory('[data-reveal]', { $context });
}
});
}

Expand All @@ -233,6 +238,10 @@ export default class ProductDetails extends ProductDetailsBase {
});
}

checkIsQuickViewChild($element) {
return !!$element.parents('.quickView').length;
}

showProductImage(image) {
if (isPlainObject(image)) {
const zoomImageUrl = utils.tools.imageSrcset.getSrcset(
Expand Down Expand Up @@ -369,6 +378,10 @@ export default class ProductDetails extends ProductDetailsBase {
const tmp = document.createElement('DIV');
tmp.innerHTML = errorMessage;

if (!this.checkIsQuickViewChild($addToCartBtn)) {
alertModal().$preModalFocusedEl = $addToCartBtn;
}

return showAlertModal(tmp.textContent || tmp.innerText);
}

Expand All @@ -380,8 +393,11 @@ export default class ProductDetails extends ProductDetailsBase {
this.previewModal.$modal.addClass('apple-pay-supported');
}

if ($addToCartBtn.parents('.quickView').length === 0) this.previewModal.$preModalFocusedEl = $addToCartBtn;
this.updateCartContent(this.previewModal, response.data.cart_item.id, () => this.previewModal.setupFocusTrap());
if (!this.checkIsQuickViewChild($addToCartBtn)) {
this.previewModal.$preModalFocusedEl = $addToCartBtn;
}

this.updateCartContent(this.previewModal, response.data.cart_item.id);
} else {
this.$overlay.show();
// if no modal, redirect to the cart page
Expand Down
10 changes: 10 additions & 0 deletions assets/js/theme/global/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ModalEvents = {
closed: 'closed.fndtn.reveal',
open: 'open.fndtn.reveal',
opened: 'opened.fndtn.reveal',
loaded: 'loaded.data.custom',
};

function getSizeFromModal($modal) {
Expand Down Expand Up @@ -188,6 +189,7 @@ export class Modal {

this.pending = false;
this.$content.html($content);
this.$modal.trigger(ModalEvents.loaded);

restrainContentHeight(this.$content);
foundation(this.$content);
Expand All @@ -204,6 +206,8 @@ export class Modal {
this.focusTrap = focusTrap.createFocusTrap(this.$modal[0], {
escapeDeactivates: false,
returnFocusOnDeactivate: false,
allowOutsideClick: true,
fallbackFocus: '[data-header-logo-link]',
});
}

Expand All @@ -230,6 +234,12 @@ export class Modal {
}

onModalOpened() {
if (this.pending) {
this.$modal.one(ModalEvents.loaded, () => this.setupFocusTrap());
} else {
this.setupFocusTrap();
}

restrainContentHeight(this.$content);
}
}
Expand Down
2 changes: 0 additions & 2 deletions assets/js/theme/global/quick-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export default function (context) {
$carousel.slick();
}

modal.setupFocusTrap();

return new ProductDetails(modal.$content.find('.quickView'), context);
});
});
Expand Down
4 changes: 1 addition & 3 deletions assets/js/theme/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import collapsibleFactory from './common/collapsible';
import ProductDetails from './common/product-details';
import videoGallery from './product/video-gallery';
import { classifyForm } from './common/utils/form-utils';
import modalFactory, { ModalEvents } from './global/modal';
import modalFactory from './global/modal';

export default class Product extends PageManager {
constructor(context) {
Expand Down Expand Up @@ -44,8 +44,6 @@ export default class Product extends PageManager {

const review = new Review($reviewForm);

$(document).on(ModalEvents.opened, '#modal-review-form', () => this.reviewModal.setupFocusTrap());

$('body').on('click', '[data-reveal-id="modal-review-form"]', () => {
validator = review.registerValidation(this.context);
this.ariaDescribeReviewInputs($reviewForm);
Expand Down
4 changes: 4 additions & 0 deletions assets/scss/components/vendor/sweetalert2/_sweetalert2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@
.swal2-icon {
display: flex;
}

.swal2-container {
z-index: 1000; // should be less then foundation modal
}
2 changes: 1 addition & 1 deletion templates/components/common/store-logo.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<a href="{{urls.home}}" class="header-logo__link">
<a href="{{urls.home}}" class="header-logo__link" data-header-logo-link>
{{#if settings.store_logo.image}}
{{#if theme_settings.logo_size '===' 'original'}}
<img class="header-logo-image-unknown-size" src="{{getImage settings.store_logo.image 'logo_size'}}" alt="{{settings.store_logo.title}}" title="{{settings.store_logo.title}}">
Expand Down

0 comments on commit ed400e7

Please sign in to comment.