Skip to content

Feat: Add aria attributes to sidebar toggle button and current page link #2254

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 20 commits into from
Nov 30, 2023
Merged
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
26 changes: 20 additions & 6 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,18 @@ export function Events(Base) {
const topMargin = config().topMargin;
// Use [id='1234'] instead of #id to handle special cases such as reserved characters and pure number id
// https://stackoverflow.com/questions/37270787/uncaught-syntaxerror-failed-to-execute-queryselector-on-document
const section = dom.find("[id='" + id + "']");
const section = dom.find(`[id="${id}"]`);
section && this.#scrollTo(section, topMargin);

const li = this.#nav[this.#getNavKey(path, id)];
const sidebar = dom.getNode('.sidebar');
const active = dom.find(sidebar, 'li.active');
active && active.classList.remove('active');
li && li.classList.add('active');
const oldActive = dom.find(sidebar, 'li.active');
const oldPage = dom.find(sidebar, `[aria-current]`);
const newActive = this.#nav[this.#getNavKey(path, id)];
const newPage = dom.find(sidebar, `[href$="${path}"]`)?.parentNode;
oldActive?.classList.remove('active');
oldPage?.removeAttribute('aria-current');
newActive?.classList.add('active');
newPage?.setAttribute('aria-current', 'page');
}

#scrollEl = dom.$.scrollingElement || dom.$.documentElement;
Expand All @@ -257,7 +261,15 @@ export function Events(Base) {
* @void
*/
#btn(el) {
const toggle = _ => dom.body.classList.toggle('close');
const toggle = _ => {
dom.body.classList.toggle('close');

const isClosed = isMobile
? dom.body.classList.contains('close')
: !dom.body.classList.contains('close');

el.setAttribute('aria-expanded', isClosed);
};

el = dom.getNode(el);
if (el === null || el === undefined) {
Expand Down Expand Up @@ -342,8 +354,10 @@ export function Events(Base) {
if (hash.indexOf(href) === 0 && !target) {
target = a;
dom.toggleClass(node, 'add', 'active');
node.setAttribute('aria-current', 'page');
} else {
dom.toggleClass(node, 'remove', 'active');
node.removeAttribute('aria-current');
}
});

Expand Down
20 changes: 9 additions & 11 deletions src/core/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,29 +275,27 @@ export function Render(Base) {

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

if (hideSidebar) {
// FIXME : better styling solution
[
document.querySelector('aside.sidebar'),
document.querySelector('button.sidebar-toggle'),
]
.filter(e => !!e)
.forEach(node => node.parentNode.removeChild(node));
document.querySelector('section.content').style.right = 'unset';
document.querySelector('section.content').style.left = 'unset';
document.querySelector('section.content').style.position = 'relative';
document.querySelector('section.content').style.width = '100%';
dom.body.classList.add('hidesidebar');
sidebarEl?.remove(sidebarEl);
sidebarToggleEl?.remove(sidebarToggleEl);

return null;
}

this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel));
sidebarToggleEl.setAttribute('aria-expanded', !isMobile);

const activeEl = this.__getAndActive(
this.router,
'.sidebar-nav',
true,
true
);

if (loadSidebar && activeEl) {
activeEl.parentNode.innerHTML +=
this.compiler.subSidebar(subMaxLevel) || '';
Expand Down
6 changes: 3 additions & 3 deletions src/core/render/tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export function main(config) {
const name = config.name ? config.name : '';

const aside = /* html */ `
<button class="sidebar-toggle" aria-label="Menu">
<div class="sidebar-toggle-button">
<button class="sidebar-toggle" aria-label="Toggle primary navigation" aria-controls="__sidebar">
<div class="sidebar-toggle-button" aria-hidden="true">
<span></span><span></span><span></span>
</div>
</button>
<aside class="sidebar" role="none">
<aside id="__sidebar" class="sidebar" role="none">
${
config.name
? /* html */ `
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/search/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function style() {
function tpl(defaultValue = '') {
const html = /* html */ `
<div class="input-wrap">
<input type="search" value="${defaultValue}" aria-label="Search text" />
<input type="search" value="${defaultValue}" />
<div class="clear-button">
<svg width="26" height="24">
<circle cx="12" cy="12" r="11" fill="#ccc" />
Expand Down
5 changes: 5 additions & 0 deletions src/themes/basic/_layout.styl
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ body.sticky
left $sidebar-width
transition left 250ms ease

body.hidesidebar &
position relative
left unset
right unset

/* markdown content found on pages */
.markdown-section
margin 0 auto
Expand Down
4 changes: 2 additions & 2 deletions test/integration/__snapshots__/docs.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ exports[`Docs Site coverpage renders and is unchanged 1`] = `
</section>"
`;

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>"`;
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>"`;

exports[`Docs Site sidebar renders and is unchanged 1`] = `
"<aside class=\\"sidebar\\" role=\\"none\\">
"<aside id=\\"__sidebar\\" class=\\"sidebar\\" role=\\"none\\">

<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>
</aside>"
Expand Down