@sit-onyx/headless 1.0.0-beta.11 → 1.0.0-beta.12

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 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.11",
4
+ "version": "1.0.0-beta.12",
5
5
  "type": "module",
6
6
  "author": "Schwarz IT KG",
7
7
  "license": "Apache-2.0",
@@ -57,11 +57,11 @@ defineExpose({ comboBox });
57
57
  @keydown.arrow-down="isExpanded = true"
58
58
  />
59
59
 
60
- <button v-bind="button">
60
+ <button v-bind="button" type="button">
61
61
  <template v-if="isExpanded">⬆️</template>
62
62
  <template v-else>⬇️</template>
63
63
  </button>
64
- <ul v-bind="listbox" :class="{ hidden: !isExpanded }" style="width: 400px">
64
+ <ul class="listbox" v-bind="listbox" :class="{ hidden: !isExpanded }">
65
65
  <li
66
66
  v-for="e in options"
67
67
  :key="e"
@@ -84,4 +84,7 @@ defineExpose({ comboBox });
84
84
  [aria-selected="true"] {
85
85
  background-color: red;
86
86
  }
87
+ .listbox {
88
+ width: 400px;
89
+ }
87
90
  </style>
@@ -209,13 +209,13 @@ export const createComboBox = createBuilder(
209
209
  return handleNavigation(event);
210
210
  };
211
211
 
212
- const autocompleteInput =
213
- autocomplete.value !== "none"
214
- ? {
215
- "aria-autocomplete": autocomplete.value,
216
- type: "text",
217
- }
218
- : null;
212
+ const autocompleteInput = computed(() => {
213
+ if (autocomplete.value === "none") return null;
214
+ return {
215
+ "aria-autocomplete": autocomplete.value,
216
+ type: "text",
217
+ };
218
+ });
219
219
 
220
220
  const {
221
221
  elements: { option, group, listbox },
@@ -264,7 +264,7 @@ export const createComboBox = createBuilder(
264
264
  activeOption.value != undefined ? getOptionId(activeOption.value) : undefined,
265
265
  onInput: handleInput,
266
266
  onKeydown: handleKeydown,
267
- ...autocompleteInput,
267
+ ...autocompleteInput.value,
268
268
  })),
269
269
  /**
270
270
  * An optional button to control the visibility of the popup.
@@ -18,7 +18,7 @@ const {
18
18
 
19
19
  <template>
20
20
  <div v-bind="root">
21
- <button v-bind="button">Toggle nav menu</button>
21
+ <button v-bind="button" type="button">Toggle nav menu</button>
22
22
  <ul v-show="isExpanded" v-bind="menu">
23
23
  <li v-for="item in items" v-bind="listItem" :key="item.value">
24
24
  <a v-bind="menuItem({ active: activeItem === item.value })" href="#">{{ item.label }}</a>
@@ -71,6 +71,41 @@ export const tabsTesting = async (options: TabsTestingOptions) => {
71
71
  await options.page.keyboard.press("Space");
72
72
  const { tabId: tabIdFirst, panelId: panelIdFirst } = await expectTabAttributes(firstTab, true);
73
73
  await expectPanelAttributes(options.page.locator(`#${panelIdFirst}`), tabIdFirst);
74
+
75
+ // should skip disabled tabs when using the keyboard
76
+ await firstTab.click();
77
+ await secondTab.evaluate((element) => (element.ariaDisabled = "true"));
78
+ await expect(secondTab, "should disable second tab when setting aria-disabled").toBeDisabled();
79
+
80
+ await options.page.keyboard.press("ArrowRight");
81
+ await expect(secondTab, "should not focus second tab if its aria-disabled").not.toBeFocused();
82
+ await expect(
83
+ options.tablist.getByRole("tab").nth(2),
84
+ "should focus next tab after disabled one when pressing arrow right",
85
+ ).toBeFocused();
86
+
87
+ await options.page.keyboard.press("ArrowLeft");
88
+ await expect(
89
+ firstTab,
90
+ "should focus tab before disabled one when pressing arrow left",
91
+ ).toBeFocused();
92
+
93
+ await secondTab.evaluate((element) => (element.ariaDisabled = null));
94
+ await firstTab.evaluate((element) => (element.ariaDisabled = "true"));
95
+ await options.page.keyboard.press("Home");
96
+ await expect(
97
+ secondTab,
98
+ "should focus second tab when pressing Home if first tab is disabled",
99
+ ).toBeFocused();
100
+
101
+ await firstTab.evaluate((element) => (element.ariaDisabled = null));
102
+ await lastTab.evaluate((element) => (element.ariaDisabled = "true"));
103
+ await firstTab.focus();
104
+ await options.page.keyboard.press("End");
105
+ await expect(
106
+ options.tablist.getByRole("tab").nth(-2),
107
+ "should focus second last tab when pressing End if last tab is disabled",
108
+ ).toBeFocused();
74
109
  };
75
110
 
76
111
  /**
@@ -37,30 +37,41 @@ export const createTabs = createBuilder(<T extends PropertyKey>(options: CreateT
37
37
  const handleKeydown = (event: KeyboardEvent) => {
38
38
  const tab = event.target as Element;
39
39
 
40
- const focusFirstTab = () => {
41
- const element = tab.parentElement?.querySelector('[role="tab"]');
40
+ const enabledTabs = Array.from(
41
+ tab.parentElement?.querySelectorAll('[role="tab"]') ?? [],
42
+ ).filter((tab) => tab.ariaDisabled !== "true");
43
+
44
+ const currentTabIndex = enabledTabs.indexOf(tab);
45
+
46
+ const focusElement = (element?: Element | null) => {
42
47
  if (element instanceof HTMLElement) element.focus();
43
48
  };
44
49
 
45
- const focusLastTab = () => {
46
- const element = Array.from(tab.parentElement?.querySelectorAll('[role="tab"]') ?? []).at(-1);
47
- if (element instanceof HTMLElement) element.focus();
50
+ const focusFirstTab = () => focusElement(enabledTabs.at(0));
51
+ const focusLastTab = () => focusElement(enabledTabs.at(-1));
52
+
53
+ /**
54
+ * Focuses the next/previous tab. Will ignore/skip disabled ones.
55
+ */
56
+ const focusTab = (direction: "next" | "previous") => {
57
+ if (currentTabIndex === -1) return;
58
+ const newIndex = direction === "next" ? currentTabIndex + 1 : currentTabIndex - 1;
59
+
60
+ if (newIndex < 0) {
61
+ return focusLastTab();
62
+ } else if (newIndex >= enabledTabs.length) {
63
+ return focusFirstTab();
64
+ }
65
+
66
+ return focusElement(enabledTabs.at(newIndex));
48
67
  };
49
68
 
50
69
  switch (event.key) {
51
70
  case "ArrowRight":
52
- if (tab.nextElementSibling && tab.nextElementSibling instanceof HTMLElement) {
53
- tab.nextElementSibling.focus();
54
- } else {
55
- focusFirstTab();
56
- }
71
+ focusTab("next");
57
72
  break;
58
73
  case "ArrowLeft":
59
- if (tab.previousElementSibling && tab.previousElementSibling instanceof HTMLElement) {
60
- tab.previousElementSibling.focus();
61
- } else {
62
- focusLastTab();
63
- }
74
+ focusTab("previous");
64
75
  break;
65
76
  case "Home":
66
77
  focusFirstTab();
@@ -86,7 +97,7 @@ export const createTabs = createBuilder(<T extends PropertyKey>(options: CreateT
86
97
  onKeydown: handleKeydown,
87
98
  })),
88
99
  tab: computed(() => {
89
- return (data: { value: T }) => {
100
+ return (data: { value: T; disabled?: boolean }) => {
90
101
  const { tabId: selectedTabId } = getId(unref(options.selectedTab));
91
102
  const { tabId, panelId } = getId(data.value);
92
103
  const isSelected = tabId === selectedTabId;
@@ -96,8 +107,9 @@ export const createTabs = createBuilder(<T extends PropertyKey>(options: CreateT
96
107
  role: "tab",
97
108
  "aria-selected": isSelected,
98
109
  "aria-controls": panelId,
110
+ "aria-disabled": data.disabled ? true : undefined,
99
111
  onClick: () => options.onSelect?.(data.value),
100
- tabindex: isSelected ? 0 : -1,
112
+ tabindex: isSelected && !data.disabled ? 0 : -1,
101
113
  } as const;
102
114
  };
103
115
  }),