Comments (1)
Right now I'm leaning towards the best design being to add an option submenu: boolean = false
. When enabled two new controls would be added, which are detailed in the issue description for the right arrow and the left arrow, no new work would really need to be done for escape. When submenu
was true, instead of returning buttonProps
from the hook for the menu button we would return parentProps
, which would be shaped like the following:
type ParentProps = {
onKeyDown: (e: React.KeyboardEvent | React.MouseEvent) => void;
onClick: (e: React.KeyboardEvent | React.MouseEvent) => void;
tabIndex: -1;
ref: React.RefObject<T>;
role: 'menuitem';
'aria-haspopup': true;
'aria-expanded': boolean;
};
Very similar to buttonProps
with some small differences. On the users side, it would be expected they'd be using the hook in some fashion similar to this:
div[role='menu'] {
visibility: hidden;
}
div[role='menu'].visible {
visibility: visible;
}
// Inner menu
const Submenu = React.forwardRef((props: {
parentMenuItem: {
text: string;
children: { text: string }[];
},
...props,
}, ref) => {
const { parentProps, itemProps, isOpen } = useDropdownMenu(props.parentMenuItem.children.length, { submenu: true });
return (
<React.Fragment>
<a {...parentProps} ref={ref}>Parent menu item</a>
<div className={isOpen ? 'visible' : ''} role='menu'>
{props.parentMenuItem.children.map((child, i) =>
<a {...itemProps[i]}>{child.text}</a>
)}
</div>
</React.Fragment>
);
})
// Outer menu
const DropdownMenu = () => {
const items = [
{
text: 'I am a submenu',
children: [
{
text: 'I am a child of a submenu'
}
}
];
const { buttonProps, itemProps, isOpen } = useDropdownMenu(items.length);
return (
<React.Fragment>
<button {...buttonProps}>Example</button>
<div className={isOpen ? 'visible' : ''} role='menu'>
<Submenu {...itemProps[0]} parentMenuItem={items[0]} />
</div>
</React.Fragment>
);
}
There's probably some issues with what I've drafted above but that would be the general idea, keep the hook mostly the same, implement the new submenu
option and related behavior, then expect developer's to leverage that behavior by encapsulating their submenus into their own components. This leverages all the behavior built into the hook already, and still does a lot of work for developers wanting to build menus with submenus while using our hook.
from react-accessible-dropdown-menu-hook.
Related Issues (20)
- Some suggestions HOT 9
- First menu item only receives focus if menu is opened via keyboard controls HOT 1
- Support closing menu with escape key while the menu button has focus HOT 1
- Add option to choose whether to focus first item on click
- MenuItems as Link of React Router HOT 1
- Reduce number of variables documented in some cases
- subMenu support? HOT 2
- How to use this hook in complex codebases HOT 3
- If you hold down the escape key then click the dropdown, it doesn't open again HOT 7
- React Native support HOT 2
- Typescript errors when using buttons in dropdown HOT 2
- Usage of deprecated method HOT 2
- Resizing elements broken on phone
- Difference to downshift? HOT 1
- Can't use moveFocus to focus on the currently selected option HOT 3
- Hovering over "Get started" button hides text HOT 1
- Change instances of `user.type` to `user.keyboard`
- Rework focus on click behavior HOT 5
- Update documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-accessible-dropdown-menu-hook.