Real flows JS behaviour

Auth flow

A real two-step sign-in: email and password, then a six-digit code whose boxes focus themselves, accept a whole paste at once and give Backspace back to the previous box; the resend timer counts, a wrong code shakes the frame, and the two-step forgot-password branch sits beside it.

Documentation

Real scenarios

The component as it is used in a product — not a specimen grid.

Two steps, six boxes, one honest timer

The sign-in hands you a six-digit code: every box focuses the next as you type, a pasted code fills all six at once, Backspace walks backwards, the wrong code shakes the frame and clears it, and the resend button sleeps for a real 30-second countdown. Forgot password branches off into its own two steps.

Northwind

Welcome back

Sign in and we’ll double-check it’s you with a code.

Northwind

Enter your code

Sent to — type it, or paste all six at once.

demo code: 284913
resend in s

Welcome back, Maya

Device trusted — next time we may skip the code.

Reset your password

Step 1 of 2 — tell us the account email and we’ll send a reset link.

<b>Step 2 of 2 — link sent.</b> It lives for 20 minutes; opening it on this device signs you straight back in.

No backend here: the code 284913 succeeds, anything else shakes; the countdown is a real interval that tears itself down with the component.

Important props

Important props
Prop Type Default What it does
steps count '2' sign-in → six-digit code → welcome; the “forgot password” link opens its own two-step path (email → sent).
boxes behaviour 'auto-advance' Each box fills with one digit and focuses the next; pasting the whole code fills every box at once, and Backspace on an empty box steps back.
resend timer '30s' The resend countdown; the button sleeps until zero and the timer clears itself when the component dies.
error animation 'shake · 380ms' A wrong code shakes the code frame, empties and reddens the boxes and parks a message beneath — no shake under prefers-reduced-motion.

Snippet

<div class="flau" x-data="{
    stage: 'login', code: ['', '', '', '', '', ''], error: '', timer: 30, tick: null,
    digits() { return this.code.join('') },
    type(i, e) {
        const d = e.target.value.replace(/\D/g, '').slice(-1);
        this.code[i] = d; e.target.value = d;
        if (d && i < 5) this.$refs.box[i + 1].focus();
    },
    back(i, e) { if (!e.target.value && i > 0) this.$refs.box[i - 1].focus(); },
    paste(e) {
        const d = (e.clipboardData.getData('text') || '').replace(/\D/g, '').slice(0, 6);
        this.code = d.padEnd(6, '').split('').slice(0, 6);
    },
    verify() {
        if (this.digits() !== '284913') {
            this.error = 'That code is not right — try again'; this.code = ['', '', '', '', '', ''];
            this.$refs.box[0].focus(); return;
        }
        this.stage = 'ok';
    },
}">
    <template x-if="stage === 'login'">
        <form @submit.prevent="stage = 'code'">
            <input type="email" placeholder="maya@nordvana.io" required>
            <input type="password" placeholder="••••••••" required>
            <button>Continue with code</button>
            <a href="#" @click.prevent="stage = 'forgot'">Forgot password?</a>
        </form>
    </template>

    <div class="flau-boxes" x-show="stage === 'code'" x-cloak :class="error && 'flau-shake'">
        <template x-for="(d, i) in code" :key="i">
            <input maxlength="1" inputmode="numeric" dir="ltr" :value="d"
                @input="type(i, $event)" @keydown.backspace="back(i, $event)" @paste="paste">
        </template>
    </div>
</div>

<style>
.flau-boxes { display: flex; gap: .5rem; }
.flau-boxes input { inline-size: 2.75rem; block-size: 3.25rem; text-align: center;
                    border: 1px solid #e4e6ef; border-radius: .8rem; font-size: 1.25rem; }
.flau-boxes input:focus { outline: 2px solid #4f46e5; outline-offset: 1px; }
.flau-shake { animation: flau-shake .38s; }
@keyframes flau-shake { 20%, 60% { translate: -7px 0; } 40%, 80% { translate: 7px 0; } }
@media (prefers-reduced-motion: reduce) { .flau-shake { animation: none; } }
</style>
// app/Pages/SignIn.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 AuthFlow from '@/components/AuthFlow';

export default function SignIn() {
  return (
    <NabuXUIProvider locale="en" linkComponent={Link}>
      <main className="nx-page">
        <AuthFlow />
      </main>
    </NabuXUIProvider>
  );
}
// AuthFlow.jsx — sign-in → six-digit code → welcome, plus the forgot branch
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';

const CODE = '284913';

export default function AuthFlow() {
  const [stage, setStage] = useState('login'); // login | code | ok | forgot-email | forgot-sent
  const [code, setCode] = useState(Array(6).fill(''));
  const [error, setError] = useState('');
  const [timer, setTimer] = useState(30);
  const boxes = useRef([]);

  // The resend countdown: one interval, cleaned on unmount (and on resend).
  useEffect(() => {
    if (stage !== 'code' || timer <= 0) return;
    const t = setTimeout(() => setTimer((s) => s - 1), 1000);
    return () => clearTimeout(t);
  }, [stage, timer]);

  // Auto-focus walks with the digits; pasting the whole code fills every box.
  const type = (i, e) => {
    const d = e.target.value.replace(/\D/g, '').slice(-1);
    setCode(code.map((c, j) => (j === i ? d : c)));
    if (d && i < 5) boxes.current[i + 1]?.focus();
  };
  const back = (i, e) => { if (!e.target.value && i > 0) boxes.current[i - 1]?.focus(); };
  const paste = (e) => {
    const d = (e.clipboardData.getData('text') || '').replace(/\D/g, '').slice(0, 6);
    if (!d) return;
    e.preventDefault();
    setCode(d.padEnd(6, '').slice(0, 6).split(''));
    boxes.current[Math.min(5, d.length)]?.focus();
  };

  const verify = () => {
    if (code.join('') !== CODE) {
      setError('That code is not right — try again');
      setCode(Array(6).fill(''));
      boxes.current[0]?.focus();
      return;
    }
    setError('');
    setStage('ok');
  };

  return (
    <>
      <style>{FLAU_CSS}</style>
      <div className="flau">
        {stage === 'login' && (
          <form className="flau-pane" onSubmit={(e) => { e.preventDefault(); setStage('code'); }}>
            <h3>Sign in to Northwind</h3>
            <label className="flau-field"><span>Email</span>
              <input type="email" placeholder="maya@nordvana.io" required /></label>
            <label className="flau-field"><span>Password</span>
              <input type="password" placeholder="••••••••" required /></label>
            <button type="submit" className="flau-go">Continue with code</button>
            <button type="button" className="flau-link"
              onClick={() => setStage('forgot-email')}>Forgot password?</button>
          </form>
        )}

        {stage === 'code' && (
          <section className="flau-pane">
            <h3>Check your inbox</h3>
            <p>We sent a six-digit code to your email. Demo code: <b>{CODE}</b>.</p>
            <div className={'flau-boxes' + (error ? ' flau-shake' : '')}>
              {code.map((d, i) => (
                <input key={i} ref={(el) => (boxes.current[i] = el)} maxLength={1} inputMode="numeric"
                  dir="ltr" value={d} onChange={(e) => type(i, e)}
                  onKeyDown={(e) => e.key === 'Backspace' && back(i, e)} onPaste={paste} />
              ))}
            </div>
            {error && <p className="flau-error" role="alert">{error}</p>}
            <button type="button" className="flau-go" onClick={verify}>Verify</button>
            <button type="button" className="flau-link" disabled={timer > 0}
              onClick={() => { setTimer(30); setError(''); }}>
              {timer > 0 ? `Resend in ${timer}s` : 'Resend code'}
            </button>
          </section>
        )}

        {stage === 'ok' && (
          <section className="flau-pane flau-ok">
            <span className="flau-check" aria-hidden="true">✓</span>
            <h3>Welcome back, Maya</h3>
            <button type="button" className="flau-go"
              onClick={() => { setStage('login'); setCode(Array(6).fill('')); setTimer(30); }}>
              Sign out
            </button>
          </section>
        )}

        {(stage === 'forgot-email' || stage === 'forgot-sent') && (
          <section className="flau-pane">
            <h3>Reset your password</h3>
            {stage === 'forgot-email' ? (
              <>
                <p>Step 1 of 2 — tell us the account email.</p>
                <form onSubmit={(e) => { e.preventDefault(); setStage('forgot-sent'); }}>
                  <label className="flau-field"><span>Email</span>
                    <input type="email" placeholder="maya@nordvana.io" required /></label>
                  <button type="submit" className="flau-go">Send reset link</button>
                </form>
              </>
            ) : (
              <>
                <p>Step 2 of 2 — the reset link is on its way. It lives for 20 minutes.</p>
                <button type="button" className="flau-go" onClick={() => setStage('login')}>
                  Back to sign in
                </button>
              </>
            )}
          </section>
        )}
      </div>
    </>
  );
}

const FLAU_CSS = `
  .flau { display: grid; justify-items: center; }
  .flau-pane { display: grid; gap: .9rem; inline-size: min(100%, 22rem); padding: 1.5rem;
               border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #fff; color: #191b2a; }
  .flau-field { display: grid; gap: .3rem; font-size: .8rem; }
  .flau-field input { min-block-size: 2.6rem; padding-inline: .8rem; border: 1px solid #e4e6ef;
                      border-radius: .7rem; font: inherit; }
  .flau-boxes { display: flex; gap: .5rem; justify-content: center; }
  .flau-boxes input { inline-size: 2.75rem; block-size: 3.25rem; text-align: center;
                      border: 1px solid #e4e6ef; border-radius: .8rem; font-size: 1.25rem; }
  .flau-boxes input:focus { outline: 2px solid #4f46e5; outline-offset: 1px; }
  .flau-error { margin: 0; color: #c62a2f; font-size: .8rem; text-align: center; }
  .flau-shake { animation: flau-shake .38s; }
  @keyframes flau-shake { 20%, 60% { translate: -7px 0; } 40%, 80% { translate: 7px 0; } }
  .flau-go { min-block-size: 2.7rem; border: none; border-radius: 999px; background: #4f46e5;
             color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
  .flau-link { border: none; background: none; color: #4f46e5; cursor: pointer; font-size: .82rem; }
  .flau-link:disabled { color: #8a8fa3; cursor: default; }
  .flau-ok { justify-items: center; text-align: center; }
  .flau-check { display: grid; place-items: center; inline-size: 3.5rem; aspect-ratio: 1; border-radius: 50%;
                background: #e7f6ec; color: #0f8a43; font-size: 1.5rem; }
  @media (prefers-reduced-motion: reduce) { .flau-shake { animation: none; } }
`;
<!-- AuthFlow.vue — sign-in → six-digit code → welcome, plus the forgot branch -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
import '@nabuxai/ui-core/css';

const CODE = '284913';
const stage = ref<'login' | 'code' | 'ok' | 'forgot-email' | 'forgot-sent'>('login');
const code = ref<string[]>(Array(6).fill(''));
const error = ref('');
const timer = ref(30);
let tick: number | undefined;
const boxes = ref<HTMLInputElement[]>([]);

// The resend countdown: one interval, cleared on unmount and on resend.
onMounted(() => {
  tick = window.setInterval(() => {
    if (stage.value === 'code' && timer.value > 0) timer.value--;
  }, 1000);
});
onBeforeUnmount(() => window.clearInterval(tick));

const type = (i: number, e: Event) => {
  const el = e.target as HTMLInputElement;
  const d = el.value.replace(/\D/g, '').slice(-1);
  code.value = code.value.map((c, j) => (j === i ? d : c));
  el.value = d;
  if (d && i < 5) boxes.value[i + 1]?.focus();
};
const back = (i: number, e: KeyboardEvent) => {
  if (!(e.target as HTMLInputElement).value && i > 0) boxes.value[i - 1]?.focus();
};
const paste = (e: ClipboardEvent) => {
  const d = (e.clipboardData?.getData('text') || '').replace(/\D/g, '').slice(0, 6);
  if (!d) return;
  e.preventDefault();
  code.value = d.padEnd(6, '').slice(0, 6).split('');
  boxes.value[Math.min(5, d.length)]?.focus();
};

const verify = () => {
  if (code.value.join('') !== CODE) {
    error.value = 'That code is not right — try again';
    code.value = Array(6).fill('');
    boxes.value[0]?.focus();
    return;
  }
  error.value = '';
  stage.value = 'ok';
};
const resend = () => { timer.value = 30; error.value = ''; };
</script>

<template>
  <div class="flau">
    <form v-if="stage === 'login'" class="flau-pane" @submit.prevent="stage = 'code'">
      <h3>Sign in to Northwind</h3>
      <label class="flau-field"><span>Email</span>
        <input type="email" placeholder="maya@nordvana.io" required /></label>
      <label class="flau-field"><span>Password</span>
        <input type="password" placeholder="••••••••" required /></label>
      <button type="submit" class="flau-go">Continue with code</button>
      <button type="button" class="flau-link" @click="stage = 'forgot-email'">Forgot password?</button>
    </form>

    <section v-else-if="stage === 'code'" class="flau-pane">
      <h3>Check your inbox</h3>
      <p>We sent a six-digit code to your email. Demo code: <b>{{ CODE }}</b>.</p>
      <!-- Auto-focus walks with the digits; pasting fills every box at once. -->
      <div class="flau-boxes" :class="{ 'flau-shake': error }">
        <input v-for="(d, i) in code" :key="i" :ref="(el) => (boxes[i] = el as HTMLInputElement)"
          maxlength="1" inputmode="numeric" dir="ltr" :value="d"
          @input="type(i, $event)" @keydown.backspace="back(i, $event)" @paste="paste" />
      </div>
      <p v-if="error" class="flau-error" role="alert">{{ error }}</p>
      <button type="button" class="flau-go" @click="verify">Verify</button>
      <button type="button" class="flau-link" :disabled="timer > 0" @click="resend">
        {{ timer > 0 ? `Resend in ${timer}s` : 'Resend code' }}
      </button>
    </section>

    <section v-else-if="stage === 'ok'" class="flau-pane flau-ok">
      <span class="flau-check" aria-hidden="true">✓</span>
      <h3>Welcome back, Maya</h3>
      <button type="button" class="flau-go"
        @click="stage = 'login'; code = Array(6).fill(''); timer = 30">Sign out</button>
    </section>

    <section v-else class="flau-pane">
      <h3>Reset your password</h3>
      <template v-if="stage === 'forgot-email'">
        <p>Step 1 of 2 — tell us the account email.</p>
        <form @submit.prevent="stage = 'forgot-sent'">
          <label class="flau-field"><span>Email</span>
            <input type="email" placeholder="maya@nordvana.io" required /></label>
          <button type="submit" class="flau-go">Send reset link</button>
        </form>
      </template>
      <template v-else>
        <p>Step 2 of 2 — the reset link is on its way. It lives for 20 minutes.</p>
        <button type="button" class="flau-go" @click="stage = 'login'">Back to sign in</button>
      </template>
    </section>
  </div>
</template>

<style scoped>
.flau { display: grid; justify-items: center; }
.flau-pane { display: grid; gap: .9rem; inline-size: min(100%, 22rem); padding: 1.5rem;
             border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #fff; color: #191b2a; }
.flau-field { display: grid; gap: .3rem; font-size: .8rem; }
.flau-field input { min-block-size: 2.6rem; padding-inline: .8rem; border: 1px solid #e4e6ef;
                    border-radius: .7rem; font: inherit; }
.flau-boxes { display: flex; gap: .5rem; justify-content: center; }
.flau-boxes input { inline-size: 2.75rem; block-size: 3.25rem; text-align: center;
                    border: 1px solid #e4e6ef; border-radius: .8rem; font-size: 1.25rem; }
.flau-boxes input:focus { outline: 2px solid #4f46e5; outline-offset: 1px; }
.flau-error { margin: 0; color: #c62a2f; font-size: .8rem; text-align: center; }
.flau-shake { animation: flau-shake .38s; }
@keyframes flau-shake { 20%, 60% { translate: -7px 0; } 40%, 80% { translate: 7px 0; } }
.flau-go { min-block-size: 2.7rem; border: none; border-radius: 999px; background: #4f46e5;
           color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
.flau-link { border: none; background: none; color: #4f46e5; cursor: pointer; font-size: .82rem; }
.flau-link:disabled { color: #8a8fa3; cursor: default; }
.flau-ok { justify-items: center; text-align: center; }
.flau-check { display: grid; place-items: center; inline-size: 3.5rem; aspect-ratio: 1; border-radius: 50%;
              background: #e7f6ec; color: #0f8a43; font-size: 1.5rem; }
@media (prefers-reduced-motion: reduce) { .flau-shake { animation: none; } }
</style>
<!-- AuthFlow.svelte — sign-in → six-digit code → welcome, plus the forgot branch -->
<script lang="ts">
  import { onMount } from 'svelte';
  import '@nabuxai/ui-core/css';

  const CODE = '284913';
  let stage = $state<'login' | 'code' | 'ok' | 'forgot-email' | 'forgot-sent'>('login');
  let code = $state<string[]>(Array(6).fill(''));
  let error = $state('');
  let timer = $state(30);
  let boxes: HTMLInputElement[] = $state([]);

  // The resend countdown: one interval, cleared on unmount and on resend.
  onMount(() => {
    const tick = window.setInterval(() => {
      if (stage === 'code' && timer > 0) timer--;
    }, 1000);
    return () => window.clearInterval(tick);
  });

  // Auto-focus walks with the digits; pasting the whole code fills every box.
  const type = (i: number, e: Event) => {
    const el = e.target as HTMLInputElement;
    const d = el.value.replace(/\D/g, '').slice(-1);
    code = code.map((c, j) => (j === i ? d : c));
    el.value = d;
    if (d && i < 5) boxes[i + 1]?.focus();
  };
  const back = (i: number, e: KeyboardEvent) => {
    if (!(e.target as HTMLInputElement).value && i > 0) boxes[i - 1]?.focus();
  };
  const paste = (e: ClipboardEvent) => {
    const d = (e.clipboardData?.getData('text') || '').replace(/\D/g, '').slice(0, 6);
    if (!d) return;
    e.preventDefault();
    code = d.padEnd(6, '').slice(0, 6).split('');
    boxes[Math.min(5, d.length)]?.focus();
  };

  const verify = () => {
    if (code.join('') !== CODE) {
      error = 'That code is not right — try again';
      code = Array(6).fill('');
      boxes[0]?.focus();
      return;
    }
    error = '';
    stage = 'ok';
  };
  const resend = () => { timer = 30; error = ''; };
</script>

<div class="flau">
  {#if stage === 'login'}
    <form class="flau-pane" onsubmit={(e) => { e.preventDefault(); stage = 'code'; }}>
      <h3>Sign in to Northwind</h3>
      <label class="flau-field"><span>Email</span>
        <input type="email" placeholder="maya@nordvana.io" required /></label>
      <label class="flau-field"><span>Password</span>
        <input type="password" placeholder="••••••••" required /></label>
      <button type="submit" class="flau-go">Continue with code</button>
      <button type="button" class="flau-link" onclick={() => (stage = 'forgot-email')}>Forgot password?</button>
    </form>
  {:else if stage === 'code'}
    <section class="flau-pane">
      <h3>Check your inbox</h3>
      <p>We sent a six-digit code to your email. Demo code: <b>{CODE}</b>.</p>
      <div class="flau-boxes" class:flau-shake={!!error}>
        {#each code as d, i (i)}
          <input bind:this={boxes[i]} maxlength={1} inputmode="numeric" dir="ltr" value={d}
            oninput={(e) => type(i, e)} onkeydown={(e) => e.key === 'Backspace' && back(i, e)}
            onpaste={paste} />
        {/each}
      </div>
      {#if error}<p class="flau-error" role="alert">{error}</p>{/if}
      <button type="button" class="flau-go" onclick={verify}>Verify</button>
      <button type="button" class="flau-link" disabled={timer > 0} onclick={resend}>
        {timer > 0 ? `Resend in ${timer}s` : 'Resend code'}
      </button>
    </section>
  {:else if stage === 'ok'}
    <section class="flau-pane flau-ok">
      <span class="flau-check" aria-hidden="true">✓</span>
      <h3>Welcome back, Maya</h3>
      <button type="button" class="flau-go"
        onclick={() => { stage = 'login'; code = Array(6).fill(''); timer = 30; }}>Sign out</button>
    </section>
  {:else}
    <section class="flau-pane">
      <h3>Reset your password</h3>
      {#if stage === 'forgot-email'}
        <p>Step 1 of 2 — tell us the account email.</p>
        <form onsubmit={(e) => { e.preventDefault(); stage = 'forgot-sent'; }}>
          <label class="flau-field"><span>Email</span>
            <input type="email" placeholder="maya@nordvana.io" required /></label>
          <button type="submit" class="flau-go">Send reset link</button>
        </form>
      {:else}
        <p>Step 2 of 2 — the reset link is on its way. It lives for 20 minutes.</p>
        <button type="button" class="flau-go" onclick={() => (stage = 'login')}>Back to sign in</button>
      {/if}
    </section>
  {/if}
</div>

<style>
  .flau { display: grid; justify-items: center; }
  .flau-pane { display: grid; gap: .9rem; inline-size: min(100%, 22rem); padding: 1.5rem;
               border: 1px solid #e4e6ef; border-radius: 1.25rem; background: #fff; color: #191b2a; }
  .flau-field { display: grid; gap: .3rem; font-size: .8rem; }
  .flau-field input { min-block-size: 2.6rem; padding-inline: .8rem; border: 1px solid #e4e6ef;
                      border-radius: .7rem; font: inherit; }
  .flau-boxes { display: flex; gap: .5rem; justify-content: center; }
  .flau-boxes input { inline-size: 2.75rem; block-size: 3.25rem; text-align: center;
                      border: 1px solid #e4e6ef; border-radius: .8rem; font-size: 1.25rem; }
  .flau-boxes input:focus { outline: 2px solid #4f46e5; outline-offset: 1px; }
  .flau-error { margin: 0; color: #c62a2f; font-size: .8rem; text-align: center; }
  .flau-shake { animation: flau-shake .38s; }
  @keyframes flau-shake { 20%, 60% { translate: -7px 0; } 40%, 80% { translate: 7px 0; } }
  .flau-go { min-block-size: 2.7rem; border: none; border-radius: 999px; background: #4f46e5;
             color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
  .flau-link { border: none; background: none; color: #4f46e5; cursor: pointer; font-size: .82rem; }
  .flau-link:disabled { color: #8a8fa3; cursor: default; }
  .flau-ok { justify-items: center; text-align: center; }
  .flau-check { display: grid; place-items: center; inline-size: 3.5rem; aspect-ratio: 1; border-radius: 50%;
                background: #e7f6ec; color: #0f8a43; font-size: 1.5rem; }
  @media (prefers-reduced-motion: reduce) { .flau-shake { animation: none; } }
</style>