Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
1 change: 1 addition & 0 deletions example/src/EditorApp.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function EditorApp() {
theme={theme}
fontFamily={FONT}
scrollEnabled={false}
avoidKeyboard
onReady={ready('nested')}
/>
<Text style={[styles.filler, { color: theme?.foreground }]}>End of the page.</Text>
Expand Down
96 changes: 50 additions & 46 deletions example/src/screens/NestedScreen.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -33,50 +34,53 @@ export function NestedScreen({ theme, onBack }: Props) {
topInset={topInsetFor(safe.top)}
onBack={onBack}
/>
<ScrollView
style={styles.scroll}
contentContainerStyle={[
styles.content,
{
paddingBottom: safe.bottom + 16,
paddingLeft: 16 + safe.left,
paddingRight: 16 + safe.right,
},
]}
keyboardShouldPersistTaps="handled"
>
<Text style={[styles.body, { color: colors.onSurface }]}>
The editor below takes its height from onContentSizeChange and leaves vertical scrolling
to this page. Long lines scroll sideways.
</Text>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<OutlinedTextField value={above} modifiers={[fillMaxWidth()]}>
<OutlinedTextField.Label>
<ComposeText>A field above</ComposeText>
</OutlinedTextField.Label>
</OutlinedTextField>
</Host>
<CodeEditor
style={{ height }}
initialValue={NESTED_SNIPPET}
language="javascript"
theme={theme}
lineNumbers
scrollEnabled={false}
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<OutlinedTextField value={below} modifiers={[fillMaxWidth()]}>
<OutlinedTextField.Label>
<ComposeText>A field below</ComposeText>
</OutlinedTextField.Label>
</OutlinedTextField>
</Host>
<Text style={[styles.body, { color: colors.onSurfaceVariant }]}>
A tall footer, so the page itself has something to scroll.
</Text>
<View style={styles.footer} />
</ScrollView>
<KeyboardAvoidingView behavior="padding" style={styles.scroll}>
<ScrollView
style={styles.scroll}
contentContainerStyle={[
styles.content,
{
paddingBottom: safe.bottom + 16,
paddingLeft: 16 + safe.left,
paddingRight: 16 + safe.right,
},
]}
keyboardShouldPersistTaps="handled"
>
<Text style={[styles.body, { color: colors.onSurface }]}>
The editor below takes its height from onContentSizeChange and leaves vertical scrolling
to this page. Long lines scroll sideways.
</Text>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<OutlinedTextField value={above} modifiers={[fillMaxWidth()]}>
<OutlinedTextField.Label>
<ComposeText>A field above</ComposeText>
</OutlinedTextField.Label>
</OutlinedTextField>
</Host>
<CodeEditor
style={{ height }}
initialValue={NESTED_SNIPPET}
language="javascript"
theme={theme}
lineNumbers
scrollEnabled={false}
avoidKeyboard
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<OutlinedTextField value={below} modifiers={[fillMaxWidth()]}>
<OutlinedTextField.Label>
<ComposeText>A field below</ComposeText>
</OutlinedTextField.Label>
</OutlinedTextField>
</Host>
<Text style={[styles.body, { color: colors.onSurfaceVariant }]}>
A tall footer, so the page itself has something to scroll.
</Text>
<View style={styles.footer} />
</ScrollView>
</KeyboardAvoidingView>
</View>
);
}
Expand Down
84 changes: 44 additions & 40 deletions example/src/screens/NestedScreen.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ 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';
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];
Expand All @@ -26,44 +27,47 @@ export function NestedScreen({ theme, onBack }: Props) {
];
return (
<View style={[styles.screen, { backgroundColor: palette.systemBackground }]}>
<ScrollView
style={styles.scroll}
contentContainerStyle={[
styles.content,
{
paddingTop: barHeight + 8,
paddingBottom: safe.bottom + 16,
paddingLeft: 16 + safe.left,
paddingRight: 16 + safe.right,
},
]}
scrollIndicatorInsets={{ top: barHeight }}
keyboardShouldPersistTaps="handled"
>
<Text style={[styles.body, { color: palette.label }]}>
The editor below takes its height from onContentSizeChange and leaves vertical scrolling
to this page. Long lines scroll sideways.
</Text>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<TextField placeholder="A field above" modifiers={field} />
</Host>
<CodeEditor
style={{ height }}
initialValue={NESTED_SNIPPET}
language="javascript"
theme={theme}
lineNumbers
scrollEnabled={false}
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<TextField placeholder="A field below" modifiers={field} />
</Host>
<Text style={[styles.body, { color: palette.secondaryLabel }]}>
A tall footer, so the page itself has something to scroll.
</Text>
<View style={styles.footer} />
</ScrollView>
<KeyboardAvoidingView behavior="padding" style={styles.scroll}>
<ScrollView
style={styles.scroll}
contentContainerStyle={[
styles.content,
{
paddingTop: barHeight + 8,
paddingBottom: safe.bottom + 16,
paddingLeft: 16 + safe.left,
paddingRight: 16 + safe.right,
},
]}
scrollIndicatorInsets={{ top: barHeight }}
keyboardShouldPersistTaps="handled"
>
<Text style={[styles.body, { color: palette.label }]}>
The editor below takes its height from onContentSizeChange and leaves vertical scrolling
to this page. Long lines scroll sideways.
</Text>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<TextField placeholder="A field above" modifiers={field} />
</Host>
<CodeEditor
style={{ height }}
initialValue={NESTED_SNIPPET}
language="javascript"
theme={theme}
lineNumbers
scrollEnabled={false}
avoidKeyboard
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
<Host matchContents={{ vertical: true }} colorScheme={scheme} style={styles.host}>
<TextField placeholder="A field below" modifiers={field} />
</Host>
<Text style={[styles.body, { color: palette.secondaryLabel }]}>
A tall footer, so the page itself has something to scroll.
</Text>
<View style={styles.footer} />
</ScrollView>
</KeyboardAvoidingView>
<View
style={styles.chrome}
pointerEvents="box-none"
Expand Down
4 changes: 2 additions & 2 deletions skills/react-native-codepad/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export function Screen() {
- **Missing language package means plain text**, with one console warning and no error. If highlighting is absent, check the package is installed and the app was rebuilt. `Codepad.availableLanguages()` lists what is linked. Use `markdown`, never `markdown_inline`. Use `php` for pages and files that start with `<?php`, and `php_only` for PHP without the tag. The HTML in a PHP page needs `react-native-codepad-lang-html` too. Use `xml` for XML files and `dtd` for a standalone DTD, both from `react-native-codepad-lang-xml`.
- **Object props are compared by identity.** A `theme` or `contentInsets` literal in JSX crosses to native on every render. Hoist it to module scope or `useMemo` it. Callback props are wrapped once, so inline arrows are fine.
- **Style padding is ignored.** Use `contentInsets` (dp on Android, points on iOS) for space inside the scroll area, for example under a floating toolbar.
- **Keyboard:** pass `avoidKeyboard` so the content follows the keyboard animation; the overlap is added on top of `contentInsets.bottom`. On Android it does nothing under `adjustPan`, where the window pans instead.
- **Inside a `ScrollView`:** set `scrollEnabled={false}` so vertical drags scroll the page (lines wider than the editor still scroll sideways; add `softWrap` to wrap them instead), feed `onContentSizeChange` height back into `style` with `Math.ceil`, leave `avoidKeyboard` off and scroll the page yourself from `getCursorRect()`. Only for documents of a few hundred lines; a large file needs its own fixed height editor.
- **Keyboard:** pass `avoidKeyboard` so the content follows the keyboard animation. The editor adds the overlap on top of `contentInsets.bottom`. On Android it does nothing under `adjustPan`, where the window pans instead. `keyboardOffset` raises the keyboard's top by the height of a toolbar that rides on it.
- **Inside a `ScrollView`:** set `scrollEnabled={false}` so vertical drags scroll the page. Lines wider than the editor still scroll sideways, or add `softWrap` to wrap them. Feed the `onContentSizeChange` height back into `style` with `Math.ceil`, and pass `avoidKeyboard` so the editor scrolls the page to keep the caret above the keyboard. An editor that ends the page needs room to lift its last lines. Give it a `KeyboardAvoidingView` with `behavior="padding"` around the `ScrollView` on native, `interactive-widget=resizes-content` in the viewport meta tag in Chrome, and space under the editor in iOS Safari. Don't add a `KeyboardAwareScrollView` on top. Keep this to documents of a few hundred lines, since a large file needs its own fixed height editor.
- **Search:** `setSearch()` rejects when a regex does not compile, so wrap it in `try`. `replaceCurrent()` resolves `false` until a match is selected, so call `findNext()` first. Replacement text is literal (no `$1`).
- **Expo Go cannot run it.** A development build is required.

Expand Down
3 changes: 2 additions & 1 deletion skills/react-native-codepad/references/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function SnippetForm() {
style={{ height }}
language="javascript"
scrollEnabled={false}
avoidKeyboard
onContentSizeChange={(e) => setHeight(Math.ceil(e.height))}
/>
<TextInput placeholder="Tags" />
Expand All @@ -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`.
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading