1.0.11 • Published 4 years ago

react-app-menu v1.0.11

Weekly downloads
17
License
ISC
Repository
github
Last release
4 years ago

react-app-menu

A simple desktop like menu bar with hotkey and keyboard navigation

BuildStatus Dependencies CodeCoverage Maintainability

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

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 1. Include the following script tag <script src='https://unpkg.com/focus-within-polyfill/dist/focus-within-polyfill.js'></script> 2. 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

PropDescription
--reactAppMenu-color-bgBackground color of menu
--reactAppMenu-color-textText color of menu
--reactAppMenu-color-bg-hoverBackground color of menu when hovered
--reactAppMenu-color-text-hoverText color of menu when hovered
--reactAppMenu-color-bg-focusBackground color of menu when clicked /focussed
--reactAppMenu-color-text-focusText color of menu when clicked /focussed
--reactAppMenu-color-borderBorder color of menu
--reactAppMenu-color-border-separatorColor for menu separator
--reactAppMenu-border-radiusBorder radius of menu
--reactAppMenu-shadowShadow for the menu
--reactAppMenu-label-submenuHeight of submenu items
--reactAppMenu-label-rootHeight of root menu item

Example

import React, {useState} from 'react';
import {Keys, Menu, MenuBar, Separator} from 'react-app-menu';
import {AiFillFolderOpen, FaPencilAlt, FaRegFile, FiBook, GoSearch, MdSettings} from "react-icons/all";
import 'react-app-menu/dist/styles/react-app-menu.css'

export const MenuBarDemo: React.FC = () => {
    let [showToolbar, setShowToolbar] = useState(true);
    let [showTooltip, setShowTooltip] = useState(false);

    const handleMenuSelect = (menuId: string): void => {
        switch (menuId) {
            case 'toolbar':
                return setShowToolbar(!showToolbar);
            case 'toolTips':
                return setShowTooltip(!showTooltip);
            default:
                console.log(`menu selected ${menuId}`)
        }
    };

    const onFolderSelect = (): void => {
        console.log('Folder selected');
    };

    return (<div style={{background: '#FBFBFB', borderBottom: '1px solid rgb(218, 220, 224)'}}>
        <MenuBar onSelect={handleMenuSelect}>
            <Menu label='File' focusKey={"F"}>
                <Menu label='New'>
                    <Menu menuId='NewNotebook' label='Notebook' icon={<FiBook/>}/>
                    <Menu menuId="NewNote" label='Note' icon={<FaRegFile/>} hotKeys={Keys.ctrlAlt('N')}/>
                    <Separator/>
                    <Menu label="Folder" icon={<AiFillFolderOpen/>} hotKeys={Keys.ctrlAlt("F")}
                          onSelect={onFolderSelect}/>
                </Menu>
                <Menu label='Settings' icon={<MdSettings/>} hotKeys={Keys.altShift("S")}/>
            </Menu>
            <Menu label='Edit' focusKey='E'>
                <Menu menuId='search' label='Search' icon={<GoSearch/>} hotKeys={Keys.ctrlShift('F')}/>
                <Menu menuId='undo' label='Undo' hotKeys={Keys.ctrl('Z')}/>
                <Menu menuId='rename' label='Rename' icon={<FaPencilAlt/>} hotKeys={Keys.shift('F6')}/>
            </Menu>
            <Menu label='View' focusKey='V'>
                <Menu menuId='toolbar' label='Toolbars' checked={showToolbar} hotKeys={Keys.ctrlAlt("T")}/>
                <Menu menuId='statusBar' label='StatusBar'/>
                <Menu menuId='toolTips' label='Tooltips' checked={showTooltip} hotKeys={Keys.ctrlAltShift("T")}/>
            </Menu>
        </MenuBar>
    </div>);
};

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

ProptypeRequiredDefaultValueDescription
onSelect(menuId:string)=>voidA 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
expandIconstring / ReactNodeThe icon to be displayed to indicate that a menu has submenus
checkedIconstring/ ReactNodeThe icon to be displayed on a menu that has checked=true
enableHotKeysbooleantrueSet it to false if you don't require hotkeys functionality
openMenusOnHoverbooleanfalseSet it to true to open menus by hovering over them
classNamestringExtra css class to add to the markup for menubar

Menu

ProptypeRequiredDefaultValueDescription
menuIdstringAn 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
labelstring / ReactNodetrueThe text label for the menu
iconstring / ReactNodeThe icon to be displayed on a menu
onSelect()=>voidA callback function to be called when this menu is clicked or its associated hotkey is pressedIf not provided, clicks are delegated to MenuBar onSelect
hotKeysArray\\<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')
focusKeystringOnly 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
checkedbooleanA menu that has checked true will display a checkedIcon
showbooleantrueSet to false to hide this menu
disabledbooleanfalseSet to true to disable this menu
1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago