Components

File upload

A field of files, each with a status: picked, dropped or pasted, checked against limits, previewed, and sent by a transport you pass in.

Previews are drawn in the theme you made.

Edit it
size
scheme
direction

Basic #

import { Bundles, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// One image. The picker is the native input's label, so the dialog, the
// keys and a form's submission are the browser's own.
export default example({
  title: 'Basic',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'button' },
  state: Bundles.FileUpload.with({ id: 'logo', accept: ['image/*'] }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view({ model, onMessage, label: 'Choose an image', name: 'logo', style }, h),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Avatar #

Up to 2.0 MB each

import { Option } from 'effect'
import { Avatar, Bundles, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Face = Recipes.Avatar.style({ size: 'lg' })

// `picture` draws the picker's face from the first file's image, here with
// the Avatar the application already has. No list is drawn.
export default example({
  title: 'Avatar',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'avatar' },
  state: Bundles.FileUpload.with({ id: 'photo', accept: ['image/*'], maxSize: 2_000_000 }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      {
        model,
        onMessage,
        label: 'Change photo',
        picture: url =>
          Avatar.view(
            {
              name: 'Ada Lovelace',
              ...Option.match(url, { onNone: () => ({}), onSome: src => ({ src }) }),
              style: Face,
            },
            h,
          ),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Drag and drop #

Contracts, invoices and anything else we should read.

Up to 5 files, 5.0 MB each

import { Bundles, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// The limits are the state's, so they are args: a file over them is refused
// and the reason listed. The prompt is a word; the label names the input.
export default example({
  title: 'Drag and drop',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone' },
  state: Bundles.FileUpload.with({
    id: 'documents',
    multiple: true,
    maxFiles: 5,
    maxSize: 5_000_000,
  }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      {
        model,
        onMessage,
        label: 'browse',
        words: { prompt: 'Drop files here or' },
        description: 'Contracts, invoices and anything else we should read.',
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

import { Bundles, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// `list: 'grid'` draws each file as a tile with its thumbnail: the same
// markup as the rows, laid out by the recipe.
export default example({
  title: 'Gallery',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone', list: 'grid' },
  state: Bundles.FileUpload.with({ id: 'gallery', multiple: true, accept: ['image/*'] }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      { model, onMessage, label: 'browse', words: { prompt: 'Drop images here or' }, style },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Progress: three at a time (name a file "fail" to see a retry) #

import { Stream } from 'effect'
import { Bundle } from 'foldkit-bundle'
import {
  Bundles,
  FileUpload,
  FileUploadProgress,
  FileUploadSlots,
  Recipes,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { simulated } from '../../snippets/upload.js'

const LANES = 3
const App = Bundle.compose({}).pipe(
  Bundle.withChild('upload', Bundles.FileUpload, {
    args: { id: 'sent', multiple: true, lanes: LANES },
    onOut: Bundle.ignore,
  }),
)
const lift = (message: FileUpload.Message) => App.Message.GotUploadMessage({ message })

// The widget sends nothing: the application's transport and the Progress
// bar are both passed in, so a field that only posts with a form ships neither.
const Bar = FileUploadProgress.make({ style: Recipes.Progress.style({}) })

export default example({
  title: 'Progress: three at a time (name a file "fail" to see a retry)',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone' },
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    subscriptions: FileUpload.lanes({
      count: LANES,
      model: (model: typeof App.Model.Type) => model.upload,
      send: job => simulated(job).pipe(Stream.map(lift)),
    }),
  }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      {
        model: model.upload,
        onMessage: message => onMessage(lift(message)),
        label: 'browse',
        words: { prompt: 'Drop files here or' },
        progress: Bar,
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Composed: the files as a table #

Files
NameSizeStatusActions
import { Option, Stream } from 'effect'
import { Bundle } from 'foldkit-bundle'
import {
  Bundles,
  FileUpload,
  FileUploadProgress,
  FileUploadSlots,
  Recipes,
  Table,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { label } from '../../snippets/layout.js'
import { simulated } from '../../snippets/upload.js'

const LANES = 2
const App = Bundle.compose({}).pipe(
  Bundle.withChild('upload', Bundles.FileUpload, {
    args: { id: 'tabled', multiple: true, lanes: LANES },
    onOut: Bundle.ignore,
  }),
)
const lift = (message: FileUpload.Message) => App.Message.GotUploadMessage({ message })
const Rows = Recipes.Table.style({})
const Ring = FileUploadProgress.make({
  style: Recipes.Progress.style({ shape: 'ring' }),
})

// `FileUpload.parts` hands back the drawings `view` assembles, apart, so the
// files are rows of a Table the application lays out.
export default example({
  title: 'Composed: the files as a table',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone' },
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    subscriptions: FileUpload.lanes({
      count: LANES,
      model: (model: typeof App.Model.Type) => model.upload,
      send: job => simulated(job).pipe(Stream.map(lift)),
    }),
  }),
  view: ({ style, model, onMessage, h }) => {
    const drawn = FileUpload.parts(
      {
        model: model.upload,
        onMessage: message => onMessage(lift(message)),
        label: 'browse',
        words: { prompt: 'Drop files here or' },
        progress: Ring,
        style,
      },
      h,
    )
    const or = (part: Option.Option<ReturnType<typeof label>>) =>
      Option.getOrElse(part, () => label(h, []))
    return drawn.root([
      drawn.input,
      ...Option.toArray(drawn.picker),
      drawn.status,
      Table.view(
        {
          caption: 'Files',
          rows: drawn.items,
          rowKey: row => row.entry.id,
          columns: [
            { header: 'Name', rowHeader: true, value: row => row.name },
            { header: 'Size', numeric: true, value: row => row.meta },
            { header: 'Status', value: row => or(Option.orElse(row.progress, () => row.state)) },
            { header: 'Actions', value: row => or(row.actions) },
          ],
          style: Rows,
        },
        h,
      ),
    ])
  },
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Image upload #

Up to 8 files

import { Stream } from 'effect'
import { Bundle } from 'foldkit-bundle'
import {
  Bundles,
  FileUpload,
  FileUploadProgress,
  FileUploadSlots,
  Recipes,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { simulated } from '../../snippets/upload.js'

const LANES = 2
const App = Bundle.compose({}).pipe(
  Bundle.withChild('upload', Bundles.FileUpload, {
    args: { id: 'images', multiple: true, accept: ['image/*'], maxFiles: 8, lanes: LANES },
    onOut: Bundle.ignore,
  }),
)
const lift = (message: FileUpload.Message) => App.Message.GotUploadMessage({ message })
const Bar = FileUploadProgress.make({ style: Recipes.Progress.style({}) })

// The gallery and the sending together: each tile shows its thumbnail at
// once, from the file in the page, and a bar until it has been sent.
export default example({
  title: 'Image upload',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone', list: 'grid' },
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    subscriptions: FileUpload.lanes({
      count: LANES,
      model: (model: typeof App.Model.Type) => model.upload,
      send: job => simulated(job).pipe(Stream.map(lift)),
    }),
  }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      {
        model: model.upload,
        onMessage: message => onMessage(lift(message)),
        label: 'browse',
        words: { prompt: 'Drop images here or' },
        progress: Bar,
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

In a card #

Attachments

Anything the reviewer should see beside the request.

import { Bundles, Card, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Panel = Recipes.Card.style({})

// Composition is nesting: the field is the card's content.
export default example({
  title: 'In a card',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone' },
  state: Bundles.FileUpload.with({ id: 'attachments', multiple: true }),
  view: ({ style, model, onMessage, h }) =>
    Card.view(
      {
        title: 'Attachments',
        description: 'Anything the reviewer should see beside the request.',
        content: FileUpload.view(
          { model, onMessage, label: 'browse', words: { prompt: 'Drop files here or' }, style },
          h,
        ),
        style: Panel,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Cover #

import { Option, Stream } from 'effect'
import { Bundle } from 'foldkit-bundle'
import {
  Bundles,
  FileUpload,
  FileUploadProgress,
  FileUploadSlots,
  Recipes,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { label } from '../../snippets/layout.js'
import { simulated } from '../../snippets/upload.js'

const App = Bundle.compose({}).pipe(
  Bundle.withChild('upload', Bundles.FileUpload, {
    args: { id: 'cover', accept: ['image/*'], lanes: 1 },
    onOut: Bundle.ignore,
  }),
)
const lift = (message: FileUpload.Message) => App.Message.GotUploadMessage({ message })
const Ring = FileUploadProgress.make({
  style: Recipes.Progress.style({ shape: 'ring' }),
})

// One wide image: the picker is the picture, and a ring stands on it while
// the file is sent.
export default example({
  title: 'Cover',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'cover' },
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    subscriptions: FileUpload.lanes({
      count: 1,
      model: (model: typeof App.Model.Type) => model.upload,
      send: job => simulated(job).pipe(Stream.map(lift)),
    }),
  }),
  view: ({ style, model, onMessage, h }) =>
    FileUpload.view(
      {
        model: model.upload,
        onMessage: message => onMessage(lift(message)),
        label: 'Cover image',
        picture: url =>
          Option.match(url, {
            onNone: () => label(h, ['Add a cover image']),
            onSome: src => h.img([h.Src(src), h.Alt('')]),
          }),
        progress: Ring,
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Configure once #

import { Bundles, FileUpload, FileUploadSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// Given once: the application's words for every upload field. `configure`
// binds `parts` too, for a layout of the application's own.
const Upload = FileUpload.configure({
  base: { words: { prompt: 'Drag files in, or', remove: 'Take {name} off', clear: 'Start over' } },
  presets: { quiet: { words: { prompt: '' } } },
})

export default example({
  title: 'Configure once',
  slots: FileUploadSlots,
  recipe: Recipes.FileUpload,
  pins: { layout: 'zone' },
  state: Bundles.FileUpload.with({ id: 'configured', multiple: true }),
  view: ({ style, model, onMessage, h }) =>
    Upload.view({ model, onMessage, label: 'choose them', style }, h),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗

  • Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).
  • What a form submits Give the widget a name and a form reads it as it reads a native field.