All files / src/features/authentication CodeField.tsx

90% Statements 9/10
94.73% Branches 18/19
80% Functions 4/5
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                63x 63x 63x 63x 63x   63x                                     378x                                                                               36x         6x                                                              
// The sign-in code input (Design canvas: SignInCode): six boxes drawn over one
// transparent numeric input, so phones can fill the code from the email
// (autocomplete="one-time-code") and pasting works.
import Box from '@mui/material/Box'
import Typography from '@mui/material/Typography'
import { useId, useState } from 'react'
import { reasonText, type FieldReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
import { SIGN_IN_CODE } from '#/server/auth/auth'
import { colors } from '#/theme/tokens'
 
interface Props {
  value: string
  onChange: (code: string) => void
  reason?: FieldReason | undefined
  disabled?: boolean
}
 
export function CodeField({
  value,
  onChange,
  reason,
  disabled = false,
}: Props) {
  const id = useId()
  const errorId = `${id}-error`
  const [focused, setFocused] = useState(false)
  const active = focused && !disabled ? Math.min(value.length, 5) : -1
  const invalid = reason !== undefined
 
  return (
    <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
      <Typography
        component="label"
        htmlFor={id}
        variant="body2"
        sx={{ fontWeight: 800, color: 'text.secondary' }}
      >
        {m.sign_in_code_label()}
      </Typography>
      <Box
        sx={{
          position: 'relative',
          display: 'grid',
          gridTemplateColumns: `repeat(${String(SIGN_IN_CODE.length)}, minmax(0, 1fr))`,
          gap: 1,
        }}
      >
        {Array.from({ length: SIGN_IN_CODE.length }, (_, i) => (
          <Box
            key={i}
            aria-hidden="true"
            sx={{
              height: 60,
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              borderRadius: '14px',
              bgcolor: 'background.paper',
              border: '2px solid',
              borderColor: invalid
                ? colors.error
                : i === active
                  ? colors.plum
                  : colors.inputBorder,
              boxShadow:
                i === active && !invalid
                  ? '0 0 0 4px rgba(255,176,32,0.35)'
                  : 'none',
              fontSize: 28,
              fontWeight: 900,
            }}
          >
            {value[i] ?? ''}
          </Box>
        ))}
        <Box
          component="input"
          id={id}
          type="text"
          inputMode="numeric"
          autoComplete="one-time-code"
          pattern="[0-9]*"
          maxLength={SIGN_IN_CODE.length}
          value={value}
          readOnly={disabled}
          aria-invalid={invalid}
          aria-describedby={invalid ? errorId : undefined}
          onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
            onChange(
              e.target.value.replace(/\D/g, '').slice(0, SIGN_IN_CODE.length),
            )
          }}
          onFocus={() => {
            setFocused(true)
          }}
          onBlur={() => {
            setFocused(false)
          }}
          sx={{
            position: 'absolute',
            inset: 0,
            width: '100%',
            height: '100%',
            opacity: 0.01,
            border: 'none',
            fontSize: 16,
            color: 'transparent',
            background: 'transparent',
            caretColor: 'transparent',
          }}
        />
      </Box>
      {invalid ? (
        <Typography
          id={errorId}
          variant="body2"
          sx={{ color: 'error.main', fontWeight: 800 }}
        >
          {reasonText(reason).text}
        </Typography>
      ) : null}
    </Box>
  )
}