Uploading report.pdf
x

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/progress.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Progress } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type ProgressParts = Component.Headless.Parts<typeof Progress>
    
    const sizes = { sm: "sm", md: "md", lg: "lg" } as const
    const variants = { default: "default", success: "success", destructive: "destructive" } as const
    
    export type ProgressSize = keyof typeof sizes
    export type ProgressVariant = keyof typeof variants
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /**
     * The `role="progressbar"` root: a two-column grid with the label and the value
     * above the track. `size` sets the track's height and `variant` the fill.
     * Without a label, chain an `aria-label`.
     */
    export const ProgressRoot = <Msg>(
      h: Html.H<Msg>,
      progress: ProgressParts,
      options: ClassOption & {
        readonly size?: ProgressSize | undefined
        readonly variant?: ProgressVariant | undefined
      } = {}
    ) =>
      progress.root(
        h.div
          .class(cn("group/progress grid w-full grid-cols-[1fr_auto] items-baseline gap-x-3 gap-y-2", options.class))
          .attrs({ "data-size": sizes[options.size ?? "md"], "data-variant": variants[options.variant ?? "default"] })
      )
    
    export const ProgressLabel = <Msg>(h: Html.H<Msg>, progress: ProgressParts, options: ClassOption = {}) =>
      progress.label(h.span.class(cn("text-[13px] font-medium text-foreground", options.class)))
    
    /** The value's text. The kit fills it in when it has no children. */
    export const ProgressValue = <Msg>(h: Html.H<Msg>, progress: ProgressParts, options: ClassOption = {}) =>
      progress.value(h.span.class(cn("text-[13px] text-muted-foreground tabular-nums", options.class)))
    
    export const ProgressTrack = <Msg>(h: Html.H<Msg>, progress: ProgressParts, options: ClassOption = {}) =>
      progress.track(
        h.div.class(cn(
          "col-span-2 h-2 overflow-hidden rounded-full bg-secondary group-data-[size=sm]/progress:h-1 " +
            "group-data-[size=lg]/progress:h-3",
          options.class
        ))
      )
    
    /**
     * The fill. The kit sets its width, and none while indeterminate, when a
     * short bar slides across the track (`animate-indeterminate` is defined in
     * `styles/tailwind.css`).
     */
    export const ProgressIndicator = <Msg>(h: Html.H<Msg>, progress: ProgressParts, options: ClassOption = {}) =>
      progress.indicator(
        h.div.class(cn(
          "h-full rounded-full bg-brand transition-[width] duration-300 ease-out " +
            "data-[indeterminate]:w-2/5 data-[indeterminate]:animate-indeterminate " +
            "group-data-[variant=success]/progress:bg-success group-data-[variant=destructive]/progress:bg-destructive",
          options.class
        ))
      )
    
  3. Import it

    import { ProgressRoot, ProgressLabel, ProgressValue, ProgressTrack, ProgressIndicator } from "#ui/progress"
    

Usage

With a label and a value.ts
import { Subscription, UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Progress } from "@effect/ui-kit"
import { Schema, Stream } from "effect"
import { Button } from "#ui/button"
import { ProgressIndicator, ProgressLabel, ProgressRoot, ProgressTrack, ProgressValue } from "#ui/progress"

const ProgressUploadMessage = UiSchema.TaggedEnum({ Ticked: {}, PressedRestart: {} })

export const ProgressUpload = Html.component({
  name: "ProgressUpload",
  model: Schema.Struct({ sent: Schema.Number }),
  message: ProgressUploadMessage
}).make({
  init: () => ({ sent: 12 }),
  update: {
    Ticked: ($) => $.evolve({ sent: (sent) => Math.min(100, sent + 4) }),
    PressedRestart: ($) => $.evolve({ sent: 0 })
  },
  subscriptions: ({ model }) => [
    model.sent < 100 &&
    Subscription.make("tick", Stream.tick("300 millis").pipe(Stream.as(ProgressUploadMessage.Ticked())))
  ],
  view: (h, model) =>
    h.div.class("flex w-full max-w-sm flex-col gap-4").children(
      Progress(h, {
        props: { value: model.sent },
        view: (progress) =>
          ProgressRoot(h, progress).children(
            ProgressLabel(h, progress).children(
              progress.status === "complete" ? "Uploaded" : "Uploading report.pdf"
            ),
            ProgressValue(h, progress),
            ProgressTrack(h, progress).children(
              ProgressIndicator(h, progress)
            )
          )
      }),
      h.div.class("flex items-center gap-2").children(
        Button(h, { variant: "outline", size: "sm", onPress: ProgressUploadMessage.PressedRestart() })
          .children("Restart")
      )
    )
})

Examples

Controlled

The bar alone, named with aria-label, following a value that buttons change.

x
40%

Indeterminate

A null value is unknown progress: the kit sets data-indeterminate and no width.

Connecting to the server
x

Sizes

Three track heights from the size option of the root.

sm
x
md
x
lg
x

Colours by state

The variant option of the root picks the fill: brand, success or destructive.

Building
x
Tests passed
x
Deploy failed
x
Waiting for a runner
x

Steps in a card

min, max and valueText turn the percentage into a step count.

Workspace
x

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <div role="progressbar">. Its aria-labelledby points at the label.
labelA <span> inside the root that names the bar.
valueA <span> that shows the value text when it has no children.
trackThe <div> the indicator fills.
indicatorThe <div> inside the track. The kit sets its width while the value is known.
status, percentage, valueTextPlain values for markup that depends on them.

API reference

Props

PropTypeDescription
valuenumber | nullHow far the task has got. Required. null means unknown (indeterminate).
minnumberThe start of the range. 0 by default.
maxnumberThe end of the range. 100 by default.
valueTextstringThe text for aria-valuetext and the value part. The rounded percentage when left out.

Data attributes

AttributeDescription
data-progressingOn every part while the value is below max.
data-completeOn every part once the value is max.
data-indeterminateOn every part while the value is null.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.