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(toc): add toc label #221

Merged
merged 2 commits into from
Mar 25, 2024
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
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
12 changes: 12 additions & 0 deletions src/components/TocLable/TocLabel.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import '../../styles/variables';
@import '../../styles/mixins';

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

[dir='rtl'] & {
margin-right: 10px;
}
}
}
44 changes: 44 additions & 0 deletions src/components/TocLable/TocLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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) {
const hasDescription = Boolean(label.description);
labelElement = (
<Label size={'xs'} theme={label.theme} className={b({offset: !hasDescription})}>
{label.title}
</Label>
);
if (hasDescription) {
const placement = direction === 'rtl' ? 'left' : 'right';
labelElement = (
<Popover
content={label.description}
placement={placement}
size={'s'}
className={b({offset: true})}
>
{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
Loading