forked from phuocng/1loc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippetItem.tsx
51 lines (41 loc) · 1.64 KB
/
SnippetItem.tsx
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
43
44
45
46
47
48
49
50
51
import React, { useEffect, useRef } from 'react';
import Markdown from './Markdown';
import { Snippet } from './snippets';
interface SnippetItemProps {
snippet: Snippet;
}
const SnippetItem: React.FC<SnippetItemProps> = ({ snippet }) => {
const containerRef = useRef<HTMLDivElement | null>(null);
const contentRef = useRef<HTMLDivElement | null>(null);
const capitalize = (str: string) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const formatName = (name: string) => capitalize(name.split('-').join(' '));
const toggle = () => {
const contentEle = contentRef.current;
contentEle && contentEle.classList.toggle('hidden');
};
const anchor = (str: string) => str.toLowerCase().split(' ').join('');
const id = anchor(snippet.name);
useEffect(() => {
const hash = location.hash ? location.hash.substr(1) : '';
if (hash === id) {
const container = containerRef.current;
container.scrollIntoView();
// Auto expand
toggle();
}
}, []);
return (
<div className='pt-2 mb-1' id={id} ref={containerRef}>
<div className='border border-gray-400'>
<h2 className='cursor-pointer py-1 px-2 bg-gray-100 flex justify-between group' onClick={toggle}>
{formatName(snippet.name)}
<a href={`#${id}`}>#</a>
</h2>
<div ref={contentRef} className='hidden border-t border-gray-400 text-sm'>
<Markdown content={snippet.body} />
</div>
</div>
</div>
);
};
export default SnippetItem;