Skip to content

Instantly share code, notes, and snippets.

@simple17
Last active February 14, 2019 12:11
Show Gist options
  • Save simple17/481a05089ab7b198deaa98384e545a0c to your computer and use it in GitHub Desktop.
Save simple17/481a05089ab7b198deaa98384e545a0c to your computer and use it in GitHub Desktop.
[Simple contex menu] #react
import React, { Component } from 'react';
import './uk-product-context-menu.scss';
import Icon from '../Icon';
const ContextMenuButton = (props) => (
<div className="uk-product-context-menu__toggle-trigger" role="button" {...props}>
<Icon type="expand"/>
</div>
);
const ContextMenuOption = ({ onActionClick, icon, label }) => (
<li
onClick={(event) => {
event.preventDefault();
onActionClick();
}}
className="uk-product-context-menu__action">
<Icon type={icon}/>
<span>{label}</span>
</li>
);
class ContextMenu extends Component {
constructor(props) {
super(props);
this.state = {
showActions: false,
}
this.show = this.show.bind(this);
this.hide = this.hide.bind(this);
}
show(event) {
if (event) {
event.preventDefault();
}
this.setState({
showActions: true
}, () => {
document.addEventListener('click', this.hide);
});
}
hide(event) {
if (event) {
event.preventDefault();
}
this.setState({
showActions: false
}, () => {
document.removeEventListener('click', this.hide);
});
}
render() {
const {
actions,
} = this.props;
const {
showActions,
} = this.state;
return (
<div className="uk-product-context-menu">
<ContextMenuButton onClick={showActions ? this.hide : this.show} />
{
showActions && (
<div className="uk-product-context-menu__actions" >
{
actions.map((action, i) => (
<ContextMenuOption
key={i}
{...action} />
))
}
</div>
)
}
</div>
);
}
}
export default ContextMenu;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment