Skip to content

Commit

Permalink
feat: optimize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jan 3, 2023
1 parent 6d2ea04 commit c1f128d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,30 @@ const Demo = () => {
}
```

Set (`data-color-mode="dark"`) dark theme.

```jsx
import CodeEditor from '@uiw/react-textarea-code-editor';

function App() {
return (
<CodeEditor
value="function add(a, b) {\n return a + b;\n}"
data-color-mode="dark"
/>
);
}
```

## Props

```ts
interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
prefixCls?: string;
/**
* Support dark-mode/night-mode
*/
['data-color-mode']?: 'dark' | 'light';
/**
* Set what programming language the code belongs to.
*/
Expand Down
55 changes: 28 additions & 27 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,40 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
[prefixCls, language, htmlStr],
);

const textareaProps: React.TextareaHTMLAttributes<HTMLTextAreaElement> = {
autoComplete: 'off',
autoCorrect: 'off',
spellCheck: 'false',
autoCapitalize: 'off',
...other,
placeholder,
onKeyDown: (evn) => {
if (!other.onKeyDown || other.onKeyDown(evn) !== false) {
shortcuts(evn);
}
},
style: {
...styles.editor,
...styles.textarea,
...contentStyle,
minHeight,
...(placeholder && !value ? { WebkitTextFillColor: 'inherit' } : {}),
},
onChange: (evn) => {
setValue(evn.target.value);
onChange && onChange(evn);
},
className: `${prefixCls}-text`,
value: value,
};

return (
<div
style={{ ...styles.container, ...style }}
className={`${prefixCls} ${className || ''}`}
data-color-mode={dataColorMode}
>
<textarea
autoComplete="off"
autoCorrect="off"
spellCheck="false"
autoCapitalize="off"
{...other}
placeholder={placeholder}
onKeyDown={(evn) => {
if (!other.onKeyDown || other.onKeyDown(evn) !== false) {
shortcuts(evn);
}
}}
style={{
...styles.editor,
...styles.textarea,
...contentStyle,
minHeight,
...(placeholder && !value ? { WebkitTextFillColor: 'inherit' } : {}),
}}
ref={textRef}
onChange={(evn) => {
setValue(evn.target.value);
onChange && onChange(evn);
}}
className={`${prefixCls}-text`}
value={value}
/>
<textarea {...textareaProps} ref={textRef} />
{preView}
</div>
);
Expand Down

0 comments on commit c1f128d

Please sign in to comment.