Checkout flow
From cart to order number in three honest steps: an address form that really validates, a card that groups in fours and slashes its own expiry while you type, and an order summary that stays sticky at your side — plus a real declined-card state and a processing spin.
Real scenarios
The component as it is used in a product — not a specimen grid.
A checkout that behaves like one
Cart, address, card, confirmation — every step guards itself: the address form validates on Continue and parks the error under its field, the card groups in fours and slashes its own expiry while the preview below follows, the classic 4000…0002 decline test card trips the honest error state, and the sticky summary recounts shipping and VAT with every click. Everything runs on local state; nothing leaves the frame.
- Cart
- Address
- Payment
- Done
Your cart
The cart is empty — everything you removed is one click away.
Shipping address
Dublin, Frankfurt, Singapore — wherever the desk is, this form checks itself before moving on.
Payment
Type and watch the mask work: fours for the number, MM/YY for the expiry. Try 4000 0000 0000 0002 to see a decline handled honestly.
<b>Card declined.</b> The bank said no to 4000…0002 — nothing was charged. Edit the number to any other 16 digits and try again, or switch to another card.
Order confirmed
Receipt is on its way to your inbox.
The whole journey is local: validation, masks, decline, order number and delivery date are computed in the frame — swap the Alpine store for your backend when you take it to production.
Important props
| Prop | Type | Default | What it does |
|---|---|---|---|
| steps | count | '3 + done' | cart → address → payment → done; each step’s Continue unlocks only when the step is honest. |
| validation | behaviour | 'on-continue' | Name, street, city and postcode are checked on Continue; each field’s error sits under it with a red rim and the step refuses to move. |
| card-format | mask | '#### #### #### ####' | The card number groups in fours as you type, the expiry becomes month/year on its own, and the card preview below updates live. |
| declined | state | '4000 0000 0000 0002' | The classic decline test card; pay with it and the error banner with its retry button arrives — any other 16-digit number goes through. |
| summary | position | 'sticky' | The order summary sticks in the side column and recounts free shipping over €150 and VAT the moment a quantity changes. |
Snippet
<div class="flck" x-data="{
step: 0, declined: false, processing: false,
form: { name: '', street: '', city: '', postcode: '' }, errors: {},
card: { number: '', exp: '', cvc: '' },
formatCard() {
const d = this.card.number.replace(/\D/g, '').slice(0, 16);
this.card.number = d.replace(/(.{4})/g, '$1 ').trim();
},
formatExp() {
const d = this.card.exp.replace(/\D/g, '').slice(0, 4);
this.card.exp = d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;
},
validate() {
this.errors = {};
if (this.form.name.trim().length < 2) this.errors.name = 'Full name is required';
if (this.form.street.trim().length < 3) this.errors.street = 'Street address is incomplete';
return Object.keys(this.errors).length === 0;
},
}">
<input x-model="card.number" @input="formatCard" dir="ltr" placeholder="4000 0000 0000 0002">
<p class="flck-error" x-show="errors.name" x-cloak x-text="errors.name"></p>
<button @click="step === 1 && !validate() ? null : step++">Continue</button>
</div>
<style>
.flck-error { margin: .25rem 0 0; color: #c62a2f; font-size: .75rem; }
input[data-invalid] { border-color: #c62a2f; box-shadow: inset 0 0 0 1px #c62a2f; }
</style>
// app/Pages/Checkout.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import CheckoutFlow from '@/components/CheckoutFlow';
export default function Checkout() {
return (
<NabuXUIProvider locale="en" linkComponent={Link}>
<main className="nx-page">
<CheckoutFlow />
</main>
</NabuXUIProvider>
);
}
// CheckoutFlow.jsx — cart → validated address → formatted card → order number
import { useMemo, useState } from 'react';
import '@nabuxai/ui-core/css';
const ITEMS = [
{ id: 'lamp', name: 'Aster desk lamp', price: 89, qty: 1 },
{ id: 'keys', name: 'Nimbus Keys K2 keyboard', price: 149, qty: 1 },
{ id: 'stand', name: 'Vela laptop stand', price: 59, qty: 2 },
];
const DECLINED = '4000000000000002';
export default function CheckoutFlow() {
const [step, setStep] = useState(0);
const [items, setItems] = useState(ITEMS);
const [form, setForm] = useState({ name: '', street: '', city: '', postcode: '' });
const [errors, setErrors] = useState({});
const [card, setCard] = useState({ number: '', exp: '', cvc: '' });
const [declined, setDeclined] = useState(false);
const [order, setOrder] = useState(null);
const subtotal = useMemo(() => items.reduce((s, i) => s + i.price * i.qty, 0), [items]);
const shipping = subtotal >= 150 ? 0 : 12;
const vat = Math.round(subtotal * 0.19);
const total = subtotal + shipping + vat;
// Masks that run on every keystroke: fours for the number, MM/YY for the expiry.
const maskNumber = (v) => v.replace(/\D/g, '').slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
const maskExp = (v) => {
const d = v.replace(/\D/g, '').slice(0, 4);
return d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;
};
const validate = () => {
const e = {};
if (form.name.trim().length < 2) e.name = 'Full name is required';
if (form.street.trim().length < 3) e.street = 'Street address is incomplete';
if (form.city.trim().length < 2) e.city = 'City is required';
if (!/^[a-z0-9-]{3,10}$/i.test(form.postcode.trim())) e.postcode = 'Postcode looks off';
setErrors(e);
return Object.keys(e).length === 0;
};
const pay = () => {
if (card.number.replace(/\s/g, '') === DECLINED) {
setDeclined(true); // the decline banner with its retry
} else {
setOrder('NBX-' + Math.floor(10000 + Math.random() * 90000));
setStep(3);
}
};
return (
<>
<style>{FLCK_CSS}</style>
<div className="flck">
<div className="flck-main">
{step === 0 && (
<section className="flck-pane">
<h3>Your cart</h3>
{items.map((i) => (
<div className="flck-row" key={i.id}>
<b>{i.name}</b>
<span className="flck-qty">
<button type="button" onClick={() => setItems(items.map((x) =>
x.id === i.id ? { ...x, qty: Math.max(1, x.qty - 1) } : x))}>−</button>
{i.qty}
<button type="button" onClick={() => setItems(items.map((x) =>
x.id === i.id ? { ...x, qty: x.qty + 1 } : x))}>+</button>
</span>
<span>€{i.price * i.qty}</span>
</div>
))}
<button type="button" className="flck-go" onClick={() => setStep(1)}>Continue to address</button>
</section>
)}
{step === 1 && (
<section className="flck-pane">
<h3>Shipping address</h3>
{['name', 'street', 'city', 'postcode'].map((f) => (
<label className="flck-field" key={f}>
<span>{f[0].toUpperCase() + f.slice(1)}</span>
<input value={form[f]} data-invalid={errors[f] || undefined}
onChange={(e) => setForm({ ...form, [f]: e.target.value })} />
{errors[f] && <small className="flck-error">{errors[f]}</small>}
</label>
))}
<div className="flck-nav">
<button type="button" onClick={() => setStep(0)}>Back</button>
<button type="button" className="flck-go" onClick={() => validate() && setStep(2)}>
Continue to payment
</button>
</div>
</section>
)}
{step === 2 && (
<section className="flck-pane">
<h3>Payment</h3>
<label className="flck-field"><span>Card number</span>
<input dir="ltr" inputMode="numeric" placeholder="4000 0000 0000 0002"
value={card.number} onChange={(e) => setCard({ ...card, number: maskNumber(e.target.value) })} />
</label>
<div className="flck-duo">
<label className="flck-field"><span>Expiry</span>
<input dir="ltr" inputMode="numeric" placeholder="MM/YY"
value={card.exp} onChange={(e) => setCard({ ...card, exp: maskExp(e.target.value) })} />
</label>
<label className="flck-field"><span>CVC</span>
<input dir="ltr" inputMode="numeric" placeholder="123"
value={card.cvc} onChange={(e) => setCard({ ...card, cvc: e.target.value.replace(/\D/g, '').slice(0, 4) })} />
</label>
</div>
{declined && <p className="flck-declined" role="alert">
Card declined — try another card or contact your bank.</p>}
<div className="flck-nav">
<button type="button" onClick={() => setStep(1)}>Back</button>
<button type="button" className="flck-go" onClick={pay}>
{declined ? 'Try again' : `Pay €${total}`}
</button>
</div>
</section>
)}
{step === 3 && (
<section className="flck-pane flck-done">
<h3>Order confirmed</h3>
<p>Order <b>{order}</b> ships to {form.city} — arriving in about 5 days.</p>
</section>
)}
</div>
<aside className="flck-aside" aria-label="Order summary">
{items.map((i) => <div key={i.id}><span>{i.qty} × {i.name}</span><span>€{i.price * i.qty}</span></div>)}
<div><span>Shipping</span><span>{shipping === 0 ? 'Free' : `€${shipping}`}</span></div>
<div><span>VAT (19%)</span><span>€{vat}</span></div>
<div className="flck-total"><span>Total</span><span>€{total}</span></div>
</aside>
</div>
</>
);
}
const FLCK_CSS = `
.flck { display: grid; grid-template-columns: minmax(0, 1fr) 15rem; gap: 1.5rem; align-items: start;
max-inline-size: 46rem; color: #191b2a; }
.flck-pane { display: grid; gap: 1rem; padding: 1.5rem; border: 1px solid #e4e6ef;
border-radius: 1.25rem; background: #fff; }
.flck-aside { position: sticky; inset-block-start: 1rem; display: grid; gap: .6rem; padding: 1.25rem;
border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #f5f6fb; font-size: .85rem; }
.flck-aside div { display: flex; justify-content: space-between; gap: 1rem; }
.flck-total { border-block-start: 2px solid #4f46e5; padding-block-start: .6rem; font-weight: 700; }
.flck-field { display: grid; gap: .3rem; font-size: .8rem; }
.flck-field input { padding: .6rem .75rem; border: 1px solid #e4e6ef; border-radius: .6rem; font: inherit; }
.flck-field input[data-invalid] { border-color: #c62a2f; box-shadow: inset 0 0 0 1px #c62a2f; }
.flck-error { color: #c62a2f; font-size: .75rem; }
.flck-declined { margin: 0; padding: .7rem .9rem; border-radius: .7rem; background: #fdecec;
border: 1px solid #f5b9bb; color: #a1191e; font-size: .82rem; }
.flck-nav { display: flex; justify-content: space-between; gap: .75rem; }
.flck-go { margin-inline-start: auto; padding: .65rem 1.2rem; border: none; border-radius: 999px;
background: #4f46e5; color: #fff; cursor: pointer; }
`;
<!-- CheckoutFlow.vue — cart → validated address → formatted card → order number -->
<script setup lang="ts">
import { computed, reactive, ref } from 'vue';
import '@nabuxai/ui-core/css';
const initial = () => ([
{ id: 'lamp', name: 'Aster desk lamp', price: 89, qty: 1 },
{ id: 'keys', name: 'Nimbus Keys K2 keyboard', price: 149, qty: 1 },
{ id: 'stand', name: 'Vela laptop stand', price: 59, qty: 2 },
]);
const DECLINED = '4000000000000002';
let items = ref(initial());
const step = ref(0);
const form = reactive({ name: '', street: '', city: '', postcode: '' });
const errors = reactive<Record<string, string>>({});
const card = reactive({ number: '', exp: '', cvc: '' });
const declined = ref(false);
const order = ref<string | null>(null);
const subtotal = computed(() => items.value.reduce((s, i) => s + i.price * i.qty, 0));
const shipping = computed(() => (subtotal.value >= 150 ? 0 : 12));
const vat = computed(() => Math.round(subtotal.value * 0.19));
const total = computed(() => subtotal.value + shipping.value + vat.value);
// Masks that run on every keystroke: fours for the number, MM/YY for the expiry.
const maskNumber = (v: string) => v.replace(/\D/g, '').slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
const maskExp = (v: string) => {
const d = v.replace(/\D/g, '').slice(0, 4);
return d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;
};
const bump = (id: string, d: number) => {
items.value = items.value.map((x) => (x.id === id ? { ...x, qty: Math.max(1, x.qty + d) } : x));
};
const validate = () => {
Object.keys(errors).forEach((k) => delete errors[k]);
if (form.name.trim().length < 2) errors.name = 'Full name is required';
if (form.street.trim().length < 3) errors.street = 'Street address is incomplete';
if (form.city.trim().length < 2) errors.city = 'City is required';
if (!/^[a-z0-9-]{3,10}$/i.test(form.postcode.trim())) errors.postcode = 'Postcode looks off';
return Object.keys(errors).length === 0;
};
const pay = () => {
if (card.number.replace(/\s/g, '') === DECLINED) {
declined.value = true; // the decline banner with its retry
} else {
order.value = 'NBX-' + Math.floor(10000 + Math.random() * 90000);
step.value = 3;
}
};
</script>
<template>
<div class="flck">
<div class="flck-main">
<section v-if="step === 0" class="flck-pane">
<h3>Your cart</h3>
<div v-for="i in items" :key="i.id" class="flck-row">
<b>{{ i.name }}</b>
<span class="flck-qty">
<button type="button" @click="bump(i.id, -1)">−</button>{{ i.qty }}<button type="button" @click="bump(i.id, 1)">+</button>
</span>
<span>€{{ i.price * i.qty }}</span>
</div>
<button type="button" class="flck-go" @click="step = 1">Continue to address</button>
</section>
<section v-else-if="step === 1" class="flck-pane">
<h3>Shipping address</h3>
<label v-for="f in ['name', 'street', 'city', 'postcode']" :key="f" class="flck-field">
<span>{{ f[0].toUpperCase() + f.slice(1) }}</span>
<input v-model="form[f]" :data-invalid="errors[f] || undefined" />
<small v-if="errors[f]" class="flck-error">{{ errors[f] }}</small>
</label>
<div class="flck-nav">
<button type="button" @click="step = 0">Back</button>
<button type="button" class="flck-go" @click="validate() && (step = 2)">Continue to payment</button>
</div>
</section>
<section v-else-if="step === 2" class="flck-pane">
<h3>Payment</h3>
<label class="flck-field"><span>Card number</span>
<input v-model="card.number" dir="ltr" inputmode="numeric" placeholder="4000 0000 0000 0002"
@input="card.number = maskNumber(card.number)" />
</label>
<div class="flck-duo">
<label class="flck-field"><span>Expiry</span>
<input v-model="card.exp" dir="ltr" inputmode="numeric" placeholder="MM/YY"
@input="card.exp = maskExp(card.exp)" />
</label>
<label class="flck-field"><span>CVC</span>
<input v-model="card.cvc" dir="ltr" inputmode="numeric" placeholder="123"
@input="card.cvc = card.cvc.replace(/\D/g, '').slice(0, 4)" />
</label>
</div>
<p v-if="declined" class="flck-declined" role="alert">
Card declined — try another card or contact your bank.</p>
<div class="flck-nav">
<button type="button" @click="step = 1">Back</button>
<button type="button" class="flck-go" @click="pay">
{{ declined ? 'Try again' : `Pay €${total}` }}
</button>
</div>
</section>
<section v-else class="flck-pane flck-done">
<h3>Order confirmed</h3>
<p>Order <b>{{ order }}</b> ships to {{ form.city }} — arriving in about 5 days.</p>
</section>
</div>
<aside class="flck-aside" aria-label="Order summary">
<div v-for="i in items" :key="i.id"><span>{{ i.qty }} × {{ i.name }}</span><span>€{{ i.price * i.qty }}</span></div>
<div><span>Shipping</span><span>{{ shipping === 0 ? 'Free' : `€${shipping}` }}</span></div>
<div><span>VAT (19%)</span><span>€{{ vat }}</span></div>
<div class="flck-total"><span>Total</span><span>€{{ total }}</span></div>
</aside>
</div>
</template>
<style scoped>
.flck { display: grid; grid-template-columns: minmax(0, 1fr) 15rem; gap: 1.5rem; align-items: start;
max-inline-size: 46rem; color: #191b2a; }
.flck-pane { display: grid; gap: 1rem; padding: 1.5rem; border: 1px solid #e4e6ef;
border-radius: 1.25rem; background: #fff; }
.flck-aside { position: sticky; inset-block-start: 1rem; display: grid; gap: .6rem; padding: 1.25rem;
border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #f5f6fb; font-size: .85rem; }
.flck-aside div { display: flex; justify-content: space-between; gap: 1rem; }
.flck-total { border-block-start: 2px solid #4f46e5; padding-block-start: .6rem; font-weight: 700; }
.flck-field { display: grid; gap: .3rem; font-size: .8rem; }
.flck-field input { padding: .6rem .75rem; border: 1px solid #e4e6ef; border-radius: .6rem; font: inherit; }
.flck-field input[data-invalid] { border-color: #c62a2f; box-shadow: inset 0 0 0 1px #c62a2f; }
.flck-error { color: #c62a2f; font-size: .75rem; }
.flck-declined { margin: 0; padding: .7rem .9rem; border-radius: .7rem; background: #fdecec;
border: 1px solid #f5b9bb; color: #a1191e; font-size: .82rem; }
.flck-nav { display: flex; justify-content: space-between; gap: .75rem; }
.flck-go { margin-inline-start: auto; padding: .65rem 1.2rem; border: none; border-radius: 999px;
background: #4f46e5; color: #fff; cursor: pointer; }
</style>
<!-- CheckoutFlow.svelte — cart → validated address → formatted card → order number -->
<script lang="ts">
import '@nabuxai/ui-core/css';
type Item = { id: string; name: string; price: number; qty: number };
const initial = (): Item[] => [
{ id: 'lamp', name: 'Aster desk lamp', price: 89, qty: 1 },
{ id: 'keys', name: 'Nimbus Keys K2 keyboard', price: 149, qty: 1 },
{ id: 'stand', name: 'Vela laptop stand', price: 59, qty: 2 },
];
const DECLINED = '4000000000000002';
let items = $state<Item[]>(initial());
let step = $state(0);
let form = $state({ name: '', street: '', city: '', postcode: '' });
let errors = $state<Record<string, string>>({});
let card = $state({ number: '', exp: '', cvc: '' });
let declined = $state(false);
let order = $state<string | null>(null);
const subtotal = $derived(items.reduce((s, i) => s + i.price * i.qty, 0));
const shipping = $derived(subtotal >= 150 ? 0 : 12);
const vat = $derived(Math.round(subtotal * 0.19));
const total = $derived(subtotal + shipping + vat);
// Masks that run on every keystroke: fours for the number, MM/YY for the expiry.
const maskNumber = (v: string) => v.replace(/\D/g, '').slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
const maskExp = (v: string) => {
const d = v.replace(/\D/g, '').slice(0, 4);
return d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;
};
const bump = (id: string, d: number) => {
items = items.map((x) => (x.id === id ? { ...x, qty: Math.max(1, x.qty + d) } : x));
};
const validate = () => {
errors = {};
if (form.name.trim().length < 2) errors.name = 'Full name is required';
if (form.street.trim().length < 3) errors.street = 'Street address is incomplete';
if (form.city.trim().length < 2) errors.city = 'City is required';
if (!/^[a-z0-9-]{3,10}$/i.test(form.postcode.trim())) errors.postcode = 'Postcode looks off';
return Object.keys(errors).length === 0;
};
const pay = () => {
if (card.number.replace(/\s/g, '') === DECLINED) {
declined = true; // the decline banner with its retry
} else {
order = 'NBX-' + Math.floor(10000 + Math.random() * 90000);
step = 3;
}
};
</script>
<div class="flck">
<div class="flck-main">
{#if step === 0}
<section class="flck-pane">
<h3>Your cart</h3>
{#each items as i (i.id)}
<div class="flck-row">
<b>{i.name}</b>
<span class="flck-qty">
<button type="button" onclick={() => bump(i.id, -1)}>−</button>{i.qty}<button type="button" onclick={() => bump(i.id, 1)}>+</button>
</span>
<span>€{i.price * i.qty}</span>
</div>
{/each}
<button type="button" class="flck-go" onclick={() => (step = 1)}>Continue to address</button>
</section>
{:else if step === 1}
<section class="flck-pane">
<h3>Shipping address</h3>
{#each ['name', 'street', 'city', 'postcode'] as f (f)}
<label class="flck-field">
<span>{f[0].toUpperCase() + f.slice(1)}</span>
<input bind:value={form[f]} data-invalid={errors[f] || undefined} />
{#if errors[f]}<small class="flck-error">{errors[f]}</small>{/if}
</label>
{/each}
<div class="flck-nav">
<button type="button" onclick={() => (step = 0)}>Back</button>
<button type="button" class="flck-go" onclick={() => validate() && (step = 2)}>Continue to payment</button>
</div>
</section>
{:else if step === 2}
<section class="flck-pane">
<h3>Payment</h3>
<label class="flck-field"><span>Card number</span>
<input dir="ltr" inputmode="numeric" placeholder="4000 0000 0000 0002" bind:value={card.number}
oninput={() => (card.number = maskNumber(card.number))} />
</label>
<div class="flck-duo">
<label class="flck-field"><span>Expiry</span>
<input dir="ltr" inputmode="numeric" placeholder="MM/YY" bind:value={card.exp}
oninput={() => (card.exp = maskExp(card.exp))} />
</label>
<label class="flck-field"><span>CVC</span>
<input dir="ltr" inputmode="numeric" placeholder="123" bind:value={card.cvc}
oninput={() => (card.cvc = card.cvc.replace(/\D/g, '').slice(0, 4))} />
</label>
</div>
{#if declined}
<p class="flck-declined" role="alert">Card declined — try another card or contact your bank.</p>
{/if}
<div class="flck-nav">
<button type="button" onclick={() => (step = 1)}>Back</button>
<button type="button" class="flck-go" onclick={pay}>
{declined ? 'Try again' : `Pay €${total}`}
</button>
</div>
</section>
{:else}
<section class="flck-pane flck-done">
<h3>Order confirmed</h3>
<p>Order <b>{order}</b> ships to {form.city} — arriving in about 5 days.</p>
</section>
{/if}
</div>
<aside class="flck-aside" aria-label="Order summary">
{#each items as i (i.id)}
<div><span>{i.qty} × {i.name}</span><span>€{i.price * i.qty}</span></div>
{/each}
<div><span>Shipping</span><span>{shipping === 0 ? 'Free' : `€${shipping}`}</span></div>
<div><span>VAT (19%)</span><span>€{vat}</span></div>
<div class="flck-total"><span>Total</span><span>€{total}</span></div>
</aside>
</div>
<style>
.flck { display: grid; grid-template-columns: minmax(0, 1fr) 15rem; gap: 1.5rem; align-items: start;
max-inline-size: 46rem; color: #191b2a; }
.flck-pane { display: grid; gap: 1rem; padding: 1.5rem; border: 1px solid #e4e6ef;
border-radius: 1.25rem; background: #fff; }
.flck-aside { position: sticky; inset-block-start: 1rem; display: grid; gap: .6rem; padding: 1.25rem;
border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #f5f6fb; font-size: .85rem; }
.flck-aside div { display: flex; justify-content: space-between; gap: 1rem; }
.flck-total { border-block-start: 2px solid #4f46e5; padding-block-start: .6rem; font-weight: 700; }
.flck-field { display: grid; gap: .3rem; font-size: .8rem; }
.flck-field input { padding: .6rem .75rem; border: 1px solid #e4e6ef; border-radius: .6rem; font: inherit; }
.flck-field input[data-invalid] { border-color: #c62a2f; box-shadow: inset 0 0 0 1px #c62a2f; }
.flck-error { color: #c62a2f; font-size: .75rem; }
.flck-declined { margin: 0; padding: .7rem .9rem; border-radius: .7rem; background: #fdecec;
border: 1px solid #f5b9bb; color: #a1191e; font-size: .82rem; }
.flck-nav { display: flex; justify-content: space-between; gap: .75rem; }
.flck-go { margin-inline-start: auto; padding: .65rem 1.2rem; border: none; border-radius: 999px;
background: #4f46e5; color: #fff; cursor: pointer; }
</style>