|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import React, { forwardRef, useCallback, useRef, useState } from 'react'; |
| 3 | +import useModalClose from '../../common/useModalClose'; |
| 4 | +import DownArrowIcon from '../../images/down-filled-triangle.svg'; |
| 5 | +import { DropdownWrapper } from '../Dropdown'; |
| 6 | + |
| 7 | +// TODO: enable arrow keys to navigate options from list |
| 8 | + |
| 9 | +const DropdownMenu = forwardRef( |
| 10 | + ( |
| 11 | + { children, anchor, 'aria-label': ariaLabel, align, className, classes }, |
| 12 | + ref |
| 13 | + ) => { |
| 14 | + // Note: need to use a ref instead of a state to avoid stale closures. |
| 15 | + const focusedRef = useRef(false); |
| 16 | + |
| 17 | + const [isOpen, setIsOpen] = useState(false); |
| 18 | + |
| 19 | + const close = useCallback(() => setIsOpen(false), [setIsOpen]); |
| 20 | + |
| 21 | + const anchorRef = useModalClose(close, ref); |
| 22 | + |
| 23 | + const toggle = useCallback(() => { |
| 24 | + setIsOpen((prevState) => !prevState); |
| 25 | + }, [setIsOpen]); |
| 26 | + |
| 27 | + const handleFocus = () => { |
| 28 | + focusedRef.current = true; |
| 29 | + }; |
| 30 | + |
| 31 | + const handleBlur = () => { |
| 32 | + focusedRef.current = false; |
| 33 | + setTimeout(() => { |
| 34 | + if (!focusedRef.current) { |
| 35 | + close(); |
| 36 | + } |
| 37 | + }, 200); |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <div ref={anchorRef} className={className}> |
| 42 | + <button |
| 43 | + className={classes.button} |
| 44 | + aria-label={ariaLabel} |
| 45 | + tabIndex="0" |
| 46 | + onClick={toggle} |
| 47 | + onBlur={handleBlur} |
| 48 | + onFocus={handleFocus} |
| 49 | + > |
| 50 | + {anchor ?? <DownArrowIcon focusable="false" aria-hidden="true" />} |
| 51 | + </button> |
| 52 | + {isOpen && ( |
| 53 | + <DropdownWrapper |
| 54 | + className={classes.list} |
| 55 | + align={align} |
| 56 | + onMouseUp={() => { |
| 57 | + setTimeout(close, 0); |
| 58 | + }} |
| 59 | + onBlur={handleBlur} |
| 60 | + onFocus={handleFocus} |
| 61 | + > |
| 62 | + {children} |
| 63 | + </DropdownWrapper> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + ); |
| 67 | + } |
| 68 | +); |
| 69 | + |
| 70 | +DropdownMenu.propTypes = { |
| 71 | + /** |
| 72 | + * Provide <MenuItem> elements as children to control the contents of the menu. |
| 73 | + */ |
| 74 | + children: PropTypes.node.isRequired, |
| 75 | + /** |
| 76 | + * Can optionally override the contents of the button which opens the menu. |
| 77 | + * Defaults to <DownArrowIcon> |
| 78 | + */ |
| 79 | + anchor: PropTypes.node, |
| 80 | + 'aria-label': PropTypes.string.isRequired, |
| 81 | + align: PropTypes.oneOf(['left', 'right']), |
| 82 | + className: PropTypes.string, |
| 83 | + classes: PropTypes.shape({ |
| 84 | + button: PropTypes.string, |
| 85 | + list: PropTypes.string |
| 86 | + }) |
| 87 | +}; |
| 88 | + |
| 89 | +DropdownMenu.defaultProps = { |
| 90 | + anchor: null, |
| 91 | + align: 'right', |
| 92 | + className: '', |
| 93 | + classes: {} |
| 94 | +}; |
| 95 | + |
| 96 | +export default DropdownMenu; |
0 commit comments