- add a RevealableCodeBlock ui component that renders its code only after the user clicks Reveal, with a storybook story - use it for the data section of the credential page - bump version to 1.21.1
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Eye, EyeOff } from 'lucide-react';
|
|
import { Button } from '../Button/Button';
|
|
import { CodeBlock } from '../CodeBlock/CodeBlock';
|
|
import styles from './RevealableCodeBlock.module.css';
|
|
|
|
export interface RevealableCodeBlockProps {
|
|
code: string;
|
|
language?: string;
|
|
}
|
|
|
|
/** A CodeBlock that starts hidden; the code is only rendered after the user reveals it. */
|
|
export function RevealableCodeBlock({ code, language }: RevealableCodeBlockProps) {
|
|
const { t } = useTranslation();
|
|
const [revealed, setRevealed] = useState(false);
|
|
|
|
return (
|
|
<div className={styles.wrapper}>
|
|
<div className={styles.content}>
|
|
{revealed ? (
|
|
<CodeBlock code={code} language={language} />
|
|
) : (
|
|
<p className={styles.placeholder}>{t('revealable.hidden')}</p>
|
|
)}
|
|
</div>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
aria-expanded={revealed}
|
|
onClick={() => setRevealed((r) => !r)}
|
|
>
|
|
{revealed ? <EyeOff size={14} /> : <Eye size={14} />}
|
|
{revealed ? t('revealable.hide') : t('revealable.reveal')}
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|