|
| 1 | +<!--rehype:ignore:start--> |
| 2 | + |
| 3 | +# Add className Extensions |
| 4 | + |
| 5 | +<!--rehype:ignore:end--> |
| 6 | + |
| 7 | +[](https://www.npmjs.com/package/@uiw/codemirror-extensions-classname) |
| 8 | + |
| 9 | +Adding a class for a specific line for CodeMirror6. |
| 10 | + |
| 11 | +[](https://uiwjs.github.io/react-codemirror/#/extensions/classname) |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @uiw/codemirror-extensions-classname --save |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```jsx |
| 22 | +import CodeMirror from '@uiw/react-codemirror'; |
| 23 | +import { classname } from '@uiw/codemirror-extensions-classname'; |
| 24 | + |
| 25 | +const themeDemo = EditorView.baseTheme({ |
| 26 | + '&dark .first-line': { backgroundColor: 'red' }, |
| 27 | + '&light .first-line': { backgroundColor: 'red' }, |
| 28 | + '&dark .line-color': { backgroundColor: 'blue' }, |
| 29 | + '&light .line-color': { backgroundColor: 'blue' }, |
| 30 | +}); |
| 31 | + |
| 32 | +function App() { |
| 33 | + const classnameExt = classname({ |
| 34 | + add: (lineNumber) => { |
| 35 | + if (lineNumber === 1) { |
| 36 | + return 'first-line'; |
| 37 | + } |
| 38 | + if (lineNumber === 5) { |
| 39 | + return 'line-color'; |
| 40 | + } |
| 41 | + }, |
| 42 | + }); |
| 43 | + return <CodeMirror value="" height="200px" extensions={[theme, classnameExt]} />; |
| 44 | +} |
| 45 | + |
| 46 | +export default App; |
| 47 | +``` |
| 48 | + |
| 49 | +```jsx |
| 50 | +import CodeMirror from '@uiw/react-codemirror'; |
| 51 | +import { classname } from '@uiw/codemirror-extensions-classname'; |
| 52 | + |
| 53 | +function App() { |
| 54 | + const classnameExt = classname({ |
| 55 | + add: (lineNumber) => { |
| 56 | + if (lineNumber === 1) { |
| 57 | + return 'first-line'; |
| 58 | + } |
| 59 | + if (lineNumber === 5) { |
| 60 | + return 'line-color'; |
| 61 | + } |
| 62 | + }, |
| 63 | + }); |
| 64 | + return <CodeMirror value="" height="200px" extensions={[classnameExt]} />; |
| 65 | +} |
| 66 | +export default App; |
| 67 | +``` |
| 68 | + |
| 69 | +```js |
| 70 | +import { EditorView } from '@codemirror/view'; |
| 71 | +import { EditorState } from '@codemirror/state'; |
| 72 | +import { classname } from '@uiw/codemirror-extensions-classname'; |
| 73 | + |
| 74 | +const classnameExt = classname({ |
| 75 | + add: (lineNumber) => { |
| 76 | + if (lineNumber === 1) { |
| 77 | + return 'first-line'; |
| 78 | + } |
| 79 | + if (lineNumber === 5) { |
| 80 | + return 'line-color'; |
| 81 | + } |
| 82 | + }, |
| 83 | +}); |
| 84 | + |
| 85 | +const state = EditorState.create({ |
| 86 | + doc: 'my source code', |
| 87 | + extensions: [classnameExt], |
| 88 | +}); |
| 89 | + |
| 90 | +const view = new EditorView({ |
| 91 | + parent: document.querySelector('#editor'), |
| 92 | + state, |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +## API |
| 97 | + |
| 98 | +```ts |
| 99 | +import { Extension } from '@codemirror/state'; |
| 100 | +export declare type ClassnameOptions = { |
| 101 | + add?: (lineNumber: number) => string | undefined; |
| 102 | +}; |
| 103 | +export declare function classname(options?: ClassnameOptions): Extension; |
| 104 | +``` |
| 105 | + |
| 106 | +## Contributors |
| 107 | + |
| 108 | +As always, thanks to our amazing contributors! |
| 109 | + |
| 110 | +<a href="https://github.com/uiwjs/react-codemirror/graphs/contributors"> |
| 111 | + <img src="https://uiwjs.github.io/react-codemirror/CONTRIBUTORS.svg" /> |
| 112 | +</a> |
| 113 | + |
| 114 | +Made with [github-action-contributors](https://github.com/jaywcjlove/github-action-contributors). |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +Licensed under the MIT License. |
0 commit comments