Lynx

Attachment Field

A component that receives host native file selection results and displays and manages attachment items.

Available since@seed-design/lynx-react@0.8.0, @seed-design/lynx-css@0.12.0
Loading Lynx example…

Installation

Default

Install the default snippet if reordering is not needed.

npx @seed-design/cli@latest add ui:attachment-field

Reorderable

Install a separate snippet to change the order of attachment items using native horizontal long-press gestures. The default snippet does not include the reorder gesture.

npx @seed-design/cli@latest add ui:attachment-field-reorderable

Lynx AttachmentField does not create a browser file input. Pass the native file selection adapter provided by the host to onSelectFiles. The fixture:// URI and fixed images in this document are document fixtures, not working pickers.

Props

AttachmentField

Prop

Type

children?React.ReactNode
label?React.ReactNode
labelWeight?"medium" | "bold" | undefined
indicator?React.ReactNode
description?React.ReactNode
errorMessage?React.ReactNode
showRequiredIndicator?boolean | undefined
id?string | undefined
flatten?boolean | undefined
accept?string | string[] | undefined
disabled?boolean | undefined
required?boolean | undefined
invalid?boolean | undefined
readOnly?boolean | undefined
maxFiles?number | undefined
maxFileSize?number | undefined
minFileSize?number | undefined
validate?((file: NativeFile) => AttachmentFileError[] | null) | undefined
onSelectFiles?(() => NativeFile[] | Promise<NativeFile[]>) | undefined
onSelectError?((error: unknown) => void) | undefined
acceptedFileEntries?AttachmentFileEntry[] | undefined
defaultAcceptedFileEntries?AttachmentFileEntry[] | undefined
onAcceptedFileEntriesChange?((entries: AttachmentFileEntry[]) => void) | undefined
onFileReject?((rejections: AttachmentFileRejection[]) => void) | undefined
onFileAccept?((entries: AttachmentFileEntry[], helpers: { updateFileEntryStatus: (id: string, details: AttachmentFileStatusDetails) => void; }) => void) | undefined
style?CSSProperties | undefined
className?string | undefined

AttachmentInput

Prop

Type

children?((context: AttachmentInputContextValue) => React.ReactNode) | undefined
onRetry?((fileEntry: FileEntry, helpers: RetryHelpers) => void) | undefined

AttachmentInputItem

Prop

Type

fileEntryAttachmentFileEntry
onRetry?(() => void) | undefined
id?string | undefined
flatten?boolean | undefined
style?CSSProperties | undefined
className?string | undefined

Usage

Combine AttachmentInput inside AttachmentField. The host adapter passed to onSelectFiles returns NativeFile[] or Promise<NativeFile[]>. Return an empty array if selection is canceled. Errors from the adapter are passed to onSelectError.

import { AttachmentField, AttachmentInput } from "@/components/ui/attachment-field";

<AttachmentField
  maxFiles={3}
  label="Upload file"
  onSelectFiles={openNativeFilePicker}
  onSelectError={handleFilePickerError}
>
  <AttachmentInput />
</AttachmentField>;

NativeFile consists of uri, name, type, size, and optional previewUrl. File, Blob, HTML <input type="file">, and object URLs are not used. When accept is set to images only, image items are displayed; if previewUrl is missing, uri is used. In general file mode, a file icon, name, and size are displayed.

Files returned by the host adapter are validated by AttachmentInput using accept, maxFiles, minFileSize, maxFileSize, and validate conditions. The picker's MIME filter is only for UI convenience; final validation must be performed by the app and upload server. If the adapter returns an empty array indicating cancellation, the list remains unchanged.

Directly Constructing Items

AttachmentInput supports context render prop. You can directly render acceptedFileEntries as AttachmentInputItem and update the app's upload status using updateFileEntryStatus.

<AttachmentField onSelectFiles={openNativeFilePicker}>
  <AttachmentInput>
    {({ acceptedFileEntries }) =>
      acceptedFileEntries.map((fileEntry) => (
        <AttachmentInputItem key={fileEntry.id} fileEntry={fileEntry} />
      ))}
  </AttachmentInput>
</AttachmentField>;

AttachmentInputItemProps includes the root native props of the actual AttachmentInput.Item, so id, accessibility props, style, dragging, etc. can be passed. fileEntry is required; if onRetry is passed, a retry action appears in error state.

Uploading Files

Trigger

The default AttachmentInput provides both file selection trigger and attachment item list. Tapping the trigger calls the host adapter and returned files are added to the list.

Loading Lynx example…

Listening to Accepted File Changes

acceptedFileEntries is the list of validated files. onAcceptedFileEntriesChange passes list changes including additions, deletions, status changes, and reordering. Run the following example to see upload callback and logs together.

Loading Lynx example…

Validating Files

Max Files

maxFiles limits the final file count at the package level. Passing a limit or filter to the picker is supplementary for UX; excess results are rejected as TOO_MANY_FILES.

Loading Lynx example…

Invalid File Type

accept accepts MIME types (image/png, image/*) or file extension patterns. Files returned by bypassing host picker limits also undergo package validation and onFileReject is called with INVALID_TYPE.

Loading Lynx example…

File Size

minFileSize and maxFileSize validate NativeFile.size (bytes). If out of range, they are rejected as FILE_TOO_SMALL and FILE_TOO_LARGE respectively.

Loading Lynx example…

Custom Validation

validate returns an array of custom error codes or null for each NativeFile. Convert to messages matching your app in onFileReject.

Loading Lynx example…

Managing File Status

Each item has pending, uploading, success, error states. Newly added items start as pending. Start your app's upload job in onFileAccept and update uploading progress, success, error with the helper. The actual upload API and retry policy are owned by the host app.

Loading Lynx example…
  • uploading: ProgressCircle is displayed. Without progress, it shows indeterminate state. Image mode uses staticWhite tone to be visible over images.
  • error: If onRetry is provided, retry action is shown. In the retry callback, change the same id back to uploading and resume app upload.
  • If picker throws synchronously or returns a rejected Promise, onSelectError is called and the existing list is preserved.

Reordering Files

AttachmentInputReorderable in ui:attachment-field-reorderable uses native horizontal long-press gesture instead of browser dnd-kit. When the gesture ends, reorderFileEntry(fromIndex, toIndex) is called and the list order changes. Drag is blocked in disabled and readOnly states.

The default item snippet is composed of SortableAttachmentInputItem. When composing directly, pass the context as-is and must pass index to the exported SortableAttachmentInputItem. Do not remap or clone callback results with Children.toArray.

import {
  AttachmentInputReorderable,
  SortableAttachmentInputItem,
} from "@/components/ui/attachment-field-reorderable";

<AttachmentField>
  <AttachmentInputReorderable>
    {({ acceptedFileEntries }) =>
      acceptedFileEntries.map((fileEntry, index) => (
        <SortableAttachmentInputItem
          key={fileEntry.id}
          fileEntry={fileEntry}
          index={index}
        />
      ))}
  </AttachmentInputReorderable>
</AttachmentField>;
Loading Lynx example…

Examples

Showing Thumbnails

Allow only image files with accept="image/*" and display thumbnail using previewUrl returned by the host.

Loading Lynx example…

Disabled

disabled blocks trigger and add/sort operations. In the Lynx contract, deletion of existing items is allowed, so use readOnly or app policy if you need to block deletion as well.

Loading Lynx example…

Read Only

readOnly blocks trigger, add, delete, clear, and sort operations. Externally hydrating or updating controlled state is allowed.

Loading Lynx example…

Controlled

The list can be controlled externally using acceptedFileEntries and onAcceptedFileEntriesChange.

Loading Lynx example…

Custom Inset

Using the --seed-attachment-input-extend-x style variable extends the horizontal item list beyond the global gutter. This is an example using real file metadata and nested fields together.

Loading Lynx example…

Field Integration

label, description, indicator, errorMessage, required, showRequiredIndicator, invalid can be connected to Field slots. Manage invalid and messages in your app's submit-equivalent validation instead of HTML form submit.

Loading Lynx example…

Customizing Items

Instead of the default item, you can directly combine the package's compound AttachmentInput.Item with ItemBadge, ItemBackdrop, ProgressCircle, and action/icon slots. Display the representative badge according to your app's ordering policy.

Loading Lynx example…

Unsupported Lynx Features and App Alternatives

Dropzone

Browser drag-and-drop events and HTML file input from React AttachmentDropzone are not supported in Lynx. No working dropzone example is provided. Instead, connect the native file picker to onSelectFiles and map any drag gestures or additional host actions supported by your app to picker calls. Use ui:attachment-field-reorderable to change file list order.

HTML form

Lynx has no browser form, FormData, input[type=file] submission path. Maintain app state with onAcceptedFileEntriesChange or onFileAccept, then pass NativeFile[] to the host upload/submit adapter in the submit action. Map server validation errors to Field's invalid and errorMessage.

react-hook-form

React examples using react-hook-form cannot run as-is because they depend on browser file input and form submission paths. In Lynx, connect acceptedFileEntries and onAcceptedFileEntriesChange to your app state, validate list length and upload status on submit, then update Field error state.

Browser file APIs

File, Blob, FileList, object URLs, and window.alert are not used in Native examples. The host adapter must provide NativeFile's uri, metadata, and optional previewUrl. Return [] on cancel, and onSelectError receives picker or permission errors.

Table of contents