-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic-tabs.js
134 lines (116 loc) · 3.54 KB
/
semantic-tabs.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
90
91
92
93
94
95
96
97
98
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
133
134
//
// USAGE
//
// <header><nav>
// <a data-href="#dashboard" class="active">Home</a>
// <a data-href="#settings">Settings</a>
// </nav></header>
// <main>
// <section data-semtab="dashboard">...</section>
// <section data-semtab="settings">...</section>
// </main>
// <script>
// SemTabs.init();
// </script>
//
// How it works:
// - add 'data-href' to links
// - add 'data-semtab' to tab elements
// - 'location.hash' changes will trigger SemTabs
// - SemTabs will toggle 'href', 'hidden', and the '.active' class for known hashes
// - SemTabs.init() reads or sets the initial 'location.hash' state
//
// That's it.
/** @typedef {HTMLAnchorElement & {dataset: {href: string}}} TabAnchor */
/** @typedef {HTMLAnchorElement & {dataset: {semtab: string}}} TabTarget */
//@ts-ignore
var SemTabs = ("object" === typeof module && exports) || {};
(function (window, SemTabs) {
"use strict";
var $SemTabs = {};
// let $ = window.$;
/**
* Select first matching element, just like console $
* @param {String} cssSelector
* @param {ParentNode} [$parent=document]
*/
function $(cssSelector, $parent = document) {
let $child = $parent.querySelector(cssSelector);
return $child;
}
/**
* @callback QuerySelectorAll
* @param {String} cssSelector
* @param {HTMLElement} [$parentElement=document]
* @returns {Array<HTMLElement>}
*/
/** @type {QuerySelectorAll} */
//@ts-ignore
function $$(cssSelector, $parent = document) {
let children = $parent.querySelectorAll(cssSelector);
let $children = Array.from(children);
//@ts-ignore - trust me bro, it's an HTMLElement
return $children;
}
// let $$ = window.$$;
$SemTabs.init = function () {
window.removeEventListener("hashchange", $SemTabs._hashChange, false);
window.addEventListener("hashchange", $SemTabs._hashChange, false);
if ("" !== location.hash.slice(1)) {
$SemTabs._hashChange();
return;
}
$SemTabs._setToFirst();
};
$SemTabs._setToFirst = function () {
/** @type {TabTarget} */ // @ts-ignore
let $firstTab = $("[data-semtab]");
let tabName = $firstTab.dataset.semtab;
location.hash = `#${tabName}`;
};
$SemTabs._hashChange = function () {
if ("#" === location.hash) {
$SemTabs._setToFirst();
return;
}
requestAnimationFrame($SemTabs._batchStateChanges);
};
$SemTabs._batchStateChanges = function () {
let tabName = location.hash.slice(1);
for (let _$tabLink of $$("a[data-href]")) {
/** @type {TabAnchor} */ // @ts-ignore
let $tabLink = _$tabLink;
console.log("link", $tabLink);
let hash = $tabLink.dataset.href;
if (location.hash === hash) {
$tabLink.classList.add("active");
$tabLink.removeAttribute("href");
continue;
}
$tabLink.classList.remove("active");
$tabLink.href = $tabLink.dataset.href;
}
if (!$(`[data-semtab="${tabName}"]`)) {
console.warn(
`[SemTabs] cowardly refusing to set 'hidden' on existing semtabs for unknown link '${location.hash}'`,
);
return;
}
for (let $tabBody of $$(`[data-semtab]`)) {
let name = $tabBody.dataset.semtab;
if (name !== tabName) {
$tabBody.hidden = true;
$tabBody.classList.remove("active");
continue;
}
$tabBody.classList.add("active");
$tabBody.hidden = false;
}
};
Object.assign(SemTabs, $SemTabs);
//@ts-ignore
window.SemTabs = $SemTabs;
})(globalThis.window || {}, SemTabs);
if ("object" === typeof module) {
module.exports = SemTabs;
}