Progress
Linear progress is a simple progress bar that can be used to show the progress of a task such as downloading a file, uploading an image, etc.
Features
Install
Install the component from your command line.
Anatomy
Import all parts and piece them together.
<script setup lang="ts">import * as progress from "@destyler/progress"import { normalizeProps, useMachine } from "@destyler/vue"import { computed,useId } from "vue"
const [state, send] = useMachine(progress.machine({ id: useId(), value: 30}))
const api = computed(() => progress.connect(state.value, send, normalizeProps),)</script>
<template> <div v-bind="api.getRootProps()"> <div v-bind="api.getLabelProps()"></div> <div v-bind="api.getTrackProps()"> <div v-bind="api.getRangeProps()" /> </div> </div></template>import * as progress from '@destyler/progress'import { normalizeProps, useMachine } from '@destyler/react'import { useId } from 'react'
export default function Progress() { const [state, send] = useMachine(progress.machine({ id: useId(), value: 30, }))
const api = progress.connect(state, send, normalizeProps)
return ( <div {...api.getRootProps()}> <div {...api.getLabelProps()}></div> <div {...api.getTrackProps()}> <div {...api.getRangeProps()}/> </div> </div> )}<script lang="ts"> import * as progress from "@destyler/progress" import { normalizeProps, useMachine } from "@destyler/svelte"
const id = $props.id()
const [state, send] = useMachine(progress.machine({ id, value: 30 }))
const api = $derived(progress.connect(state, send, normalizeProps))</script>
<div {...api.getRootProps()}> <div {...api.getLabelProps()}></div> <div {...api.getTrackProps()}> <div {...api.getRangeProps()}></div> </div></div>import * as progress from '@destyler/progress'import { normalizeProps, useMachine } from '@destyler/solid'import { createMemo, createUniqueId } from 'solid-js'
export default function Progress() { const [state, send] = useMachine(progress.machine({ id: createUniqueId(), value: 30, }))
const api = createMemo(() => progress.connect(state, send, normalizeProps))
return ( <div {...api().getRootProps()}> <div {...api().getLabelProps()}></div> <div {...api().getTrackProps()}> <div {...api().getRangeProps()}/> </div> </div> )}Setting the value
use the api.setValue method to set the value of the progress bar.
const [state, send] = useMachine( progress.machine({ value: 50, }),)Setting the min number and max number values
By default, the progress bar has a min value of 0 and a max value of 100.
You can change these values by passing the min and max options to the machine.
const [state, send] = useMachine( progress.machine({ min: 0, max: 1000, }),)Use the indeterminate state
The progress component is determinate by default,
with the value and max set to 50 and 100 respectively.
Set value to null to indicate an indeterminate value for operations whose progress can’t be determined (e.g., attempting to reconnect to a server).
const [state, send] = useMachine( progress.machine({ value: null, }),)Showing a value text
Progress bars can only be interpreted by sighted users.
To include a text description to support assistive technologies like screen readers,
use the valueText part.
const [state, send] = useMachine( progress.machine({ translations: { valueText: ({ value, max }) => value == null ? "Loading..." : `${value} of ${max} items loaded`, }, }),)Then you need to render the valueText part in your component.
<script setup lang="ts">import * as progress from "@destyler/progress"import { normalizeProps, useMachine } from "@destyler/vue"import { computed,useId } from "vue"
const [state, send] = useMachine(progress.machine({ id: useId(), value: 30, translations: { valueText: ({ value, max }) => value == null ? "Loading..." : `${value} of ${max} items loaded`, },}))
const api = computed(() => progress.connect(state.value, send, normalizeProps),)</script>
<template> <div v-bind="api.getValueTextProps()"> {{ api.valueAsString }} </div></template>import * as progress from '@destyler/progress'import { normalizeProps, useMachine } from '@destyler/react'import { useId } from 'react'
export default function Progress() { const [state, send] = useMachine(progress.machine({ id: useId(), value: 30, translations: { valueText: ({ value, max }) => value == null ? "Loading..." : `${value} of ${max} items loaded`, }, }))
const api = progress.connect(state, send, normalizeProps)
return ( <div {...api.getValueTextProps()}> {api.valueAsString} </div> )}<script lang="ts"> import * as progress from "@destyler/progress" import { normalizeProps, useMachine } from "@destyler/svelte"
const id = $props.id()
const [state, send] = useMachine(progress.machine({ id, value: 30, translations: { valueText: ({ value, max }) => value == null ? "Loading..." : `${value} of ${max} items loaded`, }, }))
const api = $derived(progress.connect(state, send, normalizeProps))</script>
<div {...api.getValueTextProps()}> {api.valueAsString}</div>import * as progress from '@destyler/progress'import { normalizeProps, useMachine } from '@destyler/solid'import { createMemo, createUniqueId } from 'solid-js'
export default function Progress() { const [state, send] = useMachine(progress.machine({ id: createUniqueId(), value: 30, translations: { valueText: ({ value, max }) => value == null ? "Loading..." : `${value} of ${max} items loaded`, }, }))
const api = createMemo(() => progress.connect(state, send, normalizeProps))
return ( <div {...api().getValueTextProps()}> {api().valueAsString} </div> )}Styling Guide
Earlier, we mentioned that each collapse part has a
data-partattribute added to them to select and style them in the DOM.
[data-scope="progress"][data-part="root"] { /* Styles for the root part */}
[data-scope="progress"][data-part="track"] { /* Styles for the track part */}
[data-scope="progress"][data-part="range"] { /* Styles for the range part */}Indeterminate state
To style the indeterminate state, you can use the [data-state=indeterminate] selector.
[data-scope="progress"][data-part="root"][data-state="indeterminate"] { /* Styles for the root indeterminate state */}
[data-scope="progress"][data-part="track"][data-state="indeterminate"] { /* Styles for the root indeterminate state */}
[data-scope="progress"][data-part="range"][data-state="indeterminate"] { /* Styles for the root indeterminate state */}Methods and Properties
Machine Context
The progress machine exposes the following context properties:
Partial<{ root: string; track: string; label: string; circle: string; }>numbernumbernumberIntlTranslations(details: ValueChangeDetails) => void"ltr" | "rtl"string() => ShadowRoot | Node | DocumentOrientationMachine API
The progress api exposes the following methods:
numberstring(value: number) => void() => void() => voidnumberstringnumbernumberbooleanData Attributes
Root
data-scopedata-partdata-maxdata-valuedata-statedata-orientationLabel
data-scopedata-partdata-orientationRange
data-scopedata-partdata-orientationdata-stateCircle Track
data-scopedata-partdata-orientationCircle Range
data-scopedata-partdata-stateView
data-scopedata-partdata-state