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

Close the dropdown when the user clicks outside of it #99179

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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 @@ -19,7 +19,7 @@ import {
getPartnerCoupon,
} from '@automattic/wpcom-checkout';
import styled from '@emotion/styled';
import { useState, useCallback, useMemo } from 'react';
import { useState, useCallback, useMemo, useEffect, useRef } from 'react';
import { has100YearPlan } from 'calypso/lib/cart-values/cart-items';
import { useExperiment } from 'calypso/lib/explat';
import { isWcMobileApp } from 'calypso/lib/mobile-app';
Expand All @@ -40,7 +40,7 @@ import type {
ResponseCartProduct,
RemoveCouponFromCart,
} from '@automattic/shopping-cart';
import type { PropsWithChildren } from 'react';
import type { PropsWithChildren, RefObject } from 'react';

const WPOrderReviewList = styled.ul`
box-sizing: border-box;
Expand Down Expand Up @@ -308,6 +308,53 @@ function LineItemWrapper( {
isDeletable = false;
}

const isVariantDropdownOpen = product.uuid === variantOpenId;
const isAkQuantityDropdownOpen = product.uuid === akQuantityOpenId;
const variantDropdownRef = useRef< HTMLDivElement >( null );
const akQuantityDropdownRef = useRef< HTMLDivElement >( null );

useEffect( () => {
const handleClickOutside =
( ref: RefObject< HTMLDivElement >, toggle: ( key: string | null ) => void ) =>
( event: MouseEvent ): void => {
if ( ref.current && ! ref.current.contains( event.target as Node ) ) {
toggle( null );
}
};

const handleClickOutsideVariantDropdown = handleClickOutside(
variantDropdownRef,
toggleVariantSelector
);

const handleClickOutsideAkQuantityDropdown = handleClickOutside(
akQuantityDropdownRef,
toggleAkQuantityDropdown
);

if ( isVariantDropdownOpen ) {
document.addEventListener( 'mousedown', handleClickOutsideVariantDropdown as EventListener );
} else {
document.removeEventListener( 'mousedown', handleClickOutsideVariantDropdown );
}

if ( isAkQuantityDropdownOpen ) {
document.addEventListener( 'mousedown', handleClickOutsideAkQuantityDropdown );
} else {
document.removeEventListener( 'mousedown', handleClickOutsideAkQuantityDropdown );
}

return () => {
document.removeEventListener( 'mousedown', handleClickOutsideVariantDropdown );
document.removeEventListener( 'mousedown', handleClickOutsideAkQuantityDropdown );
};
}, [
isVariantDropdownOpen,
toggleVariantSelector,
isAkQuantityDropdownOpen,
toggleAkQuantityDropdown,
] );

const shouldShowVariantSelector = ( () => {
if ( ! onChangeSelection ) {
return false;
Expand Down Expand Up @@ -379,25 +426,29 @@ function LineItemWrapper( {
>
<DropdownWrapper>
{ finalShouldShowVariantSelector && (
<ItemVariationPicker
id={ product.uuid }
selectedItem={ product }
onChangeItemVariant={ onChangeSelection }
isDisabled={ isDisabled }
variants={ variants }
toggle={ toggleVariantSelector }
isOpen={ variantOpenId === product.uuid }
/>
<div ref={ variantDropdownRef }>
<ItemVariationPicker
id={ product.uuid }
selectedItem={ product }
onChangeItemVariant={ onChangeSelection }
isDisabled={ isDisabled }
variants={ variants }
toggle={ toggleVariantSelector }
isOpen={ isVariantDropdownOpen }
/>
</div>
) }
{ ! isRenewal && isAkPro500Cart && (
<AkismetProQuantityDropDown
id={ product.uuid }
responseCart={ responseCart }
setForceShowAkQuantityDropdown={ setForceShowAkQuantityDropdown }
onChangeAkProQuantity={ onChangeAkProQuantity }
toggle={ toggleAkQuantityDropdown }
isOpen={ akQuantityOpenId === product.uuid }
/>
<div ref={ akQuantityDropdownRef }>
<AkismetProQuantityDropDown
id={ product.uuid }
responseCart={ responseCart }
setForceShowAkQuantityDropdown={ setForceShowAkQuantityDropdown }
onChangeAkProQuantity={ onChangeAkProQuantity }
toggle={ toggleAkQuantityDropdown }
isOpen={ isAkQuantityDropdownOpen }
/>
</div>
) }
</DropdownWrapper>
</LineItem>
Expand Down