-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathcontextualMenu.jsx
More file actions
101 lines (86 loc) · 2.71 KB
/
contextualMenu.jsx
File metadata and controls
101 lines (86 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React, { Component } from "react";
import PropTypes from "prop-types";
export default class ContextualMenu extends Component {
constructor() {
super();
this.closeAllDropdowns = this.closeAllDropdowns.bind(this);
}
componentDidMount() {
// use window instead of document, as React catches all events in document
window.addEventListener("click", this.closeAllDropdowns);
}
componentWillUnmount() {
window.removeEventListener("click", this.closeAllDropdowns);
}
itemClickHandler(event) {
this.closeAllDropdowns();
event.preventDefault(); // prevent link from changing URL
event.stopPropagation(); // prevent event from propagating to parent button and opening dropdown again
}
dropdownButtonClick(event) {
const dropdownEl = event.target.nextSibling;
const isClosed = dropdownEl.getAttribute("aria-hidden") === "true";
const tooltipMessage = dropdownEl.parentNode.previousSibling;
this.closeAllDropdowns();
if (tooltipMessage) {
tooltipMessage.classList.remove("u-hide");
}
if (isClosed && dropdownEl) {
dropdownEl.setAttribute("aria-hidden", false);
if (tooltipMessage) {
tooltipMessage.classList.add("u-hide");
}
}
event.stopPropagation();
}
closeAllDropdowns() {
[].slice
.call(document.querySelectorAll(".p-contextual-menu__dropdown"))
.forEach((dropdown) => {
dropdown.setAttribute("aria-hidden", true);
});
}
renderIcon() {
return this.props.label || <i className="p-icon--contextual-menu" />;
}
render() {
const { isDisabled, position, title, isWide } = this.props;
const className = [
"p-contextual-menu__toggle",
"u-no-margin--bottom",
isDisabled ? "is-disabled" : "",
this.props.className || "",
].join(" ");
return (
<span
className={`p-contextual-menu${
position ? `--${position}` : ""
} p-promote-button u-hide--small`}
>
<button
className={className}
title={title}
onClick={isDisabled ? null : this.dropdownButtonClick.bind(this)}
>
{this.renderIcon()}
</button>
<span
className={`p-contextual-menu__dropdown ${isWide ? "is-wide" : ""}`}
aria-hidden="true"
>
{this.props.children}
</span>
</span>
);
}
}
ContextualMenu.propTypes = {
isDisabled: PropTypes.bool,
label: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
title: PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
isWide: PropTypes.bool,
position: PropTypes.oneOf(["left", "center"]), // right is by default
appearance: PropTypes.oneOf(["base", "neutral"]),
};