-
Notifications
You must be signed in to change notification settings - Fork 2
/
RadialMenu.tsx
335 lines (301 loc) · 9.32 KB
/
RadialMenu.tsx
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module RadialMenu
*/
import "./RadialMenu.scss";
import classnames from "classnames";
import * as React from "react";
import { Key } from "ts-key-enum";
import type { IconSpec } from "../icons/IconComponent";
import { Icon } from "../icons/IconComponent";
import { Point } from "../utils/Point";
import type { CommonProps } from "../utils/Props";
import { AnnularSector, Annulus } from "./Annulus";
/** Properties for [[RadialMenu]]
* @public
*/
export interface RadialMenuProps extends CommonProps {
/** Whether to show RadialMenu */
opened: boolean;
/** Radius of inner portion of RadialMenu */
innerRadius: number;
/** Radius of outer portion of RadialMenu */
outerRadius: number;
/** Optional parameter to specify left position in viewport. */
left?: number | string;
/** Optional parameter to specify top position in viewport. */
top?: number | string;
/** Whether to rotate labels according to location on the RadialMenu, or to keep labels upright.
* Default: false
*/
labelRotate?: boolean;
/** Which child RadialButton to highlight */
selected?: number;
/** triggered in case of onClick events anywhere other than radial menu */
onBlur?: (event: any) => any;
/** triggered onKeyUp for <Esc> key */
onEsc?: (event: any) => any;
/** Content */
children?: React.ReactNode;
}
interface RadialMenuState {
sectors: AnnularSector[];
}
/**
* A context menu arranged in a radial layout.
* @public
*/
export class RadialMenu extends React.Component<
RadialMenuProps,
RadialMenuState
> {
private _root: HTMLDivElement | null = null;
private _selectedButton: RadialButton | null = null;
public static defaultProps: Partial<RadialMenuProps> = {
labelRotate: false,
selected: -1,
};
/** @internal */
public override readonly state: Readonly<RadialMenuState> = {
sectors: [],
};
constructor(props: RadialMenuProps) {
super(props);
}
public override render(): React.ReactElement {
const width = 2 * (this.props.outerRadius + 1);
let x = this.props.left,
y = this.props.top;
if (
this.props.left &&
this.props.top &&
typeof this.props.left === "number" &&
typeof this.props.top === "number"
) {
x = this.props.left;
y = this.props.top;
if (x < 0) x = 0;
if (x > window.innerWidth - width) x = window.innerWidth - width;
if (y < 0) y = 0;
if (y > window.innerHeight - width) y = window.innerHeight - width;
}
const divStyle: React.CSSProperties = {
left: x,
top: y,
...this.props.style,
};
return (
<div
ref={(el) => {
this._root = el;
}}
className={classnames(
"core-radial-menu",
{ opened: this.props.opened },
this.props.className
)}
style={divStyle}
data-testid="core-radial-menu"
>
<svg
xmlns="http://w3.org/2000/svg"
version="1.1"
width={width}
height={width}
className={"core-radial-menu-container"}
>
{React.Children.map(this.props.children, (child, index) => {
// istanbul ignore next
if (!child || typeof child !== "object" || !("props" in child))
return child;
const childElement = child as React.ReactElement<any>;
return React.cloneElement(childElement, {
key: index,
ref: (el: any) => {
if (this.props.selected === index) this._selectedButton = el;
},
selected: index === this.props.selected,
labelRotate:
childElement.props.labelRotate || this.props.labelRotate,
annularSector: this.state.sectors[index],
});
})}
</svg>
</div>
);
}
public override componentDidMount() {
this._generateAnnularSectors();
setTimeout(() => {
window.addEventListener("keyup", this._handleKeyUp);
window.addEventListener("mouseup", this._handleClick);
});
}
public override componentWillUnmount() {
window.removeEventListener("keyup", this._handleKeyUp);
window.removeEventListener("mouseup", this._handleClick);
}
public override componentDidUpdate(prevProps: RadialMenuProps) {
if (
prevProps.innerRadius !== this.props.innerRadius ||
prevProps.outerRadius !== this.props.outerRadius
) {
this._generateAnnularSectors();
}
}
private _handleKeyUp = (event: KeyboardEvent) => {
if (event.key === Key.Escape.valueOf() && this.props.onEsc)
this.props.onEsc(event);
};
private _handleClick = (event: MouseEvent) => {
if (
event.target instanceof HTMLElement &&
this._root &&
!event.target.contains(this._root) &&
this.props.onBlur
)
this.props.onBlur(event);
};
/** Manually call onSelect of highlighted button. */
public select = () => {
// istanbul ignore else
if (this._selectedButton) this._selectedButton.select();
};
private _generateAnnularSectors = () => {
const n = React.Children.count(this.props.children);
const angle = (2 * Math.PI) / n;
const outer = this.props.outerRadius;
const inner = this.props.innerRadius;
const offset = -Math.PI / 8;
const annulus = new Annulus(
new Point(outer + 1, outer + 1),
inner + 1,
outer - 1
);
const sectors: AnnularSector[] = [];
for (let i = 0; i < n; i++) {
sectors.push(
new AnnularSector(annulus, angle * i + offset, angle * (i + 1) + offset)
);
}
this.setState({ sectors });
};
}
/** Properties for [[RadialButton]] component
* @public
*/
export interface RadialButtonProps extends CommonProps {
/** Whether label is rotated to radial menu. Default: Inherit */
labelRotate?: boolean;
/** which icon to display in on the menu button */
icon?: IconSpec;
/** @internal */
annularSector?: AnnularSector;
/** listens to any onClick event, or any select event, which can be triggered by the select() method. */
onSelect?: (e: any) => any;
/** Whether item is highlighted */
selected?: boolean;
/** Content */
children?: React.ReactNode;
}
/** @internal */
interface RadialButtonState {
hover: boolean;
}
/**
* Button for use within a [[RadialMenu]]
* @public
*/
export class RadialButton extends React.Component<
RadialButtonProps,
RadialButtonState
> {
/** @internal */
public override readonly state: Readonly<RadialButtonState> = {
hover: this.props.selected || false,
};
constructor(props: RadialButtonProps) {
super(props);
}
public override render(): React.ReactElement {
const sector = this.props.annularSector;
let p = new Point();
let size = 0;
let t = "";
let path = "";
if (sector) {
size = sector.start.p1.getDistanceTo(sector.end.p2) * 2;
path = sector.path;
const parent = sector.parent;
const { x: cx, y: cy } = parent.center;
const r = (parent.inner.radius + parent.outer.radius) / 2;
const angle = (sector.startAngle + sector.endAngle) / 2;
p = new Point(cx + r * Math.cos(angle), cy + r * Math.sin(angle));
if (this.props.labelRotate) {
let a = (angle * 180) / Math.PI + 90;
while (a > 180) a -= 360;
while (a < -180)
/* istanbul ignore next */
a += 360;
if (a > 90) a -= 180;
if (a < -90) a += 180;
t = `rotate(${a} ${p.x}, ${p.y})`;
}
}
return (
<g
onMouseOver={this._handleMouseOver}
onMouseOut={this._handleMouseOut}
onClick={this._handleClick}
>
<path
className={classnames(
"core-radial-menu-sector",
{ selected: this.state.hover },
this.props.className
)}
style={this.props.style}
d={path}
></path>
<foreignObject
transform={t}
x={p.x - size / 2}
y={p.y - 16}
width={size}
height={size}
className={"core-radial-menu-button-svg"}
>
<div
{...{ xmlns: "http://www.w3.org/1999/xhtml" }}
className={"core-radial-menu-button-container"}
>
<div className="core-radial-menu-button-icon">
<Icon iconSpec={this.props.icon} />
</div>
<div className={"core-radial-menu-button-content"}>
{this.props.children}
</div>
</div>
</foreignObject>
</g>
);
}
/** Manually call this.props.onSelect */
public select = () => {
// istanbul ignore else
if (this.props.onSelect) this.props.onSelect(undefined);
};
private _handleClick = (event: React.MouseEvent<SVGElement>) => {
// istanbul ignore else
if (this.props.onSelect) this.props.onSelect(event);
};
private _handleMouseOver = (_event: React.MouseEvent<SVGElement>) => {
this.setState({ hover: true });
};
private _handleMouseOut = (_event: React.MouseEvent<SVGElement>) => {
this.setState({ hover: false });
};
}