- Accordion
- Agent Thinking
- Alert
- Alert Dialog
- Announcement
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Checkbox
- Checkbox Card
- Code Block
- Collapsible
- Color Picker
- Combobox
- Command
- Composer
- Composer Loader
- Context Menu
- Data Grid
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- File Upload
- Hover Card
- Input
- Input Group
- Input Otp
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Meter
- Native Select
- Navigation Menu
- Number Field
- Pagination
- Period Switcher
- Popover
- Progress
- Questionnaire
- Radio Card
- Radio Group
- Resizable
- Reveal
- Scroll Area
- Search Field
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Social Button
- Sonner
- Spinner
- Status Dot
- Switch
- Switch Card
- Table
- Tabs
- Tag Group
- Textarea
- Theme Toggle
- Time Field
- Toggle
- Toggle Group
- Tooltip
- Trace
- Tree View
- Agent Creator 01
- Agent Limits Card 01
- Agent Progress 01
- AI Chat 01
- AI Image Generation 01
- AI Profile 01
- Alerts Card 01
- Auth Card 01
- Calendar 01
- Composer 01
- Composer Attachments 01
- Composer Panel 01
- Finance Dashboard 01
- Home Dashboard 01
- Marketing Dashboard 01
- Medical Profile 01
- Meeting Scheduler 01
- Multi Agent Chat 01
- Notification Center 01
- Patient Info Card 01
- Project Board 01
- Questionnaire 01
- Settings Modal 01
- Sidebar 01
- Stat Cards 01
- Task List 01
- Web Search 01
- Activity Rings Card 01
- Area Chart Card 01
- Bar List Card 01
- Combo Chart Card 01
- Contributions Card 01
- Earnings Chart Card 01
- Funnel Chart Card 01
- Heatmap Chart Card 01
- Line Chart Card 01
- Most Active Days Card 01
- Orders Chart Card 01
- Radar Chart Card 01
- Radial Chart Card 01
- Revenue Chart Card 01
- Sankey Chart Card 01
- Scatter Chart Card 01
- Sleep Score Card 01
- Stage Bars Card 01
- Steps Card 01
Sleep score
Restful
22 - 28 Jun
85
Installation
Set up the @cobalt registry first, as described in Installation, then add the block:
$ pnpm dlx shadcn@latest add @cobalt/sleep-score-card-01
Pass one or more periods, each with a label, summary, and three metrics. Hover or focus a metric to highlight its segment; use the arrows to move between periods.
Source
"use client";
import { cn } from "cn";
import * as React from "react";
import { useCountUp } from "@/components/hooks/use-count-up";
import { Button } from "@/components/ui/button";
import {
Card,
CardAction,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
PeriodSwitcher,
PeriodSwitcherLabel,
PeriodSwitcherNext,
PeriodSwitcherPrevious,
} from "@/components/ui/period-switcher";
import { Reveal } from "@/components/ui/reveal";
import { Separator } from "@/components/ui/separator";
export interface SleepScoreMetric {
label: string;
detail: string;
value: number;
max: number;
}
export interface SleepScorePeriod {
label: string;
summary: string;
metrics: [SleepScoreMetric, SleepScoreMetric, SleepScoreMetric];
}
const colors = [
"var(--sleep-score-duration, var(--chart-1))",
"var(--sleep-score-routine, var(--chart-2))",
"var(--sleep-score-rest, var(--chart-3))",
];
export function SleepScoreCard({
periods,
className,
}: {
periods: [SleepScorePeriod, ...SleepScorePeriod[]];
className?: string;
}) {
const [periodIndex, setPeriodIndex] = React.useState(0);
const [active, setActive] = React.useState<number | null>(null);
const period = periods[Math.min(periodIndex, periods.length - 1)];
const score = period.metrics.reduce(
(total, metric) => total + metric.value,
0
);
const scoreRef = useCountUp<HTMLSpanElement>(score);
const maximum = period.metrics.reduce(
(total, metric) => total + Math.max(0, metric.max),
0
);
let offset = 0;
return (
<Reveal
variant="card"
render={
<Card
radius={20}
variant="muted"
size="xs"
className={cn("@container w-full max-w-md", className)}
aria-label="Sleep score"
/>
}
>
<CardHeader>
<div>
<CardDescription>Sleep score</CardDescription>
<CardTitle size="lg">{period.summary}</CardTitle>
</div>
<CardAction>
<PeriodSwitcher>
<PeriodSwitcherPrevious
disabled={periodIndex === 0}
onClick={() => {
setPeriodIndex((index) => index - 1);
setActive(null);
}}
/>
<PeriodSwitcherLabel className="min-w-[110px]">
{period.label}
</PeriodSwitcherLabel>
<PeriodSwitcherNext
disabled={periodIndex >= periods.length - 1}
onClick={() => {
setPeriodIndex((index) => index + 1);
setActive(null);
}}
/>
</PeriodSwitcher>
</CardAction>
</CardHeader>
<div
className="relative mx-auto mb-1.5 flex size-[104px] items-center justify-center"
role="img"
aria-label={`Sleep score ${score} out of ${maximum}`}
>
<svg
viewBox="0 0 104 104"
className="absolute inset-0 size-full -rotate-90"
aria-hidden="true"
>
<circle
cx="52"
cy="52"
r="45"
fill="none"
stroke="var(--command-border, var(--border))"
strokeWidth="14"
/>
{period.metrics.map((metric, index) => {
const portion =
maximum > 0 ? (Math.max(0, metric.max) / maximum) * 100 : 0;
const length = Math.max(0, portion - 4);
const start = offset + 2;
offset += portion;
return (
<circle
key={`${periodIndex}-${metric.label}`}
cx="52"
cy="52"
r="45"
fill="none"
stroke={colors[index]}
strokeWidth="14"
strokeLinecap="round"
pathLength="100"
strokeDasharray={`${length} ${100 - length}`}
strokeDashoffset={-start}
opacity={active === null || active === index ? 1 : 0.25}
className="sleep-score-card-01-draw transition-opacity duration-150 motion-reduce:transition-none"
/>
);
})}
</svg>
<span
ref={scoreRef}
className="text-3xl leading-none font-medium tabular-nums"
>
{score.toLocaleString("en-US")}
</span>
</div>
<CardContent>
<Card variant="raised" size="xs" radius={10}>
<CardContent className="gap-0">
{period.metrics.map((metric, index) => (
<React.Fragment key={metric.label}>
{index > 0 && <Separator />}
<div className="flex min-h-8">
<Button
variant="unstyled"
type="button"
onMouseEnter={() => setActive(index)}
onMouseLeave={() => setActive(null)}
onFocus={() => setActive(index)}
onBlur={() => setActive(null)}
className="w-full justify-start gap-2 text-left whitespace-normal"
>
<svg
viewBox="0 0 12 12"
className="size-3 shrink-0"
aria-hidden="true"
>
<rect
width="12"
height="12"
rx="4"
fill={colors[index]}
/>
</svg>
<span className="min-w-0 flex-1 font-normal text-muted-foreground">
{metric.label}
<span className="hidden @[280px]:inline">
: {metric.detail}
</span>
</span>
<span className="shrink-0 font-medium tabular-nums">
{metric.value}/{metric.max}
</span>
</Button>
</div>
</React.Fragment>
))}
</CardContent>
</Card>
</CardContent>
</Reveal>
);
} On this page