|
| 1 | +# Next.js Example |
| 2 | + |
| 3 | +This folder documents how to use **react-code-view** in a Next.js (App Router) project. |
| 4 | + |
| 5 | +## 1) Create a Next.js app |
| 6 | + |
| 7 | +```bash |
| 8 | +pnpm create next-app my-rcv-app --ts --eslint |
| 9 | +cd my-rcv-app |
| 10 | +``` |
| 11 | + |
| 12 | +## 2) Install react-code-view |
| 13 | + |
| 14 | +```bash |
| 15 | +pnpm add @react-code-view/react @react-code-view/core |
| 16 | +``` |
| 17 | + |
| 18 | +## 3) Add styles (global) |
| 19 | + |
| 20 | +In `app/layout.tsx` (or `pages/_app.tsx` for Pages Router): |
| 21 | + |
| 22 | +```tsx |
| 23 | +import '@react-code-view/react/styles/index.css'; |
| 24 | +``` |
| 25 | + |
| 26 | +## 4) Add a demo page (App Router) |
| 27 | + |
| 28 | +Create `app/rcv-demo/page.tsx`: |
| 29 | + |
| 30 | +```tsx |
| 31 | +'use client'; |
| 32 | + |
| 33 | +import React from 'react'; |
| 34 | +import { CodeView } from '@react-code-view/react'; |
| 35 | + |
| 36 | +const code = `const Hello = () => <button onClick={() => alert('hi')}>Click</button>; |
| 37 | +render(<Hello />);`; |
| 38 | + |
| 39 | +export default function RcvDemoPage() { |
| 40 | + return ( |
| 41 | + <main style={{ padding: 24 }}> |
| 42 | + <h1>React Code View in Next.js</h1> |
| 43 | + <CodeView |
| 44 | + language="tsx" |
| 45 | + theme="rcv-theme-default" |
| 46 | + renderPreview |
| 47 | + showCopyButton |
| 48 | + dependencies={{ React }} |
| 49 | + > |
| 50 | + {code} |
| 51 | + </CodeView> |
| 52 | + </main> |
| 53 | + ); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +> If you prefer Pages Router, create `pages/rcv-demo.tsx` with the same content (remove `'use client';` if your project defaults to client components). |
| 58 | +
|
| 59 | +## 5) Run |
| 60 | + |
| 61 | +```bash |
| 62 | +pnpm dev |
| 63 | +# visit http://localhost:3000/rcv-demo |
| 64 | +``` |
| 65 | + |
| 66 | +## Notes |
| 67 | +- No extra Next.js config is required; the package ships ESM/CJS builds consumable by Next. |
| 68 | +- Ensure the page/component using `CodeView` is a **client component** (`'use client'`) so hooks can run. |
| 69 | +- Import the CSS once (in layout or custom App). You can override CSS variables if needed. |
| 70 | +- To customize themes, set the `theme` prop (e.g., `rcv-theme-dark`) and override CSS variables in your global stylesheet. |
0 commit comments