{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"metric-card","type":"registry:ui","title":"Metric card","description":"A compact summary for a number that needs context.","categories":["data"],"dependencies":["motion"],"registryDependencies":["https://ui.teamofsilicons.com/r/silicon-motion-tokens.json","https://ui.teamofsilicons.com/r/animated-counter.json"],"files":[{"path":"registry/components/metric-card/metric-card.tsx","type":"registry:component","target":"@components/silicon-ui/metric-card/metric-card.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, type Variants } from \"motion/react\";\nimport { motionTokens } from \"../lib/motion-tokens\";\nimport { AnimatedCounter } from \"../animated-counter/animated-counter\";\nimport styles from \"./metric-card.module.css\";\nexport interface MetricCardProps { label: string; value: number; suffix?: string; context: string; change?: string }\n\n/** Copy that holds a number enters from the side it moved toward: a larger value rises from below, a smaller one drops from above. */\nconst rise: Variants = { hidden: (direction: number) => ({ opacity: 0, y: `${.3 * direction}em`, filter: `blur(${motionTokens.blur.soft}px)` }), shown: { opacity: 1, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.enter] } }, gone: (direction: number) => ({ opacity: 0, y: `${-.3 * direction}em`, filter: `blur(${motionTokens.blur.subtle}px)`, transition: { duration: motionTokens.duration.fast, ease: [...motionTokens.ease.standard] } }) };\nconst fade: Variants = { hidden: { opacity: 0, y: 0, filter: \"blur(0px)\" }, shown: { opacity: 1, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.instant } }, gone: { opacity: 0, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.instant } } };\nconst amountIn = (text: string) => Number(text.replace(/,/g, \"\").match(/-?\\d+(?:\\.\\d+)?/)?.[0] ?? NaN);\n\n/** New copy rises in while the old copy leaves; `morph` springs the wrapper to the new text's width instead of letting it snap. */\nfunction Swap({ text, morph = false, block = false }: { text: string; morph?: boolean; block?: boolean }) {\n  const reduceMotion = !!useReducedMotion();\n  const sizer = useRef<HTMLSpanElement>(null);\n  const width = useMotionValue<number | \"auto\">(\"auto\");\n  const [shown, setShown] = useState({ text, direction: 1 });\n  if (shown.text !== text) setShown({ text, direction: amountIn(text) < amountIn(shown.text) ? -1 : 1 });\n  useEffect(() => {\n    const node = sizer.current;\n    if (!node || typeof ResizeObserver === \"undefined\") return;\n    let measured: string | null = null;\n    // Layout size, not the transformed rect, so a scaling parent never leaves the text clipped. Only a new text springs; font loads jump.\n    const observer = new ResizeObserver(([entry]) => {\n      if (!entry) return;\n      const next = entry.borderBoxSize?.[0]?.inlineSize ?? node.offsetWidth;\n      if (next && measured !== null && measured !== node.textContent && !reduceMotion) animate(width, next, motionTokens.spring.morph);\n      else width.jump(next || \"auto\");\n      measured = next ? node.textContent : null;\n    });\n    observer.observe(node);\n    return () => observer.disconnect();\n  }, [morph, reduceMotion, width]);\n  return <motion.span className={block ? styles.swapBlock : styles.swap} style={morph ? { width } : undefined}>\n    {morph && <span ref={sizer} className={styles.sizer} aria-hidden=\"true\">{text}</span>}\n    <AnimatePresence mode=\"popLayout\" initial={false} custom={shown.direction}><motion.span key={text} className={styles.text} custom={shown.direction} variants={reduceMotion ? fade : rise} initial=\"hidden\" animate=\"shown\" exit=\"gone\">{text}</motion.span></AnimatePresence>\n  </motion.span>;\n}\n\nexport function MetricCard({ label, value, suffix, context, change }: MetricCardProps) {\n  const reduceMotion = !!useReducedMotion();\n  return <article className={styles.card}>\n    <div className={styles.top}><span><Swap text={label} block /></span><AnimatePresence initial={false}>{change && <motion.small key=\"change\" data-trend={/^[+]/.test(change) ? \"up\" : /^[-−]/.test(change) ? \"down\" : undefined} initial={{ opacity: 0, scale: reduceMotion ? 1 : .96 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: reduceMotion ? 1 : .96, transition: { duration: motionTokens.duration.fast, ease: [...motionTokens.ease.standard] } }} transition={reduceMotion ? { duration: 0 } : motionTokens.spring.snappy}><Swap text={change} morph /></motion.small>}</AnimatePresence></div>\n    <AnimatedCounter value={value} suffix={suffix} animateOnView />\n    <p><Swap text={context} block /></p>\n  </article>;\n}\n"},{"path":"registry/components/metric-card/metric-card.module.css","type":"registry:component","target":"@components/silicon-ui/metric-card/metric-card.module.css","content":".card { min-width: 0; border: 1px solid var(--border); border-radius: var(--radius-overlay); corner-shape: var(--corner-shape); padding: var(--space-section); background: var(--surface); box-shadow: var(--shadow-resting); }\n.top { display: flex; align-items: center; justify-content: space-between; gap: var(--space-group); margin-bottom: var(--space-block); }\n.top span { color: var(--text-secondary); font-size: var(--text-sm); }\n.top small { display: inline-flex; flex: 0 0 auto; border: 1px solid var(--border); border-radius: var(--radius-full); corner-shape: round; padding: var(--space-tight) var(--space-related); color: var(--text-secondary); font-size: var(--text-sm); font-variant-numeric: tabular-nums; }\n.card p { margin: var(--space-inset) 0 0; color: var(--text-muted); font-size: var(--text-sm); line-height: var(--leading-body); }\n@media (max-width: 380px) { .card { padding: var(--space-inset); } .top { align-items: flex-start; flex-wrap: wrap; margin-bottom: var(--space-section); } }\n/* Text swaps: the outgoing copy is popped out of flow so the wrapper never holds both widths. */\n.swap, .swapBlock { position: relative; max-width: 100%; }\n.swap { display: inline-flex; overflow-x: clip; white-space: nowrap; vertical-align: top; }\n.swapBlock { display: block; }\n.swap > .text { display: inline-block; }\n.swapBlock > .text { display: block; }\n.sizer { position: absolute; top: 0; left: 0; visibility: hidden; white-space: nowrap; pointer-events: none; }\n/* A signed change reads its direction in color as well as sign: a faint tint behind, the color on the text. */\n.top small[data-trend] span { color: inherit; }\n.top small[data-trend] { border-color: transparent; transition: color var(--duration-fast) var(--ease-standard), background-color var(--duration-fast) var(--ease-standard); }\n.top small[data-trend=\"up\"] { background: color-mix(in oklab, var(--success) 11%, transparent); color: var(--success); }\n.top small[data-trend=\"down\"] { background: color-mix(in oklab, var(--danger) 11%, transparent); color: var(--danger); }\n"},{"path":"registry/components/animated-counter/animated-counter.tsx","type":"registry:component","target":"@components/silicon-ui/animated-counter/animated-counter.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { AnimatePresence, animate, motion, useInView, useMotionValue, useReducedMotion, useTransform, type MotionValue, type Variants } from \"motion/react\";\nimport { motionTokens } from \"../lib/motion-tokens\";\nimport styles from \"./animated-counter.module.css\";\n\nexport interface AnimatedCounterProps {\n  value: number; label?: string; prefix?: string; suffix?: string; decimals?: number;\n  /** Roll every digit up from zero the first time the counter scrolls into view. */\n  animateOnView?: boolean;\n  /** Formatting locale. Fixed by default so server and client render the same digits. */\n  locale?: string;\n}\n\ntype Part = { key: string; digit: number; order: number } | { key: string; text: string };\n\nconst DIGITS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];\nconst rise: Variants = { hidden: { opacity: 0, y: \"0.3em\", filter: `blur(${motionTokens.blur.soft}px)` }, shown: { opacity: 1, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.enter] } }, gone: { opacity: 0, y: \"-0.3em\", filter: `blur(${motionTokens.blur.subtle}px)`, transition: { duration: motionTokens.duration.fast, ease: [...motionTokens.ease.standard] } } };\nconst fade: Variants = { hidden: { opacity: 0, y: 0, filter: \"blur(0px)\" }, shown: { opacity: 1, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.instant } }, gone: { opacity: 0, y: 0, filter: \"blur(0px)\", transition: { duration: motionTokens.duration.instant } } };\nconst reveal = { ...motionTokens.spring.smooth, visualDuration: motionTokens.duration.expressive };\n\n/** Split a formatted number into columns keyed by place value, so 999 → 1,000 keeps the ones column the ones column. */\nfunction partsFor(value: number, decimals: number, locale: string): Part[] {\n  const parts = new Intl.NumberFormat(locale, { minimumFractionDigits: decimals, maximumFractionDigits: decimals, numberingSystem: \"latn\" }).formatToParts(value);\n  let place = parts.reduce((count, part) => count + (part.type === \"integer\" ? part.value.length : 0), 0);\n  let fraction = 0;\n  let order = 0;\n  return parts.flatMap((part, index): Part[] => {\n    if (part.type === \"integer\") return [...part.value].map(char => ({ key: `i${--place}`, digit: Number(char), order: order++ }));\n    if (part.type === \"fraction\") return [...part.value].map(char => ({ key: `f${fraction++}`, digit: Number(char), order: order++ }));\n    return [{ key: part.type === \"group\" ? `g${place}` : part.type === \"decimal\" ? \"d\" : `${part.type}${index}`, text: part.value }];\n  });\n}\n\n/** One digit on the wheel. Its offset from the wheel position decides where it sits and how visible it is. */\nfunction Glyph({ position, digit }: { position: MotionValue<number>; digit: number }) {\n  const offset = useTransform(position, current => ((((digit - current) % 10) + 15) % 10) - 5);\n  const y = useTransform(offset, current => `${current}em`);\n  const opacity = useTransform(offset, current => Math.max(0, 1 - Math.abs(current)));\n  const visibility = useTransform(offset, current => Math.abs(current) >= 1 ? \"hidden\" : \"visible\");\n  const filter = useTransform(offset, current => Math.abs(current) < .02 || Math.abs(current) >= 1 ? \"none\" : `blur(${(Math.abs(current) * motionTokens.blur.subtle).toFixed(2)}px)`);\n  return <motion.span className={styles.glyph} style={{ y, opacity, filter, visibility }}>{digit}</motion.span>;\n}\n\nconst presence = { initial: { width: 0, opacity: 0 }, animate: { width: \"auto\", opacity: 1 }, exit: { width: 0, opacity: 0 } };\n\n/** A digit wheel. It always turns in the direction the whole number moved, wrapping 9 → 0 like an odometer. */\nfunction Column({ digit, direction, armed, delay, reduceMotion }: { digit: number; direction: number; armed: boolean; delay: number; reduceMotion: boolean }) {\n  const position = useMotionValue(armed ? 0 : digit);\n  const wheel = useRef({ digit: armed ? 0 : digit, target: armed ? 0 : digit, revealed: !armed });\n  useEffect(() => {\n    const state = wheel.current;\n    if (armed || state.digit === digit) { if (!armed) state.revealed = true; return; }\n    state.target += direction < 0 && state.revealed ? -((state.digit - digit + 10) % 10) : (digit - state.digit + 10) % 10;\n    state.digit = digit;\n    if (reduceMotion) position.jump(state.target);\n    else animate(position, state.target, state.revealed ? motionTokens.spring.smooth : { ...reveal, delay });\n    state.revealed = true;\n  }, [armed, delay, digit, direction, position, reduceMotion]);\n  return <motion.span className={styles.column} {...presence} transition={reduceMotion ? { duration: 0 } : motionTokens.spring.morph}>\n    <span className={styles.sizer}>0</span>\n    {DIGITS.map(item => <Glyph key={item} position={position} digit={item} />)}\n  </motion.span>;\n}\n\nexport function AnimatedCounter({ value, label, prefix = \"\", suffix = \"\", decimals = 0, animateOnView = false, locale = \"en-US\" }: AnimatedCounterProps) {\n  const ref = useRef<HTMLSpanElement>(null);\n  const inView = useInView(ref, { once: true, amount: .6 });\n  const reduceMotion = !!useReducedMotion();\n  const [previous, setPrevious] = useState(value);\n  const [direction, setDirection] = useState(1);\n  if (value !== previous) { setPrevious(value); setDirection(value > previous ? 1 : -1); }\n  const parts = partsFor(value, decimals, locale);\n  const text = `${prefix}${parts.map(part => \"text\" in part ? part.text : part.digit).join(\"\")}${suffix}`;\n  const armed = animateOnView && !inView;\n  return <span ref={ref} className={styles.counter}>\n    {label && <span className={styles.label}><span className={styles.labelSwap}><AnimatePresence mode=\"popLayout\" initial={false}><motion.span key={label} className={styles.labelText} variants={reduceMotion ? fade : rise} initial=\"hidden\" animate=\"shown\" exit=\"gone\">{label}</motion.span></AnimatePresence></span></span>}\n    <span className={styles.srOnly}>{text}</span>\n    <span className={styles.value} aria-hidden=\"true\">\n      {prefix && <span className={styles.symbol}>{prefix}</span>}\n      <AnimatePresence initial={false}>\n        {parts.map(part => \"digit\" in part\n          ? <Column key={part.key} digit={part.digit} direction={direction} armed={armed} delay={Math.min(part.order * motionTokens.stagger.item, .25)} reduceMotion={reduceMotion} />\n          : <motion.span key={part.key} className={styles.symbol} {...presence} transition={reduceMotion ? { duration: 0 } : motionTokens.spring.morph}>{part.text}</motion.span>)}\n      </AnimatePresence>\n      {suffix && <span className={styles.symbol}>{suffix}</span>}\n    </span>\n  </span>;\n}\n\nexport default AnimatedCounter;\n"},{"path":"registry/components/animated-counter/animated-counter.module.css","type":"registry:component","target":"@components/silicon-ui/animated-counter/animated-counter.module.css","content":".counter { position: relative; display: inline-flex; flex-direction: column; color: var(--foreground); font-family: var(--font-display); font-size: var(--text-3xl); font-weight: 500; letter-spacing: var(--tracking-display); line-height: 1; font-variant-numeric: tabular-nums; }\n.label { margin-bottom: var(--space-related); color: var(--text-muted); font-family: var(--font-body); font-size: var(--text-xs); font-weight: 400; letter-spacing: var(--tracking-body); line-height: var(--leading-body); }\n/* Each digit is a clipped wheel; separators clip sideways only so a comma keeps its tail. */\n.value { display: inline-flex; align-items: flex-start; white-space: nowrap; user-select: none; }\n/* The wheel's window reaches a little past the line box and feathers out, so a turning digit fades at the edge instead of being cut. */\n.column { --feather: .16em; position: relative; display: inline-block; overflow: hidden; margin-block: calc(var(--feather) * -1); padding-block: var(--feather); mask-image: linear-gradient(to bottom, transparent, #000 calc(var(--feather) * 1.5), #000 calc(100% - var(--feather) * 1.5), transparent); }\n.sizer { visibility: hidden; }\n.glyph { position: absolute; inset: var(--feather) 0; text-align: center; }\n.labelText { display: inline-block; }\n.labelSwap { position: relative; display: block; }\n.symbol { display: inline-block; overflow-x: clip; }\n.srOnly { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n"}],"docs":"Docs and live preview: https://ui.teamofsilicons.com/metric-card","meta":{"tier":"free","kind":"component","docs":"https://ui.teamofsilicons.com/metric-card","markdown":"https://ui.teamofsilicons.com/metric-card.md","tags":["data","summary"],"status":"stable"}}