UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

232 lines (195 loc) 6.63 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; import React from 'react'; import PropTypes from 'prop-types'; import ReactDOM from 'react-dom'; import ownerDocument from '../utils/ownerDocument'; import List from '../List'; import getScrollbarSize from '../utils/getScrollbarSize'; import { useForkRef } from '../utils/reactHelpers'; function nextItem(list, item, disableListWrap) { if (list === item) { return list.firstChild; } if (item && item.nextElementSibling) { return item.nextElementSibling; } return disableListWrap ? null : list.firstChild; } function previousItem(list, item, disableListWrap) { if (list === item) { return disableListWrap ? list.firstChild : list.lastChild; } if (item && item.previousElementSibling) { return item.previousElementSibling; } return disableListWrap ? null : list.lastChild; } function textCriteriaMatches(nextFocus, textCriteria) { if (textCriteria === undefined) { return true; } let text = nextFocus.innerText; if (text === undefined) { // jsdom doesn't support innerText text = nextFocus.textContent; } if (text === undefined) { return false; } text = text.trim().toLowerCase(); if (text.length === 0) { return false; } if (textCriteria.repeating) { return text[0] === textCriteria.keys[0]; } return text.indexOf(textCriteria.keys.join('')) === 0; } function moveFocus(list, currentFocus, disableListWrap, traversalFunction, textCriteria) { let wrappedOnce = false; let nextFocus = traversalFunction(list, currentFocus, currentFocus ? disableListWrap : false); while (nextFocus) { // Prevent infinite loop. if (nextFocus === list.firstChild) { if (wrappedOnce) { return false; } wrappedOnce = true; } // Move to the next element. if (!nextFocus.hasAttribute('tabindex') || nextFocus.disabled || nextFocus.getAttribute('aria-disabled') === 'true' || !textCriteriaMatches(nextFocus, textCriteria)) { nextFocus = traversalFunction(list, nextFocus, disableListWrap); } else { nextFocus.focus(); return true; } } return false; } const useEnhancedEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect; const MenuList = React.forwardRef(function MenuList(props, ref) { const { actions, autoFocus = false, className, onKeyDown, disableListWrap = false } = props, other = _objectWithoutPropertiesLoose(props, ["actions", "autoFocus", "className", "onKeyDown", "disableListWrap"]); const listRef = React.useRef(null); const textCriteriaRef = React.useRef({ keys: [], repeating: true, previousKeyMatched: true, lastTime: null }); useEnhancedEffect(() => { if (autoFocus) { listRef.current.focus(); } }, [autoFocus]); React.useImperativeHandle(actions, () => ({ adjustStyleForScrollbar: (containerElement, theme) => { // Let's ignore that piece of logic if users are already overriding the width // of the menu. const noExplicitWidth = !listRef.current.style.width; if (containerElement.clientHeight < listRef.current.clientHeight && noExplicitWidth) { const scrollbarSize = `${getScrollbarSize(true)}px`; listRef.current.style[theme.direction === 'rtl' ? 'paddingLeft' : 'paddingRight'] = scrollbarSize; listRef.current.style.width = `calc(100% + ${scrollbarSize})`; } return listRef.current; } }), []); const handleKeyDown = event => { const list = listRef.current; const key = event.key; /** * @type {Element} - will always be defined since we are in a keydown handler * attached to an element. A keydown event is either dispatched to the activeElement * or document.body or document.documentElement. Only the first case will * trigger this specific handler. */ const currentFocus = ownerDocument(list).activeElement; if (key === 'ArrowDown') { event.preventDefault(); moveFocus(list, currentFocus, disableListWrap, nextItem); } else if (key === 'ArrowUp') { event.preventDefault(); moveFocus(list, currentFocus, disableListWrap, previousItem); } else if (key === 'Home') { event.preventDefault(); moveFocus(list, null, disableListWrap, nextItem); } else if (key === 'End') { event.preventDefault(); moveFocus(list, null, disableListWrap, previousItem); } else if (key.length === 1) { const criteria = textCriteriaRef.current; const lowerKey = key.toLowerCase(); const currTime = performance.now(); if (criteria.keys.length > 0) { // Reset if (currTime - criteria.lastTime > 500) { criteria.keys = []; criteria.repeating = true; criteria.previousKeyMatched = true; } else if (criteria.repeating && lowerKey !== criteria.keys[0]) { criteria.repeating = false; } } criteria.lastTime = currTime; criteria.keys.push(lowerKey); const keepFocusOnCurrent = currentFocus && !criteria.repeating && textCriteriaMatches(currentFocus, criteria); if (criteria.previousKeyMatched && (keepFocusOnCurrent || moveFocus(list, currentFocus, false, nextItem, criteria))) { event.preventDefault(); } else { criteria.previousKeyMatched = false; } } if (onKeyDown) { onKeyDown(event); } }; const handleOwnRef = React.useCallback(instance => { // #StrictMode ready listRef.current = ReactDOM.findDOMNode(instance); }, []); const handleRef = useForkRef(handleOwnRef, ref); return React.createElement(List, _extends({ role: "menu", ref: handleRef, className: className, onKeyDown: handleKeyDown, tabIndex: autoFocus ? 0 : -1 }, other)); }); process.env.NODE_ENV !== "production" ? MenuList.propTypes = { /** * @ignore */ actions: PropTypes.shape({ current: PropTypes.object }), /** * If `true`, the list will be focused during the first mount. * Focus will also be triggered if the value changes from false to true. */ autoFocus: PropTypes.bool, /** * MenuList contents, normally `MenuItem`s. */ children: PropTypes.node, /** * @ignore */ className: PropTypes.string, /** * If `true`, the menu items will not wrap focus. */ disableListWrap: PropTypes.bool, /** * @ignore */ onKeyDown: PropTypes.func } : void 0; export default MenuList;