Skip to content

Commit 5a48897

Browse files
authored
Merge pull request #10
[Guess The Word Game] `Tabs` restructure
2 parents 556972a + acc69d8 commit 5a48897

File tree

9 files changed

+30
-39
lines changed

9 files changed

+30
-39
lines changed

vanilla/guess-the-word/components/info/HintsContent.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
<div id="hints-tab-nav" class="tab-nav" role="tablist">
1111
<!-- prettier-ignore -->
1212
<% for (const tab of hintsTabs) { %>
13-
<%- include("tabs/TabNavTrigger", { ...tab, contentName: "hints" }) %>
13+
<%- include("tabs/Tab", { ...tab, group: "hints" }) %>
1414
<% } %>
1515
</div>
1616
</nav>
1717
</header>
1818
<div id="hints-tab-content" class="tab-content">
1919
<!-- prettier-ignore -->
2020
<% for (const tab of hintsTabs) { %>
21-
<%- include("tabs/TabContentItem", { ...tab, navName: "hints" }) %>
21+
<%- include("tabs/TabContentItem", { ...tab, group: "hints" }) %>
2222
<ul class="hints-list"></ul>
2323
<%- include("tabs/TabContentItem", { close: true }) %>
2424
<% } %>

vanilla/guess-the-word/components/menu/AppMenu.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div id="menu-tab-nav" class="tab-nav" role="tablist">
1111
<!-- prettier-ignore -->
1212
<% for (const tab of menuTabs) { %>
13-
<%- include("tabs/TabNavTrigger", { ...tab, contentName: "menu" }) %>
13+
<%- include("tabs/Tab", { ...tab, group: "menu" }) %>
1414
<% } %>
1515
</div>
1616
</nav>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<button
2+
id="<%= name %>-<%= group %>-tab"
3+
class="tab-nav__tab"
4+
role="tab"
5+
aria-controls="<%= name %>-<%= group %>-tab-content"
6+
aria-selected="<%= locals.selected ?? false %>"
7+
>
8+
<%- include(`icon/${icon}`) %>
9+
<%= capitalize(name) %>
10+
</button>

vanilla/guess-the-word/components/tabs/TabContentItem.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<% if (!(locals.close ?? false)) { %>
22
<div
3-
id="<%= name %>-<%= navName %>-tab-content"
3+
id="<%= name %>-<%= group %>-tab-content"
44
class="tab-content__item"
55
role="tabpanel"
6-
aria-labelledby="<%= name %>-<%= navName %>-tab"
6+
aria-labelledby="<%= name %>-<%= group %>-tab"
77
ejs="<% if (locals.selected ?? false) { %>"
88
data-active
99
ejs="<% } %>"

vanilla/guess-the-word/components/tabs/TabNavTrigger.ejs

Lines changed: 0 additions & 13 deletions
This file was deleted.

vanilla/guess-the-word/src/events/listeners/click.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function setupClickListeners() {
3535
else if ($target === $showDifficulties) {
3636
$menu.showModal();
3737
MenuTabs.selectTab($difficultyTab);
38-
} else if (MenuTabs.isTabLink($target)) {
38+
} else if (MenuTabs.isTab($target)) {
3939
if ($target === $definitionsTab)
4040
DefinitionPages.renderPage(DefinitionPagination.currentPage);
4141
else if ($target === $statsTab)
@@ -44,7 +44,7 @@ export function setupClickListeners() {
4444
);
4545

4646
MenuTabs.selectTab($target);
47-
} else if (HintsTabs.isTabLink($target)) HintsTabs.selectTab($target);
47+
} else if (HintsTabs.isTab($target)) HintsTabs.selectTab($target);
4848
else if (DefinitionPagination.isTrigger($target))
4949
DefinitionPagination.handleTrigger($target);
5050
});

vanilla/guess-the-word/src/styles/tabs.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
padding: 0;
99
}
1010

11-
.tab-nav__trigger {
11+
.tab-nav__tab {
1212
position: relative;
1313
display: flex;
1414
align-items: center;
@@ -31,16 +31,16 @@
3131

3232
--icon-size: 1.25em;
3333
}
34-
.tab-nav__trigger[aria-selected="true"] {
34+
.tab-nav__tab[aria-selected="true"] {
3535
--tab-item-highlight: var(--secondary);
3636
--tab-link-font-weight: bold;
3737
background-color: var(--gray-700-15);
3838
}
39-
.tab-nav__trigger[aria-selected="false"] {
39+
.tab-nav__tab[aria-selected="false"] {
4040
color: var(--_disabled-hover-color);
4141
}
42-
.tab-nav__trigger[aria-selected="false"]:hover,
43-
.tab-nav__trigger[aria-selected="false"]:focus {
42+
.tab-nav__tab[aria-selected="false"]:hover,
43+
.tab-nav__tab[aria-selected="false"]:focus {
4444
--tab-item-highlight: var(--_disabled-hover-color);
4545
}
4646

vanilla/guess-the-word/src/tabs.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getAllElementsBySelector, getElementBySelector } from "@lib/dom";
22

33
const CLASSES = Object.freeze({
4-
TRIGGER: "tab-nav__trigger",
4+
TAB: "tab-nav__tab",
55
CONTENT: "tab-content__item",
66
});
77

@@ -16,7 +16,7 @@ const ATTRIBUTES = Object.freeze({
1616

1717
// TODO: id must end with `_tab`
1818
// aria-controls must end with `_tab-content`
19-
const TAB_VALID_SELECTOR = `.${CLASSES.TRIGGER}[id$="-tab"]`
19+
const TAB_VALID_SELECTOR = `.${CLASSES.TAB}[id$="-tab"]`
2020
.concat('[aria-controls$="-tab-content"]')
2121
.concat('[role="tab"]')
2222
.concat('[aria-selected]:not([aria-selected=""])');
@@ -71,7 +71,7 @@ export class Tabs {
7171
* @param {EventTarget | HTMLElement | null} $element
7272
* @returns {$element is HTMLButtonElement}
7373
*/
74-
isTabLink($element) {
74+
isTab($element) {
7575
return (
7676
$element instanceof HTMLButtonElement && this.#$Content.has($element)
7777
);
@@ -92,10 +92,8 @@ export class Tabs {
9292
if ($currentContent == null || $targetContent == null) return;
9393

9494
$currentTab.setAttribute(ATTRIBUTES.TAB.SELECTED, "false");
95-
$currentTab.disabled = false;
9695
$currentContent.removeAttribute(ATTRIBUTES.CONTENT.ACTIVE);
9796
$targetTab.setAttribute(ATTRIBUTES.TAB.SELECTED, "true");
98-
$targetTab.disabled = true;
9997
$targetTab.scrollIntoView();
10098
$targetContent.setAttribute(ATTRIBUTES.CONTENT.ACTIVE, "");
10199

vanilla/guess-the-word/src/tabs.test.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ const createTabs = ({ selectedTab = TABS.FIRST } = {}) => {
2525
const triggerId = `${tabGroup}_${tab}-tab`;
2626
const contentId = `${tabGroup}_${tab}-tab-content`;
2727

28-
$trigger.classList.add("tab-nav__trigger");
28+
$trigger.classList.add("tab-nav__tab");
2929
$trigger.setAttribute("id", triggerId);
3030
$trigger.setAttribute("aria-controls", contentId);
3131
$trigger.setAttribute("role", "tab");
3232
$trigger.setAttribute("aria-selected", `${tab === selectedTab}`);
3333
$trigger.textContent = `${tab.toUpperCase()} tab`;
34-
$trigger.disabled = tab === selectedTab;
3534

3635
$content.classList.add("tab-content__item");
3736
$content.setAttribute("id", contentId);
@@ -66,7 +65,7 @@ describe("Tabs", () => {
6665
}
6766
});
6867

69-
it("checks tab trigger correctly by using `.isTabLink()` method", () => {
68+
it("checks tab trigger correctly by using `.isTab()` method", () => {
7069
const { $nav, $content } = createTabs();
7170
const $intruder = document.createElement("button");
7271
$intruder.classList.add("tab-nav__trigger");
@@ -77,12 +76,12 @@ describe("Tabs", () => {
7776

7877
const TestTabs = new Tabs({ $nav, $content });
7978

80-
expect(TestTabs.isTabLink($intruder)).toBe(false);
79+
expect(TestTabs.isTab($intruder)).toBe(false);
8180

8281
for (const tab of tabNames) {
8382
const $tab = getByRole($nav, "tab", { name: `${tab.toUpperCase()} tab` });
8483

85-
expect(TestTabs.isTabLink($tab)).toBe(true);
84+
expect(TestTabs.isTab($tab)).toBe(true);
8685
}
8786
});
8887

@@ -121,11 +120,8 @@ describe("Tabs", () => {
121120

122121
expect(TestTabs.currentTab).toBe($tab);
123122
expect($selectedTab).toBe($tab);
124-
expect($tab.disabled).toBe(true);
125-
if ($tab !== $initialSelectedTab) {
123+
if ($tab !== $initialSelectedTab)
126124
expect($initialSelectedTab.getAttribute("aria-selected")).toBe("false");
127-
expect($initialSelectedTab.disabled).toBe(false);
128-
}
129125
}
130126
});
131127

0 commit comments

Comments
 (0)