Skip to content

Commit c1f128d

Browse files
committed
feat: optimize code.
1 parent 6d2ea04 commit c1f128d

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,30 @@ const Demo = () => {
137137
}
138138
```
139139

140+
Set (`data-color-mode="dark"`) dark theme.
141+
142+
```jsx
143+
import CodeEditor from '@uiw/react-textarea-code-editor';
144+
145+
function App() {
146+
return (
147+
<CodeEditor
148+
value="function add(a, b) {\n return a + b;\n}"
149+
data-color-mode="dark"
150+
/>
151+
);
152+
}
153+
```
154+
140155
## Props
141156

142157
```ts
143158
interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
144159
prefixCls?: string;
160+
/**
161+
* Support dark-mode/night-mode
162+
*/
163+
['data-color-mode']?: 'dark' | 'light';
145164
/**
146165
* Set what programming language the code belongs to.
147166
*/

src/index.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,39 +85,40 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
8585
[prefixCls, language, htmlStr],
8686
);
8787

88+
const textareaProps: React.TextareaHTMLAttributes<HTMLTextAreaElement> = {
89+
autoComplete: 'off',
90+
autoCorrect: 'off',
91+
spellCheck: 'false',
92+
autoCapitalize: 'off',
93+
...other,
94+
placeholder,
95+
onKeyDown: (evn) => {
96+
if (!other.onKeyDown || other.onKeyDown(evn) !== false) {
97+
shortcuts(evn);
98+
}
99+
},
100+
style: {
101+
...styles.editor,
102+
...styles.textarea,
103+
...contentStyle,
104+
minHeight,
105+
...(placeholder && !value ? { WebkitTextFillColor: 'inherit' } : {}),
106+
},
107+
onChange: (evn) => {
108+
setValue(evn.target.value);
109+
onChange && onChange(evn);
110+
},
111+
className: `${prefixCls}-text`,
112+
value: value,
113+
};
114+
88115
return (
89116
<div
90117
style={{ ...styles.container, ...style }}
91118
className={`${prefixCls} ${className || ''}`}
92119
data-color-mode={dataColorMode}
93120
>
94-
<textarea
95-
autoComplete="off"
96-
autoCorrect="off"
97-
spellCheck="false"
98-
autoCapitalize="off"
99-
{...other}
100-
placeholder={placeholder}
101-
onKeyDown={(evn) => {
102-
if (!other.onKeyDown || other.onKeyDown(evn) !== false) {
103-
shortcuts(evn);
104-
}
105-
}}
106-
style={{
107-
...styles.editor,
108-
...styles.textarea,
109-
...contentStyle,
110-
minHeight,
111-
...(placeholder && !value ? { WebkitTextFillColor: 'inherit' } : {}),
112-
}}
113-
ref={textRef}
114-
onChange={(evn) => {
115-
setValue(evn.target.value);
116-
onChange && onChange(evn);
117-
}}
118-
className={`${prefixCls}-text`}
119-
value={value}
120-
/>
121+
<textarea {...textareaProps} ref={textRef} />
121122
{preView}
122123
</div>
123124
);

0 commit comments

Comments
 (0)