@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,599 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useId, useLayoutEffect, useRef, useState, } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn.js';
|
|
5
|
+
import { holdPointer } from '../lib/reorder.js';
|
|
6
|
+
const ROW_HEAD = 44;
|
|
7
|
+
const COLUMN_WIDTH = 112;
|
|
8
|
+
const MIN_WIDTH = 40;
|
|
9
|
+
const letter = (index) => index < 26
|
|
10
|
+
? String.fromCharCode(65 + index)
|
|
11
|
+
: letter(Math.floor(index / 26) - 1) + letter(index % 26);
|
|
12
|
+
const rangeOf = ({ anchor, focus }) => ({
|
|
13
|
+
top: Math.min(anchor.row, focus.row),
|
|
14
|
+
bottom: Math.max(anchor.row, focus.row),
|
|
15
|
+
left: Math.min(anchor.col, focus.col),
|
|
16
|
+
right: Math.max(anchor.col, focus.col),
|
|
17
|
+
});
|
|
18
|
+
const within = (range, row, col) => row >= range.top && row <= range.bottom && col >= range.left && col <= range.right;
|
|
19
|
+
/** A plain number ("12", "-3.5", "1,250", "€ 40", "12%"): set right and in tabular figures. */
|
|
20
|
+
const looksNumeric = (value) => /^[-+]?[€$£]?\s?\d[\d.,]*\s?%?$/.test(value.trim());
|
|
21
|
+
/** A value a series can run on: a bare number. */
|
|
22
|
+
const asNumber = (value) => {
|
|
23
|
+
const trimmed = value.trim();
|
|
24
|
+
return /^[-+]?\d+(\.\d+)?$/.test(trimmed) ? Number(trimmed) : null;
|
|
25
|
+
};
|
|
26
|
+
const decimals = (value) => value.trim().split('.')[1]?.length ?? 0;
|
|
27
|
+
/**
|
|
28
|
+
* The values a fill writes at `offset` steps from the start of its source (negative before it): a
|
|
29
|
+
* run of two or more numbers a constant step apart carries on as a series, anything else repeats.
|
|
30
|
+
*/
|
|
31
|
+
const fillValue = (source, offset) => {
|
|
32
|
+
const numbers = source.map(asNumber);
|
|
33
|
+
if (source.length >= 2 && numbers.every(n => n !== null)) {
|
|
34
|
+
const list = numbers;
|
|
35
|
+
const step = (list[1] ?? 0) - (list[0] ?? 0);
|
|
36
|
+
const even = list.every((n, i) => i === 0 || Math.abs(n - (list[i - 1] ?? 0) - step) < 1e-9);
|
|
37
|
+
if (even) {
|
|
38
|
+
const places = Math.max(...source.map(decimals));
|
|
39
|
+
return ((list[0] ?? 0) + offset * step).toFixed(places);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const n = source.length;
|
|
43
|
+
return source[((offset % n) + n) % n] ?? '';
|
|
44
|
+
};
|
|
45
|
+
/** Text from a clipboard as rows of cells: tab between cells, a line a row. */
|
|
46
|
+
const parseClipboard = (text) => {
|
|
47
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
48
|
+
if (lines.length > 1 && lines.at(-1) === '')
|
|
49
|
+
lines.pop();
|
|
50
|
+
return lines.map(line => line.split('\t'));
|
|
51
|
+
};
|
|
52
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
53
|
+
export const DataGrid = ({ label, values, onValuesChange, columns, onSelectionChange, footer, toolbar, summaryRows = 0, className, ...props }) => {
|
|
54
|
+
const id = useId();
|
|
55
|
+
const rows = values.length;
|
|
56
|
+
const cols = columns?.length ?? Math.max(0, ...values.map(row => row.length));
|
|
57
|
+
const editable = onValuesChange !== undefined;
|
|
58
|
+
const [widths, setWidths] = useState(() => Array.from({ length: cols }, (_, i) => columns?.[i]?.width ?? COLUMN_WIDTH));
|
|
59
|
+
// A row without a height of its own is the cell height, so it follows the theme's size.
|
|
60
|
+
const [heights, setHeights] = useState([]);
|
|
61
|
+
const [selection, setSelectionState] = useState({
|
|
62
|
+
anchor: { row: 0, col: 0 },
|
|
63
|
+
focus: { row: 0, col: 0 },
|
|
64
|
+
});
|
|
65
|
+
// Blocks added with Ctrl or ⌘ before the one being made now.
|
|
66
|
+
const [extra, setExtra] = useState([]);
|
|
67
|
+
const [edit, setEdit] = useState(null);
|
|
68
|
+
// The cells a fill handle drag will write, while it is dragged.
|
|
69
|
+
const [fill, setFill] = useState(null);
|
|
70
|
+
const history = useRef({ past: [], future: [] });
|
|
71
|
+
const table = useRef(null);
|
|
72
|
+
const grid = useRef(null);
|
|
73
|
+
const ended = useRef(false);
|
|
74
|
+
const rangeBox = useRef(null);
|
|
75
|
+
const activeBox = useRef(null);
|
|
76
|
+
const fillBox = useRef(null);
|
|
77
|
+
const editor = useRef(null);
|
|
78
|
+
const input = useRef(null);
|
|
79
|
+
const scroller = useRef(null);
|
|
80
|
+
// The width the grid has to fill: the columns never end short of the frame (see `shown`).
|
|
81
|
+
const [room, setRoom] = useState(0);
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
const node = scroller.current;
|
|
84
|
+
if (!node)
|
|
85
|
+
return;
|
|
86
|
+
const measure = () => setRoom(node.clientWidth);
|
|
87
|
+
measure();
|
|
88
|
+
const observer = new ResizeObserver(measure);
|
|
89
|
+
observer.observe(node);
|
|
90
|
+
return () => observer.disconnect();
|
|
91
|
+
}, []);
|
|
92
|
+
// Columns added later take the default width.
|
|
93
|
+
if (widths.length < cols)
|
|
94
|
+
setWidths(current => [
|
|
95
|
+
...current,
|
|
96
|
+
...Array.from({ length: cols - current.length }, (_, i) => columns?.[current.length + i]?.width ?? COLUMN_WIDTH),
|
|
97
|
+
]);
|
|
98
|
+
const range = rangeOf(selection);
|
|
99
|
+
const active = selection.anchor;
|
|
100
|
+
const many = range.top !== range.bottom || range.left !== range.right;
|
|
101
|
+
const valueAt = (row, col) => values[row]?.[col] ?? '';
|
|
102
|
+
// Picking a selection; the blocks added before it with Ctrl or ⌘ stay unless told otherwise.
|
|
103
|
+
const setSelection = (next, others = extra) => {
|
|
104
|
+
setSelectionState(next);
|
|
105
|
+
setExtra(others);
|
|
106
|
+
onSelectionChange?.(rangeOf(next), next.anchor, [...others, rangeOf(next)]);
|
|
107
|
+
};
|
|
108
|
+
const selected = (row, col) => within(range, row, col) || extra.some(block => within(block, row, col));
|
|
109
|
+
// More than the one cell: a block, or blocks added to it.
|
|
110
|
+
const multi = many || extra.length > 0;
|
|
111
|
+
const cellNode = (row, col) => grid.current?.querySelector(`[data-row="${row}"][data-col="${col}"]`) ?? null;
|
|
112
|
+
/** A range's box in the grid, its edges on the grid lines around it. */
|
|
113
|
+
const boxOf = (block) => {
|
|
114
|
+
const first = cellNode(block.top, block.left);
|
|
115
|
+
const last = cellNode(block.bottom, block.right);
|
|
116
|
+
const frame = grid.current?.getBoundingClientRect();
|
|
117
|
+
if (!first || !last || !frame)
|
|
118
|
+
return null;
|
|
119
|
+
const a = first.getBoundingClientRect();
|
|
120
|
+
const b = last.getBoundingClientRect();
|
|
121
|
+
// A cell's own rule is on its right and bottom: the box starts on the rule before it.
|
|
122
|
+
return {
|
|
123
|
+
left: a.left - frame.left - 1,
|
|
124
|
+
top: a.top - frame.top - 1,
|
|
125
|
+
width: b.right - a.left + 1,
|
|
126
|
+
height: b.bottom - a.top + 1,
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
// The selection's marks are drawn over the cells, from where the cells really are.
|
|
130
|
+
useLayoutEffect(() => {
|
|
131
|
+
const place = (node, block) => {
|
|
132
|
+
if (!node)
|
|
133
|
+
return;
|
|
134
|
+
const box = block && boxOf(block);
|
|
135
|
+
node.style.display = box ? '' : 'none';
|
|
136
|
+
if (!box)
|
|
137
|
+
return;
|
|
138
|
+
node.style.left = `${box.left}px`;
|
|
139
|
+
node.style.top = `${box.top}px`;
|
|
140
|
+
node.style.width = `${box.width}px`;
|
|
141
|
+
node.style.height = `${box.height}px`;
|
|
142
|
+
};
|
|
143
|
+
const one = { top: active.row, bottom: active.row, left: active.col, right: active.col };
|
|
144
|
+
place(rangeBox.current, many ? range : null);
|
|
145
|
+
grid.current
|
|
146
|
+
?.querySelectorAll('[data-slot=data-grid-extra]')
|
|
147
|
+
.forEach((node, i) => {
|
|
148
|
+
place(node, extra[i] ?? null);
|
|
149
|
+
});
|
|
150
|
+
place(activeBox.current, rows && cols ? one : null);
|
|
151
|
+
place(fillBox.current, fill);
|
|
152
|
+
place(editor.current, edit ? one : null);
|
|
153
|
+
});
|
|
154
|
+
/* ---------- Changing values ---------- */
|
|
155
|
+
const commit = (next) => {
|
|
156
|
+
if (!onValuesChange)
|
|
157
|
+
return;
|
|
158
|
+
history.current.past.push(values);
|
|
159
|
+
history.current.future = [];
|
|
160
|
+
onValuesChange(next);
|
|
161
|
+
};
|
|
162
|
+
const write = (changes) => {
|
|
163
|
+
const next = values.map(row => [...row]);
|
|
164
|
+
let changed = false;
|
|
165
|
+
for (const { row, col, value } of changes) {
|
|
166
|
+
if (row >= rows || col >= cols)
|
|
167
|
+
continue;
|
|
168
|
+
const line = next[row] ?? [];
|
|
169
|
+
if ((line[col] ?? '') === value)
|
|
170
|
+
continue;
|
|
171
|
+
while (line.length < col)
|
|
172
|
+
line.push('');
|
|
173
|
+
line[col] = value;
|
|
174
|
+
next[row] = line;
|
|
175
|
+
changed = true;
|
|
176
|
+
}
|
|
177
|
+
if (changed)
|
|
178
|
+
commit(next);
|
|
179
|
+
};
|
|
180
|
+
const cellsOf = (block) => {
|
|
181
|
+
const list = [];
|
|
182
|
+
for (let row = block.top; row <= block.bottom; row++)
|
|
183
|
+
for (let col = block.left; col <= block.right; col++)
|
|
184
|
+
list.push({ row, col });
|
|
185
|
+
return list;
|
|
186
|
+
};
|
|
187
|
+
const undo = (redo) => {
|
|
188
|
+
if (!onValuesChange)
|
|
189
|
+
return;
|
|
190
|
+
const { past, future } = history.current;
|
|
191
|
+
const from = redo ? future : past;
|
|
192
|
+
const to = redo ? past : future;
|
|
193
|
+
const snapshot = from.pop();
|
|
194
|
+
if (!snapshot)
|
|
195
|
+
return;
|
|
196
|
+
to.push(values);
|
|
197
|
+
onValuesChange(snapshot);
|
|
198
|
+
};
|
|
199
|
+
/* ---------- Moving the selection ---------- */
|
|
200
|
+
/** Moves the selection from the keyboard; the added blocks go unless `keep` (Shift extending). */
|
|
201
|
+
const select = (anchor, focus = anchor, keep = false) => {
|
|
202
|
+
const fit = (cell) => ({
|
|
203
|
+
row: clamp(cell.row, 0, rows - 1),
|
|
204
|
+
col: clamp(cell.col, 0, cols - 1),
|
|
205
|
+
});
|
|
206
|
+
const next = { anchor: fit(anchor), focus: fit(focus) };
|
|
207
|
+
setSelection(next, keep ? extra : []);
|
|
208
|
+
requestAnimationFrame(() => cellNode(next.focus.row, next.focus.col)?.scrollIntoView({
|
|
209
|
+
block: 'nearest',
|
|
210
|
+
inline: 'nearest',
|
|
211
|
+
}));
|
|
212
|
+
};
|
|
213
|
+
const startEdit = (mode, draft) => {
|
|
214
|
+
if (!editable)
|
|
215
|
+
return;
|
|
216
|
+
ended.current = false;
|
|
217
|
+
setEdit({ ...active, draft: draft ?? valueAt(active.row, active.col), mode });
|
|
218
|
+
};
|
|
219
|
+
const endEdit = (save, move) => {
|
|
220
|
+
// Ending takes focus off the field, which blurs it: the edit ends once.
|
|
221
|
+
if (!edit || ended.current)
|
|
222
|
+
return;
|
|
223
|
+
ended.current = true;
|
|
224
|
+
if (save)
|
|
225
|
+
write([{ row: edit.row, col: edit.col, value: edit.draft }]);
|
|
226
|
+
setEdit(null);
|
|
227
|
+
if (move)
|
|
228
|
+
select({ row: edit.row + move.row, col: edit.col + move.col });
|
|
229
|
+
table.current?.focus({ preventScroll: true });
|
|
230
|
+
};
|
|
231
|
+
// The field takes focus as it opens, the caret after what is in it.
|
|
232
|
+
const openField = (node) => {
|
|
233
|
+
if (node && node !== input.current) {
|
|
234
|
+
node.focus({ preventScroll: true });
|
|
235
|
+
node.setSelectionRange(node.value.length, node.value.length);
|
|
236
|
+
}
|
|
237
|
+
input.current = node;
|
|
238
|
+
};
|
|
239
|
+
/* ---------- Pointer ---------- */
|
|
240
|
+
// Which cell is under the pointer, while a drag runs across the grid.
|
|
241
|
+
const cellAt = (x, y) => {
|
|
242
|
+
const node = document.elementFromPoint(x, y)?.closest('[data-row]');
|
|
243
|
+
if (!node || !grid.current?.contains(node))
|
|
244
|
+
return null;
|
|
245
|
+
return { row: Number(node.dataset.row), col: Number(node.dataset.col) };
|
|
246
|
+
};
|
|
247
|
+
const track = (onMove, onUp) => {
|
|
248
|
+
const up = () => {
|
|
249
|
+
window.removeEventListener('pointermove', onMove);
|
|
250
|
+
window.removeEventListener('pointerup', up);
|
|
251
|
+
onUp?.();
|
|
252
|
+
};
|
|
253
|
+
window.addEventListener('pointermove', onMove);
|
|
254
|
+
window.addEventListener('pointerup', up);
|
|
255
|
+
};
|
|
256
|
+
const pressCell = (event, cell) => {
|
|
257
|
+
if (event.button !== 0)
|
|
258
|
+
return;
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
if (edit)
|
|
261
|
+
endEdit(true);
|
|
262
|
+
table.current?.focus({ preventScroll: true });
|
|
263
|
+
// Ctrl or ⌘ keeps what is selected and starts another block beside it; Shift stretches the
|
|
264
|
+
// block being made; a plain press starts over.
|
|
265
|
+
const adding = event.ctrlKey || event.metaKey;
|
|
266
|
+
const others = adding ? [...extra, range] : event.shiftKey ? extra : [];
|
|
267
|
+
const anchor = event.shiftKey && !adding ? selection.anchor : cell;
|
|
268
|
+
setSelection({ anchor, focus: cell }, others);
|
|
269
|
+
track(move => {
|
|
270
|
+
const over = cellAt(move.clientX, move.clientY);
|
|
271
|
+
if (over)
|
|
272
|
+
setSelection({ anchor, focus: over }, others);
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
// A head selects its whole column (or row); dragged along the heads, the columns it passes.
|
|
276
|
+
const pressHead = (event, axis, index) => {
|
|
277
|
+
if (event.button !== 0)
|
|
278
|
+
return;
|
|
279
|
+
event.preventDefault();
|
|
280
|
+
if (edit)
|
|
281
|
+
endEdit(true);
|
|
282
|
+
table.current?.focus({ preventScroll: true });
|
|
283
|
+
const whole = (from, to) => axis === 'col'
|
|
284
|
+
? { anchor: { row: 0, col: from }, focus: { row: rows - 1, col: to } }
|
|
285
|
+
: { anchor: { row: from, col: 0 }, focus: { row: to, col: cols - 1 } };
|
|
286
|
+
const adding = event.ctrlKey || event.metaKey;
|
|
287
|
+
const others = adding ? [...extra, range] : event.shiftKey ? extra : [];
|
|
288
|
+
const from = event.shiftKey && !adding
|
|
289
|
+
? axis === 'col'
|
|
290
|
+
? selection.anchor.col
|
|
291
|
+
: selection.anchor.row
|
|
292
|
+
: index;
|
|
293
|
+
setSelection(whole(from, index), others);
|
|
294
|
+
track(move => {
|
|
295
|
+
const node = document
|
|
296
|
+
.elementFromPoint(move.clientX, move.clientY)
|
|
297
|
+
?.closest(axis === 'col' ? '[data-col]' : '[data-row]');
|
|
298
|
+
if (!node || !grid.current?.contains(node))
|
|
299
|
+
return;
|
|
300
|
+
const at = Number(axis === 'col' ? node.dataset.col : node.dataset.row);
|
|
301
|
+
if (!Number.isNaN(at))
|
|
302
|
+
setSelection(whole(from, at), others);
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
const pressFill = (event) => {
|
|
306
|
+
if (event.button !== 0)
|
|
307
|
+
return;
|
|
308
|
+
event.preventDefault();
|
|
309
|
+
event.stopPropagation();
|
|
310
|
+
const source = range;
|
|
311
|
+
let target = null;
|
|
312
|
+
track(move => {
|
|
313
|
+
const over = cellAt(move.clientX, move.clientY);
|
|
314
|
+
if (!over)
|
|
315
|
+
return;
|
|
316
|
+
// The fill runs along the axis the pointer has gone further out on.
|
|
317
|
+
const down = over.row > source.bottom ? over.row - source.bottom : 0;
|
|
318
|
+
const up = over.row < source.top ? source.top - over.row : 0;
|
|
319
|
+
const right = over.col > source.right ? over.col - source.right : 0;
|
|
320
|
+
const left = over.col < source.left ? source.left - over.col : 0;
|
|
321
|
+
const vertical = Math.max(down, up) >= Math.max(right, left);
|
|
322
|
+
if (vertical && (down || up))
|
|
323
|
+
target = down
|
|
324
|
+
? { ...source, top: source.bottom + 1, bottom: over.row }
|
|
325
|
+
: { ...source, top: over.row, bottom: source.top - 1 };
|
|
326
|
+
else if (!vertical && (right || left))
|
|
327
|
+
target = right
|
|
328
|
+
? { ...source, left: source.right + 1, right: over.col }
|
|
329
|
+
: { ...source, left: over.col, right: source.left - 1 };
|
|
330
|
+
else
|
|
331
|
+
target = null;
|
|
332
|
+
setFill(target);
|
|
333
|
+
}, () => {
|
|
334
|
+
setFill(null);
|
|
335
|
+
if (!target)
|
|
336
|
+
return;
|
|
337
|
+
const block = target;
|
|
338
|
+
const vertical = block.left === source.left && block.right === source.right;
|
|
339
|
+
const changes = cellsOf(block).map(({ row, col }) => {
|
|
340
|
+
const line = vertical
|
|
341
|
+
? Array.from({ length: source.bottom - source.top + 1 }, (_, i) => valueAt(source.top + i, col))
|
|
342
|
+
: Array.from({ length: source.right - source.left + 1 }, (_, i) => valueAt(row, source.left + i));
|
|
343
|
+
const offset = vertical ? row - source.top : col - source.left;
|
|
344
|
+
return { row, col, value: fillValue(line, offset) };
|
|
345
|
+
});
|
|
346
|
+
write(changes);
|
|
347
|
+
select({
|
|
348
|
+
row: Math.min(source.top, block.top),
|
|
349
|
+
col: Math.min(source.left, block.left),
|
|
350
|
+
}, {
|
|
351
|
+
row: Math.max(source.bottom, block.bottom),
|
|
352
|
+
col: Math.max(source.right, block.right),
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
};
|
|
356
|
+
const resize = (event, axis, index) => {
|
|
357
|
+
if (event.button !== 0)
|
|
358
|
+
return;
|
|
359
|
+
event.preventDefault();
|
|
360
|
+
event.stopPropagation();
|
|
361
|
+
const start = axis === 'col' ? event.clientX : event.clientY;
|
|
362
|
+
const head = event.currentTarget.parentElement;
|
|
363
|
+
const from = axis === 'col' ? (widths[index] ?? COLUMN_WIDTH) : (head?.offsetHeight ?? 32);
|
|
364
|
+
// A row never goes under the cell height it started at: taller, for a value that wraps, not shorter.
|
|
365
|
+
const cell = table.current
|
|
366
|
+
? Number.parseFloat(getComputedStyle(table.current).getPropertyValue('--spacing-cell')) || 32
|
|
367
|
+
: 32;
|
|
368
|
+
const release = holdPointer(axis === 'col' ? 'col-resize' : 'row-resize');
|
|
369
|
+
track(move => {
|
|
370
|
+
const size = Math.round(from + (axis === 'col' ? move.clientX : move.clientY) - start);
|
|
371
|
+
if (axis === 'col')
|
|
372
|
+
setWidths(current => current.map((w, i) => (i === index ? Math.max(MIN_WIDTH, size) : w)));
|
|
373
|
+
else
|
|
374
|
+
setHeights(current => {
|
|
375
|
+
const next = [...current];
|
|
376
|
+
next[index] = Math.max(cell, size);
|
|
377
|
+
return next;
|
|
378
|
+
});
|
|
379
|
+
}, release);
|
|
380
|
+
};
|
|
381
|
+
/* ---------- Keyboard and clipboard ---------- */
|
|
382
|
+
const key = (event) => {
|
|
383
|
+
if (edit || event.target !== table.current || !rows || !cols)
|
|
384
|
+
return;
|
|
385
|
+
const mod = event.metaKey || event.ctrlKey;
|
|
386
|
+
const step = {
|
|
387
|
+
ArrowUp: { row: -1, col: 0 },
|
|
388
|
+
ArrowDown: { row: 1, col: 0 },
|
|
389
|
+
ArrowLeft: { row: 0, col: -1 },
|
|
390
|
+
ArrowRight: { row: 0, col: 1 },
|
|
391
|
+
};
|
|
392
|
+
const delta = step[event.key];
|
|
393
|
+
if (delta) {
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
const from = event.shiftKey ? selection.focus : selection.anchor;
|
|
396
|
+
// With Ctrl or ⌘ the move runs to the grid's edge.
|
|
397
|
+
const to = mod
|
|
398
|
+
? {
|
|
399
|
+
row: delta.row ? (delta.row < 0 ? 0 : rows - 1) : from.row,
|
|
400
|
+
col: delta.col ? (delta.col < 0 ? 0 : cols - 1) : from.col,
|
|
401
|
+
}
|
|
402
|
+
: { row: from.row + delta.row, col: from.col + delta.col };
|
|
403
|
+
if (event.shiftKey)
|
|
404
|
+
select(selection.anchor, to, true);
|
|
405
|
+
else
|
|
406
|
+
select(to);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (event.key === 'Home' || event.key === 'End') {
|
|
410
|
+
event.preventDefault();
|
|
411
|
+
const col = event.key === 'Home' ? 0 : cols - 1;
|
|
412
|
+
const row = mod ? (event.key === 'Home' ? 0 : rows - 1) : active.row;
|
|
413
|
+
select({ row, col });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (mod && event.key.toLowerCase() === 'a') {
|
|
417
|
+
event.preventDefault();
|
|
418
|
+
setSelection({ anchor: { row: 0, col: 0 }, focus: { row: rows - 1, col: cols - 1 } }, []);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (mod && event.key.toLowerCase() === 'z') {
|
|
422
|
+
event.preventDefault();
|
|
423
|
+
undo(event.shiftKey);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (mod && event.key.toLowerCase() === 'y') {
|
|
427
|
+
event.preventDefault();
|
|
428
|
+
undo(true);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (event.key === 'Escape' && multi) {
|
|
432
|
+
event.preventDefault();
|
|
433
|
+
select(active);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (!editable)
|
|
437
|
+
return;
|
|
438
|
+
if (event.key === 'Enter' || event.key === 'F2') {
|
|
439
|
+
event.preventDefault();
|
|
440
|
+
startEdit('edit');
|
|
441
|
+
}
|
|
442
|
+
else if (event.key === 'Delete' || event.key === 'Backspace') {
|
|
443
|
+
event.preventDefault();
|
|
444
|
+
// Every selected block is cleared, the added ones too.
|
|
445
|
+
write([...extra, range].flatMap(cellsOf).map(cell => ({ ...cell, value: '' })));
|
|
446
|
+
}
|
|
447
|
+
else if (event.key.length === 1 && !mod && !event.altKey) {
|
|
448
|
+
// Typing over a cell replaces its value, starting with the key typed.
|
|
449
|
+
event.preventDefault();
|
|
450
|
+
startEdit('replace', event.key);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
const tsv = (block) => Array.from({ length: block.bottom - block.top + 1 }, (_, r) => Array.from({ length: block.right - block.left + 1 }, (_, c) => valueAt(block.top + r, block.left + c)).join('\t')).join('\n');
|
|
454
|
+
const copy = (event, cut) => {
|
|
455
|
+
if (edit || event.target !== table.current)
|
|
456
|
+
return;
|
|
457
|
+
event.preventDefault();
|
|
458
|
+
event.clipboardData.setData('text/plain', tsv(range));
|
|
459
|
+
if (cut && editable)
|
|
460
|
+
write(cellsOf(range).map(cell => ({ ...cell, value: '' })));
|
|
461
|
+
};
|
|
462
|
+
const paste = (event) => {
|
|
463
|
+
if (edit || !editable || event.target !== table.current)
|
|
464
|
+
return;
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
const block = parseClipboard(event.clipboardData.getData('text/plain'));
|
|
467
|
+
const single = block.length === 1 && block[0]?.length === 1;
|
|
468
|
+
// One value fills the whole selection; a block lands with its corner on the selection's.
|
|
469
|
+
if (single) {
|
|
470
|
+
write(cellsOf(range).map(cell => ({ ...cell, value: block[0]?.[0] ?? '' })));
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
write(block.flatMap((line, r) => line.map((value, c) => ({ row: range.top + r, col: range.left + c, value }))));
|
|
474
|
+
select({ row: range.top, col: range.left }, {
|
|
475
|
+
row: range.top + block.length - 1,
|
|
476
|
+
col: range.left + Math.max(...block.map(line => line.length)) - 1,
|
|
477
|
+
});
|
|
478
|
+
};
|
|
479
|
+
const editKey = (event) => {
|
|
480
|
+
if (!edit)
|
|
481
|
+
return;
|
|
482
|
+
const moves = {
|
|
483
|
+
Enter: { row: event.shiftKey ? -1 : 1, col: 0 },
|
|
484
|
+
Tab: { row: 0, col: event.shiftKey ? -1 : 1 },
|
|
485
|
+
};
|
|
486
|
+
// Typed straight over a cell, the arrows leave it, as they would have without the edit;
|
|
487
|
+
// opened with Enter or F2 they move through the text.
|
|
488
|
+
if (edit.mode === 'replace') {
|
|
489
|
+
moves.ArrowUp = { row: -1, col: 0 };
|
|
490
|
+
moves.ArrowDown = { row: 1, col: 0 };
|
|
491
|
+
moves.ArrowLeft = { row: 0, col: -1 };
|
|
492
|
+
moves.ArrowRight = { row: 0, col: 1 };
|
|
493
|
+
}
|
|
494
|
+
const move = moves[event.key];
|
|
495
|
+
if (move) {
|
|
496
|
+
event.preventDefault();
|
|
497
|
+
endEdit(true, move);
|
|
498
|
+
}
|
|
499
|
+
else if (event.key === 'Escape') {
|
|
500
|
+
event.preventDefault();
|
|
501
|
+
endEdit(false);
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
/* ---------- Drawing ---------- */
|
|
505
|
+
const align = (col, value) => (columns?.[col]?.align ?? (looksNumeric(value) ? 'right' : 'left')) === 'right';
|
|
506
|
+
// The columns as drawn: their own widths, and when those end short of the frame, the last column
|
|
507
|
+
// takes the room that is left, as a table's name column does, so the grid always fills its frame
|
|
508
|
+
// and never leaves an empty band beside the last rule. Wider than the frame, it scrolls.
|
|
509
|
+
const shown = widths.slice(0, cols);
|
|
510
|
+
const own = ROW_HEAD + shown.reduce((sum, w) => sum + w, 0);
|
|
511
|
+
if (cols > 0 && room > own)
|
|
512
|
+
shown[cols - 1] = (shown[cols - 1] ?? COLUMN_WIDTH) + room - own;
|
|
513
|
+
const total = ROW_HEAD + shown.reduce((sum, w) => sum + w, 0);
|
|
514
|
+
const headCell = 'relative border-neutral-200 border-r border-b bg-neutral-100 p-0 font-normal text-neutral-600 text-xs tabular-nums select-none transition-colors duration-150';
|
|
515
|
+
// A head over the selection darkens, and its edge facing the cells takes the accent, so the
|
|
516
|
+
// selected block is read from the heads as well as in the grid.
|
|
517
|
+
const headOn = 'bg-neutral-200 text-neutral-900';
|
|
518
|
+
const everything = many &&
|
|
519
|
+
range.top === 0 &&
|
|
520
|
+
range.left === 0 &&
|
|
521
|
+
range.bottom === rows - 1 &&
|
|
522
|
+
range.right === cols - 1;
|
|
523
|
+
return (_jsxs("div", { className: cn('group/grid flex min-h-0 flex-col overflow-hidden rounded-control border border-neutral-200 bg-neutral-50 shadow-card', className), "data-slot": "data-grid", ...props, children: [toolbar, _jsx("div", { className: "min-h-0 flex-1 overflow-auto", "data-slot": "data-grid-scroller", ref: scroller, children: _jsxs("div", { className: "relative w-max", ref: grid, children: [_jsxs("table", { "aria-activedescendant": rows && cols ? `${id}-${active.row}-${active.col}` : undefined, "aria-label": label, "aria-multiselectable": true, className: "table-fixed border-separate border-spacing-0 text-xs outline-none", "data-slot": "data-grid-table", onCopy: event => copy(event, false), onCut: event => copy(event, true), onKeyDown: key, onPaste: paste, ref: table,
|
|
524
|
+
// biome-ignore lint/a11y/noNoninteractiveElementToInteractiveRole: the ARIA grid pattern on a real table, whose cells are worked on, not only read.
|
|
525
|
+
role: "grid", style: { width: total }, tabIndex: 0, children: [_jsxs("colgroup", { children: [_jsx("col", { style: { width: ROW_HEAD } }), shown.map((width, col) => (
|
|
526
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: columns are places, not items.
|
|
527
|
+
_jsx("col", { style: { width } }, col)))] }), _jsx("thead", { children: _jsxs("tr", { className: "h-cell", children: [_jsx("th", { "aria-label": "Select all", className: cn(headCell, 'sticky top-0 left-0 z-30 cursor-pointer', everything && headOn), "data-slot": "data-grid-corner", onPointerDown: event => {
|
|
528
|
+
event.preventDefault();
|
|
529
|
+
table.current?.focus({ preventScroll: true });
|
|
530
|
+
setSelection({
|
|
531
|
+
anchor: { row: 0, col: 0 },
|
|
532
|
+
focus: { row: rows - 1, col: cols - 1 },
|
|
533
|
+
});
|
|
534
|
+
}, scope: "col" }), widths.slice(0, cols).map((_, col) => {
|
|
535
|
+
const name = columns?.[col]?.label;
|
|
536
|
+
const on = [...extra, range].some(block => col >= block.left && col <= block.right);
|
|
537
|
+
return (_jsxs("th", { className: cn(headCell, 'sticky top-0 z-20 cursor-default px-3', name
|
|
538
|
+
? columns?.[col]?.align === 'right'
|
|
539
|
+
? 'text-right'
|
|
540
|
+
: 'text-left'
|
|
541
|
+
: 'text-center', on && [
|
|
542
|
+
headOn,
|
|
543
|
+
'shadow-[inset_0_-2px_0_var(--color-neutral-400)] group-focus-within/grid:shadow-[inset_0_-2px_0_var(--color-primary-500)]',
|
|
544
|
+
]), "data-col": col, "data-slot": "data-grid-column", onPointerDown: event => pressHead(event, 'col', col), scope: "col", children: [_jsx("span", { className: "block truncate", children: name ?? letter(col) }), _jsx("span", { "aria-hidden": true,
|
|
545
|
+
// The last column's edge stays inside the grid, so it never makes the grid scroll.
|
|
546
|
+
className: cn('absolute inset-y-0 z-10 w-2 cursor-col-resize touch-none after:absolute after:inset-y-0 after:left-[3px] after:w-0.5 after:bg-primary-500 after:opacity-0 after:transition-opacity after:duration-150 after:content-[""] hover:after:opacity-100', col === cols - 1 ? 'right-0' : '-right-1'), "data-slot": "data-grid-resize", onDoubleClick: () => setWidths(current => current.map((w, i) => i === col
|
|
547
|
+
? (columns?.[col]?.width ??
|
|
548
|
+
COLUMN_WIDTH)
|
|
549
|
+
: w)), onPointerDown: event => resize(event, 'col', col) })] }, col));
|
|
550
|
+
})] }) }), _jsx("tbody", { children: values.slice(0, rows).map((line, row) => {
|
|
551
|
+
// The summary's rows, and its first, which takes the rule over it.
|
|
552
|
+
const summary = row >= rows - summaryRows;
|
|
553
|
+
const summaryStart = summaryRows > 0 && row === rows - summaryRows;
|
|
554
|
+
const rowOn = [...extra, range].some(block => row >= block.top && row <= block.bottom);
|
|
555
|
+
return (_jsxs("tr", { className: "h-cell", style: heights[row] !== undefined
|
|
556
|
+
? { height: heights[row] }
|
|
557
|
+
: undefined, children: [_jsxs("th", { className: cn(headCell, 'sticky left-0 z-10 cursor-default text-center', summaryStart &&
|
|
558
|
+
'shadow-[inset_0_1px_0_var(--color-neutral-400)]', rowOn && [
|
|
559
|
+
headOn,
|
|
560
|
+
'shadow-[inset_-2px_0_0_var(--color-neutral-400)] group-focus-within/grid:shadow-[inset_-2px_0_0_var(--color-primary-500)]',
|
|
561
|
+
]), "data-row-head": row, "data-slot": "data-grid-row", onPointerDown: event => pressHead(event, 'row', row), scope: "row", children: [row + 1, _jsx("span", { "aria-hidden": true, className: cn('absolute inset-x-0 z-10 h-2 cursor-row-resize touch-none after:absolute after:inset-x-0 after:top-[3px] after:h-0.5 after:bg-primary-500 after:opacity-0 after:transition-opacity after:duration-150 after:content-[""] hover:after:opacity-100', row === rows - 1 ? 'bottom-0' : '-bottom-1'), "data-slot": "data-grid-resize", onDoubleClick: () => setHeights(current => {
|
|
562
|
+
const next = [...current];
|
|
563
|
+
next[row] = undefined;
|
|
564
|
+
return next;
|
|
565
|
+
}), onPointerDown: event => resize(event, 'row', row) })] }), widths.slice(0, cols).map((_, col) => {
|
|
566
|
+
const value = line[col] ?? '';
|
|
567
|
+
const right = align(col, value);
|
|
568
|
+
const isActive = row === active.row && col === active.col;
|
|
569
|
+
return (
|
|
570
|
+
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: a cell of a table whose role is grid is a gridcell, which takes aria-selected.
|
|
571
|
+
_jsx("td", { "aria-selected": selected(row, col), className: cn('cursor-cell select-none overflow-hidden text-ellipsis whitespace-nowrap border-neutral-200 border-r border-b px-3 text-neutral-900',
|
|
572
|
+
// The first column names the row, as a table's first column does: in 500.
|
|
573
|
+
col === 0 && 'font-medium', summary && 'font-medium text-neutral-950',
|
|
574
|
+
// A shortfall in a total is the sheet's problem: in the error ink.
|
|
575
|
+
summary &&
|
|
576
|
+
/^\s*[-−]/.test(value) &&
|
|
577
|
+
'text-error-600', summaryStart &&
|
|
578
|
+
'shadow-[inset_0_1px_0_var(--color-neutral-400)]', 'scroll-mt-cell scroll-ml-11', right && 'text-right tabular-nums',
|
|
579
|
+
// The selected block is washed, the cell
|
|
580
|
+
// it started from left clear in its ring.
|
|
581
|
+
multi &&
|
|
582
|
+
!isActive &&
|
|
583
|
+
selected(row, col) &&
|
|
584
|
+
'bg-neutral-950/5 group-focus-within/grid:bg-primary-500/10'), "data-col": col, "data-row": row, "data-slot": "data-grid-cell", id: `${id}-${row}-${col}`, onDoubleClick: () => startEdit('edit'), onPointerDown: event => pressCell(event, { row, col }), children: value }, col));
|
|
585
|
+
})] }, row));
|
|
586
|
+
}) })] }), _jsx("div", { "aria-hidden": true, className: "pointer-events-none absolute z-5 border border-neutral-500 group-focus-within/grid:border-primary-500", "data-slot": "data-grid-range", ref: rangeBox }), _jsx("div", { "aria-hidden": true, className: "pointer-events-none absolute z-6 border-2 border-neutral-500 group-focus-within/grid:border-primary-500", "data-slot": "data-grid-active", ref: activeBox }), _jsx("div", { "aria-hidden": true, className: "pointer-events-none absolute z-6 border border-primary-500 border-dashed bg-primary-500/5", "data-slot": "data-grid-fill", ref: fillBox }), extra.map((_, i) => (_jsx("div", { "aria-hidden": true, className: "pointer-events-none absolute z-5 border border-neutral-500 group-focus-within/grid:border-primary-500", "data-slot": "data-grid-extra" }, i))), editable && !edit && extra.length === 0 && rows > 0 && cols > 0 && (_jsx(FillHandle, { box: boxOf, onPointerDown: pressFill, range: range })), edit && (_jsx("div", { className: "absolute z-7 flex bg-neutral-50 outline-2 outline-primary-500 outline-solid -outline-offset-1", "data-slot": "data-grid-editor", ref: editor, children: _jsx("input", { "aria-label": `${columns?.[edit.col]?.label ?? letter(edit.col)} ${edit.row + 1}`, className: cn('h-full w-full min-w-0 bg-tint pr-3 pl-[calc(var(--spacing)*3+1px)] text-neutral-900 text-xs outline-none', align(edit.col, edit.draft) && 'text-right tabular-nums', edit.col === 0 && 'font-medium'), onBlur: () => endEdit(true), onChange: event => setEdit({ ...edit, draft: event.target.value }), onKeyDown: editKey, ref: openField, value: edit.draft }) }))] }) }), footer != null && (_jsx("div", { className: "flex min-h-cell shrink-0 items-center gap-4 border-neutral-200 border-t bg-neutral-100 px-3 text-neutral-600 text-xs tabular-nums", "data-slot": "data-grid-footer", children: footer }))] }));
|
|
587
|
+
};
|
|
588
|
+
/** The square at the selection's corner that drags a fill out of it. */
|
|
589
|
+
const FillHandle = ({ range, box, onPointerDown, }) => {
|
|
590
|
+
const handle = useRef(null);
|
|
591
|
+
useLayoutEffect(() => {
|
|
592
|
+
const at = box(range);
|
|
593
|
+
if (!handle.current || !at)
|
|
594
|
+
return;
|
|
595
|
+
handle.current.style.left = `${at.left + at.width - 4}px`;
|
|
596
|
+
handle.current.style.top = `${at.top + at.height - 4}px`;
|
|
597
|
+
});
|
|
598
|
+
return (_jsx("div", { "aria-hidden": true, className: "absolute z-8 size-2 cursor-crosshair touch-none border border-neutral-50 bg-neutral-500 group-focus-within/grid:bg-primary-500", "data-slot": "data-grid-fill-handle", onPointerDown: onPointerDown, ref: handle, title: "Drag to fill" }));
|
|
599
|
+
};
|