Skip to content
Gx
GitHub

Progress

A bar that shows the completion of a task.

<progress.Progress value={50} label="Upload" />

Installation

Run the command in the app module.

Terminal
gx add progress

The command writes these files.

File Path in the app
Progress.gx ui/progress/Progress.gx
Progress.fixtures.go ui/progress/Progress.fixtures.go
styles.go ui/progress/styles.go

Copy each file to its path in the app. Change each import of a registry package to the path of that package in the app.

ui/progress/Progress.gx
package progress

props {
  // Value is the current value. The bar clamps it to the range 0 to Max.
  Value int = 0
  // Max is the maximum value. A zero value is 100.
  Max   int = 100
  // Label is the accessible name of the progress bar.
  Label string = ""
  // Class adds classes to the root element.
  Class string = ""
  // Attrs adds HTML attributes to the root element.
  Attrs gx.Attrs = nil
}

<div role="progressbar" aria-valuemin={0} aria-valuemax={p.max()} aria-valuenow={p.Value} aria-label={p.Label} class={gx.Cx("relative h-2 w-full overflow-hidden rounded-full bg-primary/20", p.Class)} {...p.Attrs}>
  <div class="h-full w-full flex-1 bg-primary transition-all motion-reduce:transition-none" style={gx.Style("transform: translateX(-" + p.offset() + "%)")}></div>
</div>
ui/progress/Progress.fixtures.go
package progress

import "github.com/alternayte/gx"

var ProgressFixtures = gx.Fixtures[ProgressProps]{
	"Half":  {Value: 50, Label: "Upload"},
	"Full":  {Value: 100, Label: "Upload"},
	"Empty": {Value: 0, Label: "Upload"},
}
ui/progress/styles.go
package progress

import "strconv"

// max returns the maximum value; a zero value is 100.
func (p ProgressProps) max() int {
	if p.Max == 0 {
		return 100
	}
	return p.Max
}

// offset returns the translated percentage of the unfilled part.
func (p ProgressProps) offset() string {
	v := p.Value
	max := p.max()
	if max <= 0 {
		max = 100
	}
	if v < 0 {
		v = 0
	}
	if v > max {
		v = max
	}
	return strconv.Itoa(100 - (v * 100 / max))
}

The theme must define these tokens: --primary.

Usage

<progress.Progress value={p.Done} max={p.Total} label="Upload" />

Examples

Half

<progress.Progress value={50} label="Upload" />

Full

<progress.Progress value={100} label="Upload" />

Empty

<progress.Progress value={0} label="Upload" />

API reference

A tag sets a prop by its name with a lower-case first letter: Class is class.

progress.Progress

Prop Type Default Description
Value int 0 Value is the current value. The bar clamps it to the range 0 to Max.
Max int 100 Max is the maximum value. A zero value is 100.
Label string "" Label is the accessible name of the progress bar.
Class string "" Class adds classes to the root element.
Attrs gx.Attrs nil Attrs adds HTML attributes to the root element.

Do and do not

Do

  • Set Label so the bar has an accessible name.
  • Update the value as the task advances.

Don't

  • Do not use a progress bar for an unknown duration. Use a spinner.
  • Do not animate the bar backward without a reason.

Keyboard

Key Action
None The component is static.