-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathMenubarItem.jsx
64 lines (54 loc) · 1.54 KB
/
MenubarItem.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import PropTypes from 'prop-types';
import React, { useContext, useMemo } from 'react';
import { useSelector } from 'react-redux';
import ButtonOrLink from '../../common/ButtonOrLink';
import { MenubarContext, ParentMenuContext } from './contexts';
function MenubarItem({
hideIf,
className,
role: customRole,
selected,
...rest
}) {
const parent = useContext(ParentMenuContext);
const { createMenuItemHandlers } = useContext(MenubarContext);
const handlers = useMemo(() => createMenuItemHandlers(parent), [
createMenuItemHandlers,
parent
]);
if (hideIf) {
return null;
}
const role = customRole || 'menuitem';
const ariaSelected = role === 'option' ? { 'aria-selected': selected } : {};
// change sub menu direction
let newClassName = 'nav__dropdown-item';
const direction = useSelector((state) => state.preferences.direction);
if (direction === 'rtl') newClassName = 'rtl-nav__dropdown-item';
return (
<li className={newClassName}>
<ButtonOrLink {...rest} {...handlers} {...ariaSelected} role={role} />
</li>
);
}
MenubarItem.propTypes = {
...ButtonOrLink.propTypes,
onClick: PropTypes.func,
value: PropTypes.string,
/**
* Provides a way to deal with optional items.
*/
hideIf: PropTypes.bool,
className: PropTypes.string,
role: PropTypes.oneOf(['menuitem', 'option']),
selected: PropTypes.bool
};
MenubarItem.defaultProps = {
onClick: null,
value: null,
hideIf: false,
className: 'nav__dropdown-item',
role: 'menuitem',
selected: false
};
export default MenubarItem;