Code snippet
The CodeSnippet component displays single-line or multiline code with a copy-to-clipboard button. It supports different visual variants and uses the standard rounded corners by default (radius=“default”).
Default
A default inline snippet with monospace font and copy button.
npm install @talkjs/ui<CodeSnippet code="npm install @talkjs/ui" width="w-[400px]" />Inverted
Use variant=“inverted” to invert the snippet background and text.
npm install @talkjs/ui<CodeSnippet code="npm install @talkjs/ui" variant="inverted" width="w-[400px]" />Variants
You can use color variants like blue and pink for emphasis. The copy button can also be displayed as icon or text only.
npm install @talkjs/uinpm install @talkjs/ui<CodeSnippet code="npm install @talkjs/ui" variant="blue" copyVariant="icon" /><CodeSnippet code="npm install @talkjs/ui" variant="pink" copyVariant="icon" />Syntax
Use variant=“syntax” for JavaScript and JSX highlighting, including component names, attributes, strings, and expressions.
function MyChatbox() {
const appId = "8hj9w09";
const userId = "alice";
// Render the chatbox
return (
<Chatbox
appId={appId}
userId={userId}
conversationId="backpack-order"
style={{ width: "400px", height: "500px" }}
/>
);
}<CodeSnippet variant="syntax" code={code} />Multiline
To display a multiline code snippet, pass a multiline string to the code prop. Use variant and copyVariant to style it.
import { Session, Chatbox } from "@talkjs/react";
<Session appId="t824ba8" userId="pete">
<Chatbox conversation="workshop" className="chatbox" />
</Session><CodeSnippet
code={`import { Session, Chatbox } from "@talkjs/react";
<Session appId="t824ba8" userId="pete">
<Chatbox conversation="workshop" className="chatbox" />
</Session>`}
copyVariant="icon"
variant="blue"
/>Line numbers and highlights
Use showLineNumbers to show a number beside each line. Use highlightLines=“2,4-6” to highlight individual lines or inclusive ranges. Both are optional and work with every visual variant.
function MyChatbox() {
const appId = "8hj9w09";
return (
<Chatbox appId={appId} userId="alice" />
);
}<CodeSnippet
code={code}
variant="syntax"
showLineNumbers
highlightLines="2,4-6"
/>
Line positions start at 1 after the snippet removes surrounding blank lines and common indentation. Blank lines count. Invalid selections and positions outside the snippet are ignored. Line numbers are decorative and are excluded from selection and the copy button’s output.
Deleted lines
Use deletedLines with the same range format to show removed code with a red background and strikethrough. Deleted styling takes priority over highlighting. The copy button excludes deleted lines, so the copied example contains only the remaining code.
const settings = {
theme: "legacy",
theme: "custom",
};<CodeSnippet code={code} variant="syntax" showLineNumbers deletedLines="2" highlightLines="3" />
Manual text selection still includes deleted code. Use the copy button for the example with deletions removed.
Wrapping and scrolling
Long lines scroll horizontally by default. Enable wrap to fit them within the snippet. A wrapped line keeps one number and its highlight covers all wrapped text.
const settings = {
welcomeMessage: "Welcome to the conversation. Ask a question and our team will help you get started with your first chat experience.",
};const settings = {
welcomeMessage: "Welcome to the conversation. Ask a question and our team will help you get started with your first chat experience.",
oldWelcomeMessage: "Hello",
};Inside tabs
Compose snippets with the shared Tabs wrapper. Set border=“none” on each snippet to let the wrapper provide the border.
const theme = "legacy";Line options
| Prop | Type | Default | Behaviour |
|---|---|---|---|
showLineNumbers | boolean | false | Shows one number per source line. |
highlightLines | string | "" | Highlights positions such as "2,4-6". |
deletedLines | string | "" | Strikes through selected lines and omits them from button copies. |
These options apply to block snippets only; inline snippets ignore them. Existing snippets keep their current appearance when the options are omitted. Syntax colouring remains JavaScript/JSX-focused; these options do not add language detection.
External syntax highlighting
Use renderLine(line, index) to supply highlighted React content for each line from a highlighter such as Prism. The index starts at 0. The snippet still handles line numbers, line backgrounds, scrolling and its copy button; copying uses the plain code string, not the rendered markup.
Tokenize the complete snippet before rendering individual lines so multiline strings and comments retain their colours. Set preserveWhitespace when the source already has the intended indentation and blank lines. The callback’s line positions must match that source. This hook applies to block snippets only.
<CodeSnippet
code={source}
preserveWhitespace
showLineNumbers
renderLine={(line, index) => highlightedLines[index]}
/>Optional header places metadata or controls above the code within the shared container. Optional codeProps adds attributes to the inner code element, for example a language marker or export metadata. The snippet controls its own layout classes.
Usage
Import the CodeSnippet component and pass a string as code. The component can be referenced using the following format:
import CodeSnippet from "../../components/CodeSnippet";