@sit-onyx/headless 1.0.0-beta.12 → 1.0.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/composables/menuButton/createMenuButton.ts +120 -112
- package/src/utils/timer.ts +10 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sit-onyx/headless",
|
|
3
3
|
"description": "Headless composables for Vue",
|
|
4
|
-
"version": "1.0.0-beta.
|
|
4
|
+
"version": "1.0.0-beta.13",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Schwarz IT KG",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"vue": ">= 3.5.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@vue/compiler-dom": "3.5.
|
|
31
|
-
"vue": "3.5.
|
|
30
|
+
"@vue/compiler-dom": "3.5.13",
|
|
31
|
+
"vue": "3.5.13"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "vue-tsc --build --force",
|
|
@@ -1,141 +1,149 @@
|
|
|
1
|
-
import { computed, useId, type Ref } from "vue";
|
|
1
|
+
import { computed, useId, watch, type Ref } from "vue";
|
|
2
2
|
import { createBuilder, createElRef } from "../../utils/builder";
|
|
3
3
|
import { debounce } from "../../utils/timer";
|
|
4
4
|
import { useGlobalEventListener } from "../helpers/useGlobalListener";
|
|
5
5
|
|
|
6
6
|
type CreateMenuButtonOptions = {
|
|
7
|
-
isExpanded: Ref<boolean
|
|
7
|
+
isExpanded: Readonly<Ref<boolean>>;
|
|
8
8
|
onToggle: () => void;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Based on https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/examples/disclosure-navigation/
|
|
13
13
|
*/
|
|
14
|
-
export const createMenuButton = createBuilder(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const buttonId = useId();
|
|
14
|
+
export const createMenuButton = createBuilder((options: CreateMenuButtonOptions) => {
|
|
15
|
+
const rootId = useId();
|
|
16
|
+
const menuId = useId();
|
|
17
|
+
const menuRef = createElRef<HTMLElement>();
|
|
18
|
+
const buttonId = useId();
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
useGlobalEventListener({
|
|
21
|
+
type: "keydown",
|
|
22
|
+
listener: (e) => e.key === "Escape" && setExpanded(false),
|
|
23
|
+
disabled: computed(() => !options.isExpanded.value),
|
|
24
|
+
});
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
200,
|
|
33
|
-
);
|
|
26
|
+
/**
|
|
27
|
+
* Debounced expanded state that will only be toggled after a given timeout.
|
|
28
|
+
*/
|
|
29
|
+
const updateDebouncedExpanded = debounce(() => options.onToggle(), 200);
|
|
30
|
+
watch(options.isExpanded, () => updateDebouncedExpanded.abort()); // manually changing `isExpanded` should abort debounced action
|
|
34
31
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
const setExpanded = (expanded: boolean, debounced = false) => {
|
|
33
|
+
if (expanded === options.isExpanded.value) {
|
|
34
|
+
updateDebouncedExpanded.abort();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (debounced) {
|
|
38
|
+
updateDebouncedExpanded();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
options.onToggle();
|
|
42
|
+
};
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
const focusRelativeItem = (next: "next" | "prev" | "first" | "last") => {
|
|
45
|
+
const currentMenuItem = document.activeElement as HTMLElement;
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
nextIndex = currentIndex + 1;
|
|
51
|
-
break;
|
|
52
|
-
case "prev":
|
|
53
|
-
nextIndex = currentIndex - 1;
|
|
54
|
-
break;
|
|
55
|
-
case "first":
|
|
56
|
-
nextIndex = 0;
|
|
57
|
-
break;
|
|
58
|
-
case "last":
|
|
59
|
-
nextIndex = menuItems.length - 1;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
47
|
+
// Either the current focus is on a "menuitem", then we can just get the parent menu.
|
|
48
|
+
// Or the current focus is on the button, then we can get the connected menu using the menuId
|
|
49
|
+
const currentMenu = currentMenuItem?.closest('[role="menu"]') || menuRef.value;
|
|
50
|
+
if (!currentMenu) return;
|
|
63
51
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
};
|
|
52
|
+
const menuItems = [...currentMenu.querySelectorAll<HTMLElement>('[role="menuitem"]')];
|
|
53
|
+
let nextIndex = 0;
|
|
67
54
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
case "
|
|
72
|
-
|
|
73
|
-
focusRelativeItem("next");
|
|
74
|
-
break;
|
|
75
|
-
case "ArrowUp":
|
|
76
|
-
case "ArrowLeft":
|
|
77
|
-
event.preventDefault();
|
|
78
|
-
focusRelativeItem("prev");
|
|
55
|
+
if (currentMenuItem) {
|
|
56
|
+
const currentIndex = menuItems.indexOf(currentMenuItem);
|
|
57
|
+
switch (next) {
|
|
58
|
+
case "next":
|
|
59
|
+
nextIndex = currentIndex + 1;
|
|
79
60
|
break;
|
|
80
|
-
case "
|
|
81
|
-
|
|
82
|
-
focusRelativeItem("first");
|
|
61
|
+
case "prev":
|
|
62
|
+
nextIndex = currentIndex - 1;
|
|
83
63
|
break;
|
|
84
|
-
case "
|
|
85
|
-
|
|
86
|
-
focusRelativeItem("last");
|
|
64
|
+
case "first":
|
|
65
|
+
nextIndex = 0;
|
|
87
66
|
break;
|
|
88
|
-
case "
|
|
89
|
-
|
|
90
|
-
(event.target as HTMLElement).click();
|
|
91
|
-
break;
|
|
92
|
-
case "Escape":
|
|
93
|
-
event.preventDefault();
|
|
94
|
-
isExpanded.value && onToggle();
|
|
67
|
+
case "last":
|
|
68
|
+
nextIndex = menuItems.length - 1;
|
|
95
69
|
break;
|
|
96
70
|
}
|
|
97
|
-
}
|
|
71
|
+
}
|
|
98
72
|
|
|
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
|
-
|
|
73
|
+
const nextMenuItem = menuItems[nextIndex];
|
|
74
|
+
nextMenuItem?.focus();
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const handleKeydown = (event: KeyboardEvent) => {
|
|
78
|
+
switch (event.key) {
|
|
79
|
+
case "ArrowDown":
|
|
80
|
+
case "ArrowRight":
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
focusRelativeItem("next");
|
|
83
|
+
break;
|
|
84
|
+
case "ArrowUp":
|
|
85
|
+
case "ArrowLeft":
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
focusRelativeItem("prev");
|
|
88
|
+
break;
|
|
89
|
+
case "Home":
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
focusRelativeItem("first");
|
|
92
|
+
break;
|
|
93
|
+
case "End":
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
focusRelativeItem("last");
|
|
96
|
+
break;
|
|
97
|
+
case " ":
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
(event.target as HTMLElement).click();
|
|
100
|
+
break;
|
|
101
|
+
case "Escape":
|
|
102
|
+
event.preventDefault();
|
|
103
|
+
setExpanded(false);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
elements: {
|
|
110
|
+
root: {
|
|
111
|
+
id: rootId,
|
|
112
|
+
onKeydown: handleKeydown,
|
|
113
|
+
onMouseenter: () => setExpanded(true),
|
|
114
|
+
onMouseleave: () => setExpanded(false, true),
|
|
115
|
+
onFocusout: (event) => {
|
|
116
|
+
// if focus receiving element is not part of the menu button, then close
|
|
117
|
+
if (
|
|
118
|
+
rootId &&
|
|
119
|
+
document.getElementById(rootId)?.contains(event.relatedTarget as HTMLElement)
|
|
120
|
+
) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
setExpanded(false);
|
|
133
124
|
},
|
|
134
|
-
...createMenuItems().elements,
|
|
135
125
|
},
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
126
|
+
button: computed(
|
|
127
|
+
() =>
|
|
128
|
+
({
|
|
129
|
+
"aria-controls": menuId,
|
|
130
|
+
"aria-expanded": options.isExpanded.value,
|
|
131
|
+
"aria-haspopup": true,
|
|
132
|
+
onFocus: () => setExpanded(true),
|
|
133
|
+
id: buttonId,
|
|
134
|
+
}) as const,
|
|
135
|
+
),
|
|
136
|
+
menu: {
|
|
137
|
+
id: menuId,
|
|
138
|
+
ref: menuRef,
|
|
139
|
+
role: "menu",
|
|
140
|
+
"aria-labelledby": buttonId,
|
|
141
|
+
onClick: () => setExpanded(false),
|
|
142
|
+
},
|
|
143
|
+
...createMenuItems().elements,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
});
|
|
139
147
|
|
|
140
148
|
export const createMenuItems = createBuilder(() => {
|
|
141
149
|
return {
|
package/src/utils/timer.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { toValue, type MaybeRefOrGetter } from "vue";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Debounces a given callback which will only be called when not called for the given timeout.
|
|
3
5
|
*
|
|
@@ -5,11 +7,16 @@
|
|
|
5
7
|
*/
|
|
6
8
|
export const debounce = <TArgs extends unknown[]>(
|
|
7
9
|
handler: (...args: TArgs) => void,
|
|
8
|
-
timeout: number
|
|
10
|
+
timeout: MaybeRefOrGetter<number>,
|
|
9
11
|
) => {
|
|
10
12
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
11
|
-
|
|
13
|
+
|
|
14
|
+
const func = (...lastArgs: TArgs) => {
|
|
12
15
|
clearTimeout(timer);
|
|
13
|
-
timer = setTimeout(() => handler(...lastArgs), timeout);
|
|
16
|
+
timer = setTimeout(() => handler(...lastArgs), toValue(timeout));
|
|
14
17
|
};
|
|
18
|
+
/** Abort the currently debounced action, if any. */
|
|
19
|
+
func.abort = () => clearTimeout(timer);
|
|
20
|
+
|
|
21
|
+
return func;
|
|
15
22
|
};
|