@verbb/vitepress-theme 1.0.0

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/src/sidebar.ts ADDED
@@ -0,0 +1,218 @@
1
+ import { computed, onMounted, onUnmounted, ref, watch, watchEffect, type Ref } from 'vue';
2
+ import { useData } from 'vitepress';
3
+
4
+ export type SidebarItem = {
5
+ text?: string;
6
+ link?: string;
7
+ base?: string;
8
+ icon?: string;
9
+ collapsed?: boolean;
10
+ items?: SidebarItem[];
11
+ };
12
+
13
+ export type SidebarGroup = {
14
+ text?: string;
15
+ icon?: string;
16
+ items: SidebarItem[];
17
+ };
18
+
19
+ type SidebarConfig = SidebarItem[] | Record<string, SidebarItem[] | { base?: string; items: SidebarItem[] }> | undefined;
20
+
21
+ const HASH_RE = /#.*$/;
22
+ const HASH_OR_QUERY_RE = /[?#].*$/;
23
+ const INDEX_OR_EXT_RE = /(?:(^|\/)index)?\.(?:md|html)$/;
24
+
25
+ function normalize(path: string) {
26
+ return decodeURI(path)
27
+ .replace(HASH_OR_QUERY_RE, '')
28
+ .replace(INDEX_OR_EXT_RE, '$1');
29
+ }
30
+
31
+ function ensureStartingSlash(path: string) {
32
+ return /^\//.test(path) ? path : `/${path}`;
33
+ }
34
+
35
+ function addBase(items: SidebarItem[], parentBase?: string): SidebarItem[] {
36
+ return items.map((rawItem) => {
37
+ const item = { ...rawItem };
38
+ const base = item.base || parentBase;
39
+
40
+ if (base && item.link) {
41
+ item.link = `${base}${item.link}`;
42
+ }
43
+
44
+ if (item.items) {
45
+ item.items = addBase(item.items, base);
46
+ }
47
+
48
+ return item;
49
+ });
50
+ }
51
+
52
+ export function isActivePath(currentPath: string, matchPath?: string, asRegex = false) {
53
+ if (matchPath === undefined) {
54
+ return false;
55
+ }
56
+
57
+ const normalizedCurrentPath = normalize(`/${currentPath}`);
58
+
59
+ if (asRegex) {
60
+ return new RegExp(matchPath).test(normalizedCurrentPath);
61
+ }
62
+
63
+ if (normalize(matchPath) !== normalizedCurrentPath) {
64
+ return false;
65
+ }
66
+
67
+ const hashMatch = matchPath.match(HASH_RE);
68
+
69
+ if (hashMatch) {
70
+ return typeof window !== 'undefined' && window.location.hash === hashMatch[0];
71
+ }
72
+
73
+ return true;
74
+ }
75
+
76
+ export function hasActiveSidebarItem(currentPath: string, item?: SidebarItem) {
77
+ if (!item) {
78
+ return false;
79
+ }
80
+
81
+ if (isActivePath(currentPath, item.link)) {
82
+ return true;
83
+ }
84
+
85
+ return item.items?.some((child) => hasActiveSidebarItem(currentPath, child)) ?? false;
86
+ }
87
+
88
+ function getSidebar(sidebar: SidebarConfig, path: string): SidebarItem[] {
89
+ if (Array.isArray(sidebar)) {
90
+ return addBase(sidebar);
91
+ }
92
+
93
+ if (!sidebar) {
94
+ return [];
95
+ }
96
+
97
+ const normalizedPath = ensureStartingSlash(path);
98
+ const matchingDir = Object.keys(sidebar)
99
+ .sort((a, b) => b.split('/').length - a.split('/').length)
100
+ .find((dir) => normalizedPath.startsWith(ensureStartingSlash(dir)));
101
+
102
+ const matchedSidebar = matchingDir ? sidebar[matchingDir] : [];
103
+
104
+ if (Array.isArray(matchedSidebar)) {
105
+ return addBase(matchedSidebar);
106
+ }
107
+
108
+ return addBase(matchedSidebar.items, matchedSidebar.base);
109
+ }
110
+
111
+ function getSidebarGroups(sidebar: SidebarItem[]): SidebarGroup[] {
112
+ const groups: SidebarGroup[] = [];
113
+ let lastGroupIndex = 0;
114
+
115
+ for (const item of sidebar) {
116
+ if (item.items) {
117
+ groups.push({
118
+ text: item.text,
119
+ icon: item.icon,
120
+ items: item.items,
121
+ });
122
+ lastGroupIndex = groups.length - 1;
123
+ continue;
124
+ }
125
+
126
+ if (!groups[lastGroupIndex]) {
127
+ groups.push({ items: [] });
128
+ lastGroupIndex = groups.length - 1;
129
+ }
130
+
131
+ groups[lastGroupIndex].items.push(item);
132
+ }
133
+
134
+ return groups;
135
+ }
136
+
137
+ export function useDocsSidebar() {
138
+ const { frontmatter, page, theme } = useData();
139
+ const isOpen = ref(false);
140
+ const sidebar = computed(() => getSidebar(theme.value.sidebar as SidebarConfig, page.value.relativePath));
141
+ const sidebarGroups = computed(() => getSidebarGroups(sidebar.value));
142
+ const hasSidebar = computed(() => {
143
+ return frontmatter.value.sidebar !== false
144
+ && frontmatter.value.layout !== 'home'
145
+ && sidebar.value.length > 0;
146
+ });
147
+
148
+ watch(hasSidebar, (value) => {
149
+ if (!value) {
150
+ isOpen.value = false;
151
+ }
152
+ });
153
+
154
+ watchEffect((onCleanup) => {
155
+ if (typeof document === 'undefined') {
156
+ return;
157
+ }
158
+
159
+ const previousOverflow = document.body.style.overflow;
160
+ const shouldLockScroll = isOpen.value && typeof window !== 'undefined' && window.innerWidth < 1024;
161
+
162
+ if (shouldLockScroll) {
163
+ document.body.style.overflow = 'hidden';
164
+ }
165
+
166
+ onCleanup(() => {
167
+ document.body.style.overflow = previousOverflow;
168
+ });
169
+ });
170
+
171
+ function open() {
172
+ isOpen.value = true;
173
+ }
174
+
175
+ function close() {
176
+ isOpen.value = false;
177
+ }
178
+
179
+ function toggle() {
180
+ isOpen.value = !isOpen.value;
181
+ }
182
+
183
+ return {
184
+ isOpen,
185
+ sidebar,
186
+ sidebarGroups,
187
+ hasSidebar,
188
+ open,
189
+ close,
190
+ toggle,
191
+ };
192
+ }
193
+
194
+ export function useCloseSidebarOnEscape(isOpen: Ref<boolean>, close: () => void) {
195
+ let triggerElement: Element | null = null;
196
+
197
+ watchEffect(() => {
198
+ triggerElement = isOpen.value ? document.activeElement : null;
199
+ });
200
+
201
+ const onEscape = (event: KeyboardEvent) => {
202
+ if (event.key === 'Escape' && isOpen.value) {
203
+ close();
204
+
205
+ if (triggerElement instanceof HTMLElement) {
206
+ triggerElement.focus();
207
+ }
208
+ }
209
+ };
210
+
211
+ onMounted(() => {
212
+ window.addEventListener('keyup', onEscape);
213
+ });
214
+
215
+ onUnmounted(() => {
216
+ window.removeEventListener('keyup', onEscape);
217
+ });
218
+ }