# Laravel Cloud OTP Field

A one-time password input composed of individual character slots.

Build a OTP Field in this project that looks and behaves like Laravel Cloud's. Measured from the running Laravel Cloud app (cloud.laravel.com). Keep every value exact. Map the tokens onto the project's own theme variables instead of hard-coding them. Use the project's own component library for behaviour, or [Base UI](https://base-ui.com/react/components/otp-field) if it has none.

Fidelity: Derived from the measured input and the app's mono font.

## Tokens

| Token | Light | Dark |
| --- | --- | --- |
| `--text-color-strong` | `var(--slate-dark-12)` | `var(--slate-dark-12)` |
| `--text-color-brand` | `var(--cloud-dark-11)` | `var(--cloud-dark-11)` |
| `--background-color-default` | `var(--slate-dark-2)` | `var(--slate-dark-2)` |
| `--background-color-brand-weaker` | `var(--cloud-dark-2)` | `var(--cloud-dark-2)` |
| `--border-color-default` | `var(--slate-dark-alpha-7)` | `var(--slate-dark-alpha-7)` |
| `--border-color-hovered` | `var(--slate-dark-alpha-8)` | `var(--slate-dark-alpha-8)` |
| `--border-color-brand` | `var(--cloud-dark-alpha-10)` | `var(--cloud-dark-alpha-10)` |
| `--border-color-brand-weak` | `var(--cloud-dark-alpha-5)` | `var(--cloud-dark-alpha-5)` |

## CSS

Scoped under `[data-style="laravel"]`; dark rules sit under `[data-theme="dark"]`. The `ui-*` classes and data attributes match the structure below.

```css
[data-style="laravel"] {
  .ui-otp-field {
    display: flex;
    align-items: center;
    gap: 8px;

    &[data-disabled] {
      opacity: 0.5;
    }
  }

  .ui-otp-field-input {
    box-sizing: border-box;
    width: 40px;
    height: 40px;
    margin: 0;
    padding: 0;
    border: 1px solid var(--border-color-default);
    border-radius: var(--radius-md);
    background-color: var(--background-color-default);
    color: var(--text-color-strong);
    caret-color: var(--text-color-brand);
    font-family: var(--font-mono);
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    font-variant-numeric: tabular-nums;
    text-align: center;
    outline: none;

    &:hover:not([data-disabled]) {
      border-color: var(--border-color-hovered);
    }

    &:focus {
      border-color: var(--border-color-brand);
      background-color: var(--background-color-brand-weaker);
      box-shadow: 0 0 0 3px var(--border-color-brand-weak);
    }
  }

  .ui-otp-field-separator {
    width: 8px;
    height: 1px;
    border-radius: var(--radius-rounded);
    background-color: var(--border-color-hovered);
  }
}
```

## Structure

How Replicas assembles it on Base UI.

```tsx
import { OTPField as BaseOTPField } from "@base-ui/react/otp-field";

const join = (base: string, extra?: string) => (extra ? `${base} ${extra}` : base);

function Root({ className, ...props }: Omit<BaseOTPField.Root.Props, "className"> & { className?: string }) {
  return <BaseOTPField.Root className={join("ui-otp-field", className)} {...props} />;
}

function Input({ className, ...props }: Omit<BaseOTPField.Input.Props, "className"> & { className?: string }) {
  return <BaseOTPField.Input className={join("ui-otp-field-input", className)} {...props} />;
}

function Separator({ className, ...props }: Omit<BaseOTPField.Separator.Props, "className"> & { className?: string }) {
  return <BaseOTPField.Separator className={join("ui-otp-field-separator", className)} {...props} />;
}

export const OtpField = {
  Root,
  Input,
  Separator,
};
```

## Example

```tsx
import { OtpField } from "@replicas/ui";
import { Fragment, useId } from "react";

const LENGTH = 6;

export default function OtpFieldDemo() {
  const id = useId();

  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <label htmlFor={id} style={{ fontSize: "var(--font-size-small)", color: "var(--text-secondary)" }}>
        Verification code
      </label>
      <OtpField.Root id={id} length={LENGTH} aria-describedby={`${id}-description`}>
        {Array.from({ length: LENGTH }, (_, index) => (
          <Fragment key={index}>
            {index === LENGTH / 2 ? <OtpField.Separator /> : null}
            <OtpField.Input aria-label={index === 0 ? undefined : `Character ${index + 1} of ${LENGTH}`} />
          </Fragment>
        ))}
      </OtpField.Root>
      <p
        id={`${id}-description`}
        style={{ margin: 0, fontSize: "var(--font-size-mini)", color: "var(--text-tertiary)" }}
      >
        Enter the code we sent to raphael@example.com
      </p>
    </div>
  );
}
```

## Foundations

The rest of the Laravel Cloud style, as a skill: https://replicas.raphaelsalaja.com/skills/laravel/SKILL.md