Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add aria attributes for MiniToc #244

Merged
merged 7 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 27 additions & 11 deletions src/components/MiniToc/MiniToc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
font-weight: 500;
color: var(--g-color-text-primary);
margin-bottom: 12px;
margin-top: 0;
}

&__sections {
Expand All @@ -24,34 +25,49 @@
&__section {
display: block;

& > #{$class}__section-link {
& > #{$class}__section-heading {
border-left-color: var(--g-color-line-generic);

@include focusable(-2px);
}

&-link {
&-heading {
display: flex;
align-items: center;
padding: 6px 6px 6px 12px;
min-height: 28px;

color: var(--g-color-text-secondary);
font-size: 13px;
font-weight: normal;
margin: 0;

border-left: 2px solid transparent;
text-decoration: none;

&:hover {
color: var(--g-color-text-complementary);
}
}

&_child {
#{$class}__section-link {
padding-left: 25px;
&_child {
#{$class}__section-link {
padding-left: 25px;
}
}
}

&_active > #{$class}__section-link {
&-link {
display: flex;
align-items: center;

width: 100%;
min-height: 28px;

padding: 6px 6px 6px 12px;

color: inherit;
text-decoration: none;

@include focusable(-2px);
}

&_active > #{$class}__section-heading {
color: var(--g-color-text-primary);
border-left-color: var(--g-color-text-primary);
}
Expand Down
65 changes: 55 additions & 10 deletions src/components/MiniToc/MiniToc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,60 @@ const MiniToc = memo<MinitocProps>(({headings, router, headerHeight, onItemClick
return null;
}

const miniTocContent = flatHeadings.map(({href, title, isChild}, index) => {
if (index === 0) {
if (isChild) {
return (
<li
key={href}
data-hash={href}
className={b('section')}
aria-current="location"
>
<h3 className={b('section-heading', {child: isChild})}>
<a className={b('section-link')} href={href} data-router-shallow>
{title}
</a>
</h3>
</li>
);
}
return (
<li key={href} data-hash={href} className={b('section')} aria-current="location">
<h2 className={b('section-heading', {child: isChild})}>
<a className={b('section-link')} href={href} data-router-shallow>
{title}
</a>
</h2>
</li>
);
}

if (isChild) {
return (
<li key={href} data-hash={href} className={b('section')}>
<h3 className={b('section-heading', {child: isChild})}>
<a className={b('section-link')} href={href} data-router-shallow>
{title}
</a>
</h3>
</li>
);
}
return (
<li key={href} data-hash={href} className={b('section')}>
<h2 className={b('section-heading', {child: isChild})}>
<a className={b('section-link')} href={href} data-router-shallow>
{title}
</a>
</h2>
</li>
);
});

return (
<div className={b()}>
<div className={b('title')}>{t<string>('title')}:</div>
<nav className={b()} aria-label={t('article-navigation')}>
<h1 className={b('title')}>{t<string>('title')}:</h1>
<Scrollspy
className={b('sections')}
currentClassName={b('section', {active: true})}
Expand All @@ -59,15 +110,9 @@ const MiniToc = memo<MinitocProps>(({headings, router, headerHeight, onItemClick
onSectionClick={onItemClick}
aria-label={t('description')}
>
{flatHeadings.map(({href, title, isChild}) => (
<li key={href} data-hash={href} className={b('section', {child: isChild})}>
<a href={href} className={b('section-link')} data-router-shallow>
{title}
</a>
</li>
))}
{miniTocContent}
</Scrollspy>
</div>
</nav>
);
});

Expand Down
1 change: 1 addition & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"mini-toc": {
"title": "In this article",
"article-navigation": "Article navigation",
"description": "Current article contents"
},
"toc-nav-panel": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"mini-toc": {
"title": "В этой статье",
"article-navigation": "Навигация по статье",
"description": "Содержание текущей статьи"
},
"toc-nav-panel": {
Expand Down