Skip to content

Commit

Permalink
feat: add labels for toc and mini toc (#187)
Browse files Browse the repository at this point in the history
* feat: add labels for toc and mini toc
  • Loading branch information
Kyzyl-ool authored Nov 8, 2023
1 parent b837206 commit 70a30a2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@gravity-ui/icons": "^2.5.0",
"@gravity-ui/uikit": "^5.12.2",
"@gravity-ui/uikit": "^5.19.0",
"@popperjs/core": "^2.11.2",
"bem-cn-lite": "4.1.0",
"i18next": "^19.9.2",
Expand Down
1 change: 1 addition & 0 deletions src/components/MiniToc/MiniToc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const MiniToc = memo<MinitocProps>(({headings, router, headerHeight, onItemClick
router={router}
headerHeight={headerHeight}
onSectionClick={onItemClick}
aria-label={t('description')}
>
{flatHeadings.map(({href, title, isChild}) => (
<li key={href} data-hash={href} className={b('section', {child: isChild})}>
Expand Down
10 changes: 6 additions & 4 deletions src/components/Scrollspy/Scrollspy.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {ReactElement} from 'react';
import React, {HTMLProps, ReactElement} from 'react';

import {debounce, isEqual} from 'lodash';
import scrollIntoView from 'scroll-into-view-if-needed';
Expand All @@ -12,7 +12,9 @@ interface ScrollspyDefaultProps {
headerHeight: number;
}

export interface ScrollspyProps extends Partial<ScrollspyDefaultProps> {
export interface ScrollspyProps
extends Partial<ScrollspyDefaultProps>,
Partial<HTMLProps<HTMLUListElement>> {
items: string[];
children: ReactElement[];
router: Router;
Expand Down Expand Up @@ -93,7 +95,7 @@ export class Scrollspy extends React.Component<ScrollspyInnerProps, ScrollspySta
}

render() {
const {children, currentClassName, className} = this.props;
const {children, currentClassName, className, ...rest} = this.props;
const {inViewState} = this.state;

const items = children.map((child, index) => {
Expand Down Expand Up @@ -124,7 +126,7 @@ export class Scrollspy extends React.Component<ScrollspyInnerProps, ScrollspySta
});

return (
<ul className={className} ref={this.containerRef}>
<ul className={className} ref={this.containerRef} {...rest}>
{items}
</ul>
);
Expand Down
13 changes: 10 additions & 3 deletions src/components/Toc/Toc.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';

import {getUniqId} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import {omit} from 'lodash';

Expand Down Expand Up @@ -45,11 +46,13 @@ class Toc extends React.Component<TocProps, TocState> {

containerEl: HTMLElement | null = null;
footerEl: HTMLElement | null = null;
tocTopId: string;

constructor(props: TocProps) {
super(props);

this.state = this.computeState(this.getInitialState());
this.tocTopId = getUniqId();
}

getInitialState() {
Expand Down Expand Up @@ -162,7 +165,7 @@ class Toc extends React.Component<TocProps, TocState> {
: {};

return (
<ul className={b('list')}>
<ul className={b('list')} aria-labelledby={this.tocTopId}>
{items.map((item, index) => {
const main = !this.state.registry.getParentId(item.id);
const active =
Expand Down Expand Up @@ -233,8 +236,12 @@ class Toc extends React.Component<TocProps, TocState> {
}

return (
<div className={b('top', {scrolled: contentScrolled})}>
{tocTitleIcon ? <div className={b('top-header-icon')}>{tocTitleIcon}</div> : null}
<div className={b('top', {scrolled: contentScrolled})} id={this.tocTopId}>
{tocTitleIcon ? (
<div className={b('top-header-icon')} aria-hidden={'true'}>
{tocTitleIcon}
</div>
) : null}
{topHeader}
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"lang-control-text-en": "English"
},
"mini-toc": {
"title": "In this article"
"title": "In this article",
"description": "Current article contents"
},
"toc-nav-panel": {
"hint_previous": "Previous",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"lang-control-text-en": "English"
},
"mini-toc": {
"title": "В этой статье"
"title": "В этой статье",
"description": "Содержание текущей статьи"
},
"toc-nav-panel": {
"hint_previous": "Предыдущая",
Expand Down

0 comments on commit 70a30a2

Please sign in to comment.