|
| 1 | +# SideBySideCode Component |
| 2 | + |
| 3 | +A modern, accessible component for displaying code alongside explanatory content. Perfect for documentation, tutorials, and educational content. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🎯 Side-by-side display of code and explanations |
| 8 | +- 🔍 Syntax highlighting with Shiki |
| 9 | +- 📍 Interactive line highlighting |
| 10 | +- 📋 One-click code copying |
| 11 | +- 🌐 Load code from local files or URLs |
| 12 | +- 📱 Responsive design |
| 13 | +- ♿ WCAG AA compliant |
| 14 | + |
| 15 | +## Basic Usage |
| 16 | + |
| 17 | +```mdx |
| 18 | +import { SideBySideCode } from "@components" |
| 19 | + |
| 20 | +<SideBySideCode language="solidity" codeSrc="/samples/example.sol" title="Example.sol"> |
| 21 | + This is where you put your explanation text. It will appear alongside the code. |
| 22 | +</SideBySideCode> |
| 23 | +``` |
| 24 | + |
| 25 | +## Loading Code |
| 26 | + |
| 27 | +The component supports two methods for loading code: |
| 28 | + |
| 29 | +### 1. Local Files |
| 30 | + |
| 31 | +Place your code files in the `public/samples` directory and reference them with a relative path: |
| 32 | + |
| 33 | +```mdx |
| 34 | +<SideBySideCode language="javascript" codeSrc="/samples/example.js" title="Example.js"> |
| 35 | + Explanation goes here... |
| 36 | +</SideBySideCode> |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. Remote URLs |
| 40 | + |
| 41 | +Load code directly from any HTTP(S) URL: |
| 42 | + |
| 43 | +```mdx |
| 44 | +<SideBySideCode |
| 45 | + language="python" |
| 46 | + codeSrc="https://raw.githubusercontent.com/user/repo/main/example.py" |
| 47 | + title="Example.py" |
| 48 | +> |
| 49 | + Explanation goes here... |
| 50 | +</SideBySideCode> |
| 51 | +``` |
| 52 | + |
| 53 | +## Interactive Line Highlighting |
| 54 | + |
| 55 | +Highlight specific lines of code to draw attention to important sections: |
| 56 | + |
| 57 | +```mdx |
| 58 | +<SideBySideCode |
| 59 | + language="typescript" |
| 60 | + codeSrc="/samples/example.ts" |
| 61 | + title="Example.ts" |
| 62 | + highlights={[ |
| 63 | + { |
| 64 | + lines: [1, 2, 3], |
| 65 | + label: "Imports", |
| 66 | + description: "Import required dependencies", |
| 67 | + }, |
| 68 | + { |
| 69 | + lines: [5, 6, 7], |
| 70 | + label: "Configuration", |
| 71 | + description: "Set up the configuration object", |
| 72 | + }, |
| 73 | + ]} |
| 74 | +> |
| 75 | + The highlights will create interactive cards that, when clicked, will highlight the corresponding lines of code. |
| 76 | +</SideBySideCode> |
| 77 | +``` |
| 78 | + |
| 79 | +## Props Reference |
| 80 | + |
| 81 | +| Prop | Type | Required | Description | |
| 82 | +| ------------ | ----------- | -------- | ---------------------------------------------------------------------- | |
| 83 | +| `language` | `string` | No | Programming language for syntax highlighting. Defaults to "plaintext". | |
| 84 | +| `codeSrc` | `string` | Yes | Path to local file (in public/samples) or HTTP(S) URL. | |
| 85 | +| `title` | `string` | No | Title displayed in the code header. | |
| 86 | +| `highlights` | `Array` | No | Array of line highlights (see structure below). | |
| 87 | +| `children` | `ReactNode` | No | Explanatory content to display alongside the code. | |
| 88 | + |
| 89 | +### Highlight Structure |
| 90 | + |
| 91 | +```typescript |
| 92 | +interface LineHighlight { |
| 93 | + lines: number[] // Array of line numbers to highlight |
| 94 | + label: string // Short label for the highlight |
| 95 | + description: string // Detailed description |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Supported Languages |
| 100 | + |
| 101 | +The component supports syntax highlighting for: |
| 102 | + |
| 103 | +- JavaScript/TypeScript |
| 104 | +- Solidity |
| 105 | +- Python |
| 106 | +- And many more... |
| 107 | + |
| 108 | +For a complete list, refer to [Shiki's supported languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md). |
| 109 | + |
| 110 | +## Best Practices |
| 111 | + |
| 112 | +1. **Concise Explanations** |
| 113 | + |
| 114 | + - Keep explanations clear and focused |
| 115 | + - Use bullet points for better readability |
| 116 | + - Highlight only the most important code sections |
| 117 | + |
| 118 | +2. **Responsive Design** |
| 119 | + |
| 120 | + - Component automatically adjusts for different screen sizes |
| 121 | + - Code and explanation stack vertically on mobile |
| 122 | + - Maintains readability across devices |
| 123 | + |
| 124 | +3. **Accessibility** |
| 125 | + - Use descriptive labels for highlights |
| 126 | + - Ensure good color contrast in explanations |
| 127 | + - Provide meaningful titles for code sections |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +### Basic Example |
| 132 | + |
| 133 | +```mdx |
| 134 | +<SideBySideCode language="javascript" codeSrc="/samples/hello.js" title="Hello World"> |
| 135 | + A simple hello world example in JavaScript. |
| 136 | +</SideBySideCode> |
| 137 | +``` |
| 138 | + |
| 139 | +### With Highlights |
| 140 | + |
| 141 | +```mdx |
| 142 | +<SideBySideCode |
| 143 | + language="solidity" |
| 144 | + codeSrc="/samples/contract.sol" |
| 145 | + title="Smart Contract" |
| 146 | + highlights={[ |
| 147 | + { |
| 148 | + lines: [1, 2], |
| 149 | + label: "SPDX License", |
| 150 | + description: "Specify the license for the contract", |
| 151 | + }, |
| 152 | + ]} |
| 153 | +> |
| 154 | + This smart contract demonstrates basic functionality. Click on the highlight cards to focus on specific sections. |
| 155 | +</SideBySideCode> |
| 156 | +``` |
| 157 | + |
| 158 | +### Loading from URL |
| 159 | + |
| 160 | +```mdx |
| 161 | +<SideBySideCode language="python" codeSrc="https://api.example.com/code/script.py" title="Python Script"> |
| 162 | + This code is loaded directly from a URL. |
| 163 | +</SideBySideCode> |
| 164 | +``` |
| 165 | + |
| 166 | +## Troubleshooting |
| 167 | + |
| 168 | +1. **Code Not Loading** |
| 169 | + |
| 170 | + - For local files: Ensure the file exists in `public/samples` |
| 171 | + - For URLs: Verify the URL is accessible and returns raw code |
| 172 | + - Check file permissions and CORS settings |
| 173 | + |
| 174 | +2. **Syntax Highlighting Issues** |
| 175 | + |
| 176 | + - Verify the language is supported |
| 177 | + - Check the language identifier is correct |
| 178 | + - Ensure code is properly formatted |
| 179 | + |
| 180 | +3. **Layout Problems** |
| 181 | + - Clear any conflicting CSS |
| 182 | + - Ensure parent container allows for proper width |
| 183 | + - Check for proper MDX import syntax |
| 184 | + |
| 185 | +## Contributing |
| 186 | + |
| 187 | +We welcome contributions! Please see our contributing guidelines for more details. |
0 commit comments