Skip to content

Commit

Permalink
Add onSelect handler and tabindexes when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Mar 16, 2021
1 parent 1182ad3 commit 6151d81
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/ui/menu/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as styles from './styles';
* @param {import('react').Ref<any>} forwardedRef
*/
function Menu( props, forwardedRef ) {
const { children, className, menu, ...otherProps } = useContextSystem(
const { as, children, className, menu, ...otherProps } = useContextSystem(
props,
'Menu'
);
Expand All @@ -47,7 +47,7 @@ function Menu( props, forwardedRef ) {

const classes = cx( styles.Menu, className );
const menuProps = menu || {};
const Component = menu ? ReakitMenu : View;
const Component = as || menu ? ReakitMenu : View;

return (
<MenuContext.Provider value={ contextProps }>
Expand Down
34 changes: 31 additions & 3 deletions packages/components/src/ui/menu/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ import * as styles from './styles';
*/
function MenuItem( props, forwardedRef ) {
const {
as,
children,
className,
closeOnClick = false,
isBack = false,
isOffset = false,
isSelected,
onClick = noop,
onSelect = noop,
onKeyDown = noop,
prefix,
showArrow = false,
size,
suffix,
tabIndex: tabIndexProp,
...otherProps
} = useContextSystem( props, 'MenuItem' );

Expand All @@ -56,7 +60,7 @@ function MenuItem( props, forwardedRef ) {
className
);

const Component = menu ? ReakitMenuItem : View;
const Component = as || ( menu ? ReakitMenuItem : View );

const prevArrow = useMemo(
() =>
Expand Down Expand Up @@ -112,15 +116,37 @@ function MenuItem( props, forwardedRef ) {
}, [ nextArrow, selectedContent, suffix ] );

const handleOnClick = useCallback(
( event ) => {
( /** @type {import('react').MouseEvent<HTMLDivElement>} */ event ) => {
onClick( event );
if ( menu?.hide && closeOnClick ) {
menu.hide();
} else {
onSelect( event );
}
},
[ closeOnClick, menu, onClick ]
[ closeOnClick, menu, onClick, onSelect ]
);

const handleOnKeyDown = useCallback(
(
/** @type {import('react').KeyboardEvent<HTMLDivElement>} */ event
) => {
onKeyDown( event );
switch ( event.key ) {
case 'Enter':
case 'Space':
case ' ':
onSelect( event );
break;
default:
break;
}
},
[ onSelect, onKeyDown ]
);

const tabIndex = tabIndexProp || ( onSelect !== noop ? 0 : undefined );

return (
<BaseButton
as={ Component }
Expand All @@ -130,11 +156,13 @@ function MenuItem( props, forwardedRef ) {
{ ...menu }
className={ classes }
onClick={ handleOnClick }
onKeyDown={ handleOnKeyDown }
pre={ prefixContent }
ref={ forwardedRef }
size={ size }
suffix={ suffixContent }
textAlign="left"
tabIndex={ tabIndex }
>
{ children }
</BaseButton>
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/ui/menu/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const Example = () => {
<MenuHeader>WordPress.org</MenuHeader>
<MenuItem
isSelected={ isSelected }
onClick={ () => setIsSelected( ! isSelected ) }
onKeyDown={ () => setIsSelected( ! isSelected ) }
onSelect={ () => setIsSelected( ! isSelected ) }
>
Code is Poetry
</MenuItem>
Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/ui/menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
// eslint-disable-next-line no-restricted-imports
import type { MenuStateReturn } from 'reakit';
// eslint-disable-next-line no-restricted-imports
import type { MouseEvent, KeyboardEvent } from 'react';

/**
* Internal dependencies
Expand All @@ -13,6 +15,8 @@ export type Props = {
menu?: MenuStateReturn;
};

export type SelectEvent = MouseEvent | KeyboardEvent;

export type MenuItemProps = BaseButtonProps & {
/**
* Whether to close the menu when the item is clicked.
Expand Down Expand Up @@ -40,4 +44,8 @@ export type MenuItemProps = BaseButtonProps & {
* @default false
*/
showArrow?: boolean;
/**
* Called when the menu item is clicked or when a keyDown event happens that is either Enter or Space.
*/
onSelect?: ( event: SelectEvent ) => void;
};

0 comments on commit 6151d81

Please sign in to comment.