Skip to content

Commit

Permalink
feat(menu-surface): add component (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Goo authored Oct 3, 2018
1 parent 97ecb3a commit 034abd0
Show file tree
Hide file tree
Showing 13 changed files with 1,085 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Component | Spec | MDC Web
[Floating Label](./packages/floating-label) | [Text Field Design Page](https://material.io/design/components/text-fields.html) | [MDC Floating Label](https://github.com/material-components/material-components-web/tree/master/packages/mdc-floating-label)
[Line Ripple](./packages/line-ripple) | [Text Field Design Page](https://material.io/design/components/text-fields.html) | [MDC Line Ripple](https://github.com/material-components/material-components-web/tree/master/packages/mdc-line-ripple)
[Material Icon](./packages/material-icon) | [Material Icon Design Page](https://material.io/design/iconography/system-icons.html#design-principles) | [Material Icon Tool](https://material.io/tools/icons/?style=baseline)
[Menu Surface](./packages/menu-surface) | [Menu Surface Design Page](https://material.io/design/components/menus.html#design-principles) | [MDC Menu Surface](https://github.com/material-components/material-components-web/tree/master/packages/mdc-menu-surface)
[Notched Outline](./packages/notched-outline) | [Text Field Design Page](https://material.io/design/components/text-fields.html) | [MDC Notched Outline](https://github.com/material-components/material-components-web/tree/master/packages/mdc-notched-outline)
[Ripple](./packages/ripple) | [Ripple Design Page](https://material.io/design/interaction/states.html) | [MDC Ripple](https://github.com/material-components/material-components-web/tree/master/packages/mdc-ripple)
[Select](./packages/select) | [Select Design Page](https://material.io/design/components/menus.html#) | [MDC Select](https://github.com/material-components/material-components-web/tree/master/packages/mdc-select)
Expand Down
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"floating-label",
"icon-button",
"line-ripple",
"menu-surface",
"ripple",
"select",
"top-app-bar",
Expand All @@ -60,6 +61,7 @@
"@material/icon-button": "^0.40.0",
"@material/line-ripple": "^0.40.0",
"@material/list": "^0.40.0",
"@material/menu-surface": "^0.40.0",
"@material/notched-outline": "^0.40.0",
"@material/ripple": "^0.40.0",
"@material/select": "^0.40.0",
Expand Down
1 change: 1 addition & 0 deletions packages/menu-surface/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.js
151 changes: 151 additions & 0 deletions packages/menu-surface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# React Menu Surface

A React version of an [MDC Menu Surface](https://github.com/material-components/material-components-web/tree/master/packages/mdc-menu-surface).

## Installation

```
npm install @material/react-menu-surface
```

## Usage

### Styles

with Sass:
```js
import '@material/react-menu-surface/index.scss';
```

with CSS:
```js
import '@material/react-menu-surface/dist/menu-surface.css';
```

### Javascript Instantiation

#### Anchored to Element

React Menu Surface accepts one child element. Please see the below example if you need to anchor the menu surface to a specific element. In this case, we wrapper `<button>` and `<MenuSurface>` element within the anchor, and give the `mdc-menu-surface--anchor` class to the element. You can specify a corner for the menu surface to appear from, otherwise it defaults to top-left. For a list of different anchor corner values, please see the [MDC Web Menu Surface constants.js file](https://github.com/material-components/material-components-web/blob/master/packages/mdc-menu-surface/constants.js#L74).

> NOTE: `<MenuSurface>` also has an `onClose` callback method prop, which is called when the menu closes. Use this as an opportunity to update your application's state.
```js
import React from 'react';
import MenuSurface, {Corner} from '@material/react-menu-surface';
import Button from '@material/react-button';

class MyApp extends React.Component {
state = {
open: false,
anchorElement: null,
};

setAnchorElement = (element) => {
if (this.state.anchorElement) {
return;
}
this.setState({anchorElement: element});
}

render() {
return (
<div
className='mdc-menu-surface--anchor'
ref={this.setAnchorElement}
>
<Button raised onClick={() => this.setState({open: true})}>Open Menu</Button>

<MenuSurface
open={this.state.open}
anchorCorner={Corner.BOTTOM_LEFT}
onClose={() => this.setState({open: false})}
anchorElement={anchorElement}
>
<img
style={{maxWidth: '20vw', maxHeight: '20vh'}}
src='http://images.my.photo.url' />
</MenuSurface>
</div>
);
}
}
```
#### Anchored to Coordinates (right-click)

You may want to anchor your menu surface to x, y coordinates. One example being a right-click contextmenu. Instead of passing an `anchorElement` you need to pass `coordinates`.


```js
import React from 'react';
import MenuSurface from '@material/react-menu-surface';

class MyApp extends React.Component {
state = {
open: false,
};

componentDidMount() {
this.rightClickCallback_ = (evt) => {
this.setState({
open: true,
coordinates: {x: evt.clientX, y: evt.clientY},
});
evt.preventDefault();
};

window.addEventListener('contextmenu', this.rightClickCallback_);
}

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

render() {
return (
<div className='my-app'>
<h1>Menu Surface</h1>

<MenuSurface
open={this.state.open}
onClose={() => this.setState({open: false, coordinates: null})}
coordinates={coordinates}
>
<img
style={{maxWidth: '20vw', maxHeight: '20vh'}}
src='http://images.my.photo.url' />
</MenuSurface>
</div>
);
}
}
```

> NOTE: The React menu surface component is always hoisted to the body. This is one place the component differs from MDC Web's version, which gives the option of hoisting/not hoisting. Always hoisting the menu surface to the body allows fixes a few issues, and simplifies the component API.
## Props

Prop Name | Type | Description
--- | --- | ---
className | String | Classes to be applied to the root element.
anchorCorner | Corner | Sets the corner that the menu surface will be anchored to. See [MDC Web constants.js](https://github.com/material-components/material-components-web/blob/master/packages/mdc-menu-surface/constants.js#L74).
anchorElement | Element | Sets the anchor element used as an anchor for `menu-surface` positioning logic. Should contain class `.mdc-menu-surface--anchor`.
anchorMargin | Object* | Sets the distance from the anchor point that the menu surface should be shown.
coordinates | {x: Number, y: Number} | Sets the anchor coordinates when menu surface does not use anchorElement.
open | Boolean | Opens menu surface when true.
quickOpen | Boolean | Disables the open/close animation of the menu surface, which makes the open/close instant.
fixed | Boolean | Sets the menu surface to fixed positioning.
onClose | Function | Callback triggered after menu surface closes.

> * | AnchorMargin takes the form of {top: Number, bottom: Number , left: Number , right : Number}
## 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](https://github.com/material-components/material-components-web/blob/master/packages/mdc-menu-surface/README.md#advanced-sass-mixins)

## Usage with Icons

Please see our [Best Practices doc](../../docs/best-practices.md#importing-font-icons) when importing or using icon fonts.
Loading

0 comments on commit 034abd0

Please sign in to comment.