-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemosList.tsx
91 lines (73 loc) · 2.43 KB
/
DemosList.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { createElement, FC } from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Demo } from './Demo';
import { DemosGroup } from './DemoGroup';
type DemosListProps = {
groups: DemosGroup[];
};
export const DemosList: React.FC<DemosListProps> = ({ groups }) => (
<div className="demos__content">
<main>
<DemosGroups items={groups} path={[]} />
</main>
</div>
);
type DemosGroupsProps = {
path: string[];
items: Array<DemosGroup | Demo>;
};
const DemosGroups: React.FC<DemosGroupsProps> = ({ path, items }) => (
<>
{items.map((item) => (
<GroupRoute key={item.label} path={['', ...path, item.label].join('/')}>
{DemosGroup.isDemoGroup(item) && <DemosGroupComponent path={[...path, item.label]} group={item} />}
{Demo.isDemo(item) && <DemoComponent path={[...path, item.label]} demo={item} />}
</GroupRoute>
))}
</>
);
type DemosGroupComponentProps = {
path: string[];
group: DemosGroup;
};
const DemosGroupComponent: React.FC<DemosGroupComponentProps> = ({ path, group }) => (
<div className="demos__demo-group">
<DemoGroupName className="demos__demo-group__name" depth={path.length}>
{group.label}
</DemoGroupName>
<DemosGroups path={path} items={group.children} />
</div>
);
type DemoComponentProps = {
path: string[];
demo: Demo;
};
const DemoComponent: React.FC<DemoComponentProps> = ({ path, demo }) => (
<div className="demos__demo-item">
<span className="demos__demo-item__name">{demo.label}</span>
{demo.description && <div className="demos__demo-item__description">{demo.description}</div>}
<div className="demos__demo-item__container">
<Link to={{ pathname: '/', search: 'demo-path=/' + path.join('/') }} className="demos__demo-item__view">
View demo
</Link>
<iframe src={`/?demo-path=${'/' + path.join('/')}`} frameBorder={0} className="demos__demo-iframe" />
</div>
</div>
);
type GroupRouteProps = {
path: string;
};
const GroupRoute: React.FC<GroupRouteProps> = ({ path, children }) => {
const location = useLocation();
if (location.pathname.startsWith(path.slice(0, location.pathname.length))) {
return <>{children}</>;
}
return null;
};
type DemoGroupNameProps = {
className?: string;
depth: number;
};
const DemoGroupName: FC<DemoGroupNameProps> = ({ className, depth, children }) => {
return createElement(`h${Math.min(depth + 1, 6)}`, { className }, children);
};