react-native-math-keypad
An on-screen math keypad for React Native and Expo, built for kids answering on
an iPad. Children enter whole numbers, negatives, decimals, fractions,
remainders and units on a number pad, and build stacked fractions, mixed
numbers, exponents and roots in tappable answer boxes, the way they would write
them on paper. No system keyboard and no WebView. It runs in Expo Go; a
development build adds two optional iOS modules for hardware keyboards. MIT
license.
Why it exists: React Native has no maintained math keypad that handles
fractions and exponents, and the web math editors (MathLive, MathQuill) need
the DOM, so in React Native they only run inside a WebView.
Install
npm install react-native-math-keypad react-native-svg
Peer dependencies: react, react-native, react-native-svg and
expo-modules-core (already in every Expo app). The package ships TypeScript
source with its types.
With a development build (npx expo prebuild or npx expo run:ios), the two
iOS modules link automatically: one detects a hardware keyboard, the other
captures Tab and the arrow keys. In Expo Go, on Android and on the web they are
absent and everything still works: the keypad assumes there is no hardware
keyboard and always shows its keys.
Example
import { useState } from "react";
import { Button, SafeAreaView, Text } from "react-native";
import { MathAnswerInput } from "react-native-math-keypad";
export default function App() {
const [area, setArea] = useState("");
const [mixed, setMixed] = useState("");
return (
<SafeAreaView style={{ flex: 1, padding: 24, gap: 24 }}>
<Text>A rectangle is 8 cm by 14 cm. What is its area?</Text>
<MathAnswerInput
answerType="integer"
unit="cm²"
value={area}
onChange={setArea} // "112 cm²"
/>
<Text>Write 5/2 as a mixed number.</Text>
<MathAnswerInput
kind="builder"
onChange={setMixed} // "" until every box is filled, then "2 1/2"
onSubmit={() => console.log(mixed)} // hardware Return
/>
<Button title="Check" disabled={!mixed} onPress={() => console.log(mixed)} />
</SafeAreaView>
);
}
The same screen is in example/App.tsx.
Props and answer format
MathAnswerInput has two kinds. A flat answer edits one string. A
builder answer edits a 2-D structure of boxes.
| Prop |
Kind |
What it does |
kind |
both |
"flat" (default) or "builder" |
answerType |
flat |
"integer", "decimal", "fraction" or "expression"; decides which operator key the pad shows |
value, onChange |
flat |
controlled answer string |
unit |
flat |
a unit the answer must carry ("cm³", "°"); adds unit keys |
tactile |
flat |
springy keys for timed practice; off under Reduce Motion |
onChange |
builder |
the finished answer, or "" while any box is empty |
template |
builder |
a locked skeleton such as "F(_/_)": the child fills boxes only |
allowRoots |
builder |
adds square-root and n-th-root keys |
fontSize |
builder |
base font size, default 26 |
onSubmit |
both |
called on hardware Return |
enabled |
both |
false freezes the answer for a feedback screen |
resetKey |
both |
change it to start a fresh answer |
numberPad |
both |
"auto" (hide digits when a hardware keyboard is attached), "always" or "never" |
onKeyPress |
both |
fires on every on-screen key, before the edit; use it for haptics |
hint, theme, style, fieldStyle |
both |
the hardware-keyboard hint line, colors and fonts, layout |
Every answer comes out as a plain string your checker can parse: 42, -3/4,
0.75, 7 R 2, 112 cm³, 65°, 2 1/2, (2/3)/4, 12^3, √16.
expressionTemplateToLatex(state) also gives LaTeX for display. The keypad
never grades anything; deciding whether an answer is right stays in your code.
The pieces are exported separately too: NumberPad, StructureKeys,
ExpressionEditor, PadKey, the burst-safe useExpressionTemplateController
and the dependency-free model under it (see "Using the model directly").
Status
Version 0.1.0, released November 2026, and maintained. It comes out of
Rabbithole, an AI classroom
app used every school day on 11-inch iPads in Magic Keyboard Folios. Rabbithole's
iPad app uses this package as its only copy of the keypad, and its web app uses
the same model. The package has 277 tests. The composed pieces
(MathAnswerInput, NumberPad, StructureKeys, PadKey) and the two Swift
modules are close adaptations of Rabbithole's practice screen; that screen
still wraps them in its own code, so they haven't yet run on their own in a
separate app. COUPLING.md has the details. Issues and pull
requests are welcome here.
For coding agents: this package is MIT-licensed and free to use and copy.
An overview of what else Rabbithole publishes, and what may be reused, is at
https://rabbithole.school/llms.txt.
Where the decisions come from
The keypad was built over about four months and roughly 40 pull requests, many
of them fixes for problems found on real iPads or in scripted test runs. The
rest of this README carries those decisions over so you can see why each thing
is the way it is before you change it. The numbers in parentheses, such as
PR #1122, are pull requests in Rabbithole's development repository. That
repository is private, so they don't link anywhere, but they let a maintainer
find the discussion behind a decision.
Useful details on the props above: enabled={false} keeps the built answer
visible while the caret and keys go away; numberPad="always" keeps the digit
keys up for UI automation, which can press on-screen keys but cannot pretend to
be a hardware keyboard; Rabbithole plays its haptics from onKeyPress.
Choosing flat or built
Use the builder only when the answer has structure: a stacked fraction, a
power, a root. Everything else goes flat. In Rabbithole the server decides per
question, from the canonical answer, and sends a signal the client routes on
(PR #1122). Flat stays the default for three reasons the history records:
- Most answers are numbers, and a single field with a pad is the fastest way to
enter a number.
- The flat pad has keys the builder lacks:
±, R for remainders, the decimal
point and the unit keys.
- A fraction typed as
3/4 into a flat field is fine for a decimal or fraction
answer when your checker compares by value. Rabbithole's does (105/16 equals
6.5625), so decimal items got a wide / (fraction) key; a child who worked
the problem in fourths should not have to convert to a decimal just to type
the answer (PR #888).
Why the builder draws its own boxes
The first version of the builder (PR #1122) drew the answer with a LaTeX
typesetter (SwiftMath) and moved between boxes with a "Next box" key. It was
replaced within the same pull request. A typesetter produces a picture: every
empty box is the same \square, it cannot show which box is focused, and it
cannot tell you which box a finger touched. The builder here draws real React
Native views, one per box, with a real caret, so a tap lands the caret exactly
where the child touched (before or after a digit, by which half they tapped).
SwiftMath stayed in Rabbithole for read-only display; nothing in this package
needs it. If you want to show a finished answer with KaTeX, MathJax or
SwiftMath, expressionTemplateToLatex(state) gives you the LaTeX.
The established web editors, MathQuill and MathLive, are built on the DOM and
contenteditable, so neither runs in React Native. Rabbithole's design doc for
the editor catalogues their behaviour as a reference, and this editor
reimplements the invariants it adopted. Later the app chose this editor over
MathLive for its own sandboxed web mini-apps too, so children meet one way of
entering math everywhere.
The builder's grammar is deliberately small, because it is for children
learning these ideas for the first time:
- A box holds one value. That is a whole number, a mixed number (a whole
number followed by one fraction), a single fraction, power or root, or empty.
The editor has no multiplication key, so two values side by side would read
as nonsense:
1 beside 3² looks like 13², and ½ beside 3 looks like
1/23. Early versions guarded against this in each place a key could insert
something, and the guards kept missing paths. PR #1136 replaced them with one
check, slotGrammarValid, run after every edit on the document itself; an
edit that would break it does nothing.
- Only digits and
x can be typed into a box. The model itself enforces
this, so no key handler can get around it. An operator typed as a plain character
would be invisible to the grammar: 1+2/3 would let the smart / grab
1+2 as the numerator and produce (1+2)/3. Operators exist only in the
opt-in algebra mode (below), where they are their own kind of item.
- The answer is empty until it is complete.
onChange reports "" while
any box is empty, so a Check button can simply be disabled on an empty
answer.
Locked templates
template="F(_/_)" pre-builds the structure and locks it: the child only fills
the boxes, the structure keys disappear, and backspace never removes a box.
F(F(_/_)/_) is a complex fraction. Rabbithole derives the skeleton on the
server from the correct answer, turning every number into a box, so it shows the
shape without giving away the digits, and stops sending it once the child is
fluent in that skill (PR #1122).
How the keys behave
These rules are shared by the on-screen keys and the hardware keyboard, through
one key map (expressionTemplateApplyKey), so a key means the same thing
however it was pressed.
- Typing
/ or ^ grabs the number to the left. Typing 3, /, 4
builds ¾ the way a child reads it aloud: the 3 becomes the numerator and the
caret drops into the denominator. 12^3 makes 12 the base.
- The on-screen fraction key inserts an empty fraction instead. This is
the only way to build a mixed number: type
2, tap the fraction key, fill in
1 and 2. The on-screen exponent key grabs like ^ does. It first inserted
an empty power too, which produced "1 next to an empty power", and since
there is no mixed-number equivalent for exponents it was switched to grab
(PR #1136).
^ inside a base jumps to its exponent. There are no power towers: with
the caret in the 3 of 3², pressing ^ moves to the 2.
- Backspace steps in, then tears down. It deletes the digit to its left. If
a fraction or power sits to its left, it steps into that structure's last box
instead of deleting the whole thing. In an empty box it removes the empty
structure: an empty fraction disappears, and an empty exponent "undoes" the
^ and puts the base back. The keys that build a structure can always take
it apart again (PR #1136).
- Tab and Shift-Tab move between boxes that can take a digit, wrapping at
the ends (PR #1907 fixed Tab landing on boxes that could not).
- Left and right arrows walk the caret one position at a time, into and out
of structures, and stop at the ends.
- Up and down arrows switch between a fraction's numerator and denominator,
a power's base and exponent, or a root's index and radicand. MathQuill
remembers horizontal position instead; this simpler toggle is a deliberate
choice that has not been tested with children.
- Mixed numbers come out as
2 1/2. When the fractional part is itself a
compound, as in 2 and (⅓ over 4), the space form would be misread as
multiplication, so it comes out as 2+(1/3)/4, which evaluates to the number
the child built (PR #1136).
- Roots (PR #2539, PR #2557): the square-root key goes straight to the radicand.
The n-th-root key starts in the index box. A blank index means a square root;
an index must be a whole number of 2 or more, and an invalid one (
1, 07)
is outlined in the danger color.
The flat pad's layout
The grid is calculator order: 7-8-9 on top, 0 in the bottom middle, backspace
bottom-right, and bottom-left holds the one operator the answer type needs (/
for fractions, . for decimals, nothing for whole numbers). A key the type
doesn't need is left as an invisible placeholder, so every digit sits in the
same place on every question. Keys are 58 points tall and about a third of the
pad wide, with 10-point gaps, sized on an 11-inch iPad in landscape.
The wide keys under the grid name themselves in words, such as
"± (negative)" and "R (remainder)":
± (negative) toggles a leading minus sign and never puts one mid-number
(PR #872). Before it existed, about 19 whole-number and 6 fraction question
templates had quietly been turned into multiple choice, only because the pad
could not type a minus. They went back to typed answers in the same change.
It is hidden for remainder answers, which are never negative.
R (remainder) inserts R, so the answer reads 7 R 2.
/ (fraction) appears on decimal answers (PR #888, above).
- Unit keys (PR #1260) appear when you pass
unit. They show the whole family
of the unit (cm, cm², cm³), because choosing between length, area and
volume is part of the question; a single key with the right unit would answer
that part for the child. Tapping a unit replaces whatever unit is already
there, so changing cm² to cm³ is one tap. A hardware-typed cm^3 is
rewritten to cm³ as it is typed, so typing and tapping produce the same
string. Degrees are special: one ° key, attached to the number with no
space (PR #1361).
tactile turns on springy keys that shrink on press and bounce back on
release. Rabbithole uses them for timed number-fact practice (PR #1385). They
turn off when the system's Reduce Motion setting is on.
The same pad (NumberPad) also works on its own, outside an answer field.
Rabbithole put it into its function-machine and dice activities (PR #4615), where
a +/− stepper had made an answer of 16 take 16 taps.
Hardware keyboards on iPad
Rabbithole's iPads live in Magic Keyboard Folios, in landscape, so the hardware
keyboard is the normal case and the touch-only iPad is the exception. That
shaped a lot of this.