跳至正文
获取 Pro
基础原语U.26

进度条

FreeRegistry
components/linyi/progress.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { Progress as BaseProgress } from "@base-ui/react/progress";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
interface ProgressContextValue {
ratio: number;
indeterminate: boolean;
}
 
const ProgressContext = React.createContext<ProgressContextValue>({
ratio: 0,
indeterminate: false,
});
 
export const progressTrackVariants = cva(
"relative w-full overflow-hidden rounded-pill bg-well border border-line/80",
{
variants: {
size: {
sm: "h-1.5",
md: "h-2",
lg: "h-3",
},
},
defaultVariants: {
size: "md",
},
}
);
 
export const progressIndicatorVariants = cva(
"h-full w-full origin-left rounded-pill will-change-transform transition-transform duration-240 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none",
{
variants: {
tone: {
accent: "bg-accent",
solid: "bg-inverse",
success: "bg-success",
warning: "bg-warning",
danger: "bg-danger",
},
},
defaultVariants: {
tone: "accent",
},
}
);
 
export interface ProgressProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseProgress.Root>,
"className"
> {
className?: string;
}
 
export function Progress({
className,
value,
min = 0,
max = 100,
...props
}: ProgressProps) {
const indeterminate = value === null;
const span = max - min;
const ratio =
indeterminate || span <= 0
? 0
: Math.min(1, Math.max(0, (value - min) / span));
 
return (
<ProgressContext.Provider value={{ ratio, indeterminate }}>
<BaseProgress.Root
value={value}
min={min}
max={max}
className={cn("flex w-full flex-col gap-1.5", className)}
{...props}
/>
</ProgressContext.Provider>
);
}
 
export function ProgressLabel({
className,
...props
}: Omit<
React.ComponentPropsWithRef<typeof BaseProgress.Label>,
"className"
> & {
className?: string;
}) {
return (
<BaseProgress.Label
className={cn("text-xs font-medium text-fg", className)}
{...props}
/>
);
}
 
export function ProgressValue({
className,
...props
}: Omit<
React.ComponentPropsWithRef<typeof BaseProgress.Value>,
"className"
> & {
className?: string;
}) {
return (
<BaseProgress.Value
className={cn("font-mono-num text-[11px] text-fg-secondary", className)}
{...props}
/>
);
}
 
export interface ProgressTrackProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseProgress.Track>,
"className"
>,
VariantProps<typeof progressTrackVariants> {
className?: string;
}
 
export function ProgressTrack({
className,
size = "md",
...props
}: ProgressTrackProps) {
return (
<BaseProgress.Track
className={cn(progressTrackVariants({ size }), className)}
{...props}
/>
);
}
 
export interface ProgressIndicatorProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseProgress.Indicator>,
"className"
>,
VariantProps<typeof progressIndicatorVariants> {
className?: string;
}
 
export function ProgressIndicator({
className,
tone = "accent",
style,
...props
}: ProgressIndicatorProps) {
const { ratio, indeterminate } = React.useContext(ProgressContext);
 
return (
<BaseProgress.Indicator
className={cn(
progressIndicatorVariants({ tone }),
indeterminate && "animate-pulse motion-reduce:animate-none",
className
)}
style={{
width: "100%",
transform: indeterminate ? "scaleX(0.38)" : `scaleX(${ratio})`,
...style,
}}
{...props}
/>
);
}