Repository navigation
Expand file tree
/
Copy pathmenu.js
More file actions
95 lines (85 loc) · 3.17 KB
/
Copy pathmenu.js
File metadata and controls
95 lines (85 loc) · 3.17 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
/** Whether Esc closed the menu, which drops the changes made in it */
let cancelled = false;
// On the document, since Safari doesn’t focus a checkbox on a click, so Esc after one goes to the page.
// Esc in a filled search field only clears it, so the next key or click decides how the menu closes
for (let type of ["keydown", "pointerdown"]) {
document.addEventListener(type, event => {
cancelled = event.key === "Escape";
});
}
/**
* Move the focus from a button that is about to hide to a field, e.g. from Clear to the field it emptied, or the focus would drop to the page.
* Only after a press from the keyboard, since a focused field opens the on-screen keyboard after a tap.
* @param {HTMLButtonElement} button
* @param {HTMLInputElement} field
*/
export function passFocus (button, field) {
if (button.matches(":focus-visible")) {
field.focus();
}
}
/** @type {Map<HTMLFormElement, () => void>} How to close each menu without applying it */
let dismissals = new Map();
/** Close the open menus without applying them, e.g. when the queue they change goes, as after a failed load. */
export function dismissMenus () {
for (let [form, dismiss] of dismissals) {
if (form.matches(":popover-open")) {
dismiss();
}
}
}
/**
* Make a popover form a menu that applies its changes once it closes, as GitHub’s filter menus do. Esc drops them instead.
* The form submits its fields as URL parameters, so the same values submitted again would load the same rows.
* @param {HTMLFormElement} form
* @param {{ changed: () => boolean, open?: () => void, discard: () => void }} menu -
* Whether its fields differ from the URL’s, what to do as it opens, and how to show the URL’s values again
*/
export function applyOnClose (form, { changed, open, discard }) {
/** Whether the menu closes because it was submitted, which needs no second submit, or dismissed */
let submitted = false;
dismissals.set(form, () => {
submitted = true;
form.hidePopover();
discard();
});
// Focus moved out from the keyboard, e.g. by Tab past the last button, closes the menu as a click outside does, which applies it.
// A click needs no check. Outside the menu, light dismiss closes it. On the menu’s own button, the click toggles it
document.addEventListener("focusin", event => {
if (
form.matches(":popover-open") &&
!form.contains(event.target) &&
event.target.matches(":focus-visible")
) {
form.hidePopover();
}
});
form.addEventListener("submit", event => {
if (!changed()) {
event.preventDefault();
}
submitted = true;
if (form.matches(":popover-open")) {
form.hidePopover();
}
});
form.addEventListener("toggle", event => {
if (event.newState === "open") {
cancelled = false;
submitted = false;
open?.();
// A focused field opens the on-screen keyboard over the menu on a phone
if (matchMedia("(pointer: fine)").matches) {
form.querySelector("input").focus();
}
return;
}
// A value the field rejects, e.g. a pattern that isn’t a regular expression, is dropped rather than applied
if (!cancelled && !submitted && changed() && form.checkValidity()) {
form.requestSubmit();
}
else if (!submitted) {
discard();
}
});
}