Skip to content

Commit e7414e0

Browse files
Feat: Add aria attributes to sidebar toggle button and current page link (#2254)
1 parent 4d5bf5a commit e7414e0

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

src/core/event/index.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,18 @@ export function Events(Base) {
232232
const topMargin = config().topMargin;
233233
// Use [id='1234'] instead of #id to handle special cases such as reserved characters and pure number id
234234
// https://stackoverflow.com/questions/37270787/uncaught-syntaxerror-failed-to-execute-queryselector-on-document
235-
const section = dom.find("[id='" + id + "']");
235+
const section = dom.find(`[id="${id}"]`);
236236
section && this.#scrollTo(section, topMargin);
237237

238-
const li = this.#nav[this.#getNavKey(path, id)];
239238
const sidebar = dom.getNode('.sidebar');
240-
const active = dom.find(sidebar, 'li.active');
241-
active && active.classList.remove('active');
242-
li && li.classList.add('active');
239+
const oldActive = dom.find(sidebar, 'li.active');
240+
const oldPage = dom.find(sidebar, `[aria-current]`);
241+
const newActive = this.#nav[this.#getNavKey(path, id)];
242+
const newPage = dom.find(sidebar, `[href$="${path}"]`)?.parentNode;
243+
oldActive?.classList.remove('active');
244+
oldPage?.removeAttribute('aria-current');
245+
newActive?.classList.add('active');
246+
newPage?.setAttribute('aria-current', 'page');
243247
}
244248

245249
#scrollEl = dom.$.scrollingElement || dom.$.documentElement;
@@ -257,7 +261,15 @@ export function Events(Base) {
257261
* @void
258262
*/
259263
#btn(el) {
260-
const toggle = _ => dom.body.classList.toggle('close');
264+
const toggle = _ => {
265+
dom.body.classList.toggle('close');
266+
267+
const isClosed = isMobile
268+
? dom.body.classList.contains('close')
269+
: !dom.body.classList.contains('close');
270+
271+
el.setAttribute('aria-expanded', isClosed);
272+
};
261273

262274
el = dom.getNode(el);
263275
if (el === null || el === undefined) {
@@ -342,8 +354,10 @@ export function Events(Base) {
342354
if (hash.indexOf(href) === 0 && !target) {
343355
target = a;
344356
dom.toggleClass(node, 'add', 'active');
357+
node.setAttribute('aria-current', 'page');
345358
} else {
346359
dom.toggleClass(node, 'remove', 'active');
360+
node.removeAttribute('aria-current');
347361
}
348362
});
349363

src/core/render/index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -275,29 +275,27 @@ export function Render(Base) {
275275

276276
_renderSidebar(text) {
277277
const { maxLevel, subMaxLevel, loadSidebar, hideSidebar } = this.config;
278+
const sidebarEl = dom.getNode('aside.sidebar');
279+
const sidebarToggleEl = dom.getNode('button.sidebar-toggle');
278280

279281
if (hideSidebar) {
280-
// FIXME : better styling solution
281-
[
282-
document.querySelector('aside.sidebar'),
283-
document.querySelector('button.sidebar-toggle'),
284-
]
285-
.filter(e => !!e)
286-
.forEach(node => node.parentNode.removeChild(node));
287-
document.querySelector('section.content').style.right = 'unset';
288-
document.querySelector('section.content').style.left = 'unset';
289-
document.querySelector('section.content').style.position = 'relative';
290-
document.querySelector('section.content').style.width = '100%';
282+
dom.body.classList.add('hidesidebar');
283+
sidebarEl?.remove(sidebarEl);
284+
sidebarToggleEl?.remove(sidebarToggleEl);
285+
291286
return null;
292287
}
293288

294289
this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel));
290+
sidebarToggleEl.setAttribute('aria-expanded', !isMobile);
291+
295292
const activeEl = this.__getAndActive(
296293
this.router,
297294
'.sidebar-nav',
298295
true,
299296
true
300297
);
298+
301299
if (loadSidebar && activeEl) {
302300
activeEl.parentNode.innerHTML +=
303301
this.compiler.subSidebar(subMaxLevel) || '';

src/core/render/tpl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export function main(config) {
3737
const name = config.name ? config.name : '';
3838

3939
const aside = /* html */ `
40-
<button class="sidebar-toggle" aria-label="Menu">
41-
<div class="sidebar-toggle-button">
40+
<button class="sidebar-toggle" aria-label="Toggle primary navigation" aria-controls="__sidebar">
41+
<div class="sidebar-toggle-button" aria-hidden="true">
4242
<span></span><span></span><span></span>
4343
</div>
4444
</button>
45-
<aside class="sidebar" role="none">
45+
<aside id="__sidebar" class="sidebar" role="none">
4646
${
4747
config.name
4848
? /* html */ `

src/plugins/search/component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function style() {
118118
function tpl(defaultValue = '') {
119119
const html = /* html */ `
120120
<div class="input-wrap">
121-
<input type="search" value="${defaultValue}" aria-label="Search text" />
121+
<input type="search" value="${defaultValue}" />
122122
<div class="clear-button">
123123
<svg width="26" height="24">
124124
<circle cx="12" cy="12" r="11" fill="#ccc" />

src/themes/basic/_layout.styl

+5
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ body.sticky
349349
left $sidebar-width
350350
transition left 250ms ease
351351

352+
body.hidesidebar &
353+
position relative
354+
left unset
355+
right unset
356+
352357
/* markdown content found on pages */
353358
.markdown-section
354359
margin 0 auto

test/integration/__snapshots__/docs.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ exports[`Docs Site coverpage renders and is unchanged 1`] = `
1616
</section>"
1717
`;
1818
19-
exports[`Docs Site navbar renders and is unchanged 1`] = `"<nav aria-label=\\"secondary\\" class=\\"app-nav no-badge\\"><ul><li>Translations<ul><li><a href=\\"#/\\" title=\\"undefined\\" class=\\"active\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1ec-1f1e7.png?v8.png\\" alt=\\"uk\\" class=\\"emoji\\" loading=\\"lazy\\"> English</a></li><li><a href=\\"#/zh-cn/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1e8-1f1f3.png?v8.png\\" alt=\\"cn\\" class=\\"emoji\\" loading=\\"lazy\\"> 简体中文</a></li><li><a href=\\"#/de-de/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1e9-1f1ea.png?v8.png\\" alt=\\"de\\" class=\\"emoji\\" loading=\\"lazy\\"> Deutsch</a></li><li><a href=\\"#/es/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1ea-1f1f8.png?v8.png\\" alt=\\"es\\" class=\\"emoji\\" loading=\\"lazy\\"> Español</a></li><li><a href=\\"#/ru-ru/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1f7-1f1fa.png?v8.png\\" alt=\\"ru\\" class=\\"emoji\\" loading=\\"lazy\\"> Русский</a></li></ul></li></ul></nav>"`;
19+
exports[`Docs Site navbar renders and is unchanged 1`] = `"<nav aria-label=\\"secondary\\" class=\\"app-nav no-badge\\"><ul><li>Translations<ul><li><a href=\\"#/\\" title=\\"undefined\\" class=\\"active\\" aria-current=\\"page\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1ec-1f1e7.png?v8.png\\" alt=\\"uk\\" class=\\"emoji\\" loading=\\"lazy\\"> English</a></li><li><a href=\\"#/zh-cn/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1e8-1f1f3.png?v8.png\\" alt=\\"cn\\" class=\\"emoji\\" loading=\\"lazy\\"> 简体中文</a></li><li><a href=\\"#/de-de/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1e9-1f1ea.png?v8.png\\" alt=\\"de\\" class=\\"emoji\\" loading=\\"lazy\\"> Deutsch</a></li><li><a href=\\"#/es/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1ea-1f1f8.png?v8.png\\" alt=\\"es\\" class=\\"emoji\\" loading=\\"lazy\\"> Español</a></li><li><a href=\\"#/ru-ru/\\" title=\\"undefined\\"><img src=\\"https://github.githubassets.com/images/icons/emoji/unicode/1f1f7-1f1fa.png?v8.png\\" alt=\\"ru\\" class=\\"emoji\\" loading=\\"lazy\\"> Русский</a></li></ul></li></ul></nav>"`;
2020
2121
exports[`Docs Site sidebar renders and is unchanged 1`] = `
22-
"<aside class=\\"sidebar\\" role=\\"none\\">
22+
"<aside id=\\"__sidebar\\" class=\\"sidebar\\" role=\\"none\\">
2323
2424
<div class=\\"sidebar-nav\\" role=\\"navigation\\" aria-label=\\"primary\\"><ul><li><p>Getting started</p><ul><li><a href=\\"#/quickstart\\" title=\\"undefined\\">Quick start</a></li><li><a href=\\"#/more-pages\\" title=\\"undefined\\">Writing more pages</a></li><li><a href=\\"#/custom-navbar\\" title=\\"undefined\\">Custom navbar</a></li><li><a href=\\"#/cover\\" title=\\"undefined\\">Cover page</a></li></ul></li><li><p>Customization</p><ul><li><a href=\\"#/configuration\\" title=\\"undefined\\">Configuration</a></li><li><a href=\\"#/themes\\" title=\\"undefined\\">Themes</a></li><li><a href=\\"#/plugins\\" title=\\"undefined\\">List of Plugins</a></li><li><a href=\\"#/write-a-plugin\\" title=\\"undefined\\">Write a Plugin</a></li><li><a href=\\"#/markdown\\" title=\\"undefined\\">Markdown configuration</a></li><li><a href=\\"#/language-highlight\\" title=\\"undefined\\">Language highlighting</a></li><li><a href=\\"#/emoji\\" title=\\"undefined\\">Emoji</a></li></ul></li><li><p>Guide</p><ul><li><a href=\\"#/deploy\\" title=\\"undefined\\">Deploy</a></li><li><a href=\\"#/helpers\\" title=\\"undefined\\">Helpers</a></li><li><a href=\\"#/vue\\" title=\\"undefined\\">Vue compatibility</a></li><li><a href=\\"#/cdn\\" title=\\"undefined\\">CDN</a></li><li><a href=\\"#/pwa\\" title=\\"undefined\\">Offline Mode (PWA)</a></li><li><a href=\\"#/embed-files\\" title=\\"undefined\\">Embed Files</a></li></ul></li><li><p><a href=\\"#/awesome\\" title=\\"undefined\\">Awesome docsify</a></p></li><li><p><a href=\\"#/changelog\\" title=\\"undefined\\">Changelog</a></p></li></ul></div>
2525
</aside>"

0 commit comments

Comments
 (0)