UIPackage React Native
Search
Menu

Attachment Tray

Composable attachment controls with upload progress, accessible status, and per-file retry and removal.

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 attachment-tray

Source files

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

components/ui/attachment-tray/AttachmentTray.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { File } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { Icon } from '@/components/ui/icon'
import { Progress } from '@/components/ui/progress'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export type AttachmentTrayStatus =
  { status: 'ready' } | { status: 'uploading'; progress?: number } | { status: 'failed'; reason: string }

const defaultLabels = {
  ready: 'Ready',
  uploading: 'Uploading…',
  failed: 'Upload failed',
  remove: 'Remove',
  removeAction: (name: string) => `Remove ${name}`,
  retry: 'Retry',
  retryAction: (name: string) => `Retry uploading ${name}`,
  progress: (name: string) => `Upload progress for ${name}`,
  statusChange: (name: string, status: string) => `${name}. ${status}`,
}

export interface AttachmentTrayProps extends ViewProps {
  className?: string
  children?: React.ReactNode
}

export function AttachmentTray({ className, children, ...props }: AttachmentTrayProps) {
  return (
    <View {...props} className={cn('gap-2', className)}>
      {children}
    </View>
  )
}

export interface AttachmentTrayItemProps extends Omit<ViewProps, 'children'> {
  className?: string
  name: string
  detail?: string
  state: AttachmentTrayStatus
  /** Decorative artwork. The row name supplies the file identity. */
  preview?: React.ReactNode
  /** Invoked as a removal request for any state, including uploads the caller can cancel. */
  onRemove?: () => void
  onRetry?: () => void
  disabled?: boolean
  labels?: Partial<typeof defaultLabels>
}

export function AttachmentTrayItem({
  className,
  name,
  detail,
  state,
  preview,
  onRemove,
  onRetry,
  disabled = false,
  labels,
  ...props
}: AttachmentTrayItemProps) {
  const copy = { ...defaultLabels, ...labels }
  const status = state.status
  const statusText = copy[status]
  const announcingCopy = copy.statusChange
  const previous = React.useRef(status)
  const [announcement, setAnnouncement] = React.useState<string>()
  React.useEffect(() => {
    if (previous.current === status) return
    previous.current = status
    setAnnouncement(announcingCopy(name, statusText))
  }, [announcingCopy, name, status, statusText])
  useAnnouncement(announcement)

  return (
    <View {...props} className={cn('gap-3 rounded-xl border border-border bg-card px-4 py-3', className)}>
      <View className="flex-row items-start gap-3">
        <View
          aria-hidden
          accessibilityElementsHidden
          importantForAccessibility="no-hide-descendants"
          pointerEvents="none"
          className="size-10 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-muted"
        >
          {preview ?? <Icon icon={File} size={20} className="text-muted-foreground" />}
        </View>
        <View className="min-w-0 flex-1 gap-1">
          <Text className="font-semibold">{name}</Text>
          {detail ? (
            <Text size="sm" variant="muted">
              {detail}
            </Text>
          ) : null}
          <Text size="sm" className={status === 'failed' ? 'text-destructive' : undefined}>
            {statusText}
          </Text>
        </View>
      </View>
      {state.status === 'uploading' ? (
        <Progress
          accessible
          accessibilityLabel={copy.progress(name)}
          accessibilityState={{ busy: true }}
          value={Number.isFinite(state.progress) ? state.progress : undefined}
        />
      ) : null}
      {state.status === 'failed' ? (
        <Text size="sm" variant="muted">
          {state.reason}
        </Text>
      ) : null}
      {onRemove || (state.status === 'failed' && onRetry) ? (
        <View className="flex-row flex-wrap items-center gap-2">
          {state.status === 'failed' && onRetry ? (
            <Button
              variant="outline"
              accessibilityLabel={copy.retryAction(name)}
              disabled={disabled}
              onPress={() => {
                if (!disabled) onRetry()
              }}
            >
              {copy.retry}
            </Button>
          ) : null}
          {onRemove ? (
            <Button
              variant="ghost"
              accessibilityLabel={copy.removeAction(name)}
              disabled={disabled}
              onPress={() => {
                if (!disabled) onRemove()
              }}
            >
              {copy.remove}
            </Button>
          ) : null}
        </View>
      ) : null}
    </View>
  )
}
components/ui/attachment-tray/index.ts
export {
  AttachmentTray,
  AttachmentTrayItem,
  type AttachmentTrayItemProps,
  type AttachmentTrayProps,
  type AttachmentTrayStatus,
} from './AttachmentTray'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse forms and selection