# Inbox with swipe actions

> A React Native inbox for iOS, Android and the web: tabs with unread counts, message rows with avatars and unread badges, swipe right to mark read, swipe left to archive or delete, and an empty state with a way to bring messages back. One file, built with Patine's Tabs, List, ListItem, SwipeableRow, Badge, Avatar and EmptyState.

# Inbox

Messages split into tabs that count what's unread. Swipe a row right to mark it read or unread, left to archive or delete it (a long swipe deletes, like iOS Mail), and clear a tab to reach its empty state. On the web, the rows' actions are also a focus away for keyboard users.

```tsx live device
import { useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import {
  Avatar,
  Badge,
  Button,
  EmptyState,
  Header,
  List,
  ListItem,
  SwipeableRow,
  Tabs,
  TabsList,
  TabsTrigger,
  Text,
  useTheme,
} from 'patine-ui';
import { Icon } from '@patine-ui/icons';

type Tab = 'people' | 'updates';
type Message = { id: number; tab: Tab; from: string; subject: string; time: string; unread: boolean; photo?: string };

const MESSAGES: Message[] = [
  { id: 1, tab: 'people', from: 'Rafael Mendes', subject: 'Sintra on Saturday? I can drive', time: '09:41', unread: true, photo: 'https://picsum.photos/id/1005/120/120' },
  { id: 2, tab: 'people', from: 'Noa Peretz', subject: 'Sent you the sourdough starter recipe', time: '08:15', unread: true },
  { id: 3, tab: 'people', from: 'Lea Martin', subject: 'Re: flat viewing on Rua da Rosa', time: 'Yesterday', unread: false, photo: 'https://picsum.photos/id/1027/120/120' },
  { id: 4, tab: 'people', from: 'Omer Katz', subject: 'Photos from the wedding 📸', time: 'Mon', unread: false },
  { id: 5, tab: 'updates', from: 'TAP Air Portugal', subject: 'Check-in is open for TP 1350 to Madrid', time: '07:02', unread: true },
  { id: 6, tab: 'updates', from: 'Continente', subject: 'Your order arrives between 18:00 and 20:00', time: 'Yesterday', unread: false },
  { id: 7, tab: 'updates', from: 'Fidelidade', subject: 'Your home insurance renews on 1 December', time: 'Sun', unread: false },
];

export default function Inbox() {
  const { colors } = useTheme();
  const [tab, setTab] = useState<Tab>('people');
  const [messages, setMessages] = useState(MESSAGES);
  const shown = messages.filter((m) => m.tab === tab);
  const unread = (t: Tab) => messages.filter((m) => m.tab === t && m.unread).length;

  const remove = (id: number) => setMessages((all) => all.filter((m) => m.id !== id));
  const toggleRead = (id: number) => setMessages((all) => all.map((m) => (m.id === id ? { ...m, unread: !m.unread } : m)));

  const trigger = (value: Tab, label: string) => (
    <TabsTrigger value={value}>
      {unread(value) ? `${label} · ${unread(value)}` : label}
    </TabsTrigger>
  );

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <Header
        title="Inbox"
        actions={
          <Button variant="ghost" size="icon" accessibilityLabel="New message">
            <Icon name="edit" color="primary" />
          </Button>
        }
      />
      <Tabs value={tab} onValueChange={(value) => setTab(value as Tab)} style={styles.tabs}>
        <TabsList>
          {trigger('people', 'People')}
          {trigger('updates', 'Updates')}
        </TabsList>
      </Tabs>

      {shown.length ? (
        <ScrollView contentContainerStyle={styles.list}>
          <List>
            {shown.map((m) => (
              <SwipeableRow
                key={m.id}
                fullSwipe
                leadingActions={[
                  {
                    label: m.unread ? 'Read' : 'Unread',
                    variant: 'primary',
                    icon: <Icon name="mail" size="sm" color="onPrimary" />,
                    onPress: () => toggleRead(m.id),
                  },
                ]}
                trailingActions={[
                  { label: 'Delete', variant: 'destructive', icon: <Icon name="trash" size="sm" color="#fff" />, onPress: () => remove(m.id) },
                  { label: 'Archive', icon: <Icon name="archive" size="sm" color="#fff" />, onPress: () => remove(m.id) },
                ]}
              >
                <ListItem
                  title={m.from}
                  description={m.subject}
                  titleStyle={m.unread ? styles.unread : undefined}
                  leading={<Avatar name={m.from} src={m.photo} />}
                  trailing={
                    <View style={styles.meta}>
                      <Text size="xs" color={m.unread ? 'primary' : 'textSecondary'}>
                        {m.time}
                      </Text>
                      {m.unread ? <Badge>New</Badge> : null}
                    </View>
                  }
                  onPress={() => toggleRead(m.id)}
                  showChevron={false}
                />
              </SwipeableRow>
            ))}
          </List>
          <Text size="xs" color="textSecondary" style={styles.hint}>
            Swipe a message right or left for its actions.
          </Text>
        </ScrollView>
      ) : (
        <EmptyState
          icon={<Icon name="inbox" size={40} color="textSecondary" />}
          title="You’re all caught up"
          description={tab === 'people' ? 'No messages from people right now.' : 'No updates right now.'}
          action={
            <Button variant="outline" onPress={() => setMessages(MESSAGES)}>
              Bring them back
            </Button>
          }
          style={styles.empty}
        />
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  tabs: { paddingHorizontal: 16 },
  list: { paddingVertical: 12 },
  unread: { fontWeight: '700' },
  meta: { alignItems: 'flex-end', gap: 4 },
  hint: { textAlign: 'center', marginTop: 12 },
  empty: { flex: 1 },
});
```

## Components used

- [Tabs](/docs/components/tabs): `TabsList` and `TabsTrigger`, controlled by `value`, with the unread count in each label
- [SwipeableRow](/docs/components/swipeable-row): `leadingActions`, `trailingActions` and `fullSwipe`
- [List, ListItem](/docs/components/list): the rows, with an avatar in `leading` and the time and badge in `trailing`
- [Badge](/docs/components/badge): marks the unread messages
- [EmptyState](/docs/components/empty-state): the cleared tab, with an `action` that restores the messages
- [Avatar](/docs/components/avatar): a photo, or initials for senders without one
- [Header](/docs/components/header), [Button](/docs/components/button), [Patine Icons](/docs/components/icons)

## How it works

- **Counts come from the data.** Each tab's label counts its unread messages on every render, so marking one read updates it at once.
- **Every swipe has a tap.** Tapping a row also toggles read, so no action hides behind a gesture; on the web the actions are reachable with the keyboard.
- **The empty state offers a way back** instead of a dead end.
