Skip to content

Commit

Permalink
Change/close dropdown when clicked outside (#37169)
Browse files Browse the repository at this point in the history
* Close Action button dropdown when clicked outside

* changelog
  • Loading branch information
CodeyGuyDylan authored May 2, 2024
1 parent 002b870 commit 9b64103
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Button } from '@automattic/jetpack-components';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, chevronDown, external, check } from '@wordpress/icons';
import cs from 'classnames';
import { useCallback, useState, useEffect, useMemo } from 'react';
import { useCallback, useState, useEffect, useMemo, useRef } from 'react';
import useProduct from '../../data/products/use-product';
import useOutsideAlerter from '../../hooks/use-outside-alerter';
import styles from './style.module.scss';

export const PRODUCT_STATUSES = {
Expand Down Expand Up @@ -41,6 +42,7 @@ const ActionButton = ( {
const { detail } = useProduct( slug );
const { manageUrl, purchaseUrl } = detail;
const isManageDisabled = ! manageUrl;
const dropdownRef = useRef( null );

const isBusy = isFetching || isInstallingStandalone;
const hasAdditionalActions = additionalActions?.length > 0;
Expand Down Expand Up @@ -210,6 +212,11 @@ const ActionButton = ( {
setCurrentAction( allActions[ 0 ] );
}, [ allActions ] );

// Close the dropdown when clicking outside of it.
useOutsideAlerter( dropdownRef, () => {
setIsDropdownOpen( false );
} );

if ( ! admin ) {
return (
<Button { ...buttonState } size="small" variant="link" weight="regular">
Expand All @@ -222,7 +229,7 @@ const ActionButton = ( {
}

const dropdown = hasAdditionalActions && (
<div className={ styles[ 'action-button-dropdown' ] }>
<div ref={ dropdownRef } className={ styles[ 'action-button-dropdown' ] }>
<ul className={ styles[ 'dropdown-menu' ] }>
{ [ ...additionalActions, getStatusAction() ].map( ( { label, isExternalLink }, index ) => {
const onDropdownMenuItemClick = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ $box-shadow-color: rgba( 0, 0, 0, 0.1 );
border-radius: calc( var( --jp-border-radius ) / 2 );
box-shadow: 0px 1px 1px 0px $box-shadow-color, 0px 1px 1.5px 0px $box-shadow-color, 0px 2px 3px -0.5px $box-shadow-color;
padding: var( --spacing-base );
z-index: 1;

.dropdown-item {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
interface MyJetpackConnection {
apiNonce: string;
apiRoot: string;
blogID: number;
blogID: string;
registrationNonce: string;
isSiteConnected: boolean;
topJetpackMenuItemUrl: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from 'react';
import type { MutableRefObject } from 'react';

const useOutsideAlerter = ( ref: MutableRefObject< HTMLElement >, onClickOutside: () => void ) => {
useEffect( () => {
const handleClickOutside = ( event: Event ) => {
if (
event.target instanceof Element &&
ref.current &&
! ref.current.contains( event.target )
) {
onClickOutside();
}
};

document.addEventListener( 'mousedown', handleClickOutside );
return () => {
document.removeEventListener( 'mousedown', handleClickOutside );
};
}, [ ref, onClickOutside ] );
};

export default useOutsideAlerter;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Fix z-index issue and close action button dropdown when clicked outside

0 comments on commit 9b64103

Please sign in to comment.