forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnavigation-item.tsx
More file actions
42 lines (37 loc) · 1.03 KB
/
navigation-item.tsx
File metadata and controls
42 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React, { useRef, useEffect } from 'react';
import { Link } from 'gatsby';
interface Props {
key: string;
isRead: boolean;
isActive: boolean;
slug: string;
title: string;
onClick: () => void;
autoScroll: (height: number) => void;
}
const NavigationItem = ({ isRead, isActive, slug, title, onClick, autoScroll }: Props): JSX.Element => {
let className = 't-body2 side-nav__item ';
if (isRead) {
className += 'side-nav__item--done';
} else if (isActive) {
className += 'side-nav__item--active';
}
const element = useRef<HTMLAnchorElement | null>(null);
const handleRef = (ref?: HTMLAnchorElement | null): void => {
if (ref && isActive) {
element.current = ref;
}
};
useEffect((): void => {
if (element.current) {
const height = element.current.getBoundingClientRect().top;
autoScroll(height);
}
});
return (
<Link innerRef={handleRef} to={`/learn/${slug}`} onClick={onClick} className={className}>
{title}
</Link>
);
};
export default NavigationItem;