|
| 1 | +import React, { useCallback, useEffect, useRef } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useI18nContext } from '../../../hooks/useI18nContext'; |
| 4 | +import { |
| 5 | + IconName, |
| 6 | + ModalFocus, |
| 7 | + Popover, |
| 8 | + PopoverPosition, |
| 9 | + PopoverRole, |
| 10 | + Text, |
| 11 | +} from '../../component-library'; |
| 12 | +import { MenuItem } from '../../ui/menu'; |
| 13 | +import { IconColor, TextColor } from '../../../helpers/constants/design-system'; |
| 14 | + |
| 15 | +export const NetworkListItemMenu = ({ |
| 16 | + anchorElement, |
| 17 | + onClose, |
| 18 | + onEditClick, |
| 19 | + onDeleteClick, |
| 20 | + isOpen, |
| 21 | +}) => { |
| 22 | + const t = useI18nContext(); |
| 23 | + |
| 24 | + // Handle Tab key press for accessibility inside the popover and will close the popover on the last MenuItem |
| 25 | + const lastItemRef = useRef(null); |
| 26 | + const accountDetailsItemRef = useRef(null); |
| 27 | + const removeAccountItemRef = useRef(null); |
| 28 | + const removeJWTItemRef = useRef(null); |
| 29 | + |
| 30 | + // Checks the MenuItems from the bottom to top to set lastItemRef on the last MenuItem that is not disabled |
| 31 | + useEffect(() => { |
| 32 | + if (removeJWTItemRef.current) { |
| 33 | + lastItemRef.current = removeJWTItemRef.current; |
| 34 | + } else if (removeAccountItemRef.current) { |
| 35 | + lastItemRef.current = removeAccountItemRef.current; |
| 36 | + } else { |
| 37 | + lastItemRef.current = accountDetailsItemRef.current; |
| 38 | + } |
| 39 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 40 | + }, [ |
| 41 | + removeJWTItemRef.current, |
| 42 | + removeAccountItemRef.current, |
| 43 | + accountDetailsItemRef.current, |
| 44 | + ]); |
| 45 | + |
| 46 | + const handleKeyDown = useCallback( |
| 47 | + (event) => { |
| 48 | + if (event.key === 'Tab' && event.target === lastItemRef.current) { |
| 49 | + // If Tab is pressed at the last item to close popover and focus to next element in DOM |
| 50 | + onClose(); |
| 51 | + } |
| 52 | + }, |
| 53 | + [onClose], |
| 54 | + ); |
| 55 | + |
| 56 | + // Handle click outside of the popover to close it |
| 57 | + const popoverDialogRef = useRef(null); |
| 58 | + |
| 59 | + const handleClickOutside = useCallback( |
| 60 | + (event) => { |
| 61 | + if ( |
| 62 | + popoverDialogRef?.current && |
| 63 | + !popoverDialogRef.current.contains(event.target) |
| 64 | + ) { |
| 65 | + onClose(); |
| 66 | + } |
| 67 | + }, |
| 68 | + [onClose], |
| 69 | + ); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + document.addEventListener('mousedown', handleClickOutside); |
| 73 | + |
| 74 | + return () => { |
| 75 | + document.removeEventListener('mousedown', handleClickOutside); |
| 76 | + }; |
| 77 | + }, [handleClickOutside]); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Popover |
| 81 | + className="multichain-network-list-item-menu__popover" |
| 82 | + referenceElement={anchorElement} |
| 83 | + role={PopoverRole.Dialog} |
| 84 | + position={PopoverPosition.Bottom} |
| 85 | + offset={[0, 0]} |
| 86 | + padding={0} |
| 87 | + isOpen={isOpen} |
| 88 | + isPortal |
| 89 | + preventOverflow |
| 90 | + flip |
| 91 | + > |
| 92 | + <ModalFocus restoreFocus initialFocusRef={anchorElement}> |
| 93 | + <div onKeyDown={handleKeyDown} ref={popoverDialogRef}> |
| 94 | + {onEditClick ? ( |
| 95 | + <MenuItem |
| 96 | + iconName={IconName.Edit} |
| 97 | + onClick={(e) => { |
| 98 | + e.stopPropagation(); |
| 99 | + |
| 100 | + // Pass network info? |
| 101 | + onEditClick(); |
| 102 | + }} |
| 103 | + data-testid="network-list-item-options-edit" |
| 104 | + > |
| 105 | + {t('edit')} |
| 106 | + </MenuItem> |
| 107 | + ) : null} |
| 108 | + {onDeleteClick ? ( |
| 109 | + <MenuItem |
| 110 | + iconName={IconName.Trash} |
| 111 | + iconColor={IconColor.errorDefault} |
| 112 | + onClick={(e) => { |
| 113 | + e.stopPropagation(); |
| 114 | + |
| 115 | + // Pass network info? |
| 116 | + onDeleteClick(); |
| 117 | + }} |
| 118 | + data-testid="network-list-item-options-delete" |
| 119 | + > |
| 120 | + <Text color={TextColor.errorDefault}>{t('delete')}</Text> |
| 121 | + </MenuItem> |
| 122 | + ) : null} |
| 123 | + </div> |
| 124 | + </ModalFocus> |
| 125 | + </Popover> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +NetworkListItemMenu.propTypes = { |
| 130 | + /** |
| 131 | + * Element that the menu should display next to |
| 132 | + */ |
| 133 | + anchorElement: PropTypes.instanceOf(window.Element), |
| 134 | + /** |
| 135 | + * Function that executes when the menu is closed |
| 136 | + */ |
| 137 | + onClose: PropTypes.func.isRequired, |
| 138 | + /** |
| 139 | + * Function that executes when the Edit menu item is clicked |
| 140 | + */ |
| 141 | + onEditClick: PropTypes.func, |
| 142 | + /** |
| 143 | + * Function that executes when the Delete menu item is closed |
| 144 | + */ |
| 145 | + onDeleteClick: PropTypes.func, |
| 146 | + /** |
| 147 | + * Represents if the menu is open or not |
| 148 | + * |
| 149 | + * @type {boolean} |
| 150 | + */ |
| 151 | + isOpen: PropTypes.bool.isRequired, |
| 152 | +}; |
0 commit comments