Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Prevent horizontal shift when opening the Mini-Cart drawer if scrollbars are visible #9648

Merged
merged 2 commits into from
May 30, 2023
Merged
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
12 changes: 10 additions & 2 deletions assets/js/blocks/mini-cart/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ interface Props {
hasHiddenPrice: boolean;
}

function getScrollbarWidth() {
return window.innerWidth - document.documentElement.clientWidth;
}

const MiniCartBlock = ( attributes: Props ): JSX.Element => {
const {
isInitiallyOpen = false,
Expand Down Expand Up @@ -91,10 +95,14 @@ const MiniCartBlock = ( attributes: Props ): JSX.Element => {
useEffect( () => {
const body = document.querySelector( 'body' );
if ( body ) {
const scrollBarWidth = getScrollbarWidth();
if ( isOpen ) {
Object.assign( body.style, { overflow: 'hidden' } );
Object.assign( body.style, {
overflow: 'hidden',
paddingRight: scrollBarWidth + 'px',
} );
} else {
Object.assign( body.style, { overflow: '' } );
Object.assign( body.style, { overflow: '', paddingRight: 0 } );
}
}
}, [ isOpen ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { useStoreCart } from '@woocommerce/base-context/hooks';
import { CartLineItemsTable } from '@woocommerce/base-components/cart-checkout';
import classNames from 'classnames';

type MiniCartContentsBlockProps = {
type MiniCartProductsTableBlockProps = {
className: string;
};

const Block = ( { className }: MiniCartContentsBlockProps ): JSX.Element => {
const Block = ( {
className,
}: MiniCartProductsTableBlockProps ): JSX.Element => {
const { cartItems, cartIsLoading } = useStoreCart();
return (
<div
Expand Down