Repository navigation
Expand file tree
/
Copy pathCodeExample.tsx
More file actions
113 lines (106 loc) · 3.06 KB
/
Copy pathCodeExample.tsx
File metadata and controls
113 lines (106 loc) · 3.06 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { EyeIcon, EyeOffIcon } from "lucide-react";
import { type FC, useState } from "react";
import { Button } from "#/components/Button/Button";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "#/components/Tooltip/Tooltip";
import { cn } from "#/utils/cn";
import { CopyButton } from "../CopyButton/CopyButton";
interface CodeExampleProps {
code: string;
/** Defaulting to true to be on the safe side; you should have to opt out of the secure option, not remember to opt in */
secret?: boolean;
/** Redact parts of the code if the user doesn't want to obfuscate the whole code */
redactPattern?: RegExp;
/** Replacement text for redacted content */
redactReplacement?: string;
/** Show a button to reveal the redacted parts of the code */
showRevealButton?: boolean;
className?: string;
}
/**
* Component to show single-line code examples, with a copy button
*/
export const CodeExample: FC<CodeExampleProps> = ({
code,
className,
secret = true,
redactPattern,
redactReplacement = "********",
showRevealButton,
}) => {
const [showFullValue, setShowFullValue] = useState(false);
const displayValue = secret
? obfuscateText(code)
: redactPattern && !showFullValue
? code.replace(redactPattern, redactReplacement)
: code;
const showButtonLabel = showFullValue
? "Hide sensitive data"
: "Show sensitive data";
const icon = showFullValue ? (
<EyeOffIcon className="size-4" />
) : (
<EyeIcon className="size-4" />
);
return (
<div
className={cn(
"cursor-pointer flex flex-row items-center",
"text-content-primary font-mono text-sm",
"rounded-lg p-2 leading-normal border border-solid",
"hover:bg-surface-secondary",
className,
)}
>
<code
className={cn([
"px-2 py-0 flex-grow break-all",
secret && "[-webkit-text-security:disc]", // also supported by firefox
])}
>
{secret ? (
<>
{/*
* Obfuscating text even though we have the characters replaced with
* discs in the CSS for two reasons:
* 1. The CSS property is non-standard and won't work everywhere;
* MDN warns you not to rely on it alone in production
* 2. Even with it turned on and supported, the plaintext is still
* readily available in the HTML itself
*/}
<span aria-hidden>{displayValue}</span>
<span className="sr-only">
Encrypted text. Please access via the copy button.
</span>
</>
) : (
displayValue
)}
</code>
<div className="flex items-center gap-1 select-none">
{showRevealButton && redactPattern && !secret && (
<Tooltip>
<TooltipTrigger asChild>
<Button
size="icon"
variant="subtle"
aria-label={showButtonLabel}
onClick={() => setShowFullValue(!showFullValue)}
>
{icon}
</Button>
</TooltipTrigger>
<TooltipContent>{showButtonLabel}</TooltipContent>
</Tooltip>
)}
<CopyButton text={code} label="Copy code" />
</div>
</div>
);
};
function obfuscateText(text: string): string {
return new Array(text.length).fill("*").join("");
}