Skip to content

Commit 6006d60

Browse files
committed
feat: Add placeholder props. #185
1 parent c276a31 commit 6006d60

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ export default function App() {
135135
```ts
136136
import React from 'react';
137137
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
138-
import { EditorView } from '@codemirror/view';
139-
export * from './useCodeMirror';
138+
import { EditorView, ViewUpdate } from '@codemirror/view';
140139
export * from '@codemirror/view';
141140
export * from '@codemirror/basic-setup';
142141
export * from '@codemirror/state';
142+
export * from './useCodeMirror';
143143
export interface ReactCodeMirrorProps
144144
extends Omit<EditorStateConfig, 'doc' | 'extensions'>,
145-
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
145+
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {
146146
/** value of the auto created model in the editor. */
147147
value?: string;
148148
height?: string;
@@ -153,6 +153,12 @@ export interface ReactCodeMirrorProps
153153
maxWidth?: string;
154154
/** focus on the editor. */
155155
autoFocus?: boolean;
156+
/** Enables a placeholder—a piece of example content to show when the editor is empty. */
157+
placeholder?: string | HTMLElement;
158+
/**
159+
* `light` / `dark` Defaults to `light`.
160+
* @default light
161+
*/
156162
theme?: 'light' | 'dark';
157163
/**
158164
* Whether to optional basicSetup by default
@@ -186,6 +192,10 @@ export interface ReactCodeMirrorRef {
186192
state?: EditorState;
187193
view?: EditorView;
188194
}
195+
declare const ReactCodeMirror: React.ForwardRefExoticComponent<
196+
ReactCodeMirrorProps & React.RefAttributes<ReactCodeMirrorRef>
197+
>;
198+
export default ReactCodeMirror;
189199
```
190200

191201
### Related

src/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
33
import { EditorView, ViewUpdate } from '@codemirror/view';
44
import { useCodeMirror } from './useCodeMirror';
55

6-
export * from './useCodeMirror';
76
export * from '@codemirror/view';
87
export * from '@codemirror/basic-setup';
98
export * from '@codemirror/state';
9+
export * from './useCodeMirror';
1010

1111
export interface ReactCodeMirrorProps
1212
extends Omit<EditorStateConfig, 'doc' | 'extensions'>,
13-
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
13+
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {
1414
/** value of the auto created model in the editor. */
1515
value?: string;
1616
height?: string;
@@ -21,6 +21,12 @@ export interface ReactCodeMirrorProps
2121
maxWidth?: string;
2222
/** focus on the editor. */
2323
autoFocus?: boolean;
24+
/** Enables a placeholder—a piece of example content to show when the editor is empty. */
25+
placeholder?: string | HTMLElement;
26+
/**
27+
* `light` / `dark` Defaults to `light`.
28+
* @default light
29+
*/
2430
theme?: 'light' | 'dark';
2531
/**
2632
* Whether to optional basicSetup by default
@@ -73,6 +79,7 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
7379
minWidth,
7480
maxWidth,
7581
basicSetup,
82+
placeholder,
7683
indentWithTab,
7784
editable,
7885
...other
@@ -90,6 +97,7 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
9097
minWidth,
9198
maxWidth,
9299
basicSetup,
100+
placeholder,
93101
indentWithTab,
94102
editable,
95103
selection,

src/useCodeMirror.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
22
import { basicSetup as defaultBasicSetup } from '@codemirror/basic-setup';
33
import { EditorState, StateEffect } from '@codemirror/state';
44
import { indentWithTab as defaultIndentWithTab } from '@codemirror/commands';
5-
import { EditorView, keymap, ViewUpdate } from '@codemirror/view';
5+
import { EditorView, keymap, ViewUpdate, placeholder as extendPlaceholder } from '@codemirror/view';
66
import { oneDarkTheme } from '@codemirror/theme-one-dark';
77
import { ReactCodeMirrorProps } from './';
88
import { defaultLightThemeOption } from './theme/light';
@@ -23,6 +23,7 @@ export function useCodeMirror(props: UseCodeMirror) {
2323
height = '',
2424
minHeight = '',
2525
maxHeight = '',
26+
placeholder = '',
2627
width = '',
2728
minWidth = '',
2829
maxWidth = '',
@@ -58,6 +59,10 @@ export function useCodeMirror(props: UseCodeMirror) {
5859
getExtensions.unshift(defaultBasicSetup);
5960
}
6061

62+
if (placeholder) {
63+
getExtensions.unshift(extendPlaceholder(placeholder));
64+
}
65+
6166
theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme);
6267

6368
if (editable === false) {

website/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export default function App() {
191191
extensions={extensions}
192192
autoFocus={autofocus}
193193
className={styles.codemirror}
194+
placeholder="Please enter the code."
194195
onChange={(value) => {
195196
// console.log('value:', value);
196197
}}

0 commit comments

Comments
 (0)