# Quote Estimator
*URL: https://www.grettech.com/components/quote-estimator*

A step-by-step "what do you need?" form that shows a live price range. Turns a contact page into lead qualification.

- Single- and multiple-choice steps with icons
- Live estimated range: options add a price or multiply the total
- Clickable summary to jump back to any answer
- Focus moves to each new question for keyboard and screen reader users

## Install with the shadcn CLI
```bash
npx shadcn@latest add https://www.grettech.com/api/registry/quote-estimator
```
With its demo: `npx shadcn@latest add https://www.grettech.com/api/registry/quote-estimator-demo`

**Dependencies:** npm: clsx, tailwind-merge, lucide-react; shadcn/ui: button

## Manual installation
**Step 1: Install dependencies**
Install the packages this component uses and the shadcn/ui components it builds on. It also needs the `cn` helper in `lib/utils.ts`, which `npx shadcn@latest init` adds.

```bash
npm install clsx tailwind-merge lucide-react
npx shadcn@latest add button
```

**Step 2: Copy the source code**
Add these files to your project components directory.

File: `components/ui/quote-estimator.tsx`
```tsx
"use client"

import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { ArrowLeft, ArrowRight, Check, Send } from "lucide-react"
import * as React from "react"

export interface EstimatorOption {
  id: string
  label: string
  description?: string
  icon?: React.ReactNode
  /** Added to the estimate */
  price?: number
  /** Multiplies the whole estimate, e.g. 1.25 for a rush timeline */
  multiplier?: number
}

export interface EstimatorStep {
  id: string
  question: string
  description?: string
  /** "single" picks one option, "multiple" any number */
  type: "single" | "multiple"
  options: EstimatorOption[]
}

export interface QuoteResult {
  /** Selected option ids per step id */
  answers: Record<string, string[]>
  range: [number, number]
}

export interface QuoteEstimatorProps extends Omit<
  React.HTMLAttributes<HTMLDivElement>,
  "onSubmit"
> {
  steps: EstimatorStep[]
  /** Added before any option, e.g. a setup fee */
  basePrice?: number
  /** Range width around the estimate: 0.15 shows ±15% */
  spread?: number
  /** Ranges are rounded to this step */
  roundTo?: number
  currency?: string
  locale?: string
  onSubmit?: (result: QuoteResult) => void
  submitLabel?: string
  title?: string
}

export function estimate(
  steps: EstimatorStep[],
  answers: Record<string, string[]>,
  { basePrice = 0, spread = 0.15, roundTo = 100 } = {}
): [number, number] {
  let total = basePrice
  let multiplier = 1
  for (const step of steps) {
    for (const option of step.options) {
      if (!answers[step.id]?.includes(option.id)) continue
      total += option.price ?? 0
      multiplier *= option.multiplier ?? 1
    }
  }
  const mid = total * multiplier
  const round = (n: number) => Math.round(n / roundTo) * roundTo
  return [round(mid * (1 - spread)), round(mid * (1 + spread))]
}

export const QuoteEstimator = React.forwardRef<
  HTMLDivElement,
  QuoteEstimatorProps
>(
  (
    {
      steps,
      basePrice = 0,
      spread = 0.15,
      roundTo = 100,
      currency = "USD",
      locale = "en-US",
      onSubmit,
      submitLabel = "Send my brief",
      title = "Estimate your project",
      className,
      ...props
    },
    ref
  ) => {
    const [index, setIndex] = React.useState(0)
    const [answers, setAnswers] = React.useState<Record<string, string[]>>({})
    const [sent, setSent] = React.useState(false)
    const headingRef = React.useRef<HTMLHeadingElement>(null)

    const step = steps[index]
    const isLast = index === steps.length - 1
    const picked = answers[step.id] ?? []
    // Single-choice steps need an answer; multiple-choice may be empty
    const canContinue = step.type === "multiple" || picked.length > 0

    const range = estimate(steps, answers, { basePrice, spread, roundTo })
    const hasEstimate = range[1] > 0
    const money = new Intl.NumberFormat(locale, {
      style: "currency",
      currency,
      maximumFractionDigits: 0,
    })

    const toggle = (optionId: string) => {
      setSent(false)
      setAnswers((prev) => {
        const current = prev[step.id] ?? []
        const next =
          step.type === "single"
            ? [optionId]
            : current.includes(optionId)
              ? current.filter((id) => id !== optionId)
              : [...current, optionId]
        return { ...prev, [step.id]: next }
      })
    }

    const go = (next: number) => {
      setIndex(next)
      // Keep keyboard and screen reader users on the new question
      requestAnimationFrame(() => headingRef.current?.focus())
    }

    return (
      <div
        ref={ref}
        className={cn(
          "grid w-full max-w-3xl overflow-hidden rounded-2xl border bg-card text-card-foreground shadow-sm md:grid-cols-[1fr_15rem]",
          className
        )}
        {...props}
      >
        <div className="flex flex-col p-6">
          <div className="flex items-center justify-between gap-4 text-xs text-muted-foreground">
            <span>{title}</span>
            <span className="tabular-nums">
              Step {index + 1} of {steps.length}
            </span>
          </div>
          <div
            className="mt-2 h-1 overflow-hidden rounded-full bg-muted"
            role="progressbar"
            aria-label="Progress"
            aria-valuemin={1}
            aria-valuemax={steps.length}
            aria-valuenow={index + 1}
          >
            <div
              className="h-full rounded-full bg-foreground transition-[width] duration-500 ease-out motion-reduce:transition-none"
              style={{ width: `${((index + 1) / steps.length) * 100}%` }}
            />
          </div>

          <h3
            ref={headingRef}
            tabIndex={-1}
            className="mt-6 text-lg font-semibold outline-none"
          >
            {step.question}
          </h3>
          {step.description && (
            <p className="mt-1 text-sm text-muted-foreground">
              {step.description}
            </p>
          )}

          <div
            key={step.id}
            role={step.type === "single" ? "radiogroup" : "group"}
            aria-label={step.question}
            className="mt-5 grid animate-in gap-2 duration-300 fade-in slide-in-from-right-2 motion-reduce:animate-none sm:grid-cols-2"
          >
            {step.options.map((option, o) => {
              const active = picked.includes(option.id)
              return (
                <button
                  key={`${option.id}-${o}`}
                  type="button"
                  role={step.type === "single" ? "radio" : "checkbox"}
                  aria-checked={active}
                  onClick={() => toggle(option.id)}
                  className={cn(
                    "group flex items-start gap-3 rounded-xl border p-3 text-left transition-colors focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none",
                    active
                      ? "border-foreground bg-foreground/[0.03] dark:bg-foreground/10"
                      : "hover:border-foreground/30 hover:bg-muted/60"
                  )}
                >
                  {option.icon && (
                    <span
                      aria-hidden="true"
                      className="mt-0.5 flex size-8 shrink-0 items-center justify-center rounded-lg border bg-background text-muted-foreground [&_svg]:size-4"
                    >
                      {option.icon}
                    </span>
                  )}
                  <span className="min-w-0 flex-1">
                    <span className="block text-sm font-medium">
                      {option.label}
                    </span>
                    {option.description && (
                      <span className="mt-0.5 block text-xs text-muted-foreground">
                        {option.description}
                      </span>
                    )}
                  </span>
                  <span
                    aria-hidden="true"
                    className={cn(
                      "mt-0.5 flex size-4 shrink-0 items-center justify-center border transition-colors",
                      step.type === "single" ? "rounded-full" : "rounded-[4px]",
                      active
                        ? "border-foreground bg-foreground text-background"
                        : "border-muted-foreground/40"
                    )}
                  >
                    {active && <Check className="size-3" />}
                  </span>
                </button>
              )
            })}
          </div>

          <div className="mt-auto flex items-center justify-between gap-2 pt-6">
            <Button
              type="button"
              variant="ghost"
              onClick={() => go(index - 1)}
              disabled={index === 0}
            >
              <ArrowLeft aria-hidden="true" />
              Back
            </Button>
            {isLast ? (
              <Button
                type="button"
                disabled={!canContinue || sent}
                onClick={() => {
                  onSubmit?.({ answers, range })
                  setSent(true)
                }}
              >
                {sent ? (
                  <Check aria-hidden="true" />
                ) : (
                  <Send aria-hidden="true" />
                )}
                {sent ? "Sent" : submitLabel}
              </Button>
            ) : (
              <Button
                type="button"
                disabled={!canContinue}
                onClick={() => go(index + 1)}
              >
                Next
                <ArrowRight aria-hidden="true" />
              </Button>
            )}
          </div>
        </div>

        <aside className="flex flex-col border-t bg-muted/40 p-6 md:border-t-0 md:border-l">
          <p className="text-xs font-medium text-muted-foreground">
            Estimated range
          </p>
          <p
            className="mt-1 text-2xl font-semibold tracking-tight tabular-nums"
            aria-live="polite"
          >
            {hasEstimate
              ? `${money.format(range[0])} – ${money.format(range[1])}`
              : "—"}
          </p>
          <ol className="mt-5 space-y-2 text-sm">
            {steps.map((s, i) => {
              const labels = s.options
                .filter((o) => answers[s.id]?.includes(o.id))
                .map((o) => o.label)
              return (
                <li key={`${s.id}-${i}`}>
                  <button
                    type="button"
                    onClick={() => go(i)}
                    disabled={i > index && !answers[steps[i - 1]?.id]?.length}
                    className={cn(
                      "w-full rounded-md text-left transition-colors focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none disabled:cursor-default",
                      i === index ? "text-foreground" : "text-muted-foreground"
                    )}
                  >
                    <span className="block text-xs">{s.question}</span>
                    <span className="block truncate font-medium text-foreground">
                      {labels.length > 0 ? labels.join(", ") : "—"}
                    </span>
                  </button>
                </li>
              )
            })}
          </ol>
          <p className="mt-auto pt-6 text-[11px] text-muted-foreground">
            A rough guide, not a quote. We confirm the price after a short call.
          </p>
        </aside>
      </div>
    )
  }
)
QuoteEstimator.displayName = "QuoteEstimator"

```

**Step 3: Framework Adjustments (Optional)**
If you are manually copying this code into a Vite or Remix project (without using the CLI), make sure to remove the `"use client";` directive from the very top of the file.

## Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| `steps` | `EstimatorStep[]` | - | Yes | The questions, in order. |
| `steps.id` | `string` | - | Yes | Unique id; answers are keyed by it. |
| `steps.question` | `string` | - | Yes | The question. |
| `steps.description` | `string` | - | No | Hint under the question. |
| `steps.type` | `"single" \| "multiple"` | - | Yes | Single steps need an answer; multiple steps can be left empty. |
| `steps.options` | `EstimatorOption[]` | - | Yes | The choices: { id, label, description?, icon?, price?, multiplier? }. price is added to the estimate; multiplier scales the whole estimate (e.g. 1.25 for a rush). |
| `steps.options.id` | `string` | - | Yes | - |
| `steps.options.label` | `string` | - | Yes | - |
| `steps.options.description` | `string` | - | No | - |
| `steps.options.icon` | `React.ReactNode` | - | No | - |
| `steps.options.price` | `number` | - | No | Added to the estimate |
| `steps.options.multiplier` | `number` | - | No | Multiplies the whole estimate, e.g. 1.25 for a rush timeline |
| `basePrice` | `number` | `0` | No | Added before any option, e.g. a setup fee. |
| `spread` | `number` | `0.15` | No | Width of the range around the estimate: 0.15 shows ±15%. |
| `roundTo` | `number` | `100` | No | The range is rounded to this step. |
| `currency` | `string` | `"USD"` | No | ISO 4217 currency code. |
| `locale` | `string` | `"en-US"` | No | Locale used to format prices. |
| `onSubmit` | `(result: QuoteResult) => void` | - | No | Called on the last step with { answers, range }. answers maps step ids to the chosen option ids. |
| `submitLabel` | `string` | `"Send my brief"` | No | Label of the final button. |
| `title` | `string` | `"Estimate your project"` | No | Small heading above the progress bar. |
| `className` | `string` | - | No | Extra classes for the root element. |
| `...props` | `React.HTMLAttributes<HTMLDivElement>` | - | No | Any other attributes, passed to the root element. |

## Usage
```tsx
"use client"

import {
  AppWindow,
  CreditCard,
  FileText,
  Globe,
  Languages,
  LayoutTemplate,
  LineChart,
  ShoppingBag,
  Lock,
} from "lucide-react"
import { QuoteEstimator, type EstimatorStep } from "@/components/ui/quote-estimator"

const STEPS: EstimatorStep[] = [
  {
    id: "type",
    question: "What are we building?",
    type: "single",
    options: [
      {
        id: "landing",
        label: "Landing page",
        description: "One page that converts",
        icon: <LayoutTemplate />,
        price: 1500,
      },
      {
        id: "website",
        label: "Business website",
        description: "A few pages and a blog",
        icon: <Globe />,
        price: 4000,
      },
      {
        id: "store",
        label: "Online store",
        description: "Products, cart, checkout",
        icon: <ShoppingBag />,
        price: 7000,
      },
      {
        id: "app",
        label: "Web app",
        description: "Accounts, dashboards, data",
        icon: <AppWindow />,
        price: 9000,
      },
    ],
  },
  {
    id: "features",
    question: "Which features do you need?",
    description: "Pick any, or none.",
    type: "multiple",
    options: [
      { id: "cms", label: "Editable content", icon: <FileText />, price: 1200 },
      { id: "auth", label: "Sign in", icon: <Lock />, price: 1500 },
      { id: "payments", label: "Payments", icon: <CreditCard />, price: 2000 },
      {
        id: "i18n",
        label: "Multiple languages",
        icon: <Languages />,
        price: 900,
      },
      { id: "analytics", label: "Analytics", icon: <LineChart />, price: 400 },
    ],
  },
  {
    id: "timeline",
    question: "When do you need it?",
    type: "single",
    options: [
      {
        id: "flexible",
        label: "Flexible",
        description: "2–3 months",
        multiplier: 0.9,
      },
      {
        id: "standard",
        label: "Standard",
        description: "4–6 weeks",
        multiplier: 1,
      },
      {
        id: "rush",
        label: "As soon as possible",
        description: "2–3 weeks",
        multiplier: 1.25,
      },
    ],
  },
]

export default function QuoteEstimatorDemo() {
  return (
    <div className="flex w-full justify-center p-4">
      <QuoteEstimator
        steps={STEPS}
        onSubmit={(result) => console.log("Quote request:", result)}
      />
    </div>
  )
}

```
