Docs
Shadcn

Progress 进度条

展示任务或流程的完成进度。

Preview
正在下载歌单68%
Code
"use client"

import * as React from "react"

import { Progress } from "@scopify/ui/shadcn/components/progress"

export default function ProgressDemo() {
  const [progress, setProgress] = React.useState(13)

  React.useEffect(() => {
    const timer = setTimeout(() => setProgress(66), 500)
    return () => clearTimeout(timer)
  }, [])

  return <Progress value={progress} className="w-[60%]" />
}

Scopify 说明

本页按 shadcn/ui 官方 Radix UI 组件文档的章节与示例完整整理。示例预览使用 Scopify 当前的 @scopify/ui/shadcn 实现;导入路径、主题 Token 和行为差异以本仓库源码为准。

Installation

npx shadcn@latest add progress

Usage

import { Progress } from "@scopify/ui/shadcn/components/progress"
<Progress value={33} />

Label

Use a Field component to add a label to the progress bar.

Preview
正在下载歌单68%
Code
import { Field, FieldLabel } from "@scopify/ui/shadcn/components/field"
import { Progress } from "@scopify/ui/shadcn/components/progress"

export function ProgressWithLabel() {
  return (
    <Field className="w-full max-w-sm">
      <FieldLabel htmlFor="progress-upload">
        <span>Upload progress</span>
        <span className="ml-auto">66%</span>
      </FieldLabel>
      <Progress value={66} id="progress-upload" />
    </Field>
  )
}

Controlled

A progress bar that can be controlled by a slider.

Preview
正在下载歌单68%
Code
"use client"

import * as React from "react"

import { Progress } from "@scopify/ui/shadcn/components/progress"
import { Slider } from "@scopify/ui/shadcn/components/slider"

export function ProgressControlled() {
  const [value, setValue] = React.useState([50])

  return (
    <div className="flex w-full max-w-sm flex-col gap-4">
      <Progress value={value[0]} />
      <Slider
        value={value}
        onValueChange={setValue}
        min={0}
        max={100}
        step={1}
      />
    </div>
  )
}

RTL

To enable RTL support in shadcn/ui, see the RTL configuration guide.

Preview
正在下载歌单68%
Code
"use client"

import * as React from "react"

import {
  useTranslation,
  type Translations,
} from "@/components/language-selector"
import { Field, FieldLabel } from "@/styles/radix-nova/ui-rtl/field"
import { Progress } from "@/styles/radix-nova/ui-rtl/progress"

const translations: Translations = {
  en: {
    dir: "ltr",
    values: {
      label: "Upload progress",
    },
  },
  ar: {
    dir: "rtl",
    values: {
      label: "تقدم الرفع",
    },
  },
  he: {
    dir: "rtl",
    values: {
      label: "התקדמות העלאה",
    },
  },
}

function toArabicNumerals(num: number): string {
  const arabicNumerals = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]
  return num
    .toString()
    .split("")
    .map((digit) => arabicNumerals[parseInt(digit, 10)])
    .join("")
}

export function ProgressRtl() {
  const { dir, t, language } = useTranslation(translations, "ar")

  const formatNumber = (num: number): string => {
    if (language === "ar") {
      return toArabicNumerals(num)
    }
    return num.toString()
  }

  return (
    <Field className="w-full max-w-sm" dir={dir}>
      <FieldLabel htmlFor="progress-upload">
        <span>{t.label}</span>
        <span className="ms-auto">{formatNumber(66)}%</span>
      </FieldLabel>
      <Progress value={66} id="progress-upload" className="rtl:rotate-180" />
    </Field>
  )
}

API Reference

See the Radix UI Progress documentation.

本页内容