Skip to main content

Web

Run CodePad in the browser with Expo web, serve its files, and see what works differently there.

Edit this page

CodePad runs in the browser through React Native Web, with the same CodeEditor props and ref as on Android and iOS.

The browser runs the same C++ core, compiled to WebAssembly. CodePad draws its own caret, selection handles and edit menu there too.

Set up Expo web​

Install the packages Expo needs for the web, if your app doesn't run there yet:

npx expo install react-dom react-native-web @expo/metro-runtime

Copy the files​

The browser loads the core and the languages while the app runs, from your app's public folder. Run the web command from your app's root to copy them there:

npx react-native-codepad web public

It copies the core and every installed language package to public/codepad/, including the custom languages in modules/. Run it again after you add or remove a language or update CodePad. Pass --root <app-root> to run it from another folder.

Expo serves public/ at the root of the site and copies it into dist/ on export, so run the command before both:

package.json
{
"scripts": {
"web": "react-native-codepad web public && expo start --web",
"export:web": "react-native-codepad web public && expo export --platform web"
}
}

Add public/codepad/ to .gitignore, because the command writes it again from the installed packages.

When a file is missing, the editor is blank or shows plain text, and the browser console shows the address of the file it couldn't load.

Serve the files from another path​

The editor loads its files from /codepad/. When they live somewhere else, such as a CDN or a sub path, call configureWeb once before the first editor mounts:

import { configureWeb } from 'react-native-codepad';

configureWeb({ assetsUrl: 'https://cdn.example.com/codepad/' });

configureWeb does nothing on Android and iOS, so it can stay in shared code.

Languages​

The language prop works as it does on Android and iOS. The editor loads a language the first time an editor uses it. A language inside another one, such as JavaScript in HTML, loads when the text first needs it.

Codepad.availableLanguages() returns an empty list on the web until an editor has loaded its first language.

The language command also builds the web version of a custom language. It uses tree-sitter's own command-line tool, which downloads its compiler the first time, and it needs grammar.js next to src/ in the grammar folder. When the web build fails, the command prints a warning and still writes the package for Android and iOS. The web command then stops on that package until it has a web build. Pass --no-web only when your app doesn't run on the web.

Selection​

With a mouse:

  • Drag to select. Hold the drag past the editor's edge to scroll.
  • Double-click to select a word, or triple-click to select a line with its line break. Dragging after either extends the selection by words or by lines.
  • Shift+click to extend the selection from where it started.
  • Right-click to open the edit menu.

With touch, the editor follows Android:

  • Tap to place the caret. A handle shows under it for 4 seconds.
  • Double-tap to select a word.
  • Touch and hold to select a word and open the edit menu. Move your finger without lifting it to extend the selection by words.
  • Drag a handle to move that end of the selection. The ends can cross each other.
  • Tap the caret's handle, or tap inside the selection, to open the edit menu.

The handles show only after a touch or pen press, never after a mouse press. They take the theme's cursor color.

The edit menu has Cut, Copy, Paste and Select all. Cut and Copy need selected text. Cut and Paste are hidden when readOnly is on, and Paste is also hidden when the browser doesn't let the page read the clipboard.

Limits​

  • The ref's copy(), cut() and paste() and the menu's Paste need a secure origin, which is https or localhost. They reject when the browser refuses. The menu's Cut and Copy also work over plain http.
  • On iOS, Safari shows its form bar above the keyboard while the editor has the focus.
  • The menu labels are in English.
  • Screen readers read the editor's text as page text. They don't announce the caret or your edits.
  • CodePad runs on the page's main thread, with no web worker.
  • The web tests run in Chromium and WebKit, the engines of Chrome and Safari. Firefox is untested.