-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Menu.js
89 lines (81 loc) · 2.59 KB
/
Menu.js
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
const {h, render} = require('preact')
const classNames = require('classnames')
function hide() {
document.getElementById('popupmenu-overlay').remove()
}
function show(velement, x, y) {
let element = render(velement, document.body).childNodes[0]
let {width, height} = element.getBoundingClientRect()
let {width: bodyWidth, height: bodyHeight} = document.body.getBoundingClientRect()
element.style.left = (x + width <= bodyWidth ? x : Math.max(0, x - width)) + 'px'
element.style.top = (y + height <= bodyHeight ? y : Math.max(0, y - height)) + 'px'
}
exports.buildFromTemplate = function(template) {
return {
popup: ({x, y}) => show(h('section',
{
id: 'popupmenu-overlay',
onClick: () => hide()
},
h('ul', {class: 'popupmenu'}, template.map(item =>
h('li', {
class: classNames({
checked: item.checked,
[item.type]: item.type
}),
onClick: () => item.click && item.click()
}, item.label && item.label.replace(/&/g, ''))
))
), x, y)
}
}
render(h('style', {}, `
#popupmenu-overlay {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: 1000;
}
.popupmenu {
position: absolute;
padding: 3px 0;
max-height: calc(100% - 6px);
max-width: 100%;
overflow: auto;
background: rgba(0, 0, 0, .9);
box-shadow: 0 5px 10px rgba(0, 0, 0, .5);
color: white;
list-style: none;
}
.popupmenu li:not(.separator) {
position: relative;
display: block;
height: 1em;
padding: 7px 30px;
line-height: 1em;
}
.popupmenu li.checked::before {
content: '';
position: absolute;
width: 1em;
height: 1em;
left: 9px;
top: 7px;
background: url('./node_modules/octicons/build/svg/check.svg') left top/contain no-repeat;
filter: invert(100%);
-webkit-filter: invert(100%);
}
.popupmenu li.separator {
height: 2px;
margin: 3px 0;
background: rgba(255, 255, 255, .3);
}
.popupmenu li:not(.separator):hover {
background-color: #0050C0;
}
.popupmenu li:not(.separator):active {
background-color: #0030A0;
}
`), document.body)