Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion r/stat-card.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"path": "src/components/cipher/ui/stat-card.tsx",
"type": "registry:file",
"target": "src/components/cipher/ui/stat-card.tsx",
"content": "import * as React from \"react\";\n\nimport type { CipherLinkProps } from \"../lib/link\";\nimport { cn } from \"../lib/utils\";\nimport { Card, CardContent, CardLink } from \"./card\";\nimport { Icon, type IconSource } from \"./icon\";\nimport { Media } from \"./item\";\nimport { Skeleton } from \"./skeleton\";\nimport { StatusText } from \"./text\";\n\nimport styles from \"./stat-card.module.css\";\n\nconst trendIcon = {\n up: \"arrow-up\",\n down: \"arrow-down\",\n flat: \"minus\"\n} as const;\nconst trendTone = { up: \"ok\", down: \"bad\", flat: \"neutral\" } as const;\n\nexport type StatCardProps = Omit<\n React.ComponentProps<typeof Card>,\n \"title\" | \"badges\" | \"description\" | \"hint\" | \"action\" | \"footer\" | \"footerPlacement\" | \"children\"\n> & {\n label: React.ReactNode;\n value: React.ReactNode;\n icon?: IconSource;\n /** Tone of the icon, for a metric that needs attention. */\n tone?: \"ok\" | \"warn\" | \"bad\" | \"info\";\n /** The change since the last period, such as \"+12%\". Its text carries the direction; the arrow is decoration. */\n delta?: React.ReactNode;\n trend?: \"up\" | \"down\" | \"flat\";\n /** Defaults to ok for up, bad for down and neutral for flat; set it for a metric where up is bad. */\n deltaTone?: \"ok\" | \"bad\" | \"neutral\";\n /** A short line under the value: what the number counts, the comparison period, or a link. */\n note?: React.ReactNode;\n /** Makes the whole card a link, named by the label. */\n link?: CipherLinkProps[\"link\"];\n loading?: boolean;\n /** Replaces the value with this message when the metric failed to load. */\n error?: React.ReactNode;\n};\n\nexport function StatCard({\n label,\n value,\n icon,\n tone,\n delta,\n trend = \"flat\",\n deltaTone,\n note,\n link,\n loading = false,\n error,\n variant = \"framed\",\n className,\n ...props\n}: StatCardProps) {\n const id = React.useId();\n const labelId = `${id}-label`;\n const valueId = `${id}-value`;\n const failed = error !== undefined && error !== null && error !== false;\n return (\n <Card\n data-slot=\"stat-card\"\n role=\"group\"\n aria-labelledby={labelId}\n aria-busy={loading || undefined}\n variant={variant}\n className={cn(styles.statCard, className)}\n {...props}\n >\n <CardContent className={styles.body}>\n <div className={styles.head}>\n <span id={labelId} className={styles.label}>\n {link ? (\n <CardLink link={link} aria-describedby={valueId}>\n {label}\n </CardLink>\n ) : (\n label\n )}\n </span>\n {icon && <Media icon={icon} size=\"sm\" tone={tone} aria-hidden />}\n </div>\n {loading ? (\n <Skeleton className={styles.valueSkeleton} />\n ) : failed ? (\n <StatusText tone=\"bad\" icon=\"alert\" className={styles.error}>\n {error}\n </StatusText>\n ) : (\n <div id={valueId} data-slot=\"stat-card-value\" className={styles.value}>\n {value}\n </div>\n )}\n {!loading && !failed && (delta !== undefined || note !== undefined) && (\n <div className={styles.foot}>\n {delta !== undefined && (\n <span\n data-slot=\"stat-card-delta\"\n data-tone={deltaTone ?? trendTone[trend]}\n className={styles.delta}\n >\n <Icon name={trendIcon[trend]} aria-hidden />\n {delta}\n </span>\n )}\n {note !== undefined && <span className={styles.note}>{note}</span>}\n </div>\n )}\n </CardContent>\n </Card>\n );\n}\n"
"content": "import * as React from \"react\";\n\nimport type { CipherLinkProps } from \"../lib/link\";\nimport { useInFrame } from \"../lib/frame-context\";\nimport { cn } from \"../lib/utils\";\nimport { Card, CardContent, CardLink } from \"./card\";\nimport { Icon, type IconSource } from \"./icon\";\nimport { Media } from \"./item\";\nimport { Skeleton } from \"./skeleton\";\nimport { StatusText } from \"./text\";\n\nimport styles from \"./stat-card.module.css\";\n\nconst trendIcon = {\n up: \"arrow-up\",\n down: \"arrow-down\",\n flat: \"minus\"\n} as const;\nconst trendTone = { up: \"ok\", down: \"bad\", flat: \"neutral\" } as const;\n\nexport type StatCardProps = Omit<\n React.ComponentProps<typeof Card>,\n \"title\" | \"badges\" | \"description\" | \"hint\" | \"action\" | \"footer\" | \"footerPlacement\" | \"children\"\n> & {\n label: React.ReactNode;\n value: React.ReactNode;\n icon?: IconSource;\n /** Tone of the icon, for a metric that needs attention. */\n tone?: \"ok\" | \"warn\" | \"bad\" | \"info\";\n /** The change since the last period, such as \"+12%\". Its text carries the direction; the arrow is decoration. */\n delta?: React.ReactNode;\n trend?: \"up\" | \"down\" | \"flat\";\n /** Defaults to ok for up, bad for down and neutral for flat; set it for a metric where up is bad. */\n deltaTone?: \"ok\" | \"bad\" | \"neutral\";\n /** A short line under the value: what the number counts, the comparison period, or a link. */\n note?: React.ReactNode;\n /** Makes the whole card a link, named by the label. */\n link?: CipherLinkProps[\"link\"];\n loading?: boolean;\n /** Replaces the value with this message when the metric failed to load. */\n error?: React.ReactNode;\n};\n\nexport function StatCard({\n label,\n value,\n icon,\n tone,\n delta,\n trend = \"flat\",\n deltaTone,\n note,\n link,\n loading = false,\n error,\n variant = \"framed\",\n backdrop,\n className,\n ...props\n}: StatCardProps) {\n const inFrame = useInFrame(\"StatCard\");\n const panelBackdrop = variant === \"framed\" && !inFrame;\n const id = React.useId();\n const labelId = `${id}-label`;\n const valueId = `${id}-value`;\n const failed = error !== undefined && error !== null && error !== false;\n return (\n <Card\n data-slot=\"stat-card\"\n role=\"group\"\n aria-labelledby={labelId}\n aria-busy={loading || undefined}\n variant={variant}\n className={cn(styles.statCard, className)}\n backdrop={panelBackdrop ? undefined : backdrop}\n {...props}\n >\n <CardContent className={styles.body}>\n {panelBackdrop && backdrop}\n <div className={styles.head}>\n <span id={labelId} className={styles.label}>\n {link ? (\n <CardLink link={link} aria-describedby={valueId}>\n {label}\n </CardLink>\n ) : (\n label\n )}\n </span>\n {icon && <Media icon={icon} size=\"sm\" tone={tone} aria-hidden />}\n </div>\n {loading ? (\n <Skeleton className={styles.valueSkeleton} />\n ) : failed ? (\n <StatusText tone=\"bad\" icon=\"alert\" className={styles.error}>\n {error}\n </StatusText>\n ) : (\n <div id={valueId} data-slot=\"stat-card-value\" className={styles.value}>\n {value}\n </div>\n )}\n {!loading && !failed && (delta !== undefined || note !== undefined) && (\n <div className={styles.foot}>\n {delta !== undefined && (\n <span\n data-slot=\"stat-card-delta\"\n data-tone={deltaTone ?? trendTone[trend]}\n className={styles.delta}\n >\n <Icon name={trendIcon[trend]} aria-hidden />\n {delta}\n </span>\n )}\n {note !== undefined && <span className={styles.note}>{note}</span>}\n </div>\n )}\n </CardContent>\n </Card>\n );\n}\n"
},
{
"path": "src/components/cipher/ui/stat-card.module.css",
Expand Down
6 changes: 6 additions & 0 deletions src/ui/stat-card.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";

import type { CipherLinkProps } from "../lib/link";
import { useInFrame } from "../lib/frame-context";
import { cn } from "../lib/utils";
import { Card, CardContent, CardLink } from "./card";
import { Icon, type IconSource } from "./icon";
Expand Down Expand Up @@ -53,9 +54,12 @@ export function StatCard({
loading = false,
error,
variant = "framed",
backdrop,
className,
...props
}: StatCardProps) {
const inFrame = useInFrame("StatCard");
const panelBackdrop = variant === "framed" && !inFrame;
const id = React.useId();
const labelId = `${id}-label`;
const valueId = `${id}-value`;
Expand All @@ -68,9 +72,11 @@ export function StatCard({
aria-busy={loading || undefined}
variant={variant}
className={cn(styles.statCard, className)}
backdrop={panelBackdrop ? undefined : backdrop}
{...props}
>
<CardContent className={styles.body}>
{panelBackdrop && backdrop}
<div className={styles.head}>
<span id={labelId} className={styles.label}>
{link ? (
Expand Down
Binary file modified visual/baselines/desktop/stat-card-linked--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual/baselines/desktop/stat-card-linked--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual/baselines/mobile/stat-card-linked--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual/baselines/mobile/stat-card-linked--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading