Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ export function Events(Base) {

// Collapse toggle
dom.on(sidebarElm, 'click', (/** @type {MouseEvent} */ { target }) => {
const groupTitle = /** @type {HTMLElement | null} */ (
/** @type {HTMLElement} */ (target).closest(
'.group-title[role="button"]',
)
);

if (groupTitle) {
this.#toggleSidebarGroup(groupTitle);
return;
}

const linkElm = /** @type {HTMLElement} */ (target).closest('a');
const linkParent = /** @type {HTMLLIElement} */ (
linkElm?.closest('li')
Expand All @@ -262,6 +273,38 @@ export function Events(Base) {
linkParent.classList.toggle('collapse');
}
});

dom.on(sidebarElm, 'keydown', (/** @type {KeyboardEvent} */ event) => {
const groupTitle = /** @type {HTMLElement | null} */ (
/** @type {HTMLElement} */ (event.target).closest(
'.group-title[role="button"]',
)
);

if (groupTitle && (event.key === 'Enter' || event.key === ' ')) {
event.preventDefault();
this.#toggleSidebarGroup(groupTitle);
}
});
}

/**
* Toggle a root sidebar group and keep its accessible state in sync.
*
* @param {HTMLElement} groupTitle
* @void
*/
#toggleSidebarGroup(groupTitle) {
const group = /** @type {HTMLLIElement | null} */ (
groupTitle.closest('li')
);

if (!group) {
return;
}

const isCollapsed = group.classList.toggle('collapse');
groupTitle.setAttribute('aria-expanded', String(!isCollapsed));
}

/**
Expand Down
54 changes: 51 additions & 3 deletions src/core/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ export function Render(Base) {
throw new Error('Compiler is not initialized');
}

const collapsedGroupIds = new Set(
dom
.findAll(
sidebarNavEl,
'li.group.collapse > .group-title[role="button"][data-group-id]',
)
.map(elm => elm.getAttribute('data-group-id'))
.filter(Boolean),
);

dom.setHTML('.sidebar-nav', this.compiler.sidebar(text, maxLevel));

sidebarToggleEl.setAttribute('aria-expanded', String(!isMobile()));
Expand Down Expand Up @@ -356,9 +366,47 @@ export function Render(Base) {

pageLinkGroups.forEach(elm => {
elm.classList.add('group');
elm
.querySelector(':scope > p:not(:has(> *))')
?.classList.add('group-title');

let groupTitle = [...elm.children].find(
child => child.tagName === 'P' && !child.querySelector('a'),
);

if (!groupTitle) {
const sublist = [...elm.children].find(
child => child.tagName === 'UL',
);
const titleNodes = [];

for (const child of elm.childNodes) {
if (child === sublist) {
break;
}

titleNodes.push(child);
}

if (sublist && titleNodes.some(node => node.textContent?.trim())) {
const newGroupTitle = document.createElement('p');
titleNodes.forEach(node => newGroupTitle.append(node));
groupTitle = newGroupTitle;
elm.insertBefore(newGroupTitle, sublist);
}
}

groupTitle?.classList.add('group-title');

const rootList = elm.parentElement;

if (groupTitle && rootList?.parentElement === sidebarNavEl) {
const groupId = `${[...sidebarNavEl.children].indexOf(rootList)}:${[...rootList.children].indexOf(elm)}`;
const isCollapsed = collapsedGroupIds.has(groupId);

elm.classList.toggle('collapse', isCollapsed);
groupTitle.setAttribute('data-group-id', groupId);
groupTitle.setAttribute('role', 'button');
groupTitle.setAttribute('tabindex', '0');
groupTitle.setAttribute('aria-expanded', String(!isCollapsed));
}
});
}

Expand Down
42 changes: 39 additions & 3 deletions src/themes/shared/_classes.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
var(--sidebar-chevron-expanded-color) 2.75px 4.25px,
transparent 4.25px
);
/* Chevron down (Mono) */
--sidebar-group-title-bg-expanded: no-repeat
calc(var(--_sidebar_pagelink-bg-left) - 2px) center / 5px 6px
linear-gradient(
225deg,
transparent 2.75px,
var(--sidebar-chevron-collapsed-color) 2.75px 4.25px,
transparent 4.25px
),
no-repeat calc(var(--_sidebar_pagelink-bg-left) + 3px) center / 5px 6px
linear-gradient(
135deg,
transparent 2.75px,
var(--sidebar-chevron-collapsed-color) 2.75px 4.25px,
transparent 4.25px
);
/* Dot (active without children) */
--sidebar-pagelink-bg-empty: no-repeat var(--_sidebar_pagelink-bg-left) center /
7px 7px
Expand All @@ -65,9 +81,17 @@
}

body[class*='sidebar-chevron'] {
.sidebar-nav a.page-link.no-chevron {
.sidebar-nav :is(a.page-link, p.group-title[role='button']).no-chevron {
background: none;
}

.sidebar-nav p.group-title[role='button'] {
background: var(--sidebar-pagelink-bg);

&[aria-expanded='true'] {
background: var(--sidebar-group-title-bg-expanded);
}
}
}

/* Left */
Expand All @@ -81,7 +105,7 @@ body.sidebar-chevron-left {
--_inset: 18px;

li {
a.page-link {
:is(a.page-link, p.group-title[role='button']) {
padding-left: var(--_inset);
}

Expand All @@ -104,8 +128,12 @@ body.sidebar-chevron-left {

body.sidebar-chevron-right {
.sidebar-nav {
p.group-title[role='button'] {
margin-right: 0;
}

li {
a {
:is(a, p.group-title[role='button']) {
padding-right: calc(var(--_sidebar-inset) + 15px);
}
}
Expand All @@ -120,6 +148,14 @@ body.sidebar-chevron-right {
--sidebar-group-spacing: 1em;
}

body.sidebar-group-box {
:is(.app-nav-merged, .sidebar-nav)
> ul
> li.group:is(:has(+ :not(.group)), :last-child).collapse {
padding-bottom: var(--sidebar-group-spacing);
}
}

/* prettier-ignore */
:root:has(body.sidebar-group-underline) {
--sidebar-group-spacing : 0.5em;
Expand Down
15 changes: 13 additions & 2 deletions src/themes/shared/_sidebar.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
color: var(--sidebar-group-title-color);
font-size: var(--sidebar-group-title-font-size);
font-weight: var(--sidebar-group-title-font-weight);

&[role='button'] {
cursor: pointer;
user-select: none;

&:focus-visible {
outline: 2px solid currentColor;
outline-offset: 2px;
}
}
}
}

Expand All @@ -89,7 +99,7 @@
}

&.collapse {
> :not(a) {
> :not(a):not(.group-title) {
display: none;
}
}
Expand Down Expand Up @@ -175,6 +185,7 @@
body.sticky & {
position: fixed;
overflow-y: auto;
scrollbar-gutter: stable;
}
}

Expand Down Expand Up @@ -249,7 +260,7 @@
}

/* Increase tap target size on touch-only devices */
@media screen and not (any-hover) {
@media screen and (any-hover: none) {
width: calc(var(--content-margin-inline) - 10px);
}
}
Expand Down
Loading