|
| 1 | +# @codemonster-ru/vue-codeblock |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@codemonster-ru/vue-codeblock) |
| 4 | +[](https://www.npmjs.com/package/@codemonster-ru/vue-codeblock) |
| 5 | +[](https://github.com/codemonster-ru/vue-codeblock/blob/main/LICENSE) |
| 6 | + |
| 7 | +Standalone Vue 3 code block component with built-in syntax highlighting, light/dark theme support, copy actions, line numbers, and a small shared highlighting runtime you can also use outside the component. |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +```bash |
| 12 | +npm i @codemonster-ru/vue-codeblock |
| 13 | +``` |
| 14 | + |
| 15 | +## Use Cases |
| 16 | + |
| 17 | +- package documentation |
| 18 | +- design system docs |
| 19 | +- internal developer portals |
| 20 | +- settings/admin panels that need to render code examples |
| 21 | + |
| 22 | +## Component Usage |
| 23 | + |
| 24 | +Register the plugin: |
| 25 | + |
| 26 | +```ts |
| 27 | +import { createApp } from "vue"; |
| 28 | +import App from "./App.vue"; |
| 29 | +import VueCodeBlock from "@codemonster-ru/vue-codeblock"; |
| 30 | + |
| 31 | +createApp(App).use(VueCodeBlock).mount("#app"); |
| 32 | +``` |
| 33 | + |
| 34 | +Or import the component directly: |
| 35 | + |
| 36 | +```vue |
| 37 | +<script setup lang="ts"> |
| 38 | +import { CodeBlock } from "@codemonster-ru/vue-codeblock"; |
| 39 | +</script> |
| 40 | +
|
| 41 | +<template> |
| 42 | + <CodeBlock |
| 43 | + language="vue" |
| 44 | + filename="ButtonExample.vue" |
| 45 | + :show-line-numbers="true" |
| 46 | + :code="`<Button label="Save" />`" |
| 47 | + /> |
| 48 | +</template> |
| 49 | +``` |
| 50 | + |
| 51 | +If you want the package CSS explicitly: |
| 52 | + |
| 53 | +```ts |
| 54 | +import "@codemonster-ru/vue-codeblock/style.css"; |
| 55 | +``` |
| 56 | + |
| 57 | +## Runtime Usage |
| 58 | + |
| 59 | +The package also exports the shared highlighter: |
| 60 | + |
| 61 | +```ts |
| 62 | +import { |
| 63 | + highlightCodeBlock, |
| 64 | + escapeCodeHtml, |
| 65 | +} from "@codemonster-ru/vue-codeblock"; |
| 66 | + |
| 67 | +const html = highlightCodeBlock("ts", "const answer = 42;"); |
| 68 | +const escaped = escapeCodeHtml("<Button />"); |
| 69 | +``` |
| 70 | + |
| 71 | +## Props |
| 72 | + |
| 73 | +| Prop | Type | Default | Purpose | |
| 74 | +| ----------------- | -------------------------------- | -------------- | ------------------------------------ | |
| 75 | +| `code` | `string` | `''` | Raw source code | |
| 76 | +| `language` | `CodeBlockLanguage` | `'plaintext'` | Language hint for highlighting | |
| 77 | +| `filename` | `string` | `''` | Optional filename in header | |
| 78 | +| `showHeader` | `boolean` | `true` | Shows the top meta/action bar | |
| 79 | +| `showLineNumbers` | `boolean` | `false` | Renders line numbers | |
| 80 | +| `copyable` | `boolean` | `true` | Shows copy action | |
| 81 | +| `copyLabel` | `string` | `'Copy'` | Copy button text | |
| 82 | +| `copiedLabel` | `string` | `'Copied'` | Temporary copied state label | |
| 83 | +| `copiedDuration` | `number` | `1200` | Copied state timeout in ms | |
| 84 | +| `languageLabel` | `string` | `'Language'` | Header label before language | |
| 85 | +| `disabled` | `boolean` | `false` | Disables actions | |
| 86 | +| `wrap` | `boolean` | `false` | Enables wrapped lines | |
| 87 | +| `highlight` | `boolean` | `true` | Turns highlighting on/off | |
| 88 | +| `maxHeight` | `string` | `''` | Optional scroll container max height | |
| 89 | +| `ariaLabel` | `string` | `'Code block'` | Accessibility label | |
| 90 | +| `theme` | `'inherit' \| 'light' \| 'dark'` | `'inherit'` | Theme mode override | |
| 91 | + |
| 92 | +## Events |
| 93 | + |
| 94 | +| Event | Payload | |
| 95 | +| ------ | ------------------ | |
| 96 | +| `copy` | `{ text: string }` | |
| 97 | + |
| 98 | +## Slots |
| 99 | + |
| 100 | +| Slot | Purpose | |
| 101 | +| --------- | ------------------------------------------ | |
| 102 | +| `actions` | Add custom actions next to the copy button | |
| 103 | + |
| 104 | +## Supported Languages |
| 105 | + |
| 106 | +Canonical built-in values: |
| 107 | + |
| 108 | +`plaintext`, `text`, `txt`, `js`, `javascript`, `ts`, `typescript`, `vue`, `html`, `json`, `bash`, `shell`, `sh`, `css`, `scss`, `sass` |
| 109 | + |
| 110 | +You can import them as `SUPPORTED_CODE_BLOCK_LANGUAGES`. |
| 111 | + |
| 112 | +## Theming |
| 113 | + |
| 114 | +The component ships with light and dark defaults. You can override it with: |
| 115 | + |
| 116 | +- `theme="light"` |
| 117 | +- `theme="dark"` |
| 118 | +- `theme="inherit"` and an ancestor `data-theme="dark"` |
| 119 | + |
| 120 | +Main CSS custom properties: |
| 121 | + |
| 122 | +- `--vcb-background-color` |
| 123 | +- `--vcb-text-color` |
| 124 | +- `--vcb-border-color` |
| 125 | +- `--vcb-padding` |
| 126 | +- `--vcb-font-size` |
| 127 | +- `--vcb-line-height` |
| 128 | +- `--vcb-token-keyword-color` |
| 129 | +- `--vcb-token-string-color` |
| 130 | +- `--vcb-token-number-color` |
| 131 | +- `--vcb-token-variable-color` |
| 132 | +- `--vcb-token-function-color` |
| 133 | +- `--vcb-token-property-color` |
| 134 | +- `--vcb-token-directive-color` |
| 135 | + |
| 136 | +Example: |
| 137 | + |
| 138 | +```css |
| 139 | +.docs-surface { |
| 140 | + --vcb-background-color: #081224; |
| 141 | + --vcb-border-color: rgba(96, 165, 250, 0.28); |
| 142 | + --vcb-token-keyword-color: #d8b4fe; |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Notes |
| 147 | + |
| 148 | +- The built-in highlighter is lightweight and regex-based by design. |
| 149 | +- It is tuned for documentation and UI examples, not for full IDE-grade parsing. |
| 150 | +- If you need more languages later, extend the highlighter runtime rather than patching rendered HTML. |
0 commit comments