[Feature] Add InputOtp component

Merged
DJ
4mo ago
+1882-0

Summary

Closes #449. Adds InputOtp — a Phlex/Stimulus port of shadcn's InputOTP. The underlying JS lib (input-otp) is React-only, so this is a from-scratch Stimulus reimplementation, not a wrapped package.

  • Compound API mirroring shadcn 1:1: InputOtp / InputOtpGroup / InputOtpSlot / InputOtpSlot(index:) / InputOtpSeparator.
  • Single real <input> (transparent, absolutely positioned, kept in the a11y tree — not display:none) drives state; decorative aria-hidden slot <div>s mirror its value/selection via the ruby-ui--input-otp Stimulus controller.
  • Keyboard: typing auto-advances, backspace works, all four arrow keys move the active slot (Up/Down are non-native — handled explicitly), and replacing a character re-selects the next one so repeated typing keeps replacing forward without needing an arrow key in between.
  • Paste distributes characters across slots, filtered by pattern: (default "[0-9]", a single-character regex class).
  • Dispatches ruby-ui--input-otp:complete and ruby-ui--input-otp:input custom events (bubbles, detail: { value }) for Hotwire/Turbo integration (e.g. auto-submitting a form on complete).
  • Accessibility: real input is the sole control with a value in the a11y tree, autocomplete="one-time-code" for SMS autofill, inputmode="numeric" by default, aria-invalid styling hook on slots.
  • Docs page mirrors shadcn's example set: Usage, Composition, Pattern, Four digits, Disabled, Invalid, Form (Card + resend + bigger slots), Reacting to completion.

No new dependency — no JS package, no new gem, nothing added to dependencies.yml. animate-caret-blink comes from tw-animate-css, already a baseline install used by other components the same way.

Design/plan docs for context: design/2026-06-30-input-otp-design.md, design/plans/2026-06-30-input-otp-implementation.md.

Test plan

  • cd gem && bundle exec rake — 265 tests, 0 failures, standardrb clean.
  • Manually verified in the docs app (Chrome): typing/backspace/all four arrows, paste, the complete event, and the reported bug where replacing a filled slot then typing again did nothing (fixed by re-selecting the next character after every input instead of leaving a collapsed caret).
  • Compared the rendered docs page side-by-side against the live shadcn docs page to match the example set and catch a layout bug (wrapper was inline-flex, upstream uses flex).

Summary by cubic

Adds InputOtp, a Phlex/Stimulus OTP input with keyboard navigation, paste support, and custom events, plus a new /docs/input_otp page. No new dependencies.

  • New Features

    • Compound API: InputOtp, InputOtpGroup, InputOtpSlot(index:), InputOtpSeparator.
    • Single real input drives decorative slots via ruby-ui--input-otp.
    • Typing auto-advances; backspace and all arrow keys move between slots; paste distributes characters.
    • Emits ruby-ui--input-otp:input and ruby-ui--input-otp:complete with { value }.
    • pattern: filter (default "[0-9]"), autocomplete="one-time-code", inputmode set based on pattern.
    • Docs: new /docs/input_otp with examples (composition, pattern, 4-digit, disabled, invalid, form), plus updates to sidebar, sitemap, LLMS lists, and mcp/data/registry.json.
  • Bug Fixes

    • Moved controller to the wrapper so slot targets are in scope.
    • Normalized selection after input and on arrow keys to enable replace-and-advance when full.
    • Removed native focus ring on the transparent input across browsers.
    • Fixed wrapper layout to flex to match shadcn and avoid inline wrapping issues.
    • Added aria-invalid styling hook on slots.
    • Sanitizes prefilled values on connect (enforces length/pattern before first paint/submit) and fixes a minor docs typo.