{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sleep-widget-01","title":"Sleep Widget","description":"Last night's sleep: the stages along a usage bar with a time axis, the totals per stage, a smart alarm switch and a wake-window slider.","dependencies":["@base-ui/react"],"registryDependencies":["@sortui/card","@sortui/separator","@sortui/switch","@sortui/usage-bar","@sortui/utils"],"files":[{"path":"registry/blocks/sleep-widget-01/components/sleep-widget.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { Slider as SliderPrimitive } from \"@base-ui/react/slider\"\n\nimport { cn } from \"@/lib/utils\"\nimport {\n  Card,\n  CardAction,\n  CardContent,\n  CardDescription,\n  CardHeader,\n  CardTitle,\n} from \"@/components/ui/card\"\nimport { Separator } from \"@/components/ui/separator\"\nimport { Switch } from \"@/components/ui/switch\"\nimport {\n  UsageBar,\n  UsageBarAxis,\n  UsageBarLegendItem,\n  UsageBarSegment,\n  UsageBarTrack,\n} from \"@/components/ui/usage-bar\"\n\ntype Stage = \"deep\" | \"light\" | \"rem\" | \"awake\"\n\nconst stages: { stage: Stage; label: string; className: string }[] = [\n  { stage: \"deep\", label: \"Deep\", className: \"bg-chart-2\" },\n  { stage: \"rem\", label: \"REM\", className: \"bg-chart-4\" },\n  { stage: \"light\", label: \"Light\", className: \"bg-chart-8\" },\n  { stage: \"awake\", label: \"Awake\", className: \"bg-chart-7\" },\n]\n\nconst night: [Stage, number][] = [\n  [\"light\", 18],\n  [\"deep\", 42],\n  [\"light\", 28],\n  [\"rem\", 14],\n  [\"awake\", 4],\n  [\"light\", 36],\n  [\"deep\", 48],\n  [\"light\", 22],\n  [\"rem\", 26],\n  [\"light\", 34],\n  [\"deep\", 28],\n  [\"awake\", 6],\n  [\"light\", 30],\n  [\"rem\", 32],\n  [\"light\", 24],\n  [\"deep\", 16],\n  [\"rem\", 38],\n  [\"awake\", 5],\n  [\"light\", 25],\n]\n\nconst hours = [\"11 PM\", \"1 AM\", \"3 AM\", \"5 AM\", \"7 AM\"]\n\nfunction duration(minutes: number) {\n  const h = Math.floor(minutes / 60)\n  const m = minutes % 60\n  return h > 0 ? `${h}h ${m}m` : `${m}m`\n}\n\n/** Last night's sleep: the stages on a usage bar, their totals, a smart alarm and its wake window. */\nexport function SleepWidget({ className }: { className?: string }) {\n  const [alarm, setAlarm] = React.useState(false)\n  const [wakeWindow, setWakeWindow] = React.useState(30)\n\n  const total = night.reduce((sum, [, minutes]) => sum + minutes, 0)\n  const byStage = (stage: Stage) =>\n    night\n      .filter(([s]) => s === stage)\n      .reduce((sum, [, minutes]) => sum + minutes, 0)\n  const asleep = total - byStage(\"awake\")\n  const stageClass = (stage: Stage) =>\n    stages.find((item) => item.stage === stage)?.className\n\n  return (\n    <Card className={cn(\"h-106 w-full max-w-96 gap-6\", className)}>\n      <CardHeader>\n        <CardTitle>Sleep</CardTitle>\n        <CardDescription>Last night, 11:12 PM to 7:08 AM</CardDescription>\n        <CardAction className=\"flex items-baseline gap-0.5 whitespace-nowrap\">\n          <span className=\"text-2xl leading-7 font-medium\">\n            {Math.floor(asleep / 60)}\n          </span>\n          <span className=\"text-sm text-muted-foreground\">h</span>\n          <span className=\"text-2xl leading-7 font-medium\">{asleep % 60}</span>\n          <span className=\"text-sm text-muted-foreground\">m</span>\n        </CardAction>\n      </CardHeader>\n      <CardContent className=\"flex flex-1 flex-col gap-4\">\n        <UsageBar max={total} className=\"gap-3\">\n          <UsageBarTrack>\n            {night.map(([stage, minutes], index) => (\n              <UsageBarSegment\n                key={index}\n                value={minutes}\n                className={cn(\"rounded-[1px]\", stageClass(stage))}\n              />\n            ))}\n          </UsageBarTrack>\n          <UsageBarAxis\n            ticks={hours.map((_, index) => (total * index) / 4)}\n            format={(value) => hours[Math.round((value / total) * 4)]}\n          />\n        </UsageBar>\n        <div className=\"grid grid-cols-2 gap-x-6 gap-y-2\">\n          {stages.map((item) => (\n            <div\n              key={item.stage}\n              className=\"flex items-center justify-between gap-2\"\n            >\n              <UsageBarLegendItem\n                swatchClassName={item.className}\n                className=\"text-muted-foreground\"\n              >\n                {item.label}\n              </UsageBarLegendItem>\n              <span className=\"font-medium tabular-nums\">\n                {duration(byStage(item.stage))}\n              </span>\n            </div>\n          ))}\n        </div>\n        <Separator />\n        <div className=\"flex h-40.5 flex-col justify-between\">\n          <div className=\"flex items-center gap-3\">\n            <div className=\"flex min-w-0 flex-1 flex-col\">\n              <span className=\"font-medium\">Smart alarm</span>\n              <span className=\"text-xs text-muted-foreground\">\n                Wakes you in a light stage before 7:30 AM\n              </span>\n            </div>\n            <Switch\n              checked={alarm}\n              onCheckedChange={setAlarm}\n              aria-label=\"Smart alarm\"\n            />\n          </div>\n          <div className=\"flex flex-col gap-2\">\n            <div className=\"flex items-center justify-between\">\n              <span className=\"text-muted-foreground\">Wake window</span>\n              <span className=\"font-medium tabular-nums\">{wakeWindow} min</span>\n            </div>\n            <BarSlider\n              label=\"Wake window\"\n              value={wakeWindow}\n              onValueChange={setWakeWindow}\n              min={0}\n              max={60}\n              step={5}\n              format={(value) => `${value} min`}\n            />\n          </div>\n        </div>\n      </CardContent>\n    </Card>\n  )\n}\n\nfunction BarSlider({\n  label,\n  value,\n  onValueChange,\n  min,\n  max,\n  step,\n  format,\n}: {\n  label: string\n  value: number\n  onValueChange: (value: number) => void\n  min: number\n  max: number\n  step: number\n  format: (value: number) => string\n}) {\n  const [pressed, setPressed] = React.useState(false)\n  const [pointerFocus, setPointerFocus] = React.useState(false)\n  const inputRef = React.useRef<HTMLInputElement>(null)\n  const ratio = (value - min) / (max - min)\n\n  const valueAt = (clientX: number, row: HTMLElement) => {\n    const rect = row.getBoundingClientRect()\n    const position = Math.min(\n      1,\n      Math.max(0, (clientX - rect.left) / rect.width)\n    )\n    return Math.round((position * (max - min)) / step) * step + min\n  }\n\n  return (\n    <div\n      onPointerDown={(event) => {\n        if (event.pointerType === \"mouse\" && event.button !== 0) return\n        event.preventDefault()\n        event.currentTarget.setPointerCapture(event.pointerId)\n        inputRef.current?.focus({ preventScroll: true })\n        setPointerFocus(true)\n        setPressed(true)\n        onValueChange(valueAt(event.clientX, event.currentTarget))\n      }}\n      onPointerMove={(event) => {\n        if (pressed) onValueChange(valueAt(event.clientX, event.currentTarget))\n      }}\n      onPointerUp={() => setPressed(false)}\n      onPointerCancel={() => setPressed(false)}\n      onKeyDownCapture={() => setPointerFocus(false)}\n      onBlur={() => setPointerFocus(false)}\n      style={{ \"--fill\": ratio } as React.CSSProperties}\n      className={cn(\n        \"group/bar-slider relative h-8 w-full cursor-ew-resize touch-none overflow-hidden rounded-md bg-fill-2 outline-ring transition-shadow select-none\",\n        !pointerFocus &&\n          \"has-focus-visible:ring-3 has-focus-visible:ring-ring/30 has-focus-visible:outline-1 has-focus-visible:-outline-offset-1 has-focus-visible:outline-solid\"\n      )}\n    >\n      <SliderPrimitive.Root\n        value={[value]}\n        onValueChange={(next) =>\n          onValueChange(Array.isArray(next) ? next[0] : next)\n        }\n        min={min}\n        max={max}\n        step={step}\n        className=\"pointer-events-none absolute inset-0 opacity-0\"\n      >\n        <SliderPrimitive.Control className=\"size-full\">\n          <SliderPrimitive.Track className=\"size-full\">\n            <SliderPrimitive.Thumb\n              inputRef={inputRef}\n              aria-label={label}\n              getAriaValueText={() => format(value)}\n            />\n          </SliderPrimitive.Track>\n        </SliderPrimitive.Control>\n      </SliderPrimitive.Root>\n      <span\n        aria-hidden\n        className={cn(\n          \"pointer-events-none absolute inset-y-0 left-0 w-[calc(var(--fill)*100%)] bg-fill-3\",\n          !pressed && \"transition-[width] duration-150 ease-out\"\n        )}\n      />\n      <span\n        aria-hidden\n        className={cn(\n          \"pointer-events-none absolute inset-y-2 left-[max(--spacing(2),calc(var(--fill)*100%-var(--spacing)*2))] w-0.5 rounded-full bg-foreground/30 transition-[top,bottom,background-color] duration-150 group-hover/bar-slider:inset-y-1.75 group-hover/bar-slider:bg-foreground/50\",\n          !pointerFocus &&\n            \"group-has-focus-visible/bar-slider:inset-y-1.75 group-has-focus-visible/bar-slider:bg-foreground\",\n          !pressed && \"transition-[top,bottom,background-color,left] ease-out\"\n        )}\n      />\n    </div>\n  )\n}\n","type":"registry:component"}],"cssVars":{"theme":{"color-chart-7":"var(--chart-7)","color-chart-8":"var(--chart-8)","color-fill-2":"var(--fill-2)","color-fill-3":"var(--fill-3)","text-xs--letter-spacing":"-0.005em","text-sm--letter-spacing":"-0.005em","text-2xl--letter-spacing":"-0.015em","text-xs":"0.8125rem","text-xs--line-height":"calc(1.25 / 0.8125)"},"light":{"chart-7":"oklch(0.718 0.202 349.761)","chart-8":"oklch(0.811 0.111 293.571)","fill-2":"oklch(0.556 0 0 / 8%)","fill-3":"oklch(0.556 0 0 / 12%)"},"dark":{"chart-7":"oklch(0.718 0.202 349.761)","chart-8":"oklch(0.811 0.111 293.571)","fill-2":"oklch(1 0 0 / 10%)","fill-3":"oklch(1 0 0 / 14%)"}},"meta":{"kind":"block","pro":false,"iframeHeight":520},"categories":["widgets"],"type":"registry:block"}