Skip to content

Add aria live region to communicate cart changes #744

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions src/components/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,8 @@ export default class Cart extends Component {
const data = Object.assign({}, lineItem, this.options.viewData);
const fullPrice = data.variant.priceV2.amount * data.quantity;
const formattedPrice = formatMoney(fullPrice, this.moneyFormat);
const discountAllocations = data.discountAllocations;

const {discounts, totalDiscount} = discountAllocations.reduce((discountAcc, discount) => {
const targetSelection = discount.discountApplication.targetSelection;
if (LINE_ITEM_TARGET_SELECTIONS.indexOf(targetSelection) > -1) {
const discountAmount = discount.allocatedAmount.amount;
const discountDisplayText = discount.discountApplication.title || discount.discountApplication.code;
discountAcc.totalDiscount += discountAmount;
discountAcc.discounts.push({discount: `${discountDisplayText} (-${formatMoney(discountAmount, this.moneyFormat)})`});
}
return discountAcc;
}, {
discounts: [],
totalDiscount: 0,
});

const {discounts, totalDiscount} = this.discountsForLineItem(lineItem);
data.discounts = discounts.length > 0 ? discounts : null;
data.formattedFullPrice = totalDiscount > 0 ? formattedPrice : null;
data.formattedActualPrice = formatMoney(fullPrice - totalDiscount, this.moneyFormat);
Expand Down Expand Up @@ -408,8 +395,15 @@ export default class Cart extends Component {
this.toggles.forEach((toggle) => toggle.view.render());
if (quantity > 0) {
this.view.render();
const updatedLineItem = this.model.lineItems.find((item) => item.id === id);
this.updateSummaryText(`${this.options.text.itemTotalAccessibilityLabel} ${this.formattedLineItemTotal(updatedLineItem)}`);
} else {
this.view.animateRemoveNode(id);
if (this.model.lineItems.length > 0) {
this.updateSummaryText(this.options.text.itemRemovedAccessibilityLabel);
} else {
this.updateSummaryText(`${this.options.text.itemRemovedAccessibilityLabel} ${this.options.text.empty}`, true);
}
}
return checkout;
});
Expand Down Expand Up @@ -437,6 +431,7 @@ export default class Cart extends Component {
if (!openCart) {
this.setFocus();
}
this.updateSummaryText(this.options.text.itemAddedAccessibilityLabel);
return checkout;
});
} else {
Expand All @@ -454,6 +449,7 @@ export default class Cart extends Component {
if (!openCart) {
this.setFocus();
}
this.updateSummaryText(this.options.text.itemAddedAccessibilityLabel);
return checkout;
});
}
Expand Down Expand Up @@ -495,4 +491,43 @@ export default class Cart extends Component {
this.view.setFocus();
}, 0);
}

updateSummaryText(lineItemText, hideSubtotal) {
const summaryText = hideSubtotal ? lineItemText : `${lineItemText} ${this.options.text.subtotalAccessibilityLabel} ${this.formattedTotal}`;

const summaryNode = this.view.document.querySelector(this.selectors.cart.hiddenSummary);
summaryNode.textContent = summaryText;

setTimeout(() => {
summaryNode.textContent = '';
}, 1000);
}

formattedLineItemTotal(lineItem) {
const fullPrice = lineItem.variant.priceV2.amount * lineItem.quantity;
if (!this.options.contents.discounts) {
return formatMoney(fullPrice, this.moneyFormat);
}

const {totalDiscount} = this.discountsForLineItem(lineItem);
return formatMoney(fullPrice - totalDiscount, this.moneyFormat);
}

discountsForLineItem(lineItem) {
const discountAllocations = lineItem.discountAllocations;

return discountAllocations.reduce((discountAcc, discount) => {
const targetSelection = discount.discountApplication.targetSelection;
if (LINE_ITEM_TARGET_SELECTIONS.indexOf(targetSelection) > -1) {
const discountAmount = discount.allocatedAmount.amount;
const discountDisplayText = discount.discountApplication.title || discount.discountApplication.code;
discountAcc.totalDiscount += discountAmount;
discountAcc.discounts.push({discount: `${discountDisplayText} (-${formatMoney(discountAmount, this.moneyFormat)})`});
}
return discountAcc;
}, {
discounts: [],
totalDiscount: 0,
});
}
}
5 changes: 5 additions & 0 deletions src/defaults/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ const defaults = {
note: 'shopify-buy__cart__note',
noteDescription: 'shopify-buy__cart__note__description',
noteTextArea: 'shopify-buy__cart__note__text-area',
hiddenSummary: 'shopify-buy__cart__summary visuallyhidden',
},
text: {
title: 'Cart',
Expand All @@ -240,6 +241,10 @@ const defaults = {
notice: 'Shipping and discount codes are added at checkout.',
noteDescription: 'Special instructions for seller',
closeAccessibilityLabel: 'Close cart',
subtotalAccessibilityLabel: 'Cart subtotal',
itemTotalAccessibilityLabel: 'Item total',
itemRemovedAccessibilityLabel: 'Item removed from cart.',
itemAddedAccessibilityLabel: 'Item added to cart.',
},
},
lineItem: {
Expand Down
3 changes: 2 additions & 1 deletion src/templates/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const cartTemplates = {
{{#data.isEmpty}}<p class="{{data.classes.cart.empty}} {{data.classes.cart.emptyCart}}" data-element="cart.empty">{{data.text.empty}}</p>{{/data.isEmpty}}
<ul role="list" class="{{data.classes.cart.lineItems}}" data-element="cart.lineItems">{{{data.lineItemsHtml}}}</ul>
</div>`,
footer: `{{^data.isEmpty}}
footer: `<span class="{{data.classes.cart.hiddenSummary}}" aria-live="polite" aria-atomic="true"></span>
{{^data.isEmpty}}
<div class="{{data.classes.cart.footer}}" data-element="cart.footer">
{{#data.discounts}}
<div class="{{data.classes.cart.discount}}" data-element="cart.discount">
Expand Down
Loading