Skip to content

Commit 14a4638

Browse files
committed
fix: Fix issue where autoFocusEnd did not scroll to the bottom. #688
1 parent 08e8b4d commit 14a4638

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

core/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,51 @@ export default function App() {
613613
}
614614
```
615615

616+
### Place the cursor at the end of the text
617+
618+
```jsx mdx:preview
619+
import React from "react";
620+
import MDEditor from '@uiw/react-md-editor';
621+
622+
const textSample = `# Welcome to the Markdown Editor!
623+
624+
This is a sample of the **React Markdown Editor**.
625+
626+
## ✨ Features
627+
- Real-time preview
628+
- Custom styling support
629+
- Code highlighting
630+
- Auto focus at the end of the text
631+
632+
## 📦 Sample Code
633+
634+
\`\`\`javascript
635+
function hello() {
636+
console.log("Hello, world!");
637+
}
638+
\`\`\`
639+
640+
## 🔗 Links
641+
642+
Visit [uiwjs/react-md-editor](https://github.com/uiwjs/react-md-editor) for more information.
643+
`;
644+
645+
export default function App() {
646+
const [value, setValue] = React.useState(textSample);
647+
return (
648+
<div className="container">
649+
<MDEditor
650+
autoFocus={true}
651+
value={value}
652+
autoFocusEnd={true}
653+
visibleDragbar={false}
654+
onChange={setValue}
655+
/>
656+
</div>
657+
);
658+
}
659+
```
660+
616661
### Preview Markdown
617662

618663
[![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/react-md-editor-preview-markdown-vrucl?fontsize=14&hidenavigation=1&theme=dark)
@@ -924,6 +969,7 @@ Inherit custom color variables by adding [`.wmde-markdown-var`](https://github.c
924969
- `commandsFilter?: (command: ICommand, isExtra: boolean) => false | ICommand`: Filter or modify your commands.
925970
- `extraCommands?: ICommand[]`: Displayed on the right side of the toolbar.
926971
- `autoFocus?: true`: Can be used to make `Markdown Editor` focus itself on initialization.
972+
- `autoFocusEnd?: false`: Can be used to make `Markdown Editor` focus on the end of text on initialization.
927973
- `previewOptions?: ReactMarkdown.ReactMarkdownProps`: This is reset [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview/tree/0036dd51a25c00d5be6dc83aa978905c64750038?tab=readme-ov-file#options-props) settings.
928974
- `textareaProps?: TextareaHTMLAttributes`: Set the `textarea` related props.
929975
- ~~`renderTextarea?: (props, opts) => JSX.Element;`~~: `@deprecated` Please use ~~`renderTextarea`~~ -> `components`. Use div to replace TextArea or re-render TextArea. [#193](https://github.com/uiwjs/react-md-editor/issues/193)

core/src/Types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
5959
*/
6060
visibleDragbar?: boolean;
6161
/**
62-
* @deprecated use `visibleDragbar`
62+
* @deprecated use {@link MDEditorProps.visibleDragbar}
6363
*/
6464
visiableDragbar?: boolean;
6565
/**
@@ -92,7 +92,7 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
9292
textareaProps?: ITextAreaProps;
9393
/**
9494
* Use div to replace TextArea or re-render TextArea
95-
* @deprecated Please use ~~`renderTextarea`~~ -> `components`
95+
* @deprecated Please use {@link components}
9696
*/
9797
renderTextarea?: ITextAreaProps['renderTextarea'];
9898
/**

core/src/components/TextArea/Textarea.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function Textarea(props: TextAreaProps) {
2020
tabSize,
2121
defaultTabEnable,
2222
autoFocusEnd,
23+
textareaWarp,
2324
dispatch,
2425
...otherStore
2526
} = useContext(EditorContext);
@@ -41,17 +42,20 @@ export default function Textarea(props: TextAreaProps) {
4142
}, []);
4243

4344
useEffect(() => {
44-
if (autoFocusEnd && textRef.current) {
45+
if (autoFocusEnd && textRef.current && textareaWarp) {
4546
textRef.current.focus();
4647
const length = textRef.current.value.length;
4748
textRef.current.setSelectionRange(length, length);
4849
setTimeout(() => {
50+
if (textareaWarp) {
51+
textareaWarp.scrollTop = textareaWarp.scrollHeight;
52+
}
4953
if (textRef.current) {
5054
textRef.current.scrollTop = textRef.current.scrollHeight;
5155
}
5256
}, 0);
5357
}
54-
}, []);
58+
}, [textareaWarp]);
5559

5660
const onKeyDown = (e: KeyboardEvent | React.KeyboardEvent<HTMLTextAreaElement>) => {
5761
handleKeyDown(e, tabSize, defaultTabEnable);

0 commit comments

Comments
 (0)