diff --git a/README.md b/README.md
index 5adf721a..e341c487 100644
--- a/README.md
+++ b/README.md
@@ -91,8 +91,8 @@ export function Screen() {
| `scrollEnabled` | `boolean` | `true` | Off sends vertical drags to a parent `ScrollView`. Lines wider than the view still scroll sideways. |
| `contentInsets` | `ContentInsets` | all 0 | The padding inside the scrollable area. Style padding on the view is ignored. |
| `textPadding` | `number` | `8` | The space between the text and the editor's left and right edges, inside `contentInsets`. |
-| `avoidKeyboard` | `boolean` | `false` | Adds the keyboard overlap to the bottom inset on every frame of the keyboard animation. |
-| `keyboardOffset` | `number` | `0` | While the keyboard is up, the editor treats its top as this much higher, to keep a toolbar that rides the keyboard clear. It needs `avoidKeyboard`. |
+| `avoidKeyboard` | `boolean` | `false` | Keeps the caret above the keyboard. A scrolling editor adds the keyboard overlap to its bottom inset, and an editor with `scrollEnabled` off scrolls the page around it instead. |
+| `keyboardOffset` | `number` | `0` | Raises the keyboard's top by this much while the keyboard is up, to keep the caret above a toolbar that rides on the keyboard. It needs `avoidKeyboard`. |
| `decorations` | `Decoration[]` | undefined | Underlines, gaps under lines and line backgrounds. They follow every edit until you pass a new array. |
| `decorationsRevision` | `number` | undefined | The revision the decorations were computed for, from `getRevision()`. The editor moves them through the edits made since then. |
| `onReady` | `() => void` | | Called once after the first draw. Ref methods are valid from here. |
diff --git a/example/src/EditorApp.web.tsx b/example/src/EditorApp.web.tsx
index 7e1ed952..c991a744 100644
--- a/example/src/EditorApp.web.tsx
+++ b/example/src/EditorApp.web.tsx
@@ -191,6 +191,7 @@ export function EditorApp() {
theme={theme}
fontFamily={FONT}
scrollEnabled={false}
+ avoidKeyboard
onReady={ready('nested')}
/>
End of the page.
diff --git a/example/src/screens/NestedScreen.android.tsx b/example/src/screens/NestedScreen.android.tsx
index 7271af7a..685e919c 100644
--- a/example/src/screens/NestedScreen.android.tsx
+++ b/example/src/screens/NestedScreen.android.tsx
@@ -8,6 +8,7 @@ import { fillMaxWidth } from '@expo/ui/jetpack-compose/modifiers';
import { useState } from 'react';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import { CodeEditor, type Theme } from 'react-native-codepad';
+import { KeyboardAvoidingView } from 'react-native-keyboard-controller';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useAndroidColors } from '../chrome/material';
import { NestedBar } from '../chrome/NestedBar';
@@ -16,8 +17,8 @@ import { NESTED_SNIPPET } from './nestedSnippet';
type Props = { theme: Theme; onBack: () => void };
-// Caret reveal inside a parent scroll view is the consumer's job: read getCursorRect and scroll the
-// parent to it. This screen shows the auto height loop and the sideways scroll.
+// The editor sizes to its text and avoidKeyboard scrolls this page to keep the caret above the keyboard.
+// The avoiding view shortens the page as the keyboard opens, so even the editor's last lines can rise.
export function NestedScreen({ theme, onBack }: Props) {
const scheme = schemeFor(theme);
const colors = useAndroidColors(scheme);
@@ -33,50 +34,53 @@ export function NestedScreen({ theme, onBack }: Props) {
topInset={topInsetFor(safe.top)}
onBack={onBack}
/>
-
-
- The editor below takes its height from onContentSizeChange and leaves vertical scrolling
- to this page. Long lines scroll sideways.
-
-
-
-
- A field above
-
-
-
- setHeight(Math.ceil(e.height))}
- />
-
-
-
- A field below
-
-
-
-
- A tall footer, so the page itself has something to scroll.
-
-
-
+
+
+
+ The editor below takes its height from onContentSizeChange and leaves vertical scrolling
+ to this page. Long lines scroll sideways.
+
+
+
+
+ A field above
+
+
+
+ setHeight(Math.ceil(e.height))}
+ />
+
+
+
+ A field below
+
+
+
+
+ A tall footer, so the page itself has something to scroll.
+
+
+
+
);
}
diff --git a/example/src/screens/NestedScreen.ios.tsx b/example/src/screens/NestedScreen.ios.tsx
index 03c36056..ba5622be 100644
--- a/example/src/screens/NestedScreen.ios.tsx
+++ b/example/src/screens/NestedScreen.ios.tsx
@@ -3,6 +3,7 @@ import { background, cornerRadius, padding } from '@expo/ui/swift-ui/modifiers';
import { useState } from 'react';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import { CodeEditor, type Theme } from 'react-native-codepad';
+import { KeyboardAvoidingView } from 'react-native-keyboard-controller';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { NestedBar } from '../chrome/NestedBar';
import { IOS, schemeFor, topInsetFor } from '../chrome/theme';
@@ -10,8 +11,8 @@ import { NESTED_SNIPPET } from './nestedSnippet';
type Props = { theme: Theme; onBack: () => void };
-// Caret reveal inside a parent scroll view is the consumer's job: read getCursorRect and scroll the
-// parent to it. This screen shows the auto height loop and the sideways scroll.
+// The editor sizes to its text and avoidKeyboard scrolls this page to keep the caret above the keyboard.
+// The avoiding view shortens the page as the keyboard opens, so even the editor's last lines can rise.
export function NestedScreen({ theme, onBack }: Props) {
const scheme = schemeFor(theme);
const palette = IOS[scheme];
@@ -26,44 +27,47 @@ export function NestedScreen({ theme, onBack }: Props) {
];
return (
-
-
- The editor below takes its height from onContentSizeChange and leaves vertical scrolling
- to this page. Long lines scroll sideways.
-
-
-
-
- setHeight(Math.ceil(e.height))}
- />
-
-
-
-
- A tall footer, so the page itself has something to scroll.
-
-
-
+
+
+
+ The editor below takes its height from onContentSizeChange and leaves vertical scrolling
+ to this page. Long lines scroll sideways.
+
+
+
+
+ setHeight(Math.ceil(e.height))}
+ />
+
+
+
+
+ A tall footer, so the page itself has something to scroll.
+
+
+
+
setHeight(Math.ceil(e.height))}
/>
@@ -151,4 +152,4 @@ export function SnippetForm() {
}
```
-Lines wider than the editor scroll sideways with a mostly horizontal drag, and every other drag scrolls the page. Add `softWrap` to wrap long lines instead. The editor cannot scroll the page. To keep the caret visible, read `getCursorRect()` in `onSelectionChange`, add the editor's `onLayout` offset, and call `scrollTo` on the page when the caret leaves the visible band. Handle the keyboard at the page level (`KeyboardAvoidingView` or keyboard-controller's `KeyboardAwareScrollView`), not with `avoidKeyboard`.
+Lines wider than the editor scroll sideways with a mostly horizontal drag, and every other drag scrolls the page. Add `softWrap` to wrap long lines instead. With `avoidKeyboard`, the editor scrolls the page itself to keep the caret's row visible and above the keyboard, on every caret move and keyboard change. When the editor ends the page, wrap the `ScrollView` in a `KeyboardAvoidingView` with `behavior="padding"` (keyboard-controller's or React Native's) so the page has room to lift the last lines. On the web, set `interactive-widget=resizes-content` in the viewport meta tag, and leave space under the editor for iOS Safari. For a toolbar in a `KeyboardStickyView` floating over the page, pass its height as `keyboardOffset`. Don't combine `avoidKeyboard` with keyboard-controller's `KeyboardAwareScrollView`.
diff --git a/src/types.ts b/src/types.ts
index 389269f2..2125c1dd 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -156,9 +156,9 @@ export interface CodeEditorProps extends ViewProps {
contentInsets?: ContentInsets;
/** Space in dp between the text and the left and right edges of the text area, 8 by default. */
textPadding?: number;
- /** Inset the content by the keyboard's overlap with the view, following the keyboard animation. */
+ /** Keeps the caret above the keyboard: a scrolling editor insets its content by the overlap, one with scrollEnabled off scrolls its page. */
avoidKeyboard?: boolean;
- /** While the keyboard is up, the editor treats its top as this much higher, to keep a toolbar riding the keyboard clear. Needs avoidKeyboard. */
+ /** Raises the keyboard's top by this much while it is up, to keep the caret above a toolbar riding the keyboard. Needs avoidKeyboard. */
keyboardOffset?: number;
onReady?: () => void;
onScroll?: (e: ScrollEvent) => void;
diff --git a/website/docs/guides/keyboard.md b/website/docs/guides/keyboard.md
index 5c611756..bf596ea2 100644
--- a/website/docs/guides/keyboard.md
+++ b/website/docs/guides/keyboard.md
@@ -12,7 +12,7 @@ To keep the caret visible while the soft keyboard is up, turn on `avoidKeyboard`
```
-The editor measures how much the keyboard covers it on every frame of the keyboard animation and adds that to its bottom inset. The text slides up with the keyboard and back down when it closes. A keyboard opened for another field on the screen moves the text too.
+The editor measures how much the keyboard covers it on every frame of the keyboard animation and adds that to its bottom inset. The text slides up with the keyboard and back down when it closes. A keyboard opened for another field on the screen moves the text too. An editor that grows with its text inside a page scrolls the page instead, as described in [Inside a page that scrolls](#inside-a-page-that-scrolls).
On Android the prop does nothing in two window modes, which you set with `android:windowSoftInputMode`:
@@ -21,6 +21,56 @@ On Android the prop does nothing in two window modes, which you set with `androi
`onContentSizeChange` never includes the keyboard. The prop is off by default on both platforms.
+## Inside a page that scrolls
+
+With `scrollEnabled={false}`, the editor grows with its text and the page around it does the scrolling. `avoidKeyboard` then scrolls the nearest parent that scrolls vertically, such as a `ScrollView`, by the least amount that keeps the caret's row above the keyboard. On the web, that parent is the nearest scrolling element, or the window.
+
+The editor checks the caret when the keyboard opens or changes height, when the caret moves, and when the parent changes size. A scroll you make by hand holds until one of those happens. Closing the keyboard leaves the page where it is. With no keyboard on screen, such as with a hardware keyboard or in a desktop browser, the editor keeps the caret's row inside the visible part of the parent.
+
+When the editor is the last thing on the page and the page is at its end, the page can't scroll any further. The last lines then stay under the keyboard. Give the page room as the keyboard opens:
+
+- On Android and iOS, wrap the `ScrollView` in a `KeyboardAvoidingView` with `behavior="padding"`. The `ScrollView` gets shorter as the keyboard rises, and the caret rides up with it.
+- On the web in Chrome, add `interactive-widget=resizes-content` to the page's viewport meta tag. The page then gets shorter as the keyboard rises.
+- iOS Safari has no such setting. Leave space under the editor, for example with a `paddingBottom` on the `ScrollView` content, so the page can still scroll its last lines up.
+
+This page uses the `KeyboardAvoidingView` from react-native-keyboard-controller, which needs its `KeyboardProvider` at the root of your app:
+
+```tsx title="SnippetPage.tsx"
+import { useState } from 'react';
+import { ScrollView } from 'react-native';
+import { CodeEditor } from 'react-native-codepad';
+import { KeyboardAvoidingView } from 'react-native-keyboard-controller';
+
+export function SnippetPage() {
+ const [height, setHeight] = useState(120);
+ return (
+
+
+ setHeight(Math.ceil(e.height))}
+ />
+
+
+ );
+}
+```
+
+Use `avoidKeyboard` or the `KeyboardAwareScrollView` from react-native-keyboard-controller, not both. Both scroll the same page to the caret.
+
+## `keyboardOffset`
+
+A toolbar that rides on the keyboard covers the lines just above it. `keyboardOffset` raises the keyboard's top by the toolbar's height while the keyboard is up. The caret then stays above the toolbar too. It's in dp on Android, points on iOS and CSS pixels on the web, and it needs `avoidKeyboard`:
+
+```tsx
+
+```
+
+A toolbar laid out below the `ScrollView`, inside the `KeyboardAvoidingView`, doesn't need an offset, because the `ScrollView` already ends above it.
+
## `contentInsets`
The editor ignores padding in `style`. `contentInsets` is the padding instead: space inside the scrollable area, in dp on Android and points on iOS. A bottom inset keeps the caret above a toolbar or a floating button.
diff --git a/website/docs/guides/nested-scroll.md b/website/docs/guides/nested-scroll.md
index 9ac04723..ae4de090 100644
--- a/website/docs/guides/nested-scroll.md
+++ b/website/docs/guides/nested-scroll.md
@@ -4,7 +4,7 @@ description: An editor that grows with its text inside a page that scrolls.
sidebar_position: 4
---
-When the editor is inside a page that scrolls, such as a form or a list of snippets, it can grow with its text instead of scrolling on its own. One prop and one event do that:
+When the editor is inside a page that scrolls, such as a form or a list of snippets, it can grow with its text instead of scrolling on its own. Two props and one event do that:
```tsx title="SnippetPage.tsx"
import { useState } from 'react';
@@ -20,6 +20,7 @@ export function SnippetPage() {
style={{ height }}
language="javascript"
scrollEnabled={false}
+ avoidKeyboard
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
@@ -30,25 +31,13 @@ export function SnippetPage() {
- `scrollEnabled={false}` leaves vertical drags to the page, so a drag over the editor scrolls the page. A mostly horizontal drag over lines wider than the editor scrolls them sideways. Taps, touch and hold, and handle drags still work.
- Add `softWrap` instead to wrap long lines to the page width. The editor then never scrolls sideways.
+- `avoidKeyboard` scrolls the page to keep the caret's row in view and above the keyboard.
- `onContentSizeChange` reports the size at which nothing scrolls, insets included. Feed its height back into `style`. With `softWrap` on, the height of a long document settles over a few frames as the editor measures its lines. `Math.ceil` rounds the height up, so the editor never scrolls by a fraction of a pixel.
- `keyboardShouldPersistTaps="handled"` lets a tap place the caret without the page closing the keyboard first.
## Keep the caret on screen
-The editor scrolls sideways to keep the caret in view, but it can't scroll the page. In `onSelectionChange`, read `getCursorRect()`, add the editor's position in the page from its `onLayout`, and scroll the page when the caret leaves the visible part:
-
-```tsx
-onSelectionChange={() => {
- const rect = editor.current?.getCursorRect();
- if (!rect) return;
- const y = editorTop + rect.y;
- if (y < pageScrollY || y + rect.height > pageScrollY + pageHeight) {
- page.current?.scrollTo({ y: y - pageHeight / 2, animated: true });
- }
-}}
-```
-
-Leave `avoidKeyboard` off here. An editor that doesn't scroll can't move its text above the keyboard, so let the page handle the keyboard with a `KeyboardAvoidingView` or the `KeyboardAwareScrollView` from react-native-keyboard-controller.
+With `avoidKeyboard` on, the editor scrolls the page whenever the caret would leave its visible part or go under the keyboard. When the editor is the last thing on the page, the page needs room to scroll its last lines up. The [keyboard guide](/guides/keyboard/#inside-a-page-that-scrolls) shows how to give it room on each platform.
## Keep nested documents small
diff --git a/website/docs/reference/props.md b/website/docs/reference/props.md
index f68b60cc..e878922a 100644
--- a/website/docs/reference/props.md
+++ b/website/docs/reference/props.md
@@ -46,8 +46,8 @@ sidebar_position: 1
| `scrollEnabled` | `boolean` | `true` | Lets the editor scroll up and down. When it's off, vertical drags go to a parent `ScrollView`, and lines wider than the editor still scroll sideways. |
| `contentInsets` | `ContentInsets` | All 0 | Adds space inside the scrollable area, in dp on Android and points on iOS. A bottom inset keeps the caret above a toolbar. |
| `textPadding` | `number` | `8` | The space between the text and the editor's left and right edges, inside `contentInsets`, in dp on Android and points on iOS. Set it to `0` to start the text exactly at `contentInsets.left`. |
-| `avoidKeyboard` | `boolean` | `false` | Adds the keyboard overlap to the bottom inset on every frame of the keyboard animation. See the [keyboard guide](/guides/keyboard/). |
-| `keyboardOffset` | `number` | `0` | While the keyboard is up, the editor treats its top as this much higher, in dp on Android, points on iOS and CSS pixels on the web. Use it to keep a toolbar that rides the keyboard clear. It needs `avoidKeyboard`. |
+| `avoidKeyboard` | `boolean` | `false` | Keeps the caret above the keyboard. A scrolling editor adds the keyboard overlap to its bottom inset on every frame of the keyboard animation, and an editor with `scrollEnabled` off scrolls the page around it instead. See the [keyboard guide](/guides/keyboard/). |
+| `keyboardOffset` | `number` | `0` | Raises the keyboard's top by this much while the keyboard is up, in dp on Android, points on iOS and CSS pixels on the web. Use it to keep the caret above a toolbar that rides on the keyboard. It needs `avoidKeyboard`. |
## Decorations