Skip to content

Commit

Permalink
feat(toc): Add toc label
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Mar 25, 2024
1 parent 6bc4859 commit a3bcdc1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions demo/src/Components/DocPage/page-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
"toc": {
"title": "Yandex.Cloud overview",
"href": "/docs/overview/",
"label": {
"title": "NEW",
"description": "This it new section",
"theme": "info"
},
"items": [
{
"name": "Yandex.Cloud services",
Expand Down
5 changes: 5 additions & 0 deletions demo/src/Components/DocPage/page-he.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"toc": {
"title": "סקירת פלטפורמה",
"href": "/docs/overview/",
"label": {
"title": "חדש",
"description": "זה זה סעיף חדש",
"theme": "info"
},
"items": [
{
"name": "שירותי Yandex.Cloud",
Expand Down
5 changes: 5 additions & 0 deletions demo/src/Components/DocPage/page-ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"toc": {
"title": "Обзор платформы",
"href": "/docs/overview/",
"label": {
"title": "Новое",
"description": "Это новый раздел",
"theme": "info"
},
"items": [
{
"name": "Сервисы Яндекс.Облака",
Expand Down
4 changes: 3 additions & 1 deletion src/components/Toc/Toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {isActiveItem, normalizeHash, normalizePath} from '../../utils';
import {Controls, ControlsLayout} from '../Controls';
import {HTML} from '../HTML';
import {TocItem as Item} from '../TocItem';
import TocLabel from '../TocLable/TocLabel';

import {TocItemRegistry} from './TocItemRegistry';

Expand Down Expand Up @@ -207,7 +208,7 @@ class Toc extends React.Component<TocProps, TocState> {
}

private renderTop() {
const {router, title, href, tocTitleIcon, hideTocHeader, singlePage} = this.props;
const {router, title, href, tocTitleIcon, hideTocHeader, singlePage, label} = this.props;
const {contentScrolled} = this.state;
let topHeader;

Expand Down Expand Up @@ -243,6 +244,7 @@ class Toc extends React.Component<TocProps, TocState> {
</div>
) : null}
{topHeader}
{label ? <TocLabel label={label} /> : null}
</div>
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/components/TocLable/TocLabel.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import '../../styles/variables';
@import '../../styles/mixins';

.dc-toc-label {
margin-left: 10px;

[dir='rtl'] & {
margin-right: 10px;
}
}
38 changes: 38 additions & 0 deletions src/components/TocLable/TocLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, {FC} from 'react';

import {Label, Popover, useDirection} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';

import {TocLabel as TocLabelType} from '../../models';

import './TocLabel.scss';

const b = block('dc-toc-label');

interface TocLabelProps {
label: TocLabelType;
}

const TocLabel: FC<TocLabelProps> = ({label}) => {
const direction = useDirection();

let labelElement = null;
if (label?.title) {
labelElement = (
<Label size={'xs'} theme={label.theme} className={b()}>
{label.title}
</Label>
);
if (label.description) {
const placement = direction === 'rtl' ? 'left' : 'right';
labelElement = (
<Popover content={label.description} placement={placement} size={'s'}>
{labelElement}
</Popover>
);
}
}
return labelElement;
};

export default TocLabel;
9 changes: 9 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {LabelProps} from '@gravity-ui/uikit/build/esm/components/Label/Label';

import type {Loc} from '../config/i18n';

export enum Theme {
Expand Down Expand Up @@ -96,13 +98,20 @@ export interface DocMeta {
updatedAt?: string;
}

export interface TocLabel {
title: string;
description?: string;
theme?: LabelProps['theme'];
}

export interface TocData {
title?: string;
href: string;
items: TocItem[];
stage?: string;
editable?: boolean;
singlePage?: boolean;
label?: TocLabel;
}

export interface TocItem {
Expand Down

0 comments on commit a3bcdc1

Please sign in to comment.