Skip to content

Commit

Permalink
feat(DocPage): added new prop for minitoc onclick handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yndx-birman authored and 3y3 committed Oct 12, 2023
1 parent 9921c6d commit ee0b1dc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions demo/src/Components/DocPage/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const useSettings = () => {
onChangeShowMiniToc,
textSize,
onChangeTextSize,
onMiniTocItemClick: (event: MouseEvent) => {
// eslint-disable-next-line no-console
console.log((event.target as HTMLAnchorElement).hash);
},
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/DocPage/DocPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface DocPageProps extends DocPageData, DocSettings {
onContentLoaded?: () => void;
onSubscribe?: (data: SubscribeData) => void;
pdfLink?: string;
onMiniTocItemClick?: (event: MouseEvent) => void;
}

type DocPageInnerProps = InnerProps<DocPageProps, DocSettings>;
Expand Down Expand Up @@ -483,7 +484,7 @@ class DocPage extends React.Component<DocPageInnerProps, DocPageState> {
}

private renderAsideMiniToc() {
const {headings, router, headerHeight} = this.props;
const {headings, router, headerHeight, onMiniTocItemClick} = this.props;
const {keyDOM} = this.state;

return (
Expand All @@ -493,6 +494,7 @@ class DocPage extends React.Component<DocPageInnerProps, DocPageState> {
router={router}
headerHeight={headerHeight}
key={keyDOM}
onItemClick={onMiniTocItemClick}
/>
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion src/components/MiniToc/MiniToc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface MinitocProps {
headings: DocHeadingItem[];
router: Router;
headerHeight?: number;
onItemClick?: (event: MouseEvent) => void;
}

export interface MinitocSectionProps {
Expand All @@ -34,7 +35,7 @@ function getFlatHeadings(items: DocHeadingItem[], isChild = false): FlatHeadingI
}, [] as FlatHeadingItem[]);
}

const MiniToc = memo<MinitocProps>(({headings, router, headerHeight}) => {
const MiniToc = memo<MinitocProps>(({headings, router, headerHeight, onItemClick}) => {
const {t} = useTranslation('mini-toc');
const flatHeadings = useMemo(() => getFlatHeadings(headings), [headings]);
const sectionHrefs = useMemo(
Expand All @@ -55,6 +56,7 @@ const MiniToc = memo<MinitocProps>(({headings, router, headerHeight}) => {
items={sectionHrefs}
router={router}
headerHeight={headerHeight}
onSectionClick={onItemClick}
>
{flatHeadings.map(({href, title, isChild}) => (
<li key={href} data-hash={href} className={b('section', {child: isChild})}>
Expand Down

0 comments on commit ee0b1dc

Please sign in to comment.