Code
Code is an inline code styling component with monospace font and optional background.
Import
import { Code } from "@tosui/react";
Basic Usage
- Code
- Preview
<Text>
Run <Code>npm install</Code> to install dependencies.
</Text>
Run npm install to install dependencies.
Variants
- Code
- Preview
<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>
Plain variant:
code hereSubtle variant:
code hereColors
- Code
- Preview
<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>
Default colorPrimary colorAccent colorSuccess colorError colorSizes
- Code
- Preview
<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>
Extra smallSmallMediumLargeCommon Patterns
Inline Code in Documentation
- Code
- Preview
<Text>
Use the <Code variant="subtle">useState</Code> hook to manage component state.
Import it from <Code variant="subtle">react</Code>.
</Text>
Use the useState hook to manage component state.
Import it from react.
Keyboard Shortcuts
- Code
- Preview
<Text>
Press <Code variant="subtle">Ctrl</Code> + <Code variant="subtle">C</Code> to copy.
</Text>
Press Ctrl + C to copy.
File Paths
- Code
- Preview
<Text>
Edit the configuration in <Code variant="subtle">src/config/settings.ts</Code>
</Text>
Edit the configuration in src/config/settings.ts
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
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "plain" | "subtle" | "plain" | Visual variant |
| size | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "5xl" | - | Font size |
| color | string | - | Text color (semantic color token) |
| as | ElementType | "code" | HTML element to render |
Code also accepts most Box props.
TypeScript
import { Code, type CodeVariant, type CodeProps, type CodeOwnProps } from "@tosui/react";