"use client"; import { cn } from "@/lib/utils"; import * as React from "react"; import { Badge } from "@/components/ui/badge"; import { MinusIcon, TrendingUpIcon, ArrowUpIcon, ChevronUpIcon, TrendingDownIcon, ArrowDownIcon, ChevronDownIcon } from "@/components/ui/phosphor-icons"; type DeltaIconVariant = "default" | "trend" | "arrow"; type DeltaVariant = "default" | "badge"; type DeltaContextValue = { value: number; }; const DeltaContext = React.createContext(null); function useDeltaValue() { const context = React.useContext(DeltaContext); if (!context) { throw new Error( "DeltaIcon and DeltaValue must be used inside a `Delta` component." ); } return context.value; } function Delta({ className, value, variant = "default", ...props }: React.ComponentProps<"div"> & { value: number; variant?: DeltaVariant; }) { return ( {variant === "badge" ? ( 0 ? "bg-emerald-500/10 text-emerald-500" : "bg-red-500/10 text-red-500", className )} data-slot="delta" variant="secondary" {...(props as React.ComponentProps)} /> ) : (
0 ? "text-emerald-600 dark:text-emerald-400" : "", value < 0 ? "text-rose-600 dark:text-rose-400" : "", className )} data-slot="delta" {...props} /> )} ); } function FilledShell({ value, children, }: { value: number; children: React.ReactNode; }) { return ( 0 && "bg-emerald-500", value < 0 && "bg-red-500", (!value || value === 0) && "bg-muted-foreground" )} data-slot="delta-icon" > {children} ); } function DeltaIcon({ variant = "default", filled = false, className, ...props }: Omit, "fill"> & { variant?: DeltaIconVariant; filled?: boolean; }) { const resolvedValue = useDeltaValue(); const mergedClassName = cn(className); const shell = (node: React.ReactElement) => filled ? {node} : node; const slotProps = filled ? {} : { "data-slot": "delta-icon" as const }; if (!resolvedValue || resolvedValue === 0) { return shell( ); } if (resolvedValue > 0) { if (variant === "trend") { return shell( ); } if (variant === "arrow") { return shell( ); } return shell( ); } if (variant === "trend") { return shell( ); } if (variant === "arrow") { return shell( ); } return shell( ); } function DeltaValue({ className, precision = 1, suffix = "%", absolute = true, ...props }: React.ComponentProps<"span"> & { precision?: number; suffix?: string; absolute?: boolean; }) { const resolvedValue = useDeltaValue(); const formattedValue = ( absolute ? Math.abs(resolvedValue) : resolvedValue ).toFixed(precision); return ( {formattedValue} {suffix} ); } export { Delta, DeltaIcon, DeltaValue };