-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
@use '../variables'; | ||
@use '../../../styles/mixins.scss'; | ||
|
||
$block: '.#{variables.$ns}toc'; | ||
$block: '.#{variables.$ns-new}toc'; | ||
|
||
#{$block} { | ||
&__title { | ||
font-size: var(--g-text-body-2-font-size); | ||
font-weight: 500; | ||
@include mixins.text-body-2(); | ||
|
||
color: var(--g-color-text-primary); | ||
margin-bottom: 12px; | ||
} | ||
|
||
&__sections { | ||
&__sections, | ||
&__subsections { | ||
padding: 0; | ||
margin: 0; | ||
|
||
overflow-y: auto; | ||
overflow-x: hidden; | ||
|
||
list-style: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,62 @@ | ||
import React from 'react'; | ||
|
||
import type {QAProps} from '../types'; | ||
import {block} from '../utils/cn'; | ||
import {blockNew} from '../utils/cn'; | ||
|
||
import {TocItem} from './TocItem/TocItem'; | ||
import type {TocItem as TocItemType} from './types'; | ||
import type {TocItems} from './types'; | ||
|
||
import './Toc.scss'; | ||
|
||
const b = block('toc'); | ||
const b = blockNew('toc'); | ||
|
||
export interface TocProps extends QAProps { | ||
className?: string; | ||
value: string; | ||
onUpdate: (value: string) => void; | ||
items: (TocItemType & { | ||
items?: TocItemType[]; | ||
})[]; | ||
value?: string; | ||
onUpdate?: (value: string) => void; | ||
items: TocItems; | ||
} | ||
|
||
export const Toc = React.forwardRef<HTMLDivElement, TocProps>(function Toc(props, ref) { | ||
export const Toc = React.forwardRef<HTMLElement, TocProps>(function Toc(props, ref) { | ||
const {value: activeValue, items, className, onUpdate, qa} = props; | ||
|
||
return ( | ||
<div className={b(null, className)} ref={ref} data-qa={qa}> | ||
<div className={b('sections')}> | ||
{items.map(({value, title, items: childrenItems}) => ( | ||
<React.Fragment key={value}> | ||
<nav className={b(null, className)} ref={ref} data-qa={qa}> | ||
<ul className={b('sections')}> | ||
{items.map(({value, content, href, items: childrenItems}) => ( | ||
<li key={value ?? href}> | ||
<TocItem | ||
title={title} | ||
content={content} | ||
value={value} | ||
href={href} | ||
active={activeValue === value} | ||
onClick={onUpdate} | ||
/> | ||
{childrenItems?.map(({value: childrenValue, title: childrenTitle}) => ( | ||
<TocItem | ||
key={childrenValue} | ||
title={childrenTitle} | ||
value={childrenValue} | ||
childItem={true} | ||
active={activeValue === childrenValue} | ||
onClick={onUpdate} | ||
/> | ||
))} | ||
</React.Fragment> | ||
{childrenItems?.length && ( | ||
<ul className={b('subsections')}> | ||
{childrenItems?.map( | ||
({ | ||
value: childrenValue, | ||
content: childrenContent, | ||
href: childrenHref, | ||
}) => ( | ||
<li key={childrenValue ?? childrenHref}> | ||
<TocItem | ||
content={childrenContent} | ||
value={childrenValue} | ||
href={childrenHref} | ||
childItem={true} | ||
active={activeValue === childrenValue} | ||
onClick={onUpdate} | ||
/> | ||
</li> | ||
), | ||
)} | ||
</ul> | ||
)} | ||
</li> | ||
))} | ||
</div> | ||
</div> | ||
</ul> | ||
</nav> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,48 @@ | ||
import React from 'react'; | ||
|
||
import {block} from '../../utils/cn'; | ||
import {blockNew} from '../../utils/cn'; | ||
import {useActionHandlers} from '../../utils/useActionHandlers'; | ||
import type {TocItem as TocItemType} from '../types'; | ||
|
||
import './TocItem.scss'; | ||
|
||
const b = block('toc-item'); | ||
const b = blockNew('toc-item'); | ||
|
||
export interface TocItemProps extends TocItemType { | ||
childItem?: boolean; | ||
active?: boolean; | ||
onClick: (value: string) => void; | ||
onClick?: (value: string) => void; | ||
} | ||
|
||
export const TocItem = (props: TocItemProps) => { | ||
const {childItem = false, active = false, onClick, title, value} = props; | ||
const {active = false, childItem = false, content, href, value, onClick} = props; | ||
|
||
const handleClick = () => onClick(value); | ||
const handleClick = React.useCallback(() => { | ||
if (value === undefined || !onClick) { | ||
return; | ||
} | ||
|
||
onClick(value); | ||
}, [onClick, value]); | ||
|
||
const {onKeyDown} = useActionHandlers(handleClick); | ||
|
||
return ( | ||
<div className={b('section', {child: childItem, active})}> | ||
const item = | ||
href === undefined ? ( | ||
<div | ||
role="radio" | ||
aria-checked={active} | ||
role="button" | ||
tabIndex={0} | ||
className={b('section-link')} | ||
onClick={handleClick} | ||
onKeyDown={onKeyDown} | ||
> | ||
{title} | ||
{content} | ||
</div> | ||
</div> | ||
); | ||
) : ( | ||
<a href={href} onClick={handleClick} className={b('section-link')}> | ||
{content} | ||
</a> | ||
); | ||
|
||
return <div className={b('section', {child: childItem, active})}>{item}</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.