Skip to content

Commit ae03f17

Browse files
authored
feat: Export keyboard handlers and add documentation for headless mode (#697)
1 parent 5604e13 commit ae03f17

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

core/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,49 @@ export default function App() {
120120
}
121121
```
122122

123+
### Headless Mode
124+
125+
The package exposes the necessary utilities to build a headless markdown editor with your own UI. This example creates a simple textarea that supports markdown keyboard shortcuts and correct handling of newlines.
126+
127+
```jsx mdx:preview
128+
import React from "react";
129+
import {
130+
handleKeyDown,
131+
shortcuts,
132+
TextAreaCommandOrchestrator,
133+
getCommands,
134+
} from '@uiw/react-md-editor';
135+
136+
export default function App() {
137+
const [value, setValue] = React.useState("**Hello world!!!**");
138+
const textareaRef = React.useRef(null);
139+
const orchestratorRef = React.useRef(null);
140+
141+
React.useEffect(() => {
142+
if (textareaRef.current) {
143+
orchestratorRef.current = new TextAreaCommandOrchestrator(textareaRef.current);
144+
}
145+
}, []);
146+
147+
const onKeyDown = (e) => {
148+
handleKeyDown(e, 2, false);
149+
if (orchestratorRef.current) {
150+
shortcuts(e, getCommands(), orchestratorRef.current);
151+
}
152+
};
153+
154+
return (
155+
<textarea
156+
ref={textareaRef}
157+
value={value}
158+
onChange={(e) => setValue(e.target.value)}
159+
onKeyDown={onKeyDown}
160+
style={{ width: '100%', height: 200, padding: 10 }}
161+
/>
162+
);
163+
}
164+
```
165+
123166
### Special Markdown syntax
124167

125168
**Supports for CSS Style**

core/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export * from './utils/InsertTextAtPosition';
1111
export * from './Editor';
1212
export * from './Context';
1313
export * from './Types';
14+
export { default as handleKeyDown } from './components/TextArea/handleKeyDown';
15+
export { default as shortcuts } from './components/TextArea/shortcuts';
1416

1517
export { MarkdownUtil, commands };
1618

0 commit comments

Comments
 (0)