npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@material/react-menu

v0.15.0

Published

Material Components React Menu

Downloads

4,736

Readme

React Menu

A React version of an MDC Menu.

Installation

npm install @material/react-menu

Usage

Styles

with Sass:

import '@material/react-list/index.scss';
import '@material/react-menu-surface/index.scss';
import '@material/react-menu/index.scss';

with CSS:

import '@material/react-list/dist/menu.css';
import '@material/react-menu-surface/dist/menu.css';
import '@material/react-menu/dist/menu.css';

Javascript Instantiation

import React from 'react';
import Menu, {MenuList, MenuListItem, MenuListItemText} from '@material/react-menu';

class MyApp extends React.Component {
  state = {
    open: true,
    coordinates: undefined,
  };

  componentDidMount() {
    window.addEventListener('contextmenu', this.rightClickCallback);
  }

  componentWillUnmount() {
    window.removeEventListener('contextmenu', this.rightClickCallback);
  }

  rightClickCallback = (event) => {
    this.setState({
      open: !this.state.open,
      coordinates: {x: event.clientX, y: event.clientY},
    });
    // Must preventDefault so the system context menu doesn't appear.
    // This won't be needed in other cases besides right click.
    event.preventDefault();
  }

  // Must set open to false to keep menu in the correct state.
  // This does not follow the controlled component pattern
  // (see https://reactjs.org/docs/forms.html#controlled-components).
  // Follow https://github.com/material-components/material-components-web-react/issues/785
  // to get any updates.
  onClose = () => {
    this.setState({open: false});
  }

  render() {
    const menuOptions = [
      'Save',
      'Edit',
      'Cut',
      'Copy',
      'Paste',
    ];

    return (
      <Menu
        open={this.state.open}
        onClose={this.onClose}
        coordinates={this.state.coordinates}
        onSelected={(index, item) => console.log(index, item)}
      >
        <MenuList>
          {menuOptions.map((option, index) => (
            <MenuListItem key={index}>
              <MenuListItemText primaryText={option} />
              {/* You can also use other components from list, which are documented below */}
            </MenuListItem>
          ))}
        </MenuList>
      </Menu>
    );
  }
}

Usage with HOC's

You may want to use Menu or other auxilary components with an HOC, such as styled-components. You can wrap Menu using the following:

import React from 'react';
import Menu, {MenuList, MenuListItem, MenuListItemText} from '@material/react-menu';
import styled from 'styled-components';

interface MenuState {
  coordinates?: {x: number, y: number};
  open: boolean;
};

const StyledMenuListItem = styled(MenuListItem)`
  color: blue;
`;

class MenuScreenshotTest extends React.Component<{}, MenuState> {

  state = {
    open: true,
  };

  onClose = () => {
    this.setState({open: false});
  }

  render() {
    const menuOptions = [
      'Save',
      'Edit',
      'Cut',
      'Copy',
      'Paste',
    ];

    return (
      <Menu
        open={this.state.open}
        onClose={this.onClose}
        onSelected={() => console.log('select')}
      >
        <MenuList>
          {menuOptions.map((option, index) => (
            <StyledMenuListItem key={index}>
              <MenuListItemText primaryText={option} />
            </StyledMenuListItem>
          ))}
        </MenuList>
      </Menu>
    );
  }
}

Props

Menu

Prop Name | Type | Description --- | --- | --- onSelected | (index: number, item: Element) => void | Callback that is triggered when a menu list item is selected. onClose | () => void | Callback that is triggered when the menu closes. open | boolean | If true, will open the menu. If false, will hide menu.

NOTE: onClose and open are a subset of props from Menu Surface. See Menu Surface Props for other props you can pass to Menu

MenuList

Prop Name | Type | Description --- | --- | --- innerRef | RefObject | Root Menu List element ref. handleSelect | (activatedItemIndex: Number, selected: Number | Array) => void | Callback for handling a list item selection event. selected will be an Array,Number> for checkbox lists.

NOTE: handleSelect is a subset of props from List. See List Props for other props you can pass to MenuList.

MenuListItem

See List Props for other props you can pass to MenuListItem.

MenuListItemText

See List Item Text Props for other props you can pass to MenuListItemText.

MenuListItemGraphic

See List Item Graphic Props for other props you can pass to MenuListItemGraphic.

MenuListItemMeta

See List Item Meta Props for other props you can pass to MenuListItemMeta.

MenuListDivider

See List Divider Props for other props you can pass to MenuListDivider.

MenuListGroup

See List Group Props for other props you can pass to MenuListGroup.

MenuListGroupSubheader

See List Group Subheader Props for other props you can pass to MenuListGroupSubheader.

Sass Mixins

Sass mixins may be available to customize various aspects of the components. Please refer to the MDC Web repository for more information on what mixins are available, and how to use them.

Advanced Sass Mixins

Usage with Icons

Please see our Best Practices doc when importing or using icon fonts.