Skip to Content
Dashflow Logo

Code Component

A comprehensive guide to using the Code component with examples, props, and best practices.


Overview

The Code component is used to display inline code snippets with proper styling and formatting. It's commonly used in documentation, tutorials, and technical content to highlight code elements within text.

Basic Usage

import { Code } from '@dashflowx/core'
export default function CodeExample() {
return (
<Code>console.log('Hello, World!')</Code>
)
}

Preview: console.log('Hello, World!')

Props

PropTypeDefaultDescription
classNamestring''Additional CSS classes for styling
childrenReact.ReactNode-The code content to display

Examples

Basic Inline Code

<p>
Use the <Code>useState</Code> hook to manage component state.
</p>

Preview: Use the useState hook to manage component state.

Code with Custom Styling

<Code className="bg-blue-100 text-blue-800 px-2 py-1 rounded">
npm install package-name
</Code>

Preview: npm install package-name

Different Programming Languages

<div className="space-y-2">
<p>JavaScript: <Code>const name = 'John'</Code></p>
<p>Python: <Code>name = 'John'</Code></p>
<p>CSS: <Code>color: #ff0000</Code></p>
<p>HTML: <Code>&lt;div&gt;Content&lt;/div&gt;</Code></p>
</div>

Preview: JavaScript: const name = 'John' Python: name = 'John' CSS: color: #ff0000 HTML: <div>Content</div>

Code in Documentation

<div className="prose">
<p>
To create a new React component, use the following command:
</p>
<Code>npx create-react-app my-app</Code>
<p>
This will create a new React application in the <Code>my-app</Code> directory.
</p>
</div>

Preview: To create a new React component, use the following command: npx create-react-app my-app This will create a new React application in the my-app directory.

Common Use Cases

API Documentation

<div className="space-y-4">
<h3>API Endpoint</h3>
<p>
Make a GET request to: <Code>https://api.example.com/users</Code>
</p>
<h3>Response Format</h3>
<p>
The response will be in JSON format with a <Code>data</Code> property.
</p>
<h3>Error Codes</h3>
<ul>
<li><Code>400</Code> - Bad Request</li>
<li><Code>401</Code> - Unauthorized</li>
<li><Code>404</Code> - Not Found</li>
</ul>
</div>

Technical Tutorials

<div className="space-y-4">
<h3>Step 1: Install Dependencies</h3>
<p>
First, install the required package:
</p>
<Code>npm install @dashflowx/core</Code>
<h3>Step 2: Import Component</h3>
<p>
Import the component in your file:
</p>
<Code>import { Button } from '@dashflowx/core'</Code>
<h3>Step 3: Use Component</h3>
<p>
Use the component in your JSX:
</p>
<Code>&lt;Button&gt;Click me&lt;/Button&gt;</Code>
</div>

Configuration Examples

<div className="space-y-4">
<h3>Environment Variables</h3>
<p>
Set the following environment variables:
</p>
<ul className="space-y-2">
<li><Code>NODE_ENV=production</Code></li>
<li><Code>DATABASE_URL=postgresql://...</Code></li>
<li><Code>API_KEY=your-api-key</Code></li>
</ul>
<h3>Configuration File</h3>
<p>
Update your <Code>config.json</Code> file:
</p>
<Code>{"{ \"port\": 3000, \"debug\": false }"}</Code>
</div>

Command Line Examples

<div className="space-y-4">
<h3>Git Commands</h3>
<div className="space-y-2">
<p>Clone repository: <Code>git clone https://github.com/user/repo.git</Code></p>
<p>Check status: <Code>git status</Code></p>
<p>Add files: <Code>git add .</Code></p>
<p>Commit changes: <Code>git commit -m "Initial commit"</Code></p>
<p>Push to remote: <Code>git push origin main</Code></p>
</div>
</div>

Best Practices

1. Consistent Styling

Use consistent styling for code elements throughout your application.

// ✅ Good - Consistent styling
<Code className="bg-gray-100 text-gray-800 px-2 py-1 rounded">
function example() { return 'Hello'; }
</Code>

2. Appropriate Context

Use code elements only for actual code, not for emphasis.

// ✅ Good - Actual code
<p>Use <Code>useState</Code> for state management.</p>
// ❌ Avoid - Not actual code
<p>This is <Code>important</Code> information.</p>

3. Readable Content

Keep code snippets short and readable.

// ✅ Good - Short and readable
<Code>npm install</Code>
// ❌ Avoid - Too long for inline code
<Code>npm install react react-dom @types/react @types/react-dom typescript webpack webpack-cli webpack-dev-server html-webpack-plugin</Code>

4. Proper Escaping

Escape special characters in HTML content.

// ✅ Good - Properly escaped
<Code>&lt;div&gt;Content&lt;/div&gt;</Code>
// ❌ Avoid - Unescaped HTML
<Code><div>Content</div></Code>

5. Accessibility

Ensure code elements are accessible to screen readers.

// ✅ Good - Accessible
<Code role="code" aria-label="JavaScript code snippet">
const name = 'John';
</Code>

Customization

Custom Styling

You can customize the code appearance using CSS classes:

<Code className="bg-blue-100 text-blue-800 border border-blue-200 px-3 py-1 rounded-lg font-semibold">
Custom styled code
</Code>

Different Themes

Use different themes for different contexts:

// Light theme
<Code className="bg-gray-100 text-gray-800">Light code</Code>
// Dark theme
<Code className="bg-gray-800 text-gray-100">Dark code</Code>
// Colored theme
<Code className="bg-green-100 text-green-800">Success code</Code>

Size Variations

Use different sizes for different emphasis levels:

// Small code
<Code className="text-xs">Small code</Code>
// Large code
<Code className="text-lg">Large code</Code>
// Default code
<Code>Default code</Code>

Advanced Usage

Dynamic Code Content

Use dynamic content in code elements:

const [codeSnippet, setCodeSnippet] = useState('console.log("Hello")');
<Code>{codeSnippet}</Code>

Code with Copy Functionality

Add copy functionality to code elements:

const [copied, setCopied] = useState(false);
const copyToClipboard = (text) => {
navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
<div className="relative inline-block">
<Code>npm install package</Code>
<button
onClick={() => copyToClipboard('npm install package')}
className="ml-2 text-xs text-gray-500 hover:text-gray-700"
>
{copied ? 'Copied!' : 'Copy'}
</button>
</div>

Code with Syntax Highlighting

Use code elements with syntax highlighting:

<Code className="language-javascript bg-gray-900 text-gray-100 p-4 rounded-lg block">
{`function greet(name) {
return \`Hello, \${name}!\`;
}`}
</Code>
  • Pre: For multi-line code blocks
  • Fence: For code blocks with syntax highlighting
  • Blockquote: For quoted content
  • Typography: For text formatting
  • Button: For interactive elements

API Reference

For the complete API reference and advanced usage patterns, see the Code API documentation.

Edit this page on GitHub