Rost

Block

Login

Signing in: email, password and keep me signed in, on a Card in the middle of the page. It checks the fields are filled and shows the app's answer when it isn't a yes.

Made of

Card, Text, Input, Checkbox, Button, Link, InlineBanner. Nothing else: the block's own CSS is layout, and every value in it is a token.

Use it

import { Login } from 'rost/blocks/login/Login'

<Login
  logo={<Logo />}
  lede="Welcome back."
  forgotHref="/forgot"
  onSignIn={async ({ email, password, remember }) => {
    const ok = await signIn(email, password, remember)
    if (!ok) return "That email and password don't match."
  }}
/>
src/blocks/login/Login.tsx
import { useState, type FormEvent, type ReactNode } from 'react'
import { Card } from '../../components/card/Card'
import { Text } from '../../components/text/Text'
import { Input } from '../../components/input/Input'
import { Checkbox } from '../../components/checkbox/Checkbox'
import { Button } from '../../components/button/Button'
import { Link } from '../../components/link/Link'
import { InlineBanner } from '../../components/inline-banner/InlineBanner'
import './login.css'

export interface LoginDetails {
  readonly email: string
  readonly password: string
  readonly remember: boolean
}

export interface LoginProps {
  /** The app's mark, 40px, above the title. */
  readonly logo?: ReactNode
  readonly title?: string
  readonly lede?: ReactNode
  /** Called with what was typed once both fields are filled. Return a message to show it as an error. */
  readonly onSignIn: (details: LoginDetails) => void | string | Promise<void | string>
  /** Where "Forgot your password?" goes. */
  readonly forgotHref?: string
  /** A line under the form, like a link to sign up. */
  readonly footer?: ReactNode
}

const b = 'rost-block-login'

/**
 * Signing in: email, password, keep me signed in, on a Card in the middle of
 * the page. It checks that both fields are filled; the rest is the app's.
 * A Rost block: only Rost's components, and layout from its tokens.
 */
export function Login({ logo, title = 'Sign in', lede, onSignIn, forgotHref, footer }: LoginProps) {
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [remember, setRemember] = useState(true)
  const [tried, setTried] = useState(false)
  const [problem, setProblem] = useState<string | null>(null)
  const [busy, setBusy] = useState(false)
  const emailWrong = tried && !/^\S+@\S+\.\S+$/.test(email)
  const passwordWrong = tried && password === ''
  const submit = async (e: FormEvent) => {
    e.preventDefault()
    setTried(true)
    setProblem(null)
    if (!/^\S+@\S+\.\S+$/.test(email) || password === '') return
    setBusy(true)
    try { const said = await onSignIn({ email, password, remember }); if (typeof said === 'string') setProblem(said) }
    finally { setBusy(false) }
  }
  return (
    <div className={b}>
      <Card className={`${b}__card`}>
        <form className={`${b}__form`} onSubmit={submit} noValidate>
          <div className={`${b}__head`}>
            {logo && <span className={`${b}__mark`}>{logo}</span>}
            <Text variant="sub-headline" as="h1">{title}</Text>
            {lede && <Text tone="secondary">{lede}</Text>}
          </div>
          {problem && <InlineBanner status="negative">{problem}</InlineBanner>}
          <Input label="Email address" icon={null} type="email" autoComplete="email" value={email} onChange={(e) => setEmail(e.target.value)}
            error={emailWrong} supportive={emailWrong ? 'An email address, like name@example.com.' : undefined} />
          <Input label="Password" icon={null} type="password" autoComplete="current-password" value={password} onChange={(e) => setPassword(e.target.value)}
            error={passwordWrong} supportive={passwordWrong ? 'Your password, please.' : undefined} />
          <div className={`${b}__row`}>
            <Checkbox checked={remember} onCheckedChange={setRemember}>Keep me signed in</Checkbox>
            {forgotHref && <Text variant="paragraph" as="span"><Link href={forgotHref}>Forgot your password?</Link></Text>}
          </div>
          <Button type="submit" disabled={busy} className={`${b}__submit`}>{busy ? 'Signing in…' : 'Sign in'}</Button>
          {footer && <Text tone="secondary" className={`${b}__footer`}>{footer}</Text>}
        </form>
      </Card>
    </div>
  )
}

Install

npx github:Runceanu/rost add login

It lands in src/rost/login/, with every component it's made of. It's yours from there: change it as much as the app needs. Install has the rest.