UIPackage React Native
Search
Menu

Conversation List

A native searchable conversation list with previews, drafts, unread counts, and muted states.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add conversation-list

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/conversation-list/ConversationList.tsx
import * as React from 'react'
import { FlatList, Platform, View, useWindowDimensions, type ViewProps } from 'react-native'
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
import { Badge } from '@/components/ui/badge'
import { ListItem, ListItemContent, ListItemDescription, ListItemTitle, ListSeparator } from '@/components/ui/list'
import { SearchBar } from '@/components/ui/search-bar'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export interface Conversation {
  id: string
  title: string
  initials?: string
  preview?: string
  /** Displayed in preference to the latest message preview. */
  draft?: string
  /** A caller-localized display string for the latest message time. */
  time?: string
  unreadCount?: number
  muted?: boolean
}

const defaultLabels = {
  search: 'Search conversations',
  clear: 'Clear search',
  empty: 'No conversations yet.',
  noResults: 'No conversations match your search.',
  matches: (count: number) => `${count} ${count === 1 ? 'conversation' : 'conversations'} found`,
  unread: (count: number) => `${count} unread`,
  unreadBadge: (count: number) => (count > 99 ? '99+' : String(count)),
  muted: 'Muted',
  draft: (message: string) => `Draft: ${message}`,
}

export interface ConversationListProps extends Omit<ViewProps, 'children'> {
  className?: string
  /** Provide conversations in display order with unique, stable IDs. */
  conversations?: readonly Conversation[]
  onOpen: (id: string) => void
  /** Supply both query and onQueryChange to persist a controlled search. */
  query?: string
  onQueryChange?: (query: string) => void
  locale?: string
  labels?: Partial<typeof defaultLabels>
  header?: React.ReactNode
}

export function ConversationList({
  className,
  conversations = [],
  onOpen,
  query,
  onQueryChange,
  locale,
  labels,
  header,
  ...props
}: ConversationListProps) {
  const copy = { ...defaultLabels, ...labels }
  const [internalQuery, setInternalQuery] = React.useState('')
  const resolvedQuery = query ?? internalQuery
  const term = resolvedQuery.trim().toLocaleLowerCase(locale)
  const [settledTerm, setSettledTerm] = React.useState('')
  const { width, fontScale } = useWindowDimensions()
  const stackMeta = width < 360 || fontScale >= 1.4
  const filtered = React.useMemo(
    () =>
      term
        ? conversations.filter((item) =>
            [item.title, item.preview, item.draft].some((value) => value?.toLocaleLowerCase(locale).includes(term)),
          )
        : conversations,
    [conversations, locale, term],
  )
  const count = copy.matches(filtered.length)
  React.useEffect(() => {
    if (term === settledTerm) return
    const timer = setTimeout(() => setSettledTerm(term), 300)
    return () => clearTimeout(timer)
  }, [settledTerm, term])
  useAnnouncement(term && term === settledTerm ? count : undefined)

  return (
    <View {...props} className={cn('min-h-0 flex-1', className)}>
      <FlatList
        data={filtered}
        keyExtractor={(item) => item.id}
        keyboardDismissMode={Platform.OS === 'ios' ? 'interactive' : 'on-drag'}
        keyboardShouldPersistTaps="handled"
        automaticallyAdjustKeyboardInsets={Platform.OS === 'ios'}
        contentContainerStyle={{ paddingBottom: 16 }}
        ItemSeparatorComponent={ListSeparator}
        ListHeaderComponent={
          <View className="gap-3 pb-3">
            {header}
            <SearchBar
              accessibilityLabel={copy.search}
              placeholder={copy.search}
              clearLabel={copy.clear}
              value={resolvedQuery}
              onChangeText={(next) => {
                if (query === undefined) setInternalQuery(next)
                onQueryChange?.(next)
              }}
            />
            {term ? (
              <Text size="sm" variant="muted">
                {count}
              </Text>
            ) : null}
          </View>
        }
        ListEmptyComponent={
          <Text className="rounded-xl border border-border px-4 py-6 text-center">
            {term ? copy.noResults : copy.empty}
          </Text>
        }
        renderItem={({ item }) => {
          const unread =
            item.unreadCount && Number.isSafeInteger(item.unreadCount) && item.unreadCount > 0 ? item.unreadCount : 0
          const preview = item.draft ? copy.draft(item.draft) : item.preview
          const initials = item.initials ?? [...item.title.trim()][0]?.toLocaleUpperCase(locale)
          return (
            <ListItem
              accessible
              accessibilityLabel={[
                item.title,
                preview,
                item.time,
                unread ? copy.unread(unread) : undefined,
                item.muted ? copy.muted : undefined,
              ]
                .filter(Boolean)
                .join(', ')}
              onPress={() => onOpen(item.id)}
              className="items-start px-0 py-4"
            >
              <Avatar className="shrink-0" aria-hidden>
                <AvatarFallback>{initials}</AvatarFallback>
              </Avatar>
              <ListItemContent>
                <View className={cn('gap-1', !stackMeta && 'flex-row items-baseline justify-between gap-3')}>
                  <ListItemTitle className={cn(!stackMeta && 'min-w-0 flex-1', unread > 0 && 'font-semibold')}>
                    {item.title}
                  </ListItemTitle>
                  {item.time ? (
                    <Text size="sm" variant="muted" className={cn(!stackMeta && 'max-w-[45%] text-right')}>
                      {item.time}
                    </Text>
                  ) : null}
                </View>
                {preview ? (
                  <ListItemDescription numberOfLines={stackMeta ? undefined : 2}>{preview}</ListItemDescription>
                ) : null}
                {unread || item.muted ? (
                  <View className="flex-row flex-wrap items-center gap-2">
                    {unread ? <Badge text={copy.unreadBadge(unread)} aria-hidden /> : null}
                    {item.muted ? (
                      <Text size="sm" variant="muted">
                        {copy.muted}
                      </Text>
                    ) : null}
                  </View>
                ) : null}
              </ListItemContent>
            </ListItem>
          )
        }}
      />
    </View>
  )
}
components/blocks/conversation-list/index.ts
export { ConversationList, type Conversation, type ConversationListProps } from './ConversationList'

Dependencies

Browse accounts and messaging