A PIN login for shift workers, drawn as a time clock

Build a PIN sign-in for shared tablets in React: a real password field with large keys, a time clock drawn in code, and a card that drops in and gets stamped with the real time.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

Shift workers sign in on a shared tablet by the door. So the sign-in is the time clock: pick your name, key in your PIN, and your card is stamped.

A real field under big keys

The keypad's large keys type into a real password input. Keyboards, password managers and screen readers all see a normal field; touch users see buttons sized for a thumb.

src/registry/login/time-clock/pin-pad.tsx

"use client";

import { Delete } from "lucide-react";
import { cn } from "@/lib/utils";

export const PIN_LENGTH = 4;
const DIGITS = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
const KEY =
  "grid h-14 place-items-center rounded-lg bg-white/10 text-xl font-medium transition-colors duration-150 hover:bg-white/15 active:bg-white/25 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white disabled:opacity-50";

interface PinPadProps {
  id: string;
  label: string;
  value: string;
  onChange: (pin: string) => void;
  labels: { clear: string; delete: string };
  isWrong: boolean;
  isDisabled: boolean;
}

// A PIN entry sized for a shared tablet by the door: a real password field for the keyboard and screen readers,
// and a keypad of large keys that types into it.
export function PinPad({ id, label, value, onChange, labels, isWrong, isDisabled }: PinPadProps) {
  const press = (digit: string) => value.length < PIN_LENGTH && onChange(value + digit);
  return (
    <div className="flex flex-col gap-3">
      <label htmlFor={id} className="text-sm font-medium">
        {label}
      </label>
      <input
        id={id}
        type="password"
        inputMode="numeric"
        autoComplete="off"
        maxLength={PIN_LENGTH}
        value={value}
        disabled={isDisabled}
        aria-invalid={isWrong}
        onChange={(event) => onChange(event.target.value.replace(/\D/g, "").slice(0, PIN_LENGTH))}
        className={cn(
          "h-14 w-full rounded-lg bg-white/5 text-center text-3xl tracking-widest ring-1 outline-0 focus-visible:ring-white/70",
          isWrong ? "ring-red-400" : "ring-white/20",
        )}
      />
      <div className="grid grid-cols-3 gap-2">
        {DIGITS.map((digit) => (
          <button key={digit} type="button" disabled={isDisabled} onClick={() => press(digit)} className={KEY}>
            {digit}
          </button>
        ))}
        <button type="button" disabled={isDisabled} onClick={() => onChange("")} className={cn(KEY, "text-sm")}>
          {labels.clear}
        </button>
        <button type="button" disabled={isDisabled} onClick={() => press("0")} className={KEY}>
          0
        </button>
        <button
          type="button"
          disabled={isDisabled}
          onClick={() => onChange(value.slice(0, -1))}
          aria-label={labels.delete}
          className={KEY}
        >
          <Delete aria-hidden className="size-5" />
        </button>
      </div>
    </div>
  );
}
The PIN pad

The clock is the feedback

A correct PIN drops the card into the clock, stamps it IN with the real time and brings it back up. A wrong one pushes the card back out. The states of the form are the states of the object.

src/registry/login/time-clock/punch-clock.tsx

"use client";

import type { ReactNode } from "react";
import { motion, type TargetAndTransition } from "motion/react";
import { EASE_OUT } from "@/lib/motion";
import { cn } from "@/lib/utils";

export type PunchPhase = "rest" | "punching" | "rejected";

export const PUNCH_MS = 1000;
export const REJECT_MS = 500;
// How far the card drops so today's row sits under the print head inside the clock.
const DROP = 64;

const MOVES: Record<PunchPhase, TargetAndTransition> = {
  rest: { y: 0, x: 0 },
  punching: {
    y: [0, DROP, DROP, 0],
    transition: { duration: PUNCH_MS / 1000, times: [0, 0.3, 0.55, 1], ease: EASE_OUT },
  },
  rejected: { y: [0, 20, -12, 0], x: [0, 0, -7, 7, 0], transition: { duration: REJECT_MS / 1000 } },
};

interface PunchClockProps {
  phase: PunchPhase;
  // The live time and date on the clock's display; a dash until the page has the visitor's clock.
  time: string;
  date: string;
  // The clock's three modes, left to right; the first one is lit.
  modes: string[];
  brand: ReactNode;
  // The time card standing in the slot.
  children: ReactNode;
}

// A wall time clock drawn in code, with a card standing in its slot: a dark metal body, the slot's lip, an amber
// display with the live time, and a row of mode lights with the first one lit. Punching drops the card into the
// clock, the body jolts as it prints, and the card comes back up; a rejected card is pushed back out with a shake.
export function PunchClock({ phase, time, date, modes, brand, children }: PunchClockProps) {
  return (
    <div className="relative mx-auto h-144 w-80 max-w-full shrink-0">
      <motion.div initial={false} animate={MOVES[phase]} className="absolute inset-x-6 top-0 h-104">
        {children}
      </motion.div>
      <motion.div
        initial={false}
        animate={
          phase === "punching"
            ? { y: [0, 0, 4, 0], transition: { duration: PUNCH_MS / 1000, times: [0, 0.3, 0.4, 0.55] } }
            : { y: 0 }
        }
        className="absolute inset-x-0 bottom-0 flex h-64 flex-col rounded-2xl bg-linear-to-b from-stone-700 to-stone-800 text-white shadow-2xl ring-1 inset-shadow-sm ring-white/10 inset-shadow-white/25"
      >
        <span aria-hidden className="mx-4 h-2.5 rounded-b-md bg-black inset-shadow-sm inset-shadow-black" />
        <div className="mx-5 mt-5 rounded-lg bg-black px-5 py-4 text-center inset-shadow-sm inset-shadow-white/10">
          <p className="font-mono text-5xl tracking-tight text-amber-300 tabular-nums">{time}</p>
          <p className="mt-1.5 text-xs text-amber-200/60">{date}</p>
        </div>
        <ul aria-hidden className="mx-5 mt-4 grid grid-cols-3 gap-2 text-center text-xs font-medium">
          {modes.map((mode, index) => (
            <li
              key={mode}
              className={cn(
                "rounded-md py-1.5",
                index === 0 ? "bg-amber-300 text-stone-950" : "bg-black/40 text-stone-400",
              )}
            >
              {mode}
            </li>
          ))}
        </ul>
        <div className="mt-auto flex items-center justify-between px-5 pb-4 text-sm text-stone-300">
          {brand}
          <span aria-hidden className="flex gap-1.5">
            <span className="size-1.5 rounded-full bg-stone-500" />
            <span className="size-1.5 rounded-full bg-stone-500" />
          </span>
        </div>
      </motion.div>
    </div>
  );
}
The clock and its slot

Times that don't break hydration

The clock's display shows a dash until the page has the visitor's clock, so the server's HTML and the browser's first render match.

More articles