Skip to main content

Code

Code is an inline code styling component with monospace font and optional background.

Import

import { Code } from "@tosui/react";

Basic Usage

<Text>
Run <Code>npm install</Code> to install dependencies.
</Text>

Variants

<VStack gap={4} align="flex-start">
<Text>Plain variant: <Code variant="plain">code here</Code></Text>
<Text>Subtle variant: <Code variant="subtle">code here</Code></Text>
</VStack>

Colors

<VStack gap={2} align="flex-start">
<Code variant="subtle">Default color</Code>
<Code variant="subtle" color="primary">Primary color</Code>
<Code variant="subtle" color="accent">Accent color</Code>
<Code variant="subtle" color="success">Success color</Code>
<Code variant="subtle" color="error">Error color</Code>
</VStack>

Sizes

<VStack gap={2} align="flex-start">
<Code variant="subtle" size="xs">Extra small</Code>
<Code variant="subtle" size="sm">Small</Code>
<Code variant="subtle" size="md">Medium</Code>
<Code variant="subtle" size="lg">Large</Code>
</VStack>

Common Patterns

Inline Code in Documentation

<Text>
Use the <Code variant="subtle">useState</Code> hook to manage component state.
Import it from <Code variant="subtle">react</Code>.
</Text>

Keyboard Shortcuts

<Text>
Press <Code variant="subtle">Ctrl</Code> + <Code variant="subtle">C</Code> to copy.
</Text>

File Paths

<Text>
Edit the configuration in <Code variant="subtle">src/config/settings.ts</Code>
</Text>

As Pre for Code Blocks

<Code as="pre" variant="subtle" p={4} display="block" overflow="auto">
{`function greet(name) {
return \`Hello, \${name}!\`;
}`}
</Code>

Props Reference

PropTypeDefaultDescription
variant"plain" | "subtle""plain"Visual variant
size"xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "5xl"-Font size
colorstring-Text color (semantic color token)
asElementType"code"HTML element to render

Code also accepts most Box props.

TypeScript

import { Code, type CodeVariant, type CodeProps, type CodeOwnProps } from "@tosui/react";