'use client' import { View } from '@hanzo/gui'; import { css } from '@hanzo/ui' import * as React from 'react'; import { ink } from './ink'; import { cn } from './utils'; // --------------------------------------------------------------------------- // Size presets matching the original Chakra progressCircle recipe // --------------------------------------------------------------------------- const SIZE_MAP = { xs: { size: 24, thickness: 4, fontSize: 10 }, sm: { size: 32, thickness: 5, fontSize: 10 }, md: { size: 40, thickness: 6, fontSize: 12 }, lg: { size: 48, thickness: 7, fontSize: 14 }, xl: { size: 64, thickness: 8, fontSize: 14 }, } as const; type Size = keyof typeof SIZE_MAP; // --------------------------------------------------------------------------- // Context // --------------------------------------------------------------------------- interface ProgressCircleCtx { readonly value: number | null; readonly size: Size; readonly colorPalette: string; } const Ctx = React.createContext({ value: 0, size: 'md', colorPalette: 'blue', }); // --------------------------------------------------------------------------- // Root // --------------------------------------------------------------------------- export interface ProgressCircleRootProps extends Omit, 'color'> { readonly value?: number | null; readonly size?: Size; readonly colorPalette?: string; } export const ProgressCircleRoot = React.forwardRef< HTMLDivElement, ProgressCircleRootProps >(function ProgressCircleRoot(props, ref) { const { value = 0, size = 'md', colorPalette = 'blue', className, children, ...rest } = props; const ctx = React.useMemo( () => ({ value: value ?? null, size, colorPalette }), [ value, size, colorPalette ], ); return ( { ink(children) } ); }); // --------------------------------------------------------------------------- // Ring (SVG track + range arc) // --------------------------------------------------------------------------- export interface ProgressCircleRingProps extends Omit, 'color'> { readonly trackColor?: string; readonly cap?: React.SVGAttributes['strokeLinecap']; readonly color?: string; } export const ProgressCircleRing = React.forwardRef< SVGSVGElement, ProgressCircleRingProps >(function ProgressCircleRing(props, ref) { const { trackColor, cap, color, className, ...rest } = props; const { value, size: sizeKey } = React.useContext(Ctx); const { size, thickness } = SIZE_MAP[sizeKey]; const radius = size / 2 - thickness / 2; const circumference = 2 * Math.PI * radius; const percent = value ?? 0; const offset = circumference * ((100 - percent) / 100); return ( { /* Track */ } { /* Range */ } ); }); // --------------------------------------------------------------------------- // Value text (centered number) // --------------------------------------------------------------------------- export interface ProgressCircleValueTextProps extends React.HTMLAttributes {} export const ProgressCircleValueText = React.forwardRef< HTMLDivElement, ProgressCircleValueTextProps >(function ProgressCircleValueText(props, ref) { const { className, children, ...rest } = props; const { value, size: sizeKey } = React.useContext(Ctx); const { fontSize } = SIZE_MAP[sizeKey]; return ( can, only the inside it. `ink()` below hosts the string // in a real Text, but callers may also pass their own element, so this // stays a style object rather than gui text props: it composes either // way, exactly like the Tailwind classes it replaces did through // inheritance. Tailwind's `leading-none` (line-height: 1, unitless) and // `tracking-tight` (-0.025em) are both relative to font size, so they are // computed per size tier here instead of copied as one constant. style={{ fontWeight: 500, fontSize, lineHeight: `${ fontSize }px`, letterSpacing: fontSize * -0.025, fontVariantNumeric: 'tabular-nums', }} className={ className } { ...(rest as object) } > { ink(children ?? `${ Math.round(value ?? 0) }%`) } ); });