Skip to content

Build your personal knowledge base with Trilium Notes

refactor(options): add a reusable CodeBlock component

Elian Doran5f0dfaa0c46b
diff --git a/apps/client/src/widgets/react/CodeBlock.css b/apps/client/src/widgets/react/CodeBlock.css
new file mode 100644
index 0000000000..5a3ec82756
--- /dev/null
+++ b/apps/client/src/widgets/react/CodeBlock.css
@@ -0,0 +1,56 @@
+/*
+ * Mirrors the code-block treatment text notes get from theme-next
+ * (stylesheets/theme-next/notes/text.css → "Code Blocks"), reusing the same
+ * --code-block-* variables so both stay in step when a theme retunes them.
+ */
+
+.code-block {
+ position: relative;
+ margin: 0;
+ padding: 0.75em 1em;
+ border: 0;
+ border-radius: 6px;
+ background-color: var(--accented-background-color);
+ box-shadow: var(--code-block-box-shadow);
+ overflow-x: auto;
+}
+
+.code-block > code {
+ display: block;
+ font-variant: none;
+ tab-size: var(--code-block-tab-width, 4);
+ white-space: pre;
+}
+
+.code-block.wrap > code {
+ white-space: pre-wrap;
+ word-break: break-word;
+}
+
+/* Reserve room so a long line never slides under the button. The margin centres the
+ button on single-line blocks, matching the calculation used for text notes. */
+.code-block:has(> .code-block-copy) {
+ --code-block-copy-size: 1.8em;
+ padding-inline-end: calc(var(--code-block-copy-size) + 1.5em);
+}
+
+.code-block > .code-block-copy {
+ position: absolute;
+ top: 0.4em;
+ inset-inline-end: 0.4em;
+ font-size: 1em; /* --icon-button-size misbehaves with em units; matches text.css */
+ opacity: 0;
+ transition: opacity 150ms ease;
+}
+
+.code-block:hover > .code-block-copy,
+.code-block > .code-block-copy:focus-visible {
+ opacity: 1;
+}
+
+/* Touch devices have no hover, so the affordance must always be visible. */
+@media (hover: none) {
+ .code-block > .code-block-copy {
+ opacity: 1;
+ }
+}
diff --git a/apps/client/src/widgets/react/CodeBlock.tsx b/apps/client/src/widgets/react/CodeBlock.tsx
new file mode 100644
index 0000000000..9dac943978
--- /dev/null
+++ b/apps/client/src/widgets/react/CodeBlock.tsx
@@ -0,0 +1,44 @@
+import "./CodeBlock.css";
+
+import { copyTextWithToast } from "../../services/clipboard_ext";
+import { t } from "../../services/i18n";
+import ActionButton from "./ActionButton";
+
+interface CodeBlockProps {
+ /** The code to display. Rendered verbatim — never HTML. */
+ code: string;
+ /** Shows a copy-to-clipboard button in the corner. */
+ copyable?: boolean;
+ /** Wraps long lines instead of scrolling horizontally. */
+ wrap?: boolean;
+ className?: string;
+}
+
+/**
+ * A read-only code block styled to match the ones rendered inside text notes.
+ *
+ * Text notes get their code blocks from CKEditor, which the jQuery helpers in
+ * `services/syntax_highlight` decorate after the fact. That path can't be reused from
+ * Preact, so this component reproduces the same treatment — shared `--code-block-*`
+ * variables, the same copy affordance — for code samples rendered directly by the UI
+ * (options screens, dialogs). It does not syntax-highlight: these are short, static
+ * samples, and pulling in highlight.js for them would cost far more than it returns.
+ */
+export default function CodeBlock({ code, copyable = true, wrap, className }: CodeBlockProps) {
+ return (
+ <pre className={`code-block ${wrap ? "wrap" : ""} ${className ?? ""}`}>
+ <code>{code}</code>
+ {copyable && (
+ <ActionButton
+ className="code-block-copy"
+ icon="bx bx-copy"
+ text={t("code_block.copy_title")}
+ onClick={(e) => {
+ e.stopPropagation();
+ copyTextWithToast(code);
+ }}
+ />
+ )}
+ </pre>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsx
index 4a84bceb96..988069786f 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.tsx
+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx
@@ -9,6 +9,7 @@ import server from "../../../services/server";
import { isStandalone } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
import Button from "../../react/Button";
+import CodeBlock from "../../react/CodeBlock";
import Collapsible from "../../react/Collapsible";
import FormTextBox from "../../react/FormTextBox";
import FormToggle from "../../react/FormToggle";
@@ -180,9 +181,9 @@ function McpSettings() {
<Collapsible title={t("llm.mcp_config_title")} initiallyExpanded>
<p>{t("llm.mcp_config_description")}</p>
- <pre><code>{buildMcpClientConfig(localUrl, t("llm.mcp_config_token_placeholder"))}</code></pre>
+ <CodeBlock code={buildMcpClientConfig(localUrl, t("llm.mcp_config_token_placeholder"))} />
<p>{t("llm.mcp_config_cli_description")}</p>
- <pre><code>{buildMcpClientCommand(localUrl, t("llm.mcp_config_token_placeholder"))}</code></pre>
+ <CodeBlock code={buildMcpClientCommand(localUrl, t("llm.mcp_config_token_placeholder"))} wrap />
<p class="mcp-config-warning">{t("llm.mcp_config_warning")}</p>
</Collapsible>
</>
diff --git a/apps/client/src/widgets/type_widgets/options/security.tsx b/apps/client/src/widgets/type_widgets/options/security.tsx
index 8d5d6464fd..b2c5315282 100644
--- a/apps/client/src/widgets/type_widgets/options/security.tsx
+++ b/apps/client/src/widgets/type_widgets/options/security.tsx
@@ -3,6 +3,7 @@ import { useState } from "preact/hooks";
import { t } from "../../../services/i18n";
import { isElectron, restartDesktopApp } from "../../../services/utils";
import Button from "../../react/Button";
+import CodeBlock from "../../react/CodeBlock";
import Collapsible from "../../react/Collapsible";
import { useTriliumOptionBool } from "../../react/hooks";
import OptionsPageHeader from "./components/OptionsPageHeader";
@@ -70,9 +71,9 @@ function ServerConfigHint({ configKey, envVar }: { configKey: string; envVar: st
return (
<Collapsible title={t("security.how_to_enable")}>
<p>{t("security.server_config_hint")}</p>
- <pre><code>{`[Security]\n${configKey}=true`}</code></pre>
+ <CodeBlock code={`[Security]\n${configKey}=true`} />
<p>{t("security.server_env_hint")}</p>
- <pre><code>{envVar}=true</code></pre>
+ <CodeBlock code={`${envVar}=true`} />
</Collapsible>
);
}