react-app-menu
A simple desktop like menu bar with hotkey and keyboard navigation
Introduction
React App Menu is a simple React component that renders navigation menu similar to desktop applications with support for hotkeys and keyboard navigation. It aims to do most things through css alone and relies on javascript only when absolutely necessary. This makes the code extremely small (< 20kb) and together with the css, the whole library is less than 25 kb without any compression
Installation
npm install react-app-menu
Demo
Browser Support
This component relies on the css pseudo selector focus-within to open menus. This should work on Chrome and Firefox out of the box. For browsers that don't support the focus-within pseduo selector (notably IE and version of Edge prior to Edge 79), you will need to include a small polyfill focus-within-polyfill. The demo above is using Edge with the polyfill
To use the polyfill, you can either
- Include the following script tag
<script src='https://unpkg.com/focus-within-polyfill/dist/focus-within-polyfill.js'></script>
- Install and import the focus-within-polyfill
This polyfill will only kick in for browsers that don't support the focus-within polyfill and adds a listener for blur and focus events and adds a .focus-within class as necessary.
Usage
Simply import MenuBar and Menu and start composing your menu. A css is also provided in dist/styles/react-app-menu.css
.
Be sure to import the css
Customizing the themes
In order to customize the look of the menu, certain css variables are provided that you can override on your menu
Prop | Description |
---|---|
--reactAppMenu-color-bg | Background color of menu |
--reactAppMenu-color-text | Text color of menu |
--reactAppMenu-color-bg-hover | Background color of menu when hovered |
--reactAppMenu-color-text-hover | Text color of menu when hovered |
--reactAppMenu-color-bg-focus | Background color of menu when clicked /focussed |
--reactAppMenu-color-text-focus | Text color of menu when clicked /focussed |
--reactAppMenu-color-border | Border color of menu |
--reactAppMenu-color-border-separator | Color for menu separator |
--reactAppMenu-border-radius | Border radius of menu |
--reactAppMenu-shadow | Shadow for the menu |
--reactAppMenu-label-submenu | Height of submenu items |
--reactAppMenu-label-root | Height of root menu item |
Example
;;; ;
Props
MenuBar
Menubar should be the container of all menus. It provides some options that are common to all menus. It is also responsible for registering keyboard event handlers for navigation and hotkeys
Prop | type | Required | DefaultValue | Description |
---|---|---|---|---|
onSelect | (menuId:string)=>void | A function that is called with the menuId of the menu that was clicked. This function is only called when the menu does not have its own handler |
||
expandIcon | string / ReactNode | ⮞ | The icon to be displayed to indicate that a menu has submenus | |
checkedIcon | string/ ReactNode | ✔ | The icon to be displayed on a menu that has checked=true | |
enableHotKeys | boolean | true | Set it to false if you don't require hotkeys functionality | |
openMenusOnHover | boolean | false | Set it to true to open menus by hovering over them | |
className | string | Extra css class to add to the markup for menubar |
| |
Menu
Prop | type | Required | DefaultValue | Description |
---|---|---|---|---|
menuId | string | An identifier that uniquely identifies the menu. If a menu does not have its own select handler, then the select handler of the parent menuBar is called with the menuId | ||
label | string / ReactNode | true | The text label for the menu | |
icon | string / ReactNode | The icon to be displayed on a menu | ||
onSelect | ()=>void | A callback function to be called when this menu is clicked or its associated hotkey is pressed If not provided, clicks are delegated to MenuBar onSelect |
||
hotKeys | Array\\<string\\> | Hotkeys are used to trigger the action for the menu. Hotkeys should be an array of form \['Ctrl','Alt','F'\]. You can use the Keys helper to generate this, for eg Keys.ctrlAlt('F') | ||
focusKey | string | Only applicable for root menus. The key which when pressed with Alt will trigger open the menu, eg if File menu has focus key F, pressing Alt F will open the File menu | ||
checked | boolean | A menu that has checked true will display a checkedIcon | ||
show | boolean | true | Set to false to hide this menu | |
disabled | boolean | false | Set to true to disable this menu |