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

Fix the kebab menu close when we click outside the kebab. #1595

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion locales/en/plugin__odf-console.json
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,8 @@
"Edit labels": "Edit labels",
"Edit annotations": "Edit annotations",
"Edit bucket": "Edit bucket",
"Objects": "Objects",
"Refresh": "Refresh",
"Objects": "Objects",
"Created via OBC": "Created via OBC",
"Created via S3": "Created via S3",
"MCG": "MCG",
Expand Down
38 changes: 33 additions & 5 deletions packages/shared/src/kebab/kebab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useCallback, useEffect } from 'react';
import { getName, getNamespace } from '@odf/shared/selectors';
import {
K8sResourceCommon,
Expand All @@ -21,6 +22,32 @@ import { ModalKeys, defaultModalMap } from '../modals/types';
import { useCustomTranslation } from '../useCustomTranslationHook';
import { referenceForModel } from '../utils';

const useClickOutside = (
ref: React.RefObject<HTMLElement>,
callback: () => void
) => {
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback();
}
};

const handleEscapeKey = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
callback();
}
};

document.addEventListener('mousedown', handleClickOutside);
alfonsomthd marked this conversation as resolved.
Show resolved Hide resolved
document.addEventListener('keydown', handleEscapeKey);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('keydown', handleEscapeKey);
};
}, [ref, callback]);
};

export type CustomKebabItem = {
key: string;
value: string;
Expand Down Expand Up @@ -102,19 +129,19 @@ export const Kebab: React.FC<KebabProps> & KebabStaticProperties = ({
hideItems,
}) => {
const { t } = useCustomTranslation();

const launchModal = useModal();

const eventRef = React.useRef(undefined);
const dropdownToggleRef = React.useRef();
const [toggleDirection, setToggleDirection] =
React.useState<DropdownPopperProps['direction']>('down');
const [isOpen, setOpen] = React.useState(false);
const closeDropdown = useCallback(() => setOpen(false), []);

const { resourceModel, resource } = extraProps;
// Use the custom hook to detect clicks outside the Kebab menu
useClickOutside(dropdownToggleRef, closeDropdown);

const { resourceModel, resource } = extraProps;
const resourceLabel = resourceModel.label;

const navigate = useNavigate();

const [canCreate, createLoading] = useAccessReview({
Expand Down Expand Up @@ -241,6 +268,7 @@ export const Kebab: React.FC<KebabProps> & KebabStaticProperties = ({
const content = _.has(resource.metadata, 'deletionTimestamp')
? terminatingTooltip || t('Resource is being deleted.')
: '';

return (
<Tooltip
content={
Expand All @@ -263,7 +291,7 @@ export const Kebab: React.FC<KebabProps> & KebabStaticProperties = ({
ref={dropdownToggleRef}
aria-label="Dropdown toggle"
variant={toggleType === 'Kebab' ? 'plain' : 'default'}
onClick={() => setOpen((o) => !o)}
onClick={() => setOpen(!isOpen)}
isExpanded={isOpen}
data-test="kebab-button"
isDisabled={isDisabled}
Expand Down
Loading