@cxxi-lab/hubbyware-ui 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cxxi-lab/hubbyware-ui",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "Hubbyware's design system: React components, design tokens and fonts (formerly @cxxi-lab/hub-ui).",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,67 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+
3
+ /**
4
+ * A code typed digit by digit (a PIN, a 2FA code): one box per digit, as a phone's code screens
5
+ * show it. One real input under the boxes, see-through: a phone opens its numeric keypad, a pasted
6
+ * or auto-filled code (`one-time-code`) lands whole, backspace and arrows behave as in a field.
7
+ * `masked`: dots instead of the digits (a PIN). `onComplete` when the last digit is typed.
8
+ */
9
+ export function CodeInput({ length = 6, value, onChange, onComplete, masked = false, autoFocus = false, isDisabled = false, isInvalid = false, label }: {
10
+ length?: number
11
+ value: string
12
+ onChange: (value: string) => void
13
+ onComplete?: (value: string) => void
14
+ masked?: boolean
15
+ autoFocus?: boolean
16
+ isDisabled?: boolean
17
+ /** the boxes in red (a wrong code) */
18
+ isInvalid?: boolean
19
+ /** said to screen readers */
20
+ label: string
21
+ }) {
22
+ const input = useRef<HTMLInputElement>(null)
23
+ const [focused, setFocused] = useState(false)
24
+ useEffect(() => { if (autoFocus) input.current?.focus() }, [autoFocus])
25
+
26
+ const change = (raw: string) => {
27
+ const digits = raw.replace(/\D/g, '').slice(0, length)
28
+ onChange(digits)
29
+ if (digits.length === length) onComplete?.(digits)
30
+ }
31
+
32
+ return (
33
+ <div className="relative inline-flex gap-2" onClick={() => input.current?.focus()}>
34
+ {Array.from({ length }, (_, i) => {
35
+ const digit = value[i]
36
+ const current = focused && (i === value.length || (i === length - 1 && value.length === length))
37
+ return (
38
+ <div
39
+ key={i}
40
+ aria-hidden
41
+ className={[
42
+ 'flex items-center justify-center w-11 h-13 max-sm:w-10 max-sm:h-12 rounded border text-xl font-semibold text-white bg-steel-950 transition-colors',
43
+ isInvalid ? 'border-red-500' : current ? 'border-primary-500' : digit !== undefined ? 'border-steel-300' : 'border-steel-700'
44
+ ].join(' ')}
45
+ >
46
+ {digit === undefined ? '' : masked ? <span className="size-2.5 rounded-full bg-white" /> : digit}
47
+ </div>
48
+ )
49
+ })}
50
+ <input
51
+ ref={input}
52
+ aria-label={label}
53
+ type="text"
54
+ inputMode="numeric"
55
+ pattern="[0-9]*"
56
+ autoComplete="one-time-code"
57
+ maxLength={length}
58
+ value={value}
59
+ disabled={isDisabled}
60
+ onChange={e => change(e.target.value)}
61
+ onFocus={() => setFocused(true)}
62
+ onBlur={() => setFocused(false)}
63
+ className="absolute inset-0 w-full h-full opacity-0 cursor-text caret-transparent text-transparent bg-transparent border-0 outline-none"
64
+ />
65
+ </div>
66
+ )
67
+ }
@@ -0,0 +1 @@
1
+ export { CodeInput } from './CodeInput'
@@ -8,3 +8,4 @@ export * from './SpoilerField'
8
8
  export * from './DropdownField'
9
9
  export * from './TagsField'
10
10
  export * from './UploadField'
11
+ export * from './CodeInput'