-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.tsx
61 lines (56 loc) · 1.94 KB
/
index.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
import React, { useState, useEffect, ReactNode, ReactElement, JSX } from "react";
import { LangSpecific } from "./LangSpecific";
// This is a rather dirty hack, but it's kinda a necessary evil......
import { useTabs, type TabsProps } from "@docusaurus/theme-common/internal";
import style from "./styles.module.css";
export { LanguageSelector } from "./LanguageSelector";
export { DynamicMultiTargetCodeblock } from "./DynamicMultiTargetCodeblock";
export { LangSpecific, NoSelectorTargetCodeBlock } from "./LangSpecific";
export { ShowIf, ShowIfs, ShowIfsInline } from "./ShowIf";
// See https://danielbarta.com/literal-iteration-typescript/
export const targets = ["c", "cpp", "py", "rs", "ts"] as const;
export type TargetsType = (typeof targets)[number];
export const TargetToNameMap: Map<TargetsType, string> = new Map([
["c", "C"],
["cpp", "C++"],
["py", "Python"],
["rs", "Rust"],
["ts", "TypeScript"],
]);
export const TargetToOrderingMap: Map<TargetsType, number> = new Map([
["c", 0],
["cpp", 100],
["py", 200],
["rs", 300],
["ts", 400],
]);
export const ShowOnly = ({
children,
inline,
...suppliedTargets
}: Record<TargetsType, boolean | null> & {
children: ReactNode;
inline?: boolean;
}): JSX.Element => {
// We "fake" a tab here to receive metadata. This way the website doesn't look weird when things are hidden......
// useTabs is supposed to be internal though.... But we use it anyway. It could break I guess??
const fakeTabProps: TabsProps = {
values: [...TargetToNameMap].map(([target, targetName]) => ({
value: target,
label: targetName,
})),
children: [],
groupId: "target-languages",
};
const { selectedValue, selectValue, tabValues } = useTabs(fakeTabProps);
const ShowOnlyTag = inline === true ? "span" : "div";
return (
<ShowOnlyTag
className={
suppliedTargets[selectedValue] === true ? undefined : style.hidden
}
>
{children}
</ShowOnlyTag>
);
};