Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ResponsiveRangeComponent = ({ control }) => {
}, []);

const { label } = control.params;
const { hideResponsive, units, defaultVal, min, max } =
const { hideResponsive, units, defaultVal, min, max, unitsMax } =
control.params.input_attrs;

const suffixValue = () => {
Expand All @@ -58,6 +58,16 @@ const ResponsiveRangeComponent = ({ control }) => {

const isRelativeUnit = () => ['em', 'rem'].includes(suffixValue());

// Allow a different max per unit (e.g. 1200 for px but 100 for %).
const maxForUnit = (unit) => {
if (unitsMax && unit && unitsMax[unit] !== undefined) {
return unitsMax[unit];
}
return max || 100;
};

const currentMax = () => maxForUnit(suffixValue());

const unitButtons = () => {
if (!units) {
return null;
Expand Down Expand Up @@ -90,6 +100,12 @@ const ResponsiveRangeComponent = ({ control }) => {
nextValue[currentDevice]
);
}
// Clamp the value to the new unit's max (e.g. when
// switching from px to %, 600 becomes 100).
const unitMax = maxForUnit(unit);
if (nextValue[currentDevice] > unitMax) {
nextValue[currentDevice] = unitMax;
}
setValue(nextValue);
control.setting.set(JSON.stringify(nextValue));
}}
Expand Down Expand Up @@ -134,7 +150,7 @@ const ResponsiveRangeComponent = ({ control }) => {
<RangeControl
value={displayValue}
min={min || 0}
max={max || 100}
max={currentMax()}
step={isRelativeUnit() ? 0.1 : 1}
allowReset
onChange={(nextValue) => {
Expand Down
50 changes: 50 additions & 0 deletions assets/js/src/frontend/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const initNavigation = () => {
handleMobileDropdowns();
handleSearch();
handleMiniCartPosition();
handleMiniCartMobileToggle();
window.HFG.initSearch = function () {
handleSearch();
handleMobileDropdowns();
Expand Down Expand Up @@ -238,6 +239,55 @@ function handleMiniCartPosition() {

window.addEventListener('resize', handleMiniCartPosition);

/**
* Toggle the dropdown mini cart on tap for mobile.
*
* On desktop the dropdown mini cart opens on hover. Touch devices have no
* hover, so without this the cart icon would just follow its link to the cart
* page. Below the laptop breakpoint we toggle a `cart-dropdown-open` class on
* tap instead; the dropdown's appearance is reused from the desktop styles
* (see the woocommerce nav-cart styles), so customers can preview the cart and
* keep shopping. It closes on a second tap or when tapping outside of it.
*/
function handleMiniCartMobileToggle() {
const carts = document.querySelectorAll('.responsive-nav-cart.dropdown');
if (carts.length === 0) {
return;
}

// Mirrors the $laptop (960px) breakpoint where the hover dropdown applies.
const isMobile = () => window.matchMedia('(max-width: 959px)').matches;

carts.forEach((cart) => {
const openButton = cart.querySelector('.cart-icon-wrapper');
if (openButton === null) {
return;
}
openButton.addEventListener('click', function (e) {
if (!isMobile() || cart.classList.contains('cart-is-empty')) {
return;
}
e.preventDefault();
cart.classList.toggle('cart-dropdown-open');
});
});

// Close an open dropdown when tapping outside of it.
document.addEventListener('click', function (e) {
if (!isMobile()) {
return;
}
carts.forEach((cart) => {
if (
cart.classList.contains('cart-dropdown-open') &&
!cart.contains(e.target)
) {
cart.classList.remove('cart-dropdown-open');
}
});
});
}

/**
* Create an overlay to allow closing.
*
Expand Down
12 changes: 12 additions & 0 deletions assets/scss/components/compat/woocommerce/_nav-cart.scss
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ $cart-width: 360px;
}
}

// Mobile: tapping the cart icon toggles `cart-dropdown-open` (see
// navigation.js). The dropdown appearance is already defined above, so we only
// need to reveal it here — reusing the same element and styles as the desktop.
@media (max-width: 959px) {

.responsive-nav-cart.dropdown.cart-dropdown-open .nv-nav-cart {
display: block;
opacity: 1;
visibility: visible;
}
}

@mixin nav-cart--laptop() {

.nv-nav-cart {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"gzip": false,
"running": false,
"limit": "7 KB",
"limit": "8 KB",
"path": "assets/js/build/modern/frontend.js"
},
{
Expand Down
Loading