@contui/ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +30 -0
- package/README.md +150 -2
- package/cli/contui.mjs +250 -0
- package/dist/charts/area-chart.d.ts +8 -0
- package/dist/charts/area-chart.js +9 -0
- package/dist/charts/bar-chart.d.ts +9 -0
- package/dist/charts/bar-chart.js +10 -0
- package/dist/charts/bubble-chart.d.ts +60 -0
- package/dist/charts/bubble-chart.js +392 -0
- package/dist/charts/bullet-chart.d.ts +53 -0
- package/dist/charts/bullet-chart.js +123 -0
- package/dist/charts/candlestick-chart.d.ts +77 -0
- package/dist/charts/candlestick-chart.js +558 -0
- package/dist/charts/cartesian.d.ts +121 -0
- package/dist/charts/cartesian.js +625 -0
- package/dist/charts/chart-card.d.ts +80 -0
- package/dist/charts/chart-card.js +114 -0
- package/dist/charts/donut-chart.d.ts +40 -0
- package/dist/charts/donut-chart.js +88 -0
- package/dist/charts/funnel-chart.d.ts +16 -0
- package/dist/charts/funnel-chart.js +164 -0
- package/dist/charts/horizontal-bar-chart.d.ts +67 -0
- package/dist/charts/horizontal-bar-chart.js +481 -0
- package/dist/charts/index.d.ts +21 -0
- package/dist/charts/index.js +32 -0
- package/dist/charts/legend.d.ts +23 -0
- package/dist/charts/legend.js +22 -0
- package/dist/charts/lib/candlestick-window.d.ts +15 -0
- package/dist/charts/lib/candlestick-window.js +23 -0
- package/dist/charts/lib/color.d.ts +39 -0
- package/dist/charts/lib/color.js +83 -0
- package/dist/charts/lib/format.d.ts +11 -0
- package/dist/charts/lib/format.js +33 -0
- package/dist/charts/lib/frame.d.ts +6 -0
- package/dist/charts/lib/frame.js +6 -0
- package/dist/charts/lib/layout.d.ts +11 -0
- package/dist/charts/lib/layout.js +71 -0
- package/dist/charts/lib/path.d.ts +35 -0
- package/dist/charts/lib/path.js +203 -0
- package/dist/charts/lib/scale.d.ts +41 -0
- package/dist/charts/lib/scale.js +80 -0
- package/dist/charts/lib/use-active.d.ts +18 -0
- package/dist/charts/lib/use-active.js +71 -0
- package/dist/charts/lib/use-size.d.ts +9 -0
- package/dist/charts/lib/use-size.js +40 -0
- package/dist/charts/line-chart.d.ts +7 -0
- package/dist/charts/line-chart.js +8 -0
- package/dist/charts/radar-chart.d.ts +27 -0
- package/dist/charts/radar-chart.js +165 -0
- package/dist/charts/rose-chart.d.ts +35 -0
- package/dist/charts/rose-chart.js +242 -0
- package/dist/charts/sparkline.d.ts +35 -0
- package/dist/charts/sparkline.js +110 -0
- package/dist/charts/tooltip.d.ts +35 -0
- package/dist/charts/tooltip.js +22 -0
- package/dist/charts/treemap.d.ts +28 -0
- package/dist/charts/treemap.js +103 -0
- package/dist/charts/uptime-strip.d.ts +40 -0
- package/dist/charts/uptime-strip.js +178 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +34 -0
- package/dist/components/activity-item.d.ts +12 -0
- package/dist/components/activity-item.js +6 -0
- package/dist/components/alert.d.ts +21 -0
- package/dist/components/alert.js +58 -0
- package/dist/components/assistant-answer.d.ts +52 -0
- package/dist/components/assistant-answer.js +40 -0
- package/dist/components/assistant-prompt.d.ts +63 -0
- package/dist/components/assistant-prompt.js +122 -0
- package/dist/components/avatar.d.ts +28 -0
- package/dist/components/avatar.js +75 -0
- package/dist/components/badge.d.ts +16 -0
- package/dist/components/badge.js +33 -0
- package/dist/components/bar-list.d.ts +65 -0
- package/dist/components/bar-list.js +119 -0
- package/dist/components/block-slider.d.ts +43 -0
- package/dist/components/block-slider.js +63 -0
- package/dist/components/branch-graph.d.ts +87 -0
- package/dist/components/branch-graph.js +231 -0
- package/dist/components/breadcrumb.d.ts +41 -0
- package/dist/components/breadcrumb.js +53 -0
- package/dist/components/button.d.ts +23 -0
- package/dist/components/button.js +108 -0
- package/dist/components/calendar.d.ts +5 -0
- package/dist/components/calendar.js +90 -0
- package/dist/components/card.d.ts +65 -0
- package/dist/components/card.js +104 -0
- package/dist/components/chat-attachment.d.ts +47 -0
- package/dist/components/chat-attachment.js +63 -0
- package/dist/components/chat-event.d.ts +7 -0
- package/dist/components/chat-event.js +7 -0
- package/dist/components/chat-message.d.ts +59 -0
- package/dist/components/chat-message.js +91 -0
- package/dist/components/chat-reference.d.ts +20 -0
- package/dist/components/chat-reference.js +41 -0
- package/dist/components/chat-shared.d.ts +21 -0
- package/dist/components/chat-shared.js +23 -0
- package/dist/components/chat-suggestions.d.ts +59 -0
- package/dist/components/chat-suggestions.js +88 -0
- package/dist/components/chat-thread.d.ts +39 -0
- package/dist/components/chat-thread.js +172 -0
- package/dist/components/chat-typing.d.ts +35 -0
- package/dist/components/chat-typing.js +66 -0
- package/dist/components/checkbox.d.ts +18 -0
- package/dist/components/checkbox.js +33 -0
- package/dist/components/checklist.d.ts +26 -0
- package/dist/components/checklist.js +18 -0
- package/dist/components/chip.d.ts +29 -0
- package/dist/components/chip.js +61 -0
- package/dist/components/code-input.d.ts +40 -0
- package/dist/components/code-input.js +285 -0
- package/dist/components/color-picker.d.ts +38 -0
- package/dist/components/color-picker.js +855 -0
- package/dist/components/combobox.d.ts +75 -0
- package/dist/components/combobox.js +112 -0
- package/dist/components/command-menu.d.ts +58 -0
- package/dist/components/command-menu.js +166 -0
- package/dist/components/copy-button.d.ts +16 -0
- package/dist/components/copy-button.js +31 -0
- package/dist/components/data-grid.d.ts +50 -0
- package/dist/components/data-grid.js +599 -0
- package/dist/components/date-picker.d.ts +109 -0
- package/dist/components/date-picker.js +268 -0
- package/dist/components/description-list.d.ts +12 -0
- package/dist/components/description-list.js +10 -0
- package/dist/components/dialog.d.ts +57 -0
- package/dist/components/dialog.js +140 -0
- package/dist/components/dock.d.ts +45 -0
- package/dist/components/dock.js +79 -0
- package/dist/components/drop-zone.d.ts +46 -0
- package/dist/components/drop-zone.js +101 -0
- package/dist/components/dropdown-menu.d.ts +50 -0
- package/dist/components/dropdown-menu.js +78 -0
- package/dist/components/editable-cell.d.ts +34 -0
- package/dist/components/editable-cell.js +89 -0
- package/dist/components/empty-state.d.ts +17 -0
- package/dist/components/empty-state.js +9 -0
- package/dist/components/field.d.ts +43 -0
- package/dist/components/field.js +75 -0
- package/dist/components/filter-condition.d.ts +188 -0
- package/dist/components/filter-condition.js +113 -0
- package/dist/components/filter-menu.d.ts +57 -0
- package/dist/components/filter-menu.js +46 -0
- package/dist/components/header-bar.d.ts +84 -0
- package/dist/components/header-bar.js +94 -0
- package/dist/components/hold-button.d.ts +15 -0
- package/dist/components/hold-button.js +88 -0
- package/dist/components/icon-tile.d.ts +12 -0
- package/dist/components/icon-tile.js +29 -0
- package/dist/components/info-tip.d.ts +13 -0
- package/dist/components/info-tip.js +26 -0
- package/dist/components/input.d.ts +17 -0
- package/dist/components/input.js +83 -0
- package/dist/components/kbd.d.ts +6 -0
- package/dist/components/kbd.js +7 -0
- package/dist/components/menu-item.d.ts +56 -0
- package/dist/components/menu-item.js +94 -0
- package/dist/components/message-composer.d.ts +75 -0
- package/dist/components/message-composer.js +199 -0
- package/dist/components/nav-rail.d.ts +31 -0
- package/dist/components/nav-rail.js +85 -0
- package/dist/components/notice.d.ts +45 -0
- package/dist/components/notice.js +66 -0
- package/dist/components/optimistic-undo.d.ts +28 -0
- package/dist/components/optimistic-undo.js +43 -0
- package/dist/components/option-row.d.ts +15 -0
- package/dist/components/option-row.js +14 -0
- package/dist/components/page-content.d.ts +11 -0
- package/dist/components/page-content.js +31 -0
- package/dist/components/pagination.d.ts +34 -0
- package/dist/components/pagination.js +41 -0
- package/dist/components/popover.d.ts +27 -0
- package/dist/components/popover.js +25 -0
- package/dist/components/progress.d.ts +33 -0
- package/dist/components/progress.js +75 -0
- package/dist/components/radio-group.d.ts +7 -0
- package/dist/components/radio-group.js +33 -0
- package/dist/components/readonly-field.d.ts +33 -0
- package/dist/components/readonly-field.js +80 -0
- package/dist/components/segmented-control.d.ts +21 -0
- package/dist/components/segmented-control.js +108 -0
- package/dist/components/select.d.ts +42 -0
- package/dist/components/select.js +86 -0
- package/dist/components/separator.d.ts +10 -0
- package/dist/components/separator.js +7 -0
- package/dist/components/setting-row.d.ts +53 -0
- package/dist/components/setting-row.js +93 -0
- package/dist/components/sidebar-switcher.d.ts +47 -0
- package/dist/components/sidebar-switcher.js +20 -0
- package/dist/components/sidebar.d.ts +220 -0
- package/dist/components/sidebar.js +327 -0
- package/dist/components/skeleton.d.ts +18 -0
- package/dist/components/skeleton.js +23 -0
- package/dist/components/slider.d.ts +15 -0
- package/dist/components/slider.js +28 -0
- package/dist/components/spinner.d.ts +14 -0
- package/dist/components/spinner.js +19 -0
- package/dist/components/status-dot.d.ts +24 -0
- package/dist/components/status-dot.js +35 -0
- package/dist/components/stepper.d.ts +60 -0
- package/dist/components/stepper.js +197 -0
- package/dist/components/switch.d.ts +9 -0
- package/dist/components/switch.js +26 -0
- package/dist/components/table-cell.d.ts +38 -0
- package/dist/components/table-cell.js +107 -0
- package/dist/components/table-context.d.ts +48 -0
- package/dist/components/table-context.js +69 -0
- package/dist/components/table-head.d.ts +42 -0
- package/dist/components/table-head.js +188 -0
- package/dist/components/table-tree.d.ts +96 -0
- package/dist/components/table-tree.js +137 -0
- package/dist/components/table.d.ts +78 -0
- package/dist/components/table.js +119 -0
- package/dist/components/tabs.d.ts +27 -0
- package/dist/components/tabs.js +79 -0
- package/dist/components/textarea.d.ts +3 -0
- package/dist/components/textarea.js +20 -0
- package/dist/components/theme.d.ts +41 -0
- package/dist/components/theme.js +60 -0
- package/dist/components/thinking.d.ts +68 -0
- package/dist/components/thinking.js +94 -0
- package/dist/components/toast.d.ts +39 -0
- package/dist/components/toast.js +110 -0
- package/dist/components/toolbar.d.ts +25 -0
- package/dist/components/toolbar.js +92 -0
- package/dist/components/tooltip.d.ts +23 -0
- package/dist/components/tooltip.js +27 -0
- package/dist/components/top-nav.d.ts +129 -0
- package/dist/components/top-nav.js +211 -0
- package/dist/components/truncate.d.ts +29 -0
- package/dist/components/truncate.js +138 -0
- package/dist/deprecated.d.ts +43 -0
- package/dist/deprecated.js +47 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +93 -0
- package/dist/lib/cn.d.ts +5 -0
- package/dist/lib/cn.js +14 -0
- package/dist/lib/color.d.ts +136 -0
- package/dist/lib/color.js +307 -0
- package/dist/lib/contrast.d.ts +53 -0
- package/dist/lib/contrast.js +169 -0
- package/dist/lib/create-theme.d.ts +240 -0
- package/dist/lib/create-theme.js +461 -0
- package/dist/lib/harmony.d.ts +102 -0
- package/dist/lib/harmony.js +426 -0
- package/dist/lib/import-theme.d.ts +22 -0
- package/dist/lib/import-theme.js +221 -0
- package/dist/lib/overflow-scroll.d.ts +25 -0
- package/dist/lib/overflow-scroll.js +66 -0
- package/dist/lib/radio-toggle-render.d.ts +3 -0
- package/dist/lib/radio-toggle-render.js +9 -0
- package/dist/lib/reorder.d.ts +38 -0
- package/dist/lib/reorder.js +193 -0
- package/dist/lib/spacing.d.ts +101 -0
- package/dist/lib/spacing.js +118 -0
- package/dist/lib/tailwind-palette.d.ts +5 -0
- package/dist/lib/tailwind-palette.js +351 -0
- package/dist/lib/truncate.d.ts +18 -0
- package/dist/lib/truncate.js +69 -0
- package/dist/lib/use-single-value.d.ts +5 -0
- package/dist/lib/use-single-value.js +17 -0
- package/dist/styles.css +7988 -0
- package/mcp/server.mjs +87590 -0
- package/package.json +58 -5
- package/skills/continuum-fix-padding/SKILL.md +69 -0
- package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
- package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
- package/skills/continuum-taste/SKILL.md +186 -0
- package/skills/continuum-taste/assets/tokens.css +647 -0
- package/skills/continuum-taste/references/charts.md +375 -0
- package/skills/continuum-taste/references/components.md +1369 -0
- package/skills/continuum-taste/references/foundations.md +539 -0
- package/skills/continuum-taste/references/media/canvas.md +75 -0
- package/skills/continuum-taste/references/media/css.md +61 -0
- package/skills/continuum-taste/references/media/react.md +161 -0
- package/skills/continuum-taste/references/method.md +87 -0
- package/skills/continuum-taste/references/pages.md +391 -0
- package/skills/continuum-taste/references/patterns.md +77 -0
- package/skills/continuum-taste/references/screens/agent.tsx +450 -0
- package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
- package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
- package/skills/continuum-taste/references/screens/customer.tsx +475 -0
- package/skills/continuum-taste/references/screens/customers.tsx +608 -0
- package/skills/continuum-taste/references/screens/index.tsx +72 -0
- package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
- package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
- package/skills/continuum-taste/references/screens/overview.tsx +196 -0
- package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
- package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
- package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
- package/skills/continuum-taste/references/screens/settings.tsx +288 -0
- package/skills/continuum-taste/references/screens/shared.tsx +204 -0
- package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
- package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
- package/skills/continuum-taste/references/theming.md +95 -0
- package/src/charts/area-chart.tsx +17 -0
- package/src/charts/bar-chart.tsx +15 -0
- package/src/charts/bubble-chart.tsx +913 -0
- package/src/charts/bullet-chart.tsx +419 -0
- package/src/charts/candlestick-chart.tsx +1242 -0
- package/src/charts/cartesian.tsx +1437 -0
- package/src/charts/chart-card.tsx +394 -0
- package/src/charts/donut-chart.tsx +299 -0
- package/src/charts/funnel-chart.tsx +373 -0
- package/src/charts/horizontal-bar-chart.tsx +991 -0
- package/src/charts/index.ts +76 -0
- package/src/charts/legend.tsx +59 -0
- package/src/charts/lib/candlestick-window.ts +42 -0
- package/src/charts/lib/color.ts +110 -0
- package/src/charts/lib/format.ts +42 -0
- package/src/charts/lib/frame.ts +6 -0
- package/src/charts/lib/layout.ts +78 -0
- package/src/charts/lib/path.ts +225 -0
- package/src/charts/lib/scale.ts +91 -0
- package/src/charts/lib/use-active.ts +87 -0
- package/src/charts/lib/use-size.ts +45 -0
- package/src/charts/line-chart.tsx +16 -0
- package/src/charts/radar-chart.tsx +485 -0
- package/src/charts/rose-chart.tsx +553 -0
- package/src/charts/sparkline.tsx +293 -0
- package/src/charts/tooltip.tsx +105 -0
- package/src/charts/treemap.tsx +271 -0
- package/src/charts/uptime-strip.tsx +469 -0
- package/src/components/accordion.tsx +136 -0
- package/src/components/activity-item.tsx +43 -0
- package/src/components/alert.tsx +135 -0
- package/src/components/assistant-answer.tsx +185 -0
- package/src/components/assistant-prompt.tsx +352 -0
- package/src/components/avatar.tsx +157 -0
- package/src/components/badge.tsx +55 -0
- package/src/components/bar-list.tsx +350 -0
- package/src/components/block-slider.tsx +189 -0
- package/src/components/branch-graph.tsx +580 -0
- package/src/components/breadcrumb.tsx +173 -0
- package/src/components/button.tsx +157 -0
- package/src/components/calendar.tsx +154 -0
- package/src/components/card.tsx +296 -0
- package/src/components/chat-attachment.tsx +255 -0
- package/src/components/chat-event.tsx +34 -0
- package/src/components/chat-message.tsx +332 -0
- package/src/components/chat-reference.tsx +100 -0
- package/src/components/chat-shared.ts +41 -0
- package/src/components/chat-suggestions.tsx +188 -0
- package/src/components/chat-thread.tsx +348 -0
- package/src/components/chat-typing.tsx +155 -0
- package/src/components/checkbox.tsx +83 -0
- package/src/components/checklist.tsx +146 -0
- package/src/components/chip.tsx +152 -0
- package/src/components/code-input.tsx +406 -0
- package/src/components/color-picker.tsx +1385 -0
- package/src/components/combobox.tsx +420 -0
- package/src/components/command-menu.tsx +405 -0
- package/src/components/copy-button.tsx +87 -0
- package/src/components/data-grid.tsx +965 -0
- package/src/components/date-picker.tsx +652 -0
- package/src/components/description-list.tsx +39 -0
- package/src/components/dialog.tsx +367 -0
- package/src/components/dock.tsx +192 -0
- package/src/components/drop-zone.tsx +283 -0
- package/src/components/dropdown-menu.tsx +323 -0
- package/src/components/editable-cell.tsx +219 -0
- package/src/components/empty-state.tsx +50 -0
- package/src/components/field.tsx +148 -0
- package/src/components/filter-condition.tsx +463 -0
- package/src/components/filter-menu.tsx +196 -0
- package/src/components/header-bar.tsx +220 -0
- package/src/components/hold-button.tsx +146 -0
- package/src/components/icon-tile.tsx +43 -0
- package/src/components/info-tip.tsx +87 -0
- package/src/components/input.tsx +136 -0
- package/src/components/kbd.tsx +17 -0
- package/src/components/menu-item.tsx +216 -0
- package/src/components/message-composer.tsx +458 -0
- package/src/components/nav-rail.tsx +235 -0
- package/src/components/notice.tsx +133 -0
- package/src/components/optimistic-undo.ts +68 -0
- package/src/components/option-row.tsx +54 -0
- package/src/components/page-content.tsx +44 -0
- package/src/components/pagination.tsx +182 -0
- package/src/components/popover.tsx +113 -0
- package/src/components/progress.tsx +222 -0
- package/src/components/radio-group.tsx +80 -0
- package/src/components/readonly-field.tsx +251 -0
- package/src/components/segmented-control.tsx +205 -0
- package/src/components/select.tsx +259 -0
- package/src/components/separator.tsx +30 -0
- package/src/components/setting-row.tsx +277 -0
- package/src/components/sidebar-switcher.tsx +134 -0
- package/src/components/sidebar.tsx +804 -0
- package/src/components/skeleton.tsx +57 -0
- package/src/components/slider.tsx +106 -0
- package/src/components/spinner.tsx +43 -0
- package/src/components/status-dot.tsx +100 -0
- package/src/components/stepper.tsx +459 -0
- package/src/components/switch.tsx +54 -0
- package/src/components/table-cell.tsx +239 -0
- package/src/components/table-context.ts +135 -0
- package/src/components/table-head.tsx +393 -0
- package/src/components/table-tree.tsx +357 -0
- package/src/components/table.tsx +295 -0
- package/src/components/tabs.tsx +191 -0
- package/src/components/textarea.tsx +44 -0
- package/src/components/theme.tsx +120 -0
- package/src/components/thinking.tsx +295 -0
- package/src/components/toast.tsx +308 -0
- package/src/components/toolbar.tsx +166 -0
- package/src/components/tooltip.tsx +84 -0
- package/src/components/top-nav.tsx +660 -0
- package/src/components/truncate.tsx +234 -0
- package/src/deprecated.ts +61 -0
- package/src/index.ts +184 -0
- package/src/lib/cn.ts +22 -0
- package/src/lib/color.ts +469 -0
- package/src/lib/contrast.ts +220 -0
- package/src/lib/create-theme.ts +659 -0
- package/src/lib/harmony.ts +609 -0
- package/src/lib/import-theme.ts +272 -0
- package/src/lib/overflow-scroll.tsx +108 -0
- package/src/lib/radio-toggle-render.tsx +15 -0
- package/src/lib/reorder.ts +222 -0
- package/src/lib/spacing.ts +189 -0
- package/src/lib/tailwind-palette.ts +355 -0
- package/src/lib/truncate.ts +79 -0
- package/src/lib/use-single-value.ts +23 -0
- package/src/styles/continuum.css +3 -0
- package/src/styles/theme.css +524 -0
- package/src/styles/tokens.css +647 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { ArrowUpRight, Check, Copy, Download, EllipsisVertical } from 'lucide-react';
|
|
4
|
+
import { type ReactNode, useMemo, useState } from 'react';
|
|
5
|
+
import { Button } from '../components/button';
|
|
6
|
+
import {
|
|
7
|
+
DropdownMenu,
|
|
8
|
+
DropdownMenuContent,
|
|
9
|
+
DropdownMenuItem,
|
|
10
|
+
DropdownMenuTrigger,
|
|
11
|
+
} from '../components/dropdown-menu';
|
|
12
|
+
import { cn } from '../lib/cn';
|
|
13
|
+
import { Cartesian, type CartesianProps, type ChartRow, type ChartSeries } from './cartesian';
|
|
14
|
+
import { CHANGE_INK, type ChartColor, chartColor } from './lib/color';
|
|
15
|
+
|
|
16
|
+
/** The figures behind a chart, as a table: what its download and copy hand over. */
|
|
17
|
+
export type ChartTable = { columns: string[]; rows: (string | number)[][] };
|
|
18
|
+
|
|
19
|
+
export type ChartFrameProps = {
|
|
20
|
+
/** What is measured: "Visitors". It names the chart for a screen reader too. */
|
|
21
|
+
title: string;
|
|
22
|
+
/** A 16px icon before the title, for a dashboard of many cards. */
|
|
23
|
+
icon?: ReactNode;
|
|
24
|
+
/** The figure the card leads with, for the whole period. */
|
|
25
|
+
value?: ReactNode;
|
|
26
|
+
/** The change on the period before, the figure alone: "+10.7%". */
|
|
27
|
+
delta?: ReactNode;
|
|
28
|
+
/** What the change is measured against, read after it in the quiet ink: "on last week". */
|
|
29
|
+
comparison?: ReactNode;
|
|
30
|
+
/** Which way the change is to be read; a leading minus is read as down without it. */
|
|
31
|
+
trend?: 'up' | 'down' | 'neutral';
|
|
32
|
+
/**
|
|
33
|
+
* The card's own controls at its top right: a range, a metric, a view. What narrows the
|
|
34
|
+
* data (filters) belongs to the page around the card, not to the card.
|
|
35
|
+
*/
|
|
36
|
+
actions?: ReactNode;
|
|
37
|
+
/** The series key, standing close over the chart it names. */
|
|
38
|
+
legend?: ReactNode;
|
|
39
|
+
/** With the figures, the card offers them as a CSV download and on the clipboard. */
|
|
40
|
+
table?: ChartTable;
|
|
41
|
+
/** The download's name, without its extension; the title unless given. */
|
|
42
|
+
filename?: string;
|
|
43
|
+
/** While the data loads: the figure and its change stand in as skeleton bars. */
|
|
44
|
+
loading?: boolean;
|
|
45
|
+
/** Where the chart opens in full: a round button at the header's end. */
|
|
46
|
+
onOpen?: () => void;
|
|
47
|
+
className?: string;
|
|
48
|
+
children: ReactNode;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const trendInk = CHANGE_INK;
|
|
52
|
+
|
|
53
|
+
const trendOf = (delta: ReactNode): keyof typeof trendInk =>
|
|
54
|
+
typeof delta === 'string' && /^\s*[-−–]/.test(delta) ? 'down' : 'up';
|
|
55
|
+
|
|
56
|
+
const csvCell = (cell: string | number) => {
|
|
57
|
+
const text = String(cell);
|
|
58
|
+
return /[",\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** The table as CSV, or with tabs between its cells for pasting into a sheet. */
|
|
62
|
+
const serialize = ({ columns, rows }: ChartTable, separator: ',' | '\t') =>
|
|
63
|
+
[columns, ...rows]
|
|
64
|
+
.map(row =>
|
|
65
|
+
row.map(cell => (separator === ',' ? csvCell(cell) : String(cell))).join(separator),
|
|
66
|
+
)
|
|
67
|
+
.join('\n');
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The card any chart sits in, the same for every chart in the kit, in two parts with clear
|
|
71
|
+
* room between them.
|
|
72
|
+
*
|
|
73
|
+
* The head says what the card is and what it came to: the title on the line its controls
|
|
74
|
+
* stand on, the figure under it, and whatever narrows the data under that. The figure is the
|
|
75
|
+
* loudest thing on the card and nothing inside the chart is as large, so the eye lands on it
|
|
76
|
+
* first and reads the chart as where it came from.
|
|
77
|
+
*
|
|
78
|
+
* The plot is the key and the chart, the key close over the chart it names.
|
|
79
|
+
*
|
|
80
|
+
* In a theme that frames its cards (`cards: framed`), the head stands on a frame a step off the
|
|
81
|
+
* page and the plot on a stage of its own inside it, its corners the frame's less the gap between
|
|
82
|
+
* them, so the two stay concentric. The card-frame, card-head and card-stage utilities do it.
|
|
83
|
+
*/
|
|
84
|
+
export const ChartFrame = ({
|
|
85
|
+
title,
|
|
86
|
+
icon,
|
|
87
|
+
value,
|
|
88
|
+
delta,
|
|
89
|
+
comparison,
|
|
90
|
+
trend,
|
|
91
|
+
actions,
|
|
92
|
+
legend,
|
|
93
|
+
table,
|
|
94
|
+
filename,
|
|
95
|
+
onOpen,
|
|
96
|
+
loading = false,
|
|
97
|
+
className,
|
|
98
|
+
children,
|
|
99
|
+
}: ChartFrameProps) => {
|
|
100
|
+
return (
|
|
101
|
+
<section
|
|
102
|
+
aria-label={title}
|
|
103
|
+
className={cn(
|
|
104
|
+
// 24 round it and the plot 20 under the head, as plain; framed, the utilities move them.
|
|
105
|
+
'card-frame flex min-w-0 flex-col border border-neutral-200 shadow-card [--card-gap:calc(var(--spacing)*5)] [--card-pad:calc(var(--spacing)*6)]',
|
|
106
|
+
className,
|
|
107
|
+
)}
|
|
108
|
+
data-slot="chart-card"
|
|
109
|
+
>
|
|
110
|
+
<header className="card-head flex min-w-0 flex-col" data-slot="chart-card-head">
|
|
111
|
+
{/* The title stands level with the controls, so the two read as one line. */}
|
|
112
|
+
<div className="flex min-h-control min-w-0 items-center justify-between gap-4">
|
|
113
|
+
<h3 className="flex min-w-0 items-center gap-2 font-medium text-neutral-600 text-xs">
|
|
114
|
+
{icon && (
|
|
115
|
+
<span aria-hidden className="flex shrink-0 [&_svg]:size-4">
|
|
116
|
+
{icon}
|
|
117
|
+
</span>
|
|
118
|
+
)}
|
|
119
|
+
<span className="truncate">{title}</span>
|
|
120
|
+
</h3>
|
|
121
|
+
{(actions || table || onOpen) && (
|
|
122
|
+
<div className="-mr-2 flex shrink-0 items-center gap-1">
|
|
123
|
+
{actions && <div className="mr-1 flex items-center">{actions}</div>}
|
|
124
|
+
{table && <ChartFigures filename={filename ?? title} table={table} />}
|
|
125
|
+
{onOpen && (
|
|
126
|
+
<Button
|
|
127
|
+
aria-label={`Open ${title}`}
|
|
128
|
+
onClick={onOpen}
|
|
129
|
+
size="icon"
|
|
130
|
+
variant="ghost"
|
|
131
|
+
>
|
|
132
|
+
<ArrowUpRight />
|
|
133
|
+
</Button>
|
|
134
|
+
)}
|
|
135
|
+
</div>
|
|
136
|
+
)}
|
|
137
|
+
</div>
|
|
138
|
+
{loading && value != null ? (
|
|
139
|
+
// The figure's own line, so nothing moves when it lands: a bar at the figure's
|
|
140
|
+
// cap height, and a short one for its change.
|
|
141
|
+
<p aria-hidden className="flex items-center gap-2 text-2xl">
|
|
142
|
+
<span className="invisible w-0">0</span>
|
|
143
|
+
<span className="skeleton h-[0.75em] w-24 rounded-chip" />
|
|
144
|
+
<span className="skeleton h-2.5 w-16 rounded-chip" />
|
|
145
|
+
</p>
|
|
146
|
+
) : (
|
|
147
|
+
value != null && (
|
|
148
|
+
<p className="flex min-w-0 flex-wrap items-baseline gap-x-2">
|
|
149
|
+
<span className="font-medium text-2xl text-neutral-950 tabular-nums">
|
|
150
|
+
{value}
|
|
151
|
+
</span>
|
|
152
|
+
{(delta != null || comparison != null) && (
|
|
153
|
+
// A line under a figure that has no change to show still reads.
|
|
154
|
+
<span className="text-xs">
|
|
155
|
+
{delta != null && (
|
|
156
|
+
<span
|
|
157
|
+
className={cn(
|
|
158
|
+
'font-medium tabular-nums',
|
|
159
|
+
trendInk[trend ?? trendOf(delta)],
|
|
160
|
+
)}
|
|
161
|
+
>
|
|
162
|
+
{delta}
|
|
163
|
+
</span>
|
|
164
|
+
)}
|
|
165
|
+
{comparison != null && (
|
|
166
|
+
<span className="text-neutral-600">
|
|
167
|
+
{delta != null && ' '}
|
|
168
|
+
{comparison}
|
|
169
|
+
</span>
|
|
170
|
+
)}
|
|
171
|
+
</span>
|
|
172
|
+
)}
|
|
173
|
+
</p>
|
|
174
|
+
)
|
|
175
|
+
)}
|
|
176
|
+
</header>
|
|
177
|
+
<div
|
|
178
|
+
className={cn(
|
|
179
|
+
// The key names the chart's colours, so it stands with the chart, close over it.
|
|
180
|
+
'card-stage flex min-w-0 flex-1 flex-col gap-2.5',
|
|
181
|
+
)}
|
|
182
|
+
data-slot="chart-card-plot"
|
|
183
|
+
>
|
|
184
|
+
{legend}
|
|
185
|
+
{children}
|
|
186
|
+
</div>
|
|
187
|
+
</section>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** The card's menu for its figures: a CSV download, and a copy for pasting into a sheet. */
|
|
192
|
+
const ChartFigures = ({ table, filename }: { table: ChartTable; filename: string }) => {
|
|
193
|
+
const [copied, setCopied] = useState(false);
|
|
194
|
+
|
|
195
|
+
const download = () => {
|
|
196
|
+
const blob = new Blob([serialize(table, ',')], { type: 'text/csv;charset=utf-8' });
|
|
197
|
+
const url = URL.createObjectURL(blob);
|
|
198
|
+
const link = document.createElement('a');
|
|
199
|
+
link.href = url;
|
|
200
|
+
link.download = `${filename.toLowerCase().replace(/[^a-z0-9]+/g, '-')}.csv`;
|
|
201
|
+
link.click();
|
|
202
|
+
URL.revokeObjectURL(url);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const copy = async () => {
|
|
206
|
+
await navigator.clipboard.writeText(serialize(table, '\t'));
|
|
207
|
+
setCopied(true);
|
|
208
|
+
window.setTimeout(() => setCopied(false), 1600);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<DropdownMenu>
|
|
213
|
+
<DropdownMenuTrigger
|
|
214
|
+
render={<Button aria-label="The chart's figures" size="icon" variant="ghost" />}
|
|
215
|
+
>
|
|
216
|
+
<EllipsisVertical />
|
|
217
|
+
</DropdownMenuTrigger>
|
|
218
|
+
<DropdownMenuContent align="end">
|
|
219
|
+
<DropdownMenuItem onClick={download}>
|
|
220
|
+
<Download strokeWidth={2} />
|
|
221
|
+
Download CSV
|
|
222
|
+
</DropdownMenuItem>
|
|
223
|
+
<DropdownMenuItem closeOnClick={false} onClick={copy}>
|
|
224
|
+
{copied ? <Check strokeWidth={2} /> : <Copy strokeWidth={2} />}
|
|
225
|
+
{copied ? 'Copied' : 'Copy for a sheet'}
|
|
226
|
+
</DropdownMenuItem>
|
|
227
|
+
</DropdownMenuContent>
|
|
228
|
+
</DropdownMenu>
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export type ChartCardProps<Row extends ChartRow, X = unknown> = Omit<
|
|
233
|
+
CartesianProps<Row, X>,
|
|
234
|
+
'aria-label' | 'className' | 'style' | 'onRead' | 'kind'
|
|
235
|
+
> &
|
|
236
|
+
Omit<ChartFrameProps, 'children' | 'legend' | 'table'> & {
|
|
237
|
+
kind?: CartesianProps<Row, X>['kind'];
|
|
238
|
+
/** Offer the figures as a download and a copy; on unless turned off. */
|
|
239
|
+
figures?: boolean;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const dateCell = (value: unknown) =>
|
|
243
|
+
value instanceof Date ? value.toISOString().slice(0, 10) : String(value ?? '');
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A plotted chart in its card. With two series and more, the key under the header is also
|
|
247
|
+
* the way to turn one off: a series switched off leaves the chart and gives the rest its
|
|
248
|
+
* room, and keeps its colour for when it comes back.
|
|
249
|
+
*/
|
|
250
|
+
export const ChartCard = <Row extends ChartRow, X = unknown>({
|
|
251
|
+
title,
|
|
252
|
+
icon,
|
|
253
|
+
value,
|
|
254
|
+
delta,
|
|
255
|
+
comparison,
|
|
256
|
+
trend,
|
|
257
|
+
actions,
|
|
258
|
+
filename,
|
|
259
|
+
onOpen,
|
|
260
|
+
figures = true,
|
|
261
|
+
kind = 'area',
|
|
262
|
+
className,
|
|
263
|
+
series,
|
|
264
|
+
height = 220,
|
|
265
|
+
...chart
|
|
266
|
+
}: ChartCardProps<Row, X>) => {
|
|
267
|
+
// Each series keeps the colour of its place, so turning one off never repaints the rest.
|
|
268
|
+
const placed = useMemo(
|
|
269
|
+
() =>
|
|
270
|
+
series.map((line, index) => ({
|
|
271
|
+
...line,
|
|
272
|
+
color: line.color ?? (`series-${Math.min(index + 1, 5)}` as ChartColor),
|
|
273
|
+
})),
|
|
274
|
+
[series],
|
|
275
|
+
);
|
|
276
|
+
const [off, setOff] = useState<ReadonlySet<string>>(new Set());
|
|
277
|
+
const shown = placed.filter(line => !off.has(line.key));
|
|
278
|
+
|
|
279
|
+
const toggle = (key: string) =>
|
|
280
|
+
setOff(current => {
|
|
281
|
+
const next = new Set(current);
|
|
282
|
+
if (next.has(key)) next.delete(key);
|
|
283
|
+
// The last series showing stays: a chart of nothing says nothing.
|
|
284
|
+
else if (placed.length - next.size > 1) next.add(key);
|
|
285
|
+
return next;
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const table = useMemo<ChartTable>(
|
|
289
|
+
() => ({
|
|
290
|
+
columns: [chart.x, ...shown.map(line => line.name ?? line.key)],
|
|
291
|
+
rows: chart.data.map(row => [
|
|
292
|
+
dateCell(row[chart.x]),
|
|
293
|
+
...shown.map(line => {
|
|
294
|
+
const cell = row[line.key];
|
|
295
|
+
return typeof cell === 'number' ? cell : '';
|
|
296
|
+
}),
|
|
297
|
+
]),
|
|
298
|
+
}),
|
|
299
|
+
[chart.data, chart.x, shown],
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
return (
|
|
303
|
+
<ChartFrame
|
|
304
|
+
actions={actions}
|
|
305
|
+
className={className}
|
|
306
|
+
comparison={comparison}
|
|
307
|
+
delta={delta}
|
|
308
|
+
filename={filename}
|
|
309
|
+
icon={icon}
|
|
310
|
+
loading={chart.loading}
|
|
311
|
+
onOpen={onOpen}
|
|
312
|
+
legend={
|
|
313
|
+
placed.length > 1 ? (
|
|
314
|
+
<ChartKey off={off} onToggle={toggle} series={placed} />
|
|
315
|
+
) : undefined
|
|
316
|
+
}
|
|
317
|
+
table={figures ? table : undefined}
|
|
318
|
+
title={title}
|
|
319
|
+
trend={trend}
|
|
320
|
+
value={value}
|
|
321
|
+
>
|
|
322
|
+
<Cartesian {...chart} aria-label={title} height={height} kind={kind} series={shown} />
|
|
323
|
+
</ChartFrame>
|
|
324
|
+
);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
export type ChartKeyProps = {
|
|
328
|
+
/** Each series' colour as the chart draws it: `series-1` to `series-5` by place unless given. */
|
|
329
|
+
series: readonly ChartSeries[];
|
|
330
|
+
/** The series switched off. */
|
|
331
|
+
off?: ReadonlySet<string>;
|
|
332
|
+
/** Given, each series is a toggle; without it the key only names them. */
|
|
333
|
+
onToggle?: (key: string) => void;
|
|
334
|
+
className?: string;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The key over a chart in its card, the same for every chart: a stroke of each colour and its
|
|
339
|
+
* name, in the body size and the quiet ink. As toggles, a series switched off keeps its place
|
|
340
|
+
* and its colour, struck through, so it can be found to turn back on.
|
|
341
|
+
*/
|
|
342
|
+
export const ChartKey = ({ series, off, onToggle, className }: ChartKeyProps) => (
|
|
343
|
+
<ul
|
|
344
|
+
className={cn('-ml-1.5 flex min-w-0 flex-wrap gap-x-1 gap-y-1', className)}
|
|
345
|
+
data-slot="chart-key"
|
|
346
|
+
>
|
|
347
|
+
{series.map((line, index) => {
|
|
348
|
+
const hidden = off?.has(line.key) ?? false;
|
|
349
|
+
const face = (
|
|
350
|
+
<>
|
|
351
|
+
<span
|
|
352
|
+
aria-hidden
|
|
353
|
+
className="h-3 w-0.75 shrink-0 rounded-full transition-opacity duration-150"
|
|
354
|
+
style={{
|
|
355
|
+
background: chartColor(
|
|
356
|
+
line.color ?? (`series-${Math.min(index + 1, 5)}` as ChartColor),
|
|
357
|
+
),
|
|
358
|
+
opacity: hidden ? 0.3 : 1,
|
|
359
|
+
}}
|
|
360
|
+
/>
|
|
361
|
+
<span
|
|
362
|
+
className={cn(
|
|
363
|
+
'truncate transition-colors duration-150',
|
|
364
|
+
hidden ? 'text-neutral-400 line-through' : 'text-neutral-600',
|
|
365
|
+
)}
|
|
366
|
+
>
|
|
367
|
+
{line.name ?? line.key}
|
|
368
|
+
</span>
|
|
369
|
+
</>
|
|
370
|
+
);
|
|
371
|
+
const shape = 'flex h-6 min-w-0 items-center gap-2 rounded-chip px-1.5 text-xs';
|
|
372
|
+
|
|
373
|
+
return (
|
|
374
|
+
<li key={line.key}>
|
|
375
|
+
{onToggle ? (
|
|
376
|
+
<button
|
|
377
|
+
aria-pressed={!hidden}
|
|
378
|
+
className={cn(
|
|
379
|
+
shape,
|
|
380
|
+
'cursor-pointer outline-none transition-colors duration-150 hover:bg-neutral-950/5 focus-visible:outline-2 focus-visible:outline-focus focus-visible:outline-solid focus-visible:outline-offset-1 motion-reduce:transition-none',
|
|
381
|
+
)}
|
|
382
|
+
onClick={() => onToggle(line.key)}
|
|
383
|
+
type="button"
|
|
384
|
+
>
|
|
385
|
+
{face}
|
|
386
|
+
</button>
|
|
387
|
+
) : (
|
|
388
|
+
<span className={shape}>{face}</span>
|
|
389
|
+
)}
|
|
390
|
+
</li>
|
|
391
|
+
);
|
|
392
|
+
})}
|
|
393
|
+
</ul>
|
|
394
|
+
);
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { type ReactNode, useState } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn';
|
|
5
|
+
import { type ChartColor, chartColor, seriesColor } from './lib/color';
|
|
6
|
+
import { formatCompact, formatNumber, formatPercent } from './lib/format';
|
|
7
|
+
import { ringSegmentPath } from './lib/path';
|
|
8
|
+
|
|
9
|
+
export type DonutSlice = { name: string; value: number; color?: ChartColor };
|
|
10
|
+
|
|
11
|
+
export type DonutChartProps = {
|
|
12
|
+
/** Says what the chart shows; it names the chart for a screen reader. */
|
|
13
|
+
'aria-label': string;
|
|
14
|
+
data: readonly DonutSlice[];
|
|
15
|
+
/** Across, in px. */
|
|
16
|
+
size?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The top half of the ring, the figure standing under its arch: the same shares in half the
|
|
19
|
+
* height, for a short card or a row of them.
|
|
20
|
+
*/
|
|
21
|
+
half?: boolean;
|
|
22
|
+
/** What the total in the centre counts: "sessions". */
|
|
23
|
+
label?: ReactNode;
|
|
24
|
+
/**
|
|
25
|
+
* The total in the centre. Off in a card that already leads with it: the middle then stays
|
|
26
|
+
* empty until a slice is pointed at, rather than saying the card's figure twice.
|
|
27
|
+
*/
|
|
28
|
+
total?: boolean;
|
|
29
|
+
formatValue?: (value: number) => string;
|
|
30
|
+
/** `tint`, the kit's look, a tint of each colour with a solid edge; `solid` fills each slice. */
|
|
31
|
+
fill?: 'tint' | 'solid';
|
|
32
|
+
/** While the data loads: the chart's own shape stands in, in the skeleton wash, at the size it will have. */
|
|
33
|
+
loading?: boolean;
|
|
34
|
+
className?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Past five slices the rest are one, in grey: six colours are not told apart. */
|
|
38
|
+
const MOST = 5;
|
|
39
|
+
const GAP = 0.04;
|
|
40
|
+
const EDGE = 2.5;
|
|
41
|
+
/** The corner of each slice, on all four of its ends. */
|
|
42
|
+
const CORNER = 4;
|
|
43
|
+
|
|
44
|
+
/** The stand-in's slices, as shares of the ring: one large, one middling, two small. */
|
|
45
|
+
const SKELETON_SHARES = [0.44, 0.27, 0.18, 0.11];
|
|
46
|
+
|
|
47
|
+
/** The stand-in's edge, a step stronger than its wash, as a slice's line is over its tint. */
|
|
48
|
+
const SKELETON_EDGE = 'color-mix(in oklab, var(--color-neutral-950) 14%, transparent)';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Shares of a whole as a ring, drawn the way every chart in the kit is: each slice a tint of
|
|
52
|
+
* its colour with a solid edge along the outside, as a line stands over the tint under it.
|
|
53
|
+
* The total sits in the middle; pointing at a slice puts its own figure there instead and
|
|
54
|
+
* lets the rest step back. Half, the ring runs over the top from left to right and the
|
|
55
|
+
* figure stands on the line its two feet stand on.
|
|
56
|
+
*/
|
|
57
|
+
export const DonutChart = ({
|
|
58
|
+
'aria-label': label,
|
|
59
|
+
data,
|
|
60
|
+
size = 160,
|
|
61
|
+
half = false,
|
|
62
|
+
label: caption,
|
|
63
|
+
total: showTotal = true,
|
|
64
|
+
formatValue = formatNumber,
|
|
65
|
+
fill = 'tint',
|
|
66
|
+
loading = false,
|
|
67
|
+
className,
|
|
68
|
+
}: DonutChartProps) => {
|
|
69
|
+
const [active, setActive] = useState<number | null>(null);
|
|
70
|
+
|
|
71
|
+
const slices =
|
|
72
|
+
data.length > MOST
|
|
73
|
+
? [
|
|
74
|
+
...data.slice(0, MOST - 1),
|
|
75
|
+
{
|
|
76
|
+
name: 'Other',
|
|
77
|
+
value: data.slice(MOST - 1).reduce((sum, slice) => sum + slice.value, 0),
|
|
78
|
+
color: 'neutral' as ChartColor,
|
|
79
|
+
},
|
|
80
|
+
]
|
|
81
|
+
: data;
|
|
82
|
+
const total = slices.reduce((sum, slice) => sum + Math.max(0, slice.value), 0);
|
|
83
|
+
const outer = size / 2;
|
|
84
|
+
const thickness = Math.max(14, Math.round(size * 0.14));
|
|
85
|
+
const inner = outer - thickness;
|
|
86
|
+
// A ring starts at twelve o'clock and goes all the way round; half of one starts at nine.
|
|
87
|
+
const origin = half ? -Math.PI / 2 : 0;
|
|
88
|
+
const turn = half ? Math.PI : Math.PI * 2;
|
|
89
|
+
// Half a ring is as tall as its radius, and a pixel more for the edge's round end.
|
|
90
|
+
const height = half ? Math.ceil(outer) + 1 : size;
|
|
91
|
+
|
|
92
|
+
let start = origin;
|
|
93
|
+
const arcs = slices.map((slice, index) => {
|
|
94
|
+
const sweep = total > 0 ? (Math.max(0, slice.value) / total) * turn : 0;
|
|
95
|
+
const from = start + (slices.length > 1 ? GAP / 2 : 0);
|
|
96
|
+
const to = start + sweep - (slices.length > 1 ? GAP / 2 : 0);
|
|
97
|
+
start += sweep;
|
|
98
|
+
return { ...slice, from, to, color: chartColor(slice.color ?? seriesColor(index)) };
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const shown = active !== null ? slices[active] : undefined;
|
|
102
|
+
const edge = outer - EDGE / 2;
|
|
103
|
+
const point = (angle: number) => ({
|
|
104
|
+
// Rounded, so the server and the browser draw the same path to the last digit.
|
|
105
|
+
x: Math.round((outer + edge * Math.sin(angle)) * 100) / 100,
|
|
106
|
+
y: Math.round((outer - edge * Math.cos(angle)) * 100) / 100,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<figure
|
|
111
|
+
aria-busy={loading || undefined}
|
|
112
|
+
aria-label={label}
|
|
113
|
+
className={cn('relative m-0 shrink-0', className)}
|
|
114
|
+
data-slot="donut-chart"
|
|
115
|
+
style={{ height, width: size }}
|
|
116
|
+
>
|
|
117
|
+
<svg aria-hidden="true" className="block" height={height} width={size}>
|
|
118
|
+
{loading ? null : total <= 0 ? (
|
|
119
|
+
<path
|
|
120
|
+
d={ringSegmentPath(
|
|
121
|
+
outer,
|
|
122
|
+
outer,
|
|
123
|
+
outer,
|
|
124
|
+
inner,
|
|
125
|
+
origin,
|
|
126
|
+
origin + turn,
|
|
127
|
+
CORNER,
|
|
128
|
+
)}
|
|
129
|
+
fill="var(--color-neutral-950)"
|
|
130
|
+
fillOpacity={0.05}
|
|
131
|
+
/>
|
|
132
|
+
) : (
|
|
133
|
+
arcs.map((arc, index) =>
|
|
134
|
+
arc.to <= arc.from ? null : (
|
|
135
|
+
<g
|
|
136
|
+
className="cursor-default transition-opacity duration-150 motion-reduce:transition-none"
|
|
137
|
+
key={arc.name}
|
|
138
|
+
onPointerEnter={() => setActive(index)}
|
|
139
|
+
onPointerLeave={() => setActive(null)}
|
|
140
|
+
opacity={active === null || active === index ? 1 : 0.35}
|
|
141
|
+
>
|
|
142
|
+
<path
|
|
143
|
+
d={ringSegmentPath(
|
|
144
|
+
outer,
|
|
145
|
+
outer,
|
|
146
|
+
outer,
|
|
147
|
+
inner,
|
|
148
|
+
arc.from,
|
|
149
|
+
arc.to,
|
|
150
|
+
CORNER,
|
|
151
|
+
)}
|
|
152
|
+
fill={arc.color}
|
|
153
|
+
fillOpacity={fill === 'solid' ? 1 : 0.2}
|
|
154
|
+
/>
|
|
155
|
+
{/* The slice's own line along its outside edge. */}
|
|
156
|
+
{fill === 'tint' && (
|
|
157
|
+
<path
|
|
158
|
+
d={`M${point(arc.from + 0.02).x},${point(arc.from + 0.02).y} A${edge},${edge} 0 ${arc.to - arc.from > Math.PI ? 1 : 0} 1 ${point(arc.to - 0.02).x},${point(arc.to - 0.02).y}`}
|
|
159
|
+
fill="none"
|
|
160
|
+
stroke={arc.color}
|
|
161
|
+
strokeLinecap="round"
|
|
162
|
+
strokeWidth={EDGE}
|
|
163
|
+
/>
|
|
164
|
+
)}
|
|
165
|
+
</g>
|
|
166
|
+
),
|
|
167
|
+
)
|
|
168
|
+
)}
|
|
169
|
+
</svg>
|
|
170
|
+
{loading && (
|
|
171
|
+
<DonutSkeleton
|
|
172
|
+
fill={fill}
|
|
173
|
+
height={height}
|
|
174
|
+
inner={inner}
|
|
175
|
+
origin={origin}
|
|
176
|
+
outer={outer}
|
|
177
|
+
turn={turn}
|
|
178
|
+
/>
|
|
179
|
+
)}
|
|
180
|
+
<figcaption
|
|
181
|
+
aria-live="polite"
|
|
182
|
+
className={cn(
|
|
183
|
+
'pointer-events-none absolute inset-x-0 flex flex-col items-center gap-0.5 text-center',
|
|
184
|
+
// Half, the figure stands on the feet's line, inside the arch.
|
|
185
|
+
half ? 'bottom-0 justify-end' : 'inset-y-0 justify-center',
|
|
186
|
+
)}
|
|
187
|
+
>
|
|
188
|
+
{loading && showTotal && (
|
|
189
|
+
<>
|
|
190
|
+
{/* The figure's own line, so nothing moves when it lands; what it counts stays. */}
|
|
191
|
+
<span aria-hidden className="flex items-center text-lg">
|
|
192
|
+
<span className="invisible w-0">0</span>
|
|
193
|
+
<span className="skeleton h-[0.75em] w-12 rounded-chip" />
|
|
194
|
+
</span>
|
|
195
|
+
<span
|
|
196
|
+
className="truncate text-neutral-600 text-xxs"
|
|
197
|
+
style={{ maxWidth: Math.max(0, inner * 2 - 16) }}
|
|
198
|
+
>
|
|
199
|
+
{caption}
|
|
200
|
+
</span>
|
|
201
|
+
</>
|
|
202
|
+
)}
|
|
203
|
+
{!loading && (shown || showTotal) && (
|
|
204
|
+
<>
|
|
205
|
+
<span className="font-medium text-lg text-neutral-950 tabular-nums">
|
|
206
|
+
{shown ? formatValue(shown.value) : formatCompact(total)}
|
|
207
|
+
</span>
|
|
208
|
+
<span
|
|
209
|
+
className="truncate text-neutral-600 text-xxs"
|
|
210
|
+
style={{ maxWidth: Math.max(0, inner * 2 - 16) }}
|
|
211
|
+
>
|
|
212
|
+
{shown
|
|
213
|
+
? `${shown.name} · ${formatPercent(total ? shown.value / total : 0)}`
|
|
214
|
+
: caption}
|
|
215
|
+
</span>
|
|
216
|
+
</>
|
|
217
|
+
)}
|
|
218
|
+
</figcaption>
|
|
219
|
+
{!loading && (
|
|
220
|
+
<div className="sr-only">
|
|
221
|
+
<table>
|
|
222
|
+
<caption>{label}</caption>
|
|
223
|
+
<tbody>
|
|
224
|
+
{slices.map(slice => (
|
|
225
|
+
<tr key={slice.name}>
|
|
226
|
+
<th scope="row">{slice.name}</th>
|
|
227
|
+
<td>{formatValue(slice.value)}</td>
|
|
228
|
+
</tr>
|
|
229
|
+
))}
|
|
230
|
+
</tbody>
|
|
231
|
+
</table>
|
|
232
|
+
</div>
|
|
233
|
+
)}
|
|
234
|
+
</figure>
|
|
235
|
+
);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* A ring that is loading: the ring at its own radii, split into a few slices a little apart, in
|
|
240
|
+
* the skeleton wash with the light crossing it, and with the slices' edges along the outside
|
|
241
|
+
* when the loaded ring has them. It is drawn where the ring will be, so nothing moves when the
|
|
242
|
+
* data lands.
|
|
243
|
+
*/
|
|
244
|
+
const DonutSkeleton = ({
|
|
245
|
+
outer,
|
|
246
|
+
inner,
|
|
247
|
+
origin,
|
|
248
|
+
turn,
|
|
249
|
+
height,
|
|
250
|
+
fill,
|
|
251
|
+
}: {
|
|
252
|
+
outer: number;
|
|
253
|
+
inner: number;
|
|
254
|
+
origin: number;
|
|
255
|
+
turn: number;
|
|
256
|
+
height: number;
|
|
257
|
+
fill: 'tint' | 'solid';
|
|
258
|
+
}) => {
|
|
259
|
+
let start = origin;
|
|
260
|
+
const arcs = SKELETON_SHARES.map(share => {
|
|
261
|
+
const from = start + GAP / 2;
|
|
262
|
+
start += share * turn;
|
|
263
|
+
return { from, to: start - GAP / 2 };
|
|
264
|
+
});
|
|
265
|
+
const wash = arcs
|
|
266
|
+
.map(arc => ringSegmentPath(outer, outer, outer, inner, arc.from, arc.to, CORNER))
|
|
267
|
+
.join(' ');
|
|
268
|
+
// The edge as the loaded one is drawn: a band along the outside with round ends, short of the corners.
|
|
269
|
+
const edge = arcs
|
|
270
|
+
.map(arc =>
|
|
271
|
+
ringSegmentPath(
|
|
272
|
+
outer,
|
|
273
|
+
outer,
|
|
274
|
+
outer,
|
|
275
|
+
outer - EDGE,
|
|
276
|
+
arc.from + 0.02,
|
|
277
|
+
arc.to - 0.02,
|
|
278
|
+
EDGE / 2,
|
|
279
|
+
),
|
|
280
|
+
)
|
|
281
|
+
.join(' ');
|
|
282
|
+
|
|
283
|
+
return (
|
|
284
|
+
<div
|
|
285
|
+
aria-hidden
|
|
286
|
+
className="absolute inset-x-0 top-0"
|
|
287
|
+
data-slot="chart-skeleton"
|
|
288
|
+
style={{ height }}
|
|
289
|
+
>
|
|
290
|
+
<div className="skeleton absolute inset-0" style={{ clipPath: `path('${wash}')` }} />
|
|
291
|
+
{fill === 'tint' && (
|
|
292
|
+
<div
|
|
293
|
+
className="skeleton absolute inset-0"
|
|
294
|
+
style={{ backgroundColor: SKELETON_EDGE, clipPath: `path('${edge}')` }}
|
|
295
|
+
/>
|
|
296
|
+
)}
|
|
297
|
+
</div>
|
|
298
|
+
);
|
|
299
|
+
};
|