shadcn.io is not affiliated with official shadcn/ui
Quick Calculator Dialog Block for React
A contractor's quote with a calculator dialog shown open: a display with the sum above its result, a four by five keypad, memory keys, a history of the last five sums, working keyboard keys and a Use result button that writes the figure on the quote.
Installation
Overview
Let people work out a figure without leaving the field it belongs to, with this calculator dialog for React with Base UI. It opens from quote 0417 in Quoteloom, a quoting tool for contractors: the page's header holds an outline Open calculator Button, and the quote, four lines each with an Amount field, is drawn under a soft scrim inside a hairline frame with a plus mark on each corner. The dialog has a display, the sum above and its result below, a row of memory keys, MC, MR, M- and M+, with an M mark that shows while memory holds something, and a four by five keypad of Buttons: the digits, a point, plus, minus, times, divide, percent, plus or minus, C, Backspace and equals. A history button swaps the keypad for the last five sums in the same space, and pressing one brings its result back. The keys of a keyboard work too. Use result, the only Shine Button, writes the figure on the quote's labour line. For preview the dialog is drawn open in place when the page loads; the header button opens the real Dialog, with the same sum and the same state. For quoting, invoicing and expense tools, and any form with a field that is easier to fill by adding up.
FAQ
Why is the dialog already open, and what do I keep?
So you can see it without a click. On load the dialog is a copy drawn in place over the page: a native dialog element that isn't modal, so nothing is trapped, hidden or focused, and the page around the preview keeps working. The Open calculator button opens the real Dialog, modal, with its focus trap, Escape and focus return, and the copy stays closed until a reload. Both show the same Pad component and share the block's state, so a sum started in the copy is there in the real one. In your app keep the Dialog and drop the copy: the "in-place" state, the scrim and the dialog element inside the frame.
How is the sum worked out, and is it safe?
By a small tokenizer and a recursive-descent parser in the file, evaluate, never eval or new Function, so nothing you type can run as code. It reads numbers, plus, minus, times and divide with the usual precedence, a minus in front of a number, and a percent after one: a percent on its own is a hundredth (50% is 0.5), and after a plus or minus it is that share of what came before, so 200 + 10% is 220, the way a pocket calculator does it. A trailing operator is ignored while you type, results are rounded to ten decimals so 0.1 + 0.2 is 0.3, and a result past a quadrillion says it is too large.
What happens on a divide by zero?
Pressing equals on a sum that divides by zero shows Cannot divide by zero in the display and says it in the polite status message, and the sum stays as typed so you can fix it with Backspace; the next key clears the message. While you type, a sum that can't be read yet shows two dashes instead of a result, so nothing flashes an error half-way through a number.
Which keyboard keys work?
While focus is inside the display or the keypad: the digits, the point (a comma too), plus, minus, the asterisk or x for times, the slash for divide, percent, Enter or equals for equals, Backspace, and C or Delete to clear. Enter is equals there, not a press of the focused key; Space still presses it. Escape closes the dialog as it always does, and nothing is bound outside the dialog.
Does anything leave the dialog but the number, and how do I wire it up?
No. Use result takes the result as whole cents, writes it on the drawn labour line, closes the dialog and says in a polite status message that nothing was saved or sent; it stays quiet and says what it is waiting for until there is a result above zero. The history and the memory stay in the block while the page is open. In your app pass the cents to your form's field in use, validate them there as you would a typed amount, and keep the history somewhere only if people should find it again.
Is it accessible from the keyboard?
Yes. The real dialog is named by its title and described by its sentence, focus starts on the first key of the keypad and stays inside until it closes, and Escape, Cancel and the close button all return focus to Open calculator. The trigger is passed to DialogTrigger through render, and the dialog is controlled with open and onOpenChange. Every key is a named button, the history button says whether the history is open, and the polite status message speaks an answer, a memory change or a message, never every digit. The copy shown on load moves no focus; Escape inside it, Cancel and its close button close it and put focus on the header button.
Related
- Pro
Tip Calculator Dialog
Drawers and popoversA café terminal's pay screen with a tip popover shown open beside it: the bill, a tip of 15, 18, 20 or 25 percent or your own, how many people share it, a switch that rounds each share up to a dollar, and four lines that add it up.
- Pro
A recipe app's unit converter shown open: four tabs of two linked number fields with unit pickers, a swap button, preset chips, the factor stated in a line and results rounded honestly to four significant figures.
- Pro
Split Bill Dialog
Payments and plansA group-dining app's open check with a dialog that splits it by item: each item has a row of initials that toggle who shared it, and beside them what each person pays, reconciled to the cent, with the tax and tip spread fairly.
- Pro
Media Upload Dialog
Media and widgetsA video library with its upload dialog shown open: a drop zone for images, video and audio that names the files it turns away and why, tiles for the ones that pass with a check each, bulk removal, a total and an Add button that puts the clips in the library.
- Pro
Price Alert Dialog
Media and widgetsA price tracker's product page with an alert dialog shown open: a chart of the last 30 daily prices with a dashed target line that moves as you type, dots where the alert would have fired, and the form that sets it.
- Pro
QR Scanner Dialog
Media and widgetsAn event check-in page with a simulated scanner dialog shown open: a viewfinder around a real sample QR code, a Wi-Fi, link and ticket payload read in turn, a result card that names the kind and cuts it into parts, a typed-code tab and a recent-scans strip.