Skip to content

Solid

@anil-labs/file-picker-solid wraps the core engine in a Solid hook and component. Reactive values are exposed as accessors (call them as functions: selected()).

Install

bash
npm i @anil-labs/file-picker-solid

Quick start

The <FilePicker> component renders a ready-made trigger and selected-thumbnails strip. Options are passed as props:

tsx
import { FilePicker } from '@anil-labs/file-picker-solid'
import { createMemoryAdapter } from '@anil-labs/file-picker-core'
import '@anil-labs/file-picker-core/styles.css'

const adapter = createMemoryAdapter({ media: [], folders: [] })

export function App() {
  return (
    <FilePicker
      adapter={adapter}
      multiple
      label="Choose media"
      onSelect={(items) => console.log('confirmed', items)}
      onChange={(items) => console.log('changed', items)}
      onUpload={(items) => console.log('uploaded', items)}
    />
  )
}

<FilePicker> props

The component spreads all FilePickerOptions (adapter, multiple, title, theme, …) plus:

PropTypeDefaultDescription
labelstring'Select File'Trigger label.
showSelectedbooleantrueRender the selected-thumbnails strip.
onSelect(items: MediaItem[]) => voidFires when the user confirms with Done.
onChange(items: MediaItem[]) => voidFires on every selection change.
onUpload(items: MediaItem[]) => voidFires after an upload completes.

useFilePicker

For full control, use the hook and render your own UI. It creates the picker on mount and cleans up on scope dispose; reactive values are accessors.

tsx
import { useFilePicker } from '@anil-labs/file-picker-solid'
import { For } from 'solid-js'

function Gallery() {
  const { open, selected, isOpen, setSelected } = useFilePicker({
    adapter,
    multiple: true,
    onSelect: (items) => console.log('confirmed', items),
  })

  return (
    <div>
      <button onClick={open}>Open library {isOpen() ? '(open)' : ''}</button>
      <ul>
        <For each={selected()}>{(item) => <li>{item.filename}</li>}</For>
      </ul>
      <button onClick={() => setSelected(null)}>Clear</button>
    </div>
  )
}

Returned controller

FieldTypeDescription
selectedAccessor<MediaItem[]>The current selection — call it: selected().
isOpenAccessor<boolean>Whether the dialog is open — isOpen().
open() => voidOpen the dialog.
close() => voidClose the dialog.
setSelected(items: MediaItem[] | MediaItem | null) => voidReplace the selection.
pickerFilePicker | nullThe underlying engine.

The hook also accepts onSelect, onChange and onUpload callbacks alongside the standard options.

Released under the MIT License.