Examples
Sign in
An email and password form that checks itself on submit, says what went wrong next to the field, explains a failed attempt in an alert above the form, and keeps the button busy while it waits. Try any email with the password tidepool; anything else fails.
Loading…
import { useState } from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; import { Alert, Button, Heading, HStack, Link, Separator, Text, TextField, useTheme } from 'patine-ui'; import { Icon } from '@patine-ui/icons'; const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // The demo's server: one password works. const signIn = (email: string, password: string) => new Promise<boolean>((resolve) => setTimeout(() => resolve(password === 'tidepool'), 900)); export default function SignIn() { const { colors, radius } = useTheme(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [submitted, setSubmitted] = useState(false); const [loading, setLoading] = useState(false); const [failed, setFailed] = useState(false); const [signedIn, setSignedIn] = useState(false); const emailError = !email ? 'Enter your email' : EMAIL.test(email) ? undefined : 'That doesn’t look like an email'; const passwordError = password.length >= 6 ? undefined : 'At least 6 characters'; const submit = async () => { setSubmitted(true); setFailed(false); if (emailError || passwordError) return; setLoading(true); const ok = await signIn(email, password); setLoading(false); setFailed(!ok); setSignedIn(ok); }; return ( <ScrollView style={{ backgroundColor: colors.background }} contentContainerStyle={styles.screen} keyboardShouldPersistTaps="handled"> <View style={[styles.logo, { backgroundColor: colors.primary, borderRadius: radius.lg }]}> <Icon name="compass" size={28} color="onPrimary" /> </View> <View style={styles.titles}> <Heading level={2}>Welcome back</Heading> <Text color="textSecondary">Sign in to keep planning your trips.</Text> </View> {failed ? ( <Alert variant="destructive" title="Wrong email or password" description="Check both and try again, or reset your password." /> ) : null} {signedIn ? <Alert variant="success" title="You’re in" description={`Signed in as ${email}.`} /> : null} <TextField label="Email" placeholder="[email protected]" value={email} onValueChange={setEmail} error={submitted ? emailError : undefined} keyboardType="email-address" autoComplete="email" autoCapitalize="none" startIcon={<Icon name="mail" size="sm" />} /> <TextField label="Password" value={password} onValueChange={setPassword} error={submitted ? passwordError : undefined} secureTextEntry passwordToggle autoComplete="current-password" startIcon={<Icon name="lock" size="sm" />} onSubmitEditing={submit} /> <Link onPress={() => {}} style={styles.forgot}> Forgot password? </Link> <Button size="lg" loading={loading} onPress={submit}> {loading ? 'Signing in…' : 'Sign in'} </Button> <HStack align="center" gap="md"> <Separator style={styles.rule} /> <Text size="sm" color="textSecondary"> or </Text> <Separator style={styles.rule} /> </HStack> <Button size="lg" variant="outline" startIcon={<Icon name="fingerprint" size="sm" color="text" />}> Sign in with a passkey </Button> <Text color="textSecondary" style={styles.footer}> New here? <Link onPress={() => {}}>Create an account</Link> </Text> </ScrollView> ); } const styles = StyleSheet.create({ screen: { flexGrow: 1, justifyContent: 'center', gap: 16, padding: 24 }, logo: { width: 52, height: 52, alignItems: 'center', justifyContent: 'center' }, titles: { gap: 4, marginBottom: 8 }, forgot: { alignSelf: 'flex-end' }, rule: { flex: 1 }, footer: { textAlign: 'center', marginTop: 8 }, });
Components used
- TextField: the email and the password, with
error,startIconandpasswordToggle - Button:
loadingwhile the request runs, and anoutlinebutton for the passkey - Alert: the
destructivealert after a failed attempt, and asuccessone after - Link: forgot password and create an account
- Heading, Text, Separator, Patine Icons
How it works
- Errors wait for the first submit. Each field's error is worked out on every render, but only shown once
submittedis true, so nobody is told off for a field they haven't reached. - The alert is for what the fields can't say. A wrong password isn't the field's fault, so the message goes above the form, where a screen reader announces it.
autoCompletelets the keyboard and password managers fill the email and the saved password.