Skip to content

Commit

Permalink
feat: add labels for toc and mini toc
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmch committed Nov 3, 2023
1 parent b837206 commit 96dcdbe
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
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
10 changes: 6 additions & 4 deletions src/components/Toc/Toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react';

import block from 'bem-cn-lite';
import {omit} from 'lodash';
import {WithTranslation} from 'react-i18next';

import {withTranslation} from '../../hoc/withTranslation';
import {PopperPosition} from '../../hooks';
import {ControlSizes, Router, TocData, TocItem} from '../../models';
import {isActiveItem, normalizeHash, normalizePath} from '../../utils';
Expand All @@ -21,7 +23,7 @@ function zip<T>(array: string[], fill: T): Record<string, T> {
return array.reduce((acc, item) => Object.assign(acc, {[item]: fill}), {});
}

export interface TocProps extends TocData {
export interface TocProps extends TocData, WithTranslation {
router: Router;
headerHeight?: number;
tocTitleIcon?: React.ReactNode;
Expand Down Expand Up @@ -153,7 +155,7 @@ class Toc extends React.Component<TocProps, TocState> {

private renderList = (items: TocItem[]) => {
const {toggleItem} = this;
const {singlePage} = this.props;
const {singlePage, t} = this.props;
const {activeId, fixedById} = this.state;

const activeItem = activeId && this.state.registry.getItemById(activeId);
Expand All @@ -162,7 +164,7 @@ class Toc extends React.Component<TocProps, TocState> {
: {};

return (
<ul className={b('list')}>
<ul className={b('list')} aria-label={t('description')}>
{items.map((item, index) => {
const main = !this.state.registry.getParentId(item.id);
const active =
Expand Down Expand Up @@ -336,4 +338,4 @@ class Toc extends React.Component<TocProps, TocState> {
};
}

export default Toc;
export default withTranslation('toc')(Toc);
9 changes: 9 additions & 0 deletions src/hoc/withTranslation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {withTranslation as withTranslationReacti18n} from 'react-i18next';

import {configure} from '../config';

export function withTranslation(namespace: string) {
configure();

return withTranslationReacti18n(namespace);
}
6 changes: 5 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
"lang-control-text-en": "English"
},
"mini-toc": {
"title": "In this article"
"title": "In this article",
"description": "Current article contents"
},
"toc": {
"description": "Table of contents"
},
"toc-nav-panel": {
"hint_previous": "Previous",
Expand Down
6 changes: 5 additions & 1 deletion src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
"lang-control-text-en": "English"
},
"mini-toc": {
"title": "В этой статье"
"title": "В этой статье",
"description": "Содержание текущей статьи"
},
"toc": {
"description": "Содержание"
},
"toc-nav-panel": {
"hint_previous": "Предыдущая",
Expand Down

0 comments on commit 96dcdbe

Please sign in to comment.