Skip to content

Commit 1e9b19b

Browse files
committed
wip: Re-write script to data attr because conflicts with docsify menu plugin
1 parent 6b57d0b commit 1e9b19b

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

html/assets/css/themeable-customization.css

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
background-color: #fff;
1919
}
2020

21-
.sidebar-nav ul > li.collapse > a {
22-
color: var(--sidebar-nav-link-color);
23-
background: var(--sidebar-nav-pagelink-background) !important;
21+
[data-submenu-state="collapsed"] > ul {
22+
display: none;
2423
}
25-
.sidebar-nav ul > li.collapse.active > a {
26-
color: var(--sidebar-nav-link-color--active, var(--sidebar-nav-link-color));
27-
background: var(--sidebar-nav-pagelink-background--active) !important;
24+
.sidebar-nav [data-submenu-state] > a {
25+
padding: var(--sidebar-nav-pagelink-padding);
26+
}
27+
.sidebar-nav [data-submenu-state="collapsed"] > a {
28+
background: var(--sidebar-nav-pagelink-background);
29+
}
30+
.sidebar-nav [data-submenu-state="opened"] > a {
31+
background: var(--sidebar-nav-pagelink-background--active);
32+
color: var(--sidebar-nav-link-color--active);
2833
}

html/assets/js/plugins/menu-collapse.js

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,51 @@
22
const menuCollapse = function (hook, vm) {
33
// Hook to execute after the page is fully loaded https://github.com/docsifyjs/docsify/blob/develop/docs/write-a-plugin.md
44
hook.doneEach(() => {
5+
// Select all parent items
56
const parentItems = document.querySelectorAll('.sidebar-nav > ul > li');
67

78
parentItems.forEach(item => {
89
/**
910
* Skip items that already have link or not rendered
1011
* Check on empty string required because an original docsify menu does not provide hooks after load
1112
*/
12-
if (item.firstChild.nodeType !== Node.TEXT_NODE || item.firstChild.textContent.trim() === "") return;
13+
const textNode = item.firstChild;
14+
if (textNode.nodeType !== Node.TEXT_NODE || textNode.textContent.trim() === "") return;
15+
16+
// Create link for text item
17+
const link = document.createElement('a');
18+
link.href = '#';
19+
link.textContent = textNode.textContent;
20+
textNode.parentNode.replaceChild(link, textNode);
1321

1422
// Check if the parent item has a sublist
1523
const sublist = item.querySelector('ul');
24+
if (!sublist) return;
1625

17-
if (sublist) {
18-
// Check if any nested items in the sublist have the 'active' class
19-
const nestedItems = sublist.querySelectorAll('li');
20-
const hasActiveItems = Array.from(nestedItems).some(nestedItem => nestedItem.classList.contains('active'));
21-
22-
/**
23-
* Add the 'collapse' class to items without active nested items, otherwise add 'active'
24-
* That is need to highlight menu item on a page load when subitem is active
25-
*/
26-
if (!hasActiveItems) {
27-
item.classList.add('collapse');
28-
} else {
29-
item.classList.add('active');
30-
item.classList.remove('collapse');
31-
}
32-
33-
// Add class to sublist
34-
sublist.classList.add('app-sub-sidebar');
35-
36-
// Wrap text item in a link to follow default menu behavior and classes
37-
const textNode = item.firstChild;
38-
const link = document.createElement('a');
39-
link.href = '#/';
40-
link.textContent = textNode.textContent;
41-
textNode.parentNode.replaceChild(link, textNode);
26+
// Check if any nested items in the sublist have the 'active' class
27+
const hasActiveItems = sublist.querySelector('.active');
4228

43-
// Add click event listener to link to prevent default behavior
44-
link.addEventListener('click', (e) => {
45-
e.preventDefault();
46-
// Close all other collapsed items when a link is clicked
47-
parentItems.forEach(otherItem => {
48-
if (otherItem !== item) {
49-
otherItem.classList.remove('collapse');
50-
}
51-
});
52-
});
29+
/**
30+
* Set data-submenu-state attribute to items without active nested items, otherwise set it to 'active'
31+
* That is need to highlight menu item on a page load when subitem is active
32+
*/
33+
if (hasActiveItems) {
34+
item.setAttribute('data-submenu-state', 'opened');
35+
} else {
36+
item.setAttribute('data-submenu-state', 'collapsed');
5337
}
38+
39+
// Add click event listener to link to toggle submenu state
40+
link.addEventListener('click', (e) => {
41+
e.preventDefault();
42+
const submenuState = item.getAttribute('data-submenu-state');
43+
item.setAttribute('data-submenu-state', submenuState === 'collapsed' ? 'opened' : 'collapsed');
44+
});
5445
});
5546
});
5647
};
5748

5849
// Add the plugin to the Docsify plugin array
5950
$docsify = $docsify || {};
60-
$docsify.plugins = [].concat(menuCollapse, $docsify.plugins || []);
51+
$docsify.plugins = [$docsify.plugins || [], menuCollapse];
6152
})();

0 commit comments

Comments
 (0)