--- describes: CodeEditor --- A wrapper around the popular [CodeMirror](https://codemirror.net/) code editor component. CodeMirror provides a text input field with features like line gutters, syntax highlighting, and autocompletion. ### Built-in features SourceCodeEditor has a lot of built-in features that makes editing code easier. ##### Command keybinding The editor has a lot of handy key bindings for commands like copying and deleting lines, moving lines up and down, selection and indentation, etc. See the keymaps here: [defaultKeymap](https://codemirror.net/docs/ref/#commands.defaultKeymap), [closeBracketsKeymap](https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap), [historyKeymap](https://codemirror.net/docs/ref/#commands.historyKeymap), [foldKeymap](https://codemirror.net/docs/ref/#language.foldKeymap), [completionKeymap](https://codemirror.net/docs/ref/#autocomplete.completionKeymap), [lintKeymap](https://codemirror.net/docs/ref/#lint.lintKeymap). ##### History The history feature remembers the steps of the code editing and selections, and lets you undo and redo them. ##### Cursor and selection Instead of using the browser's native selection and cursor, SourceCodeEditor uses its own system. This allows the editor to display secondary selection ranges, and tends to produce a type of selection more in line with that users expect in a text editor. It also allows **multiple** cursors to be placed (`Cmd/Ctrl` + click), multiple ranges to be selected and edited at the same time. **Rectangular selections:** by default, it will react to left mouse drag with the `Option/Alt` key held down. When such a selection occurs, the text within the rectangle that was dragged over will be selected, as one selection range per line. The editor highlights text that matches the current selection. ##### Bracket matching and closing Whenever the cursor is next to a bracket, that bracket and the one it matches are highlighted. Or, when no matching bracket is found, another highlighting style is used to indicate this. When a closeable bracket is typed, its closing bracket is immediately inserted after the cursor. ### Language support Setting the correct language adds **syntax highlighting** and other helpful features to the editor, like **code folding**, **auto-indentation**, **syntax-aware selection** and **autocompletion** features. **Note:** In case you need support for additional languages, please contact us on [GitHub](https://github.com/instructure/instructure-ui)! ```js --- type: example --- const languages = { json: `{ "name": "@instructure/ui-source-code-editor", "version": "8.24.2", "description": "A UI component library made by Instructure Inc.", "author": "Instructure, Inc. Engineering and Product Design", "module": "./es/index.js", "main": "./lib/index.js", "types": "./types/index.d.ts", "repository": { "type": "git", "url": "https://github.com/instructure/instructure-ui.git" }, }`, javascript: `const fruit: string = "apple" const re = new RegExp('ab+c') function exampleMethod(props: Props) { return props ? props.value : null } /** * This is an example * @param {Object} props */ const Example = () => { return ( } placement='end center' offsetX='20px' > Positioned content ) } render()`, html: ` Example app
`, css: `a { text-decoration: none; &:hover { text-decoration: underline; } } a:link, a:visited, a:hover, a:active { background-color: green; color: white; padding: 10px 25px; text-align: center; text-decoration: none; display: inline-block; } .centertext { text-align: center; } img { opacity: 0.5; filter: alpha(opacity=50); }`, markdown: `#### The quarterly results look great! > - Revenue was off the chart. > - Profits were higher than ever. *Everything* is going according to **plan**. --- type: example ---`, shell: `#!/bin/bash # example of using arguments to a script echo "My first name is $1" echo "My surname is $2" echo "Total number of arguments is $#" ________________________________________ $ chmod a+x name.sh $ ./name.sh Hans-Wolfgang Loidl My first name is Hans-Wolfgang My surname is Loidl Total number of arguments is 2`, yml: `--- doe: "a deer, a female deer" ray: "a drop of golden sun" pi: 3.14159 xmas: true french-hens: 3 calling-birds: - huey - dewey - louie - fred xmas-fifth-day: calling-birds: four french-hens: 3 golden-rings: 5 partridges: count: 1 location: "a pear tree" turtle-doves: two` } const languageMap = { json: languages.json, js: languages.javascript, jsx: languages.javascript, javascript: languages.javascript, html: languages.html, css: languages.css, markdown: languages.markdown, sh: languages.shell, shell: languages.shell, bash: languages.shell, yml: languages.yml, yaml: languages.yml } const LanguageExamples = () => { const [currentLanguage, setCurrentLanguage] = useState('javascript') const [currentValue, setCurrentValue] = useState(languageMap.javascript) const languageKeys = Object.keys(languageMap) return ( { setCurrentLanguage(newLanguage) setCurrentValue(languageMap[newLanguage]) }} > {languageKeys.map((language) => ( ))} { setCurrentValue(value) }} lineNumbers lineWrapping highlightActiveLine highlightActiveLineGutter /> ) } render() ``` ### Controlled mode SourceCodeEditor works best as an uncontrolled component (with the `defaultValue` prop), and that is how we recommend it to be used. As an uncontrolled component, the underlying CodeMirror component can take care of all interactions. We've implemented the "controlled" usage, but please let us know if you run into any performance issues or bugs. ```js --- type: example --- const ControlledExample = () => { const [value, setValue] = useState(`const fruit: string = "apple" function exampleMethod(props: Props) { return props ? props.value : null }`) const textAreaRef = useRef(null) return ( setValue(newValue)} highlightActiveLine highlightActiveLineGutter lineWrapping lineNumbers foldGutter spellcheck />