Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix(Menu): Don't call popover.update on open, #115
Browse files Browse the repository at this point in the history
Also updates use-item-list to the latest version,
allowing to simplify some highlighting logic
  • Loading branch information
diondiondion committed Oct 27, 2020
1 parent 1ca64d1 commit 93e7d81
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"react-merge-refs": "^1.0.0",
"react-popper": "^2.2.3",
"react-resize-aware": "^3.0.1",
"use-item-list": "0.0.1"
"use-item-list": "0.1.1"
},
"commitlint": {
"extends": [
Expand Down
14 changes: 5 additions & 9 deletions src/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function Menu({id, menuPlacement = 'bottom', menuPositionFixed, children}) {
onOpen: () => {
menuListRef.current?.focus();
itemList.setHighlightedItem(0);
popover.update();
},
onClose: () => {
// If focus is on or within the popover when it's closed,
Expand Down Expand Up @@ -64,7 +63,7 @@ function Menu({id, menuPlacement = 'bottom', menuPositionFixed, children}) {
const isFocusInMenuOrOnButton =
buttonRef.current === document.activeElement ||
popoverRef.current === document.activeElement ||
popoverRef.current.contains(document.activeElement);
popoverRef.current?.contains(document.activeElement);

if (isFocusInMenuOrOnButton) {
if (event.keyCode === KEY_CODES.ARROW_UP) {
Expand All @@ -79,16 +78,13 @@ function Menu({id, menuPlacement = 'bottom', menuPositionFixed, children}) {
event.keyCode === KEY_CODES.SPACE ||
event.keyCode === KEY_CODES.ENTER
) {
if (itemList.highlightedIndex.current !== null) {
const item = itemList.getHighlightedItem();
if (item) {
event.preventDefault();
// Trigger a click on the highlighted item
// to select it (unless it's disabled)
const item =
itemList.items.current[
itemList.highlightedIndex.current
];
if (item && !item.ref.current.ariaDisabled) {
item.ref.current.click();
if (!item.ref.current?.ariaDisabled) {
item.ref.current?.click();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function MenuItem({icon, isDisabled, onClick, children}) {
ref: itemRef,
text: children,
value: children,
disabled: isDisabled,
});

return (
Expand Down
21 changes: 7 additions & 14 deletions src/NavMenu/NavMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function NavMenu({id, menuPlacement, menuPositionFixed, children}) {
});

const {isOpen, open, close, toggle} = usePopoverState({
onOpen: popover.update,
onClose: () => {
itemList.clearHighlightedItem();

Expand All @@ -47,11 +46,8 @@ function NavMenu({id, menuPlacement, menuPositionFixed, children}) {
},
});

function onHighlight(index) {
const highlightedItem = itemList.items.current[index];
if (highlightedItem) {
highlightedItem.ref.current.focus();
}
function onHighlight(item) {
item?.ref.current?.focus();
}

function onSelect() {
Expand All @@ -65,7 +61,7 @@ function NavMenu({id, menuPlacement, menuPositionFixed, children}) {
const isFocusInMenuOrOnButton =
buttonRef.current === document.activeElement ||
popoverRef.current === document.activeElement ||
popoverRef.current.contains(document.activeElement);
popoverRef.current?.contains(document.activeElement);

if (isFocusInMenuOrOnButton) {
if (event.keyCode === KEY_CODES.ARROW_UP) {
Expand All @@ -80,14 +76,11 @@ function NavMenu({id, menuPlacement, menuPositionFixed, children}) {
event.keyCode === KEY_CODES.SPACE ||
event.keyCode === KEY_CODES.ENTER
) {
if (itemList.highlightedIndex.current !== null) {
const item = itemList.getHighlightedItem();
if (item) {
event.preventDefault();
const item =
itemList.items.current[
itemList.highlightedIndex.current
];
if (item) {
item.ref.current.click();
if (!event.ctrlKey && !event.metaKey) {
item.ref.current?.click();
}
itemList.selectHighlightedItem();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Select/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as MenuListUI from '../MenuList';

import {SelectContext} from './Select';

function Option({value, icon, children}) {
function Option({value, icon, isDisabled, children}) {
const itemRef = useRef();
const {itemList, setSelectedItem} = useContext(SelectContext);
const {
Expand All @@ -19,7 +19,7 @@ function Option({value, icon, children}) {
ref: itemRef,
value: value ?? children,
text: children,
// disabled: isDisabled,
disabled: isDisabled,
});

useEffect(() => {
Expand All @@ -44,15 +44,15 @@ function Option({value, icon, children}) {
id={id}
role="option"
aria-selected={selected ? 'true' : null}
// aria-disabled={isDisabled ? 'true' : null}
aria-disabled={isDisabled ? 'true' : null}
onClick={select}
onMouseEnter={highlight}
onMouseLeave={clearHighlightedItem}
>
<MenuListUI.Link
forwardedAs="span"
isActive={selected}
// isDisabled={isDisabled}
isDisabled={isDisabled}
isHighlighted={useHighlighted()}
>
{icon && <MenuListUI.ItemIcon name={icon} />}
Expand All @@ -79,7 +79,7 @@ Option.propTypes = {
/**
* Disable the option
*/
// isDisabled: PropTypes.bool,
isDisabled: PropTypes.bool,
};

// @component
Expand Down
6 changes: 5 additions & 1 deletion src/Select/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ There are known issues around this pattern when using VoiceOver with Safari on m
onChange={setSelectedValue}
>
{options.map(item => (
<Option key={item} value={item}>
<Option
key={item}
value={item}
isDisabled={item === 'Mango'}
>
{item}
</Option>
))}
Expand Down
8 changes: 3 additions & 5 deletions src/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ function Select({
adaptivePositioning: true,
});

const {isOpen, open, close, toggle} = usePopoverState({
onOpen: popover.update,
});
const {isOpen, open, close, toggle} = usePopoverState();

const itemList = useItemList({
id,
selected: value,
onSelect: value => {
onChange(value);
onSelect: selectedItem => {
onChange(selectedItem.value);
close();
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/usePopoverState/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ function usePopoverState({openDelay = 0, onOpen, onClose} = {}) {
}

function close() {
setOpen(false);
if (onClose) {
onClose();
}
setOpen(false);
resetTimeout();
}

Expand Down

0 comments on commit 93e7d81

Please sign in to comment.