Progress
Communicate determinate, indeterminate, complete, and failed progress.
Default
open Giraffe
open Microsoft.Extensions.DependencyInjection
open StarFederation.Datastar
open StarFederation.Datastar.DependencyInjection
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let progressExampleRegion percentage =
fveProgress {
_id "progress-example-value"
_ariaLabel "Example task"
_value (string percentage)
_max "100"
if percentage = 100 then
_state ProgressState.Complete
}
let operationalProgressExample =
div {
_class "grid gap-4"
_dataSignals "{components_progress_pending: false, components_progress_done: false}"
progressExampleRegion 0
// The trigger stays mounted while the server patches the progress region.
fveButton {
_id "progress-example-trigger"
_class "justify-self-start"
_color Color.Primary
_variant ButtonVariant.Solid
_dataIndicator "components_progress_pending"
_dataAttr ("disabled", "$components_progress_pending")
_dataAttr ("aria-busy", "$components_progress_pending ? 'true' : null")
_dataOn (
"click",
"@post('/components/progress/run', {filterSignals: {exclude: /.*/}, requestCancellation: 'cleanup', retry: 'never'})"
)
span {
_dataText
"$components_progress_pending ? 'Running…' : $components_progress_done ? 'Run again' : 'Run example'"
"Run example"
}
}
}
let progressExampleHandler: HttpHandler =
fun _ context ->
task {
let datastar = context.RequestServices.GetRequiredService<IDatastarService>()
try
do! datastar.PatchSignalsAsync({| components_progress_done = false |}, context.RequestAborted)
for percentage in 0..10..100 do
// This short delay simulates work; real work supplies the completed amount.
if percentage > 0 then
do! System.Threading.Tasks.Task.Delay(300, context.RequestAborted)
do!
datastar.PatchElementsAsync(
progressExampleRegion percentage |> Render.toString,
cancellationToken = context.RequestAborted
)
do! datastar.PatchSignalsAsync({| components_progress_done = true |}, context.RequestAborted)
with :? OperationCanceledException when context.RequestAborted.IsCancellationRequested ->
()
return Some context
}
let progressExampleRoutes: HttpHandler =
Giraffe.Core.POST
>=> route "/components/progress/run"
>=> progressExampleHandlerOn this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add progress --config src/Acme.Components/fve.jsonNo additional JavaScript import is required.
The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open Giraffe
open Microsoft.Extensions.DependencyInjection
open StarFederation.Datastar
open StarFederation.Datastar.DependencyInjection
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let progressExampleRegion percentage =
fveProgress {
_id "progress-example-value"
_ariaLabel "Example task"
_value (string percentage)
_max "100"
if percentage = 100 then
_state ProgressState.Complete
}
let operationalProgressExample =
div {
_class "grid gap-4"
_dataSignals "{components_progress_pending: false, components_progress_done: false}"
progressExampleRegion 0
// The trigger stays mounted while the server patches the progress region.
fveButton {
_id "progress-example-trigger"
_class "justify-self-start"
_color Color.Primary
_variant ButtonVariant.Solid
_dataIndicator "components_progress_pending"
_dataAttr ("disabled", "$components_progress_pending")
_dataAttr ("aria-busy", "$components_progress_pending ? 'true' : null")
_dataOn (
"click",
"@post('/components/progress/run', {filterSignals: {exclude: /.*/}, requestCancellation: 'cleanup', retry: 'never'})"
)
span {
_dataText
"$components_progress_pending ? 'Running…' : $components_progress_done ? 'Run again' : 'Run example'"
"Run example"
}
}
}
let progressExampleHandler: HttpHandler =
fun _ context ->
task {
let datastar = context.RequestServices.GetRequiredService<IDatastarService>()
try
do! datastar.PatchSignalsAsync({| components_progress_done = false |}, context.RequestAborted)
for percentage in 0..10..100 do
// This short delay simulates work; real work supplies the completed amount.
if percentage > 0 then
do! System.Threading.Tasks.Task.Delay(300, context.RequestAborted)
do!
datastar.PatchElementsAsync(
progressExampleRegion percentage |> Render.toString,
cancellationToken = context.RequestAborted
)
do! datastar.PatchSignalsAsync({| components_progress_done = true |}, context.RequestAborted)
with :? OperationCanceledException when context.RequestAborted.IsCancellationRequested ->
()
return Some context
}
let progressExampleRoutes: HttpHandler =
Giraffe.Core.POST
>=> route "/components/progress/run"
>=> progressExampleHandlerIndeterminate
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let indeterminateProgressExample =
fveProgress {
_ariaLabel "Preparing files"
_ariaValuetext "Checking source files"
p {
_class "text-sm text-[var(--fve-muted-text)]"
"No completion percentage is available yet."
}
}Complete
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let completedProgressExample =
fveProgress {
_ariaLabel "File upload"
_value "100"
_max "100"
_ariaValuetext "100%"
_state ProgressState.Complete
}Failed
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let failedProgressExample =
fveProgress {
_ariaLabel "File upload"
_value "68"
_max "100"
_ariaValuetext "Stopped at 68%"
_state ProgressState.Failed
p {
_class "text-sm text-[var(--fve-muted-text)]"
"The upload stopped. Try again."
}
}API reference
type ProgressState =
| Active
| Complete
| Failed
module ProgressDsl =
val fveProgress : ComponentElementBuilder
module ProgressAttributes =
val Html._state.Static : value:ProgressState -> HtmlAttribute