@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,965 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ClipboardEvent,
|
|
5
|
+
type ComponentProps,
|
|
6
|
+
type KeyboardEvent,
|
|
7
|
+
type PointerEvent,
|
|
8
|
+
type ReactNode,
|
|
9
|
+
useId,
|
|
10
|
+
useLayoutEffect,
|
|
11
|
+
useRef,
|
|
12
|
+
useState,
|
|
13
|
+
} from 'react';
|
|
14
|
+
import { cn } from '../lib/cn';
|
|
15
|
+
import { holdPointer } from '../lib/reorder';
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* A grid of values worked on cell by cell, the way a spreadsheet is: where a Table is a list of
|
|
19
|
+
* objects read a row at a time, here every cell is its own value, and the reader selects a range of
|
|
20
|
+
* them, types over them, pastes into them and drags a fill down them.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type DataGridColumn = {
|
|
24
|
+
/** The column's name; a letter (A, B, …) when left out. */
|
|
25
|
+
label?: string;
|
|
26
|
+
/** Its width in px, 112 by default; the reader resizes it from the head's right edge. */
|
|
27
|
+
width?: number;
|
|
28
|
+
/** Left by default; a cell that holds a number goes right on its own. */
|
|
29
|
+
align?: 'left' | 'right';
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** A block of cells, by row and column index, both ends included. */
|
|
33
|
+
export type DataGridRange = { top: number; left: number; bottom: number; right: number };
|
|
34
|
+
|
|
35
|
+
type Cell = { row: number; col: number };
|
|
36
|
+
type Selection = { anchor: Cell; focus: Cell };
|
|
37
|
+
type Edit = Cell & { draft: string; mode: 'replace' | 'edit' };
|
|
38
|
+
|
|
39
|
+
export type DataGridProps = Omit<ComponentProps<'div'>, 'children' | 'onChange'> & {
|
|
40
|
+
/** The grid's name, for a screen reader. */
|
|
41
|
+
label: string;
|
|
42
|
+
/** The values, a row at a time. */
|
|
43
|
+
values: string[][];
|
|
44
|
+
/** Makes the grid editable: called with the whole grid after each edit, paste, fill or undo. */
|
|
45
|
+
onValuesChange?: (values: string[][]) => void;
|
|
46
|
+
/** The columns; left out, as many lettered columns as the widest row has values. */
|
|
47
|
+
columns?: DataGridColumn[];
|
|
48
|
+
/** Called as the selection changes, with its range and the cell it started from. */
|
|
49
|
+
/**
|
|
50
|
+
* Called as the selection changes, with the block being made, the cell it started from, and
|
|
51
|
+
* every selected block (those added with Ctrl or ⌘ first, this one last).
|
|
52
|
+
*/
|
|
53
|
+
onSelectionChange?: (range: DataGridRange, active: Cell, ranges: DataGridRange[]) => void;
|
|
54
|
+
/** Inside the frame under the grid, ruled off: a sum of the selection, a count. */
|
|
55
|
+
footer?: ReactNode;
|
|
56
|
+
/**
|
|
57
|
+
* Inside the frame over the column heads, ruled off: what works on this grid (a scenario, a
|
|
58
|
+
* view, where it stands), as a `TableToolbar` on a table. It stays put while the grid scrolls.
|
|
59
|
+
*/
|
|
60
|
+
toolbar?: ReactNode;
|
|
61
|
+
/**
|
|
62
|
+
* The last rows are the sheet's summary (a total, a target, a gap): set apart from the rows they
|
|
63
|
+
* sum by a firmer rule over the first, and written in 500, as a spreadsheet writes its totals.
|
|
64
|
+
*/
|
|
65
|
+
summaryRows?: number;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const ROW_HEAD = 44;
|
|
69
|
+
const COLUMN_WIDTH = 112;
|
|
70
|
+
const MIN_WIDTH = 40;
|
|
71
|
+
|
|
72
|
+
const letter = (index: number): string =>
|
|
73
|
+
index < 26
|
|
74
|
+
? String.fromCharCode(65 + index)
|
|
75
|
+
: letter(Math.floor(index / 26) - 1) + letter(index % 26);
|
|
76
|
+
|
|
77
|
+
const rangeOf = ({ anchor, focus }: Selection): DataGridRange => ({
|
|
78
|
+
top: Math.min(anchor.row, focus.row),
|
|
79
|
+
bottom: Math.max(anchor.row, focus.row),
|
|
80
|
+
left: Math.min(anchor.col, focus.col),
|
|
81
|
+
right: Math.max(anchor.col, focus.col),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const within = (range: DataGridRange, row: number, col: number) =>
|
|
85
|
+
row >= range.top && row <= range.bottom && col >= range.left && col <= range.right;
|
|
86
|
+
|
|
87
|
+
/** A plain number ("12", "-3.5", "1,250", "€ 40", "12%"): set right and in tabular figures. */
|
|
88
|
+
const looksNumeric = (value: string) => /^[-+]?[€$£]?\s?\d[\d.,]*\s?%?$/.test(value.trim());
|
|
89
|
+
|
|
90
|
+
/** A value a series can run on: a bare number. */
|
|
91
|
+
const asNumber = (value: string) => {
|
|
92
|
+
const trimmed = value.trim();
|
|
93
|
+
return /^[-+]?\d+(\.\d+)?$/.test(trimmed) ? Number(trimmed) : null;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const decimals = (value: string) => value.trim().split('.')[1]?.length ?? 0;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The values a fill writes at `offset` steps from the start of its source (negative before it): a
|
|
100
|
+
* run of two or more numbers a constant step apart carries on as a series, anything else repeats.
|
|
101
|
+
*/
|
|
102
|
+
const fillValue = (source: string[], offset: number) => {
|
|
103
|
+
const numbers = source.map(asNumber);
|
|
104
|
+
if (source.length >= 2 && numbers.every(n => n !== null)) {
|
|
105
|
+
const list = numbers as number[];
|
|
106
|
+
const step = (list[1] ?? 0) - (list[0] ?? 0);
|
|
107
|
+
const even = list.every(
|
|
108
|
+
(n, i) => i === 0 || Math.abs(n - (list[i - 1] ?? 0) - step) < 1e-9,
|
|
109
|
+
);
|
|
110
|
+
if (even) {
|
|
111
|
+
const places = Math.max(...source.map(decimals));
|
|
112
|
+
return ((list[0] ?? 0) + offset * step).toFixed(places);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const n = source.length;
|
|
116
|
+
return source[((offset % n) + n) % n] ?? '';
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** Text from a clipboard as rows of cells: tab between cells, a line a row. */
|
|
120
|
+
const parseClipboard = (text: string) => {
|
|
121
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
122
|
+
if (lines.length > 1 && lines.at(-1) === '') lines.pop();
|
|
123
|
+
return lines.map(line => line.split('\t'));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
|
|
127
|
+
|
|
128
|
+
export const DataGrid = ({
|
|
129
|
+
label,
|
|
130
|
+
values,
|
|
131
|
+
onValuesChange,
|
|
132
|
+
columns,
|
|
133
|
+
onSelectionChange,
|
|
134
|
+
footer,
|
|
135
|
+
toolbar,
|
|
136
|
+
summaryRows = 0,
|
|
137
|
+
className,
|
|
138
|
+
...props
|
|
139
|
+
}: DataGridProps) => {
|
|
140
|
+
const id = useId();
|
|
141
|
+
const rows = values.length;
|
|
142
|
+
const cols = columns?.length ?? Math.max(0, ...values.map(row => row.length));
|
|
143
|
+
const editable = onValuesChange !== undefined;
|
|
144
|
+
|
|
145
|
+
const [widths, setWidths] = useState(() =>
|
|
146
|
+
Array.from({ length: cols }, (_, i) => columns?.[i]?.width ?? COLUMN_WIDTH),
|
|
147
|
+
);
|
|
148
|
+
// A row without a height of its own is the cell height, so it follows the theme's size.
|
|
149
|
+
const [heights, setHeights] = useState<(number | undefined)[]>([]);
|
|
150
|
+
const [selection, setSelectionState] = useState<Selection>({
|
|
151
|
+
anchor: { row: 0, col: 0 },
|
|
152
|
+
focus: { row: 0, col: 0 },
|
|
153
|
+
});
|
|
154
|
+
// Blocks added with Ctrl or ⌘ before the one being made now.
|
|
155
|
+
const [extra, setExtra] = useState<DataGridRange[]>([]);
|
|
156
|
+
const [edit, setEdit] = useState<Edit | null>(null);
|
|
157
|
+
// The cells a fill handle drag will write, while it is dragged.
|
|
158
|
+
const [fill, setFill] = useState<DataGridRange | null>(null);
|
|
159
|
+
const history = useRef<{ past: string[][][]; future: string[][][] }>({ past: [], future: [] });
|
|
160
|
+
|
|
161
|
+
const table = useRef<HTMLTableElement>(null);
|
|
162
|
+
const grid = useRef<HTMLDivElement>(null);
|
|
163
|
+
const ended = useRef(false);
|
|
164
|
+
const rangeBox = useRef<HTMLDivElement>(null);
|
|
165
|
+
const activeBox = useRef<HTMLDivElement>(null);
|
|
166
|
+
const fillBox = useRef<HTMLDivElement>(null);
|
|
167
|
+
const editor = useRef<HTMLDivElement>(null);
|
|
168
|
+
const input = useRef<HTMLInputElement>(null);
|
|
169
|
+
const scroller = useRef<HTMLDivElement>(null);
|
|
170
|
+
|
|
171
|
+
// The width the grid has to fill: the columns never end short of the frame (see `shown`).
|
|
172
|
+
const [room, setRoom] = useState(0);
|
|
173
|
+
useLayoutEffect(() => {
|
|
174
|
+
const node = scroller.current;
|
|
175
|
+
if (!node) return;
|
|
176
|
+
const measure = () => setRoom(node.clientWidth);
|
|
177
|
+
measure();
|
|
178
|
+
const observer = new ResizeObserver(measure);
|
|
179
|
+
observer.observe(node);
|
|
180
|
+
return () => observer.disconnect();
|
|
181
|
+
}, []);
|
|
182
|
+
|
|
183
|
+
// Columns added later take the default width.
|
|
184
|
+
if (widths.length < cols)
|
|
185
|
+
setWidths(current => [
|
|
186
|
+
...current,
|
|
187
|
+
...Array.from(
|
|
188
|
+
{ length: cols - current.length },
|
|
189
|
+
(_, i) => columns?.[current.length + i]?.width ?? COLUMN_WIDTH,
|
|
190
|
+
),
|
|
191
|
+
]);
|
|
192
|
+
|
|
193
|
+
const range = rangeOf(selection);
|
|
194
|
+
const active = selection.anchor;
|
|
195
|
+
const many = range.top !== range.bottom || range.left !== range.right;
|
|
196
|
+
const valueAt = (row: number, col: number) => values[row]?.[col] ?? '';
|
|
197
|
+
|
|
198
|
+
// Picking a selection; the blocks added before it with Ctrl or ⌘ stay unless told otherwise.
|
|
199
|
+
const setSelection = (next: Selection, others: DataGridRange[] = extra) => {
|
|
200
|
+
setSelectionState(next);
|
|
201
|
+
setExtra(others);
|
|
202
|
+
onSelectionChange?.(rangeOf(next), next.anchor, [...others, rangeOf(next)]);
|
|
203
|
+
};
|
|
204
|
+
const selected = (row: number, col: number) =>
|
|
205
|
+
within(range, row, col) || extra.some(block => within(block, row, col));
|
|
206
|
+
// More than the one cell: a block, or blocks added to it.
|
|
207
|
+
const multi = many || extra.length > 0;
|
|
208
|
+
|
|
209
|
+
const cellNode = (row: number, col: number) =>
|
|
210
|
+
grid.current?.querySelector<HTMLElement>(`[data-row="${row}"][data-col="${col}"]`) ?? null;
|
|
211
|
+
|
|
212
|
+
/** A range's box in the grid, its edges on the grid lines around it. */
|
|
213
|
+
const boxOf = (block: DataGridRange) => {
|
|
214
|
+
const first = cellNode(block.top, block.left);
|
|
215
|
+
const last = cellNode(block.bottom, block.right);
|
|
216
|
+
const frame = grid.current?.getBoundingClientRect();
|
|
217
|
+
if (!first || !last || !frame) return null;
|
|
218
|
+
const a = first.getBoundingClientRect();
|
|
219
|
+
const b = last.getBoundingClientRect();
|
|
220
|
+
// A cell's own rule is on its right and bottom: the box starts on the rule before it.
|
|
221
|
+
return {
|
|
222
|
+
left: a.left - frame.left - 1,
|
|
223
|
+
top: a.top - frame.top - 1,
|
|
224
|
+
width: b.right - a.left + 1,
|
|
225
|
+
height: b.bottom - a.top + 1,
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// The selection's marks are drawn over the cells, from where the cells really are.
|
|
230
|
+
useLayoutEffect(() => {
|
|
231
|
+
const place = (node: HTMLElement | null, block: DataGridRange | null) => {
|
|
232
|
+
if (!node) return;
|
|
233
|
+
const box = block && boxOf(block);
|
|
234
|
+
node.style.display = box ? '' : 'none';
|
|
235
|
+
if (!box) return;
|
|
236
|
+
node.style.left = `${box.left}px`;
|
|
237
|
+
node.style.top = `${box.top}px`;
|
|
238
|
+
node.style.width = `${box.width}px`;
|
|
239
|
+
node.style.height = `${box.height}px`;
|
|
240
|
+
};
|
|
241
|
+
const one = { top: active.row, bottom: active.row, left: active.col, right: active.col };
|
|
242
|
+
place(rangeBox.current, many ? range : null);
|
|
243
|
+
grid.current
|
|
244
|
+
?.querySelectorAll<HTMLElement>('[data-slot=data-grid-extra]')
|
|
245
|
+
.forEach((node, i) => {
|
|
246
|
+
place(node, extra[i] ?? null);
|
|
247
|
+
});
|
|
248
|
+
place(activeBox.current, rows && cols ? one : null);
|
|
249
|
+
place(fillBox.current, fill);
|
|
250
|
+
place(editor.current, edit ? one : null);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
/* ---------- Changing values ---------- */
|
|
254
|
+
|
|
255
|
+
const commit = (next: string[][]) => {
|
|
256
|
+
if (!onValuesChange) return;
|
|
257
|
+
history.current.past.push(values);
|
|
258
|
+
history.current.future = [];
|
|
259
|
+
onValuesChange(next);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const write = (changes: { row: number; col: number; value: string }[]) => {
|
|
263
|
+
const next = values.map(row => [...row]);
|
|
264
|
+
let changed = false;
|
|
265
|
+
for (const { row, col, value } of changes) {
|
|
266
|
+
if (row >= rows || col >= cols) continue;
|
|
267
|
+
const line = next[row] ?? [];
|
|
268
|
+
if ((line[col] ?? '') === value) continue;
|
|
269
|
+
while (line.length < col) line.push('');
|
|
270
|
+
line[col] = value;
|
|
271
|
+
next[row] = line;
|
|
272
|
+
changed = true;
|
|
273
|
+
}
|
|
274
|
+
if (changed) commit(next);
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const cellsOf = (block: DataGridRange) => {
|
|
278
|
+
const list: Cell[] = [];
|
|
279
|
+
for (let row = block.top; row <= block.bottom; row++)
|
|
280
|
+
for (let col = block.left; col <= block.right; col++) list.push({ row, col });
|
|
281
|
+
return list;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const undo = (redo: boolean) => {
|
|
285
|
+
if (!onValuesChange) return;
|
|
286
|
+
const { past, future } = history.current;
|
|
287
|
+
const from = redo ? future : past;
|
|
288
|
+
const to = redo ? past : future;
|
|
289
|
+
const snapshot = from.pop();
|
|
290
|
+
if (!snapshot) return;
|
|
291
|
+
to.push(values);
|
|
292
|
+
onValuesChange(snapshot);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/* ---------- Moving the selection ---------- */
|
|
296
|
+
|
|
297
|
+
/** Moves the selection from the keyboard; the added blocks go unless `keep` (Shift extending). */
|
|
298
|
+
const select = (anchor: Cell, focus: Cell = anchor, keep = false) => {
|
|
299
|
+
const fit = (cell: Cell) => ({
|
|
300
|
+
row: clamp(cell.row, 0, rows - 1),
|
|
301
|
+
col: clamp(cell.col, 0, cols - 1),
|
|
302
|
+
});
|
|
303
|
+
const next = { anchor: fit(anchor), focus: fit(focus) };
|
|
304
|
+
setSelection(next, keep ? extra : []);
|
|
305
|
+
requestAnimationFrame(() =>
|
|
306
|
+
cellNode(next.focus.row, next.focus.col)?.scrollIntoView({
|
|
307
|
+
block: 'nearest',
|
|
308
|
+
inline: 'nearest',
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const startEdit = (mode: Edit['mode'], draft?: string) => {
|
|
314
|
+
if (!editable) return;
|
|
315
|
+
ended.current = false;
|
|
316
|
+
setEdit({ ...active, draft: draft ?? valueAt(active.row, active.col), mode });
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const endEdit = (save: boolean, move?: { row: number; col: number }) => {
|
|
320
|
+
// Ending takes focus off the field, which blurs it: the edit ends once.
|
|
321
|
+
if (!edit || ended.current) return;
|
|
322
|
+
ended.current = true;
|
|
323
|
+
if (save) write([{ row: edit.row, col: edit.col, value: edit.draft }]);
|
|
324
|
+
setEdit(null);
|
|
325
|
+
if (move) select({ row: edit.row + move.row, col: edit.col + move.col });
|
|
326
|
+
table.current?.focus({ preventScroll: true });
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// The field takes focus as it opens, the caret after what is in it.
|
|
330
|
+
const openField = (node: HTMLInputElement | null) => {
|
|
331
|
+
if (node && node !== input.current) {
|
|
332
|
+
node.focus({ preventScroll: true });
|
|
333
|
+
node.setSelectionRange(node.value.length, node.value.length);
|
|
334
|
+
}
|
|
335
|
+
input.current = node;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/* ---------- Pointer ---------- */
|
|
339
|
+
|
|
340
|
+
// Which cell is under the pointer, while a drag runs across the grid.
|
|
341
|
+
const cellAt = (x: number, y: number) => {
|
|
342
|
+
const node = document.elementFromPoint(x, y)?.closest<HTMLElement>('[data-row]');
|
|
343
|
+
if (!node || !grid.current?.contains(node)) return null;
|
|
344
|
+
return { row: Number(node.dataset.row), col: Number(node.dataset.col) };
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const track = (onMove: (event: globalThis.PointerEvent) => void, onUp?: () => void) => {
|
|
348
|
+
const up = () => {
|
|
349
|
+
window.removeEventListener('pointermove', onMove);
|
|
350
|
+
window.removeEventListener('pointerup', up);
|
|
351
|
+
onUp?.();
|
|
352
|
+
};
|
|
353
|
+
window.addEventListener('pointermove', onMove);
|
|
354
|
+
window.addEventListener('pointerup', up);
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const pressCell = (event: PointerEvent<HTMLElement>, cell: Cell) => {
|
|
358
|
+
if (event.button !== 0) return;
|
|
359
|
+
event.preventDefault();
|
|
360
|
+
if (edit) endEdit(true);
|
|
361
|
+
table.current?.focus({ preventScroll: true });
|
|
362
|
+
// Ctrl or ⌘ keeps what is selected and starts another block beside it; Shift stretches the
|
|
363
|
+
// block being made; a plain press starts over.
|
|
364
|
+
const adding = event.ctrlKey || event.metaKey;
|
|
365
|
+
const others = adding ? [...extra, range] : event.shiftKey ? extra : [];
|
|
366
|
+
const anchor = event.shiftKey && !adding ? selection.anchor : cell;
|
|
367
|
+
setSelection({ anchor, focus: cell }, others);
|
|
368
|
+
track(move => {
|
|
369
|
+
const over = cellAt(move.clientX, move.clientY);
|
|
370
|
+
if (over) setSelection({ anchor, focus: over }, others);
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// A head selects its whole column (or row); dragged along the heads, the columns it passes.
|
|
375
|
+
const pressHead = (event: PointerEvent<HTMLElement>, axis: 'col' | 'row', index: number) => {
|
|
376
|
+
if (event.button !== 0) return;
|
|
377
|
+
event.preventDefault();
|
|
378
|
+
if (edit) endEdit(true);
|
|
379
|
+
table.current?.focus({ preventScroll: true });
|
|
380
|
+
const whole = (from: number, to: number): Selection =>
|
|
381
|
+
axis === 'col'
|
|
382
|
+
? { anchor: { row: 0, col: from }, focus: { row: rows - 1, col: to } }
|
|
383
|
+
: { anchor: { row: from, col: 0 }, focus: { row: to, col: cols - 1 } };
|
|
384
|
+
const adding = event.ctrlKey || event.metaKey;
|
|
385
|
+
const others = adding ? [...extra, range] : event.shiftKey ? extra : [];
|
|
386
|
+
const from =
|
|
387
|
+
event.shiftKey && !adding
|
|
388
|
+
? axis === 'col'
|
|
389
|
+
? selection.anchor.col
|
|
390
|
+
: selection.anchor.row
|
|
391
|
+
: index;
|
|
392
|
+
setSelection(whole(from, index), others);
|
|
393
|
+
track(move => {
|
|
394
|
+
const node = document
|
|
395
|
+
.elementFromPoint(move.clientX, move.clientY)
|
|
396
|
+
?.closest<HTMLElement>(axis === 'col' ? '[data-col]' : '[data-row]');
|
|
397
|
+
if (!node || !grid.current?.contains(node)) return;
|
|
398
|
+
const at = Number(axis === 'col' ? node.dataset.col : node.dataset.row);
|
|
399
|
+
if (!Number.isNaN(at)) setSelection(whole(from, at), others);
|
|
400
|
+
});
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const pressFill = (event: PointerEvent<HTMLDivElement>) => {
|
|
404
|
+
if (event.button !== 0) return;
|
|
405
|
+
event.preventDefault();
|
|
406
|
+
event.stopPropagation();
|
|
407
|
+
const source = range;
|
|
408
|
+
let target: DataGridRange | null = null;
|
|
409
|
+
track(
|
|
410
|
+
move => {
|
|
411
|
+
const over = cellAt(move.clientX, move.clientY);
|
|
412
|
+
if (!over) return;
|
|
413
|
+
// The fill runs along the axis the pointer has gone further out on.
|
|
414
|
+
const down = over.row > source.bottom ? over.row - source.bottom : 0;
|
|
415
|
+
const up = over.row < source.top ? source.top - over.row : 0;
|
|
416
|
+
const right = over.col > source.right ? over.col - source.right : 0;
|
|
417
|
+
const left = over.col < source.left ? source.left - over.col : 0;
|
|
418
|
+
const vertical = Math.max(down, up) >= Math.max(right, left);
|
|
419
|
+
if (vertical && (down || up))
|
|
420
|
+
target = down
|
|
421
|
+
? { ...source, top: source.bottom + 1, bottom: over.row }
|
|
422
|
+
: { ...source, top: over.row, bottom: source.top - 1 };
|
|
423
|
+
else if (!vertical && (right || left))
|
|
424
|
+
target = right
|
|
425
|
+
? { ...source, left: source.right + 1, right: over.col }
|
|
426
|
+
: { ...source, left: over.col, right: source.left - 1 };
|
|
427
|
+
else target = null;
|
|
428
|
+
setFill(target);
|
|
429
|
+
},
|
|
430
|
+
() => {
|
|
431
|
+
setFill(null);
|
|
432
|
+
if (!target) return;
|
|
433
|
+
const block: DataGridRange = target;
|
|
434
|
+
const vertical = block.left === source.left && block.right === source.right;
|
|
435
|
+
const changes = cellsOf(block).map(({ row, col }) => {
|
|
436
|
+
const line = vertical
|
|
437
|
+
? Array.from({ length: source.bottom - source.top + 1 }, (_, i) =>
|
|
438
|
+
valueAt(source.top + i, col),
|
|
439
|
+
)
|
|
440
|
+
: Array.from({ length: source.right - source.left + 1 }, (_, i) =>
|
|
441
|
+
valueAt(row, source.left + i),
|
|
442
|
+
);
|
|
443
|
+
const offset = vertical ? row - source.top : col - source.left;
|
|
444
|
+
return { row, col, value: fillValue(line, offset) };
|
|
445
|
+
});
|
|
446
|
+
write(changes);
|
|
447
|
+
select(
|
|
448
|
+
{
|
|
449
|
+
row: Math.min(source.top, block.top),
|
|
450
|
+
col: Math.min(source.left, block.left),
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
row: Math.max(source.bottom, block.bottom),
|
|
454
|
+
col: Math.max(source.right, block.right),
|
|
455
|
+
},
|
|
456
|
+
);
|
|
457
|
+
},
|
|
458
|
+
);
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const resize = (event: PointerEvent<HTMLElement>, axis: 'col' | 'row', index: number) => {
|
|
462
|
+
if (event.button !== 0) return;
|
|
463
|
+
event.preventDefault();
|
|
464
|
+
event.stopPropagation();
|
|
465
|
+
const start = axis === 'col' ? event.clientX : event.clientY;
|
|
466
|
+
const head = event.currentTarget.parentElement;
|
|
467
|
+
const from = axis === 'col' ? (widths[index] ?? COLUMN_WIDTH) : (head?.offsetHeight ?? 32);
|
|
468
|
+
// A row never goes under the cell height it started at: taller, for a value that wraps, not shorter.
|
|
469
|
+
const cell = table.current
|
|
470
|
+
? Number.parseFloat(
|
|
471
|
+
getComputedStyle(table.current).getPropertyValue('--spacing-cell'),
|
|
472
|
+
) || 32
|
|
473
|
+
: 32;
|
|
474
|
+
const release = holdPointer(axis === 'col' ? 'col-resize' : 'row-resize');
|
|
475
|
+
track(move => {
|
|
476
|
+
const size = Math.round(from + (axis === 'col' ? move.clientX : move.clientY) - start);
|
|
477
|
+
if (axis === 'col')
|
|
478
|
+
setWidths(current =>
|
|
479
|
+
current.map((w, i) => (i === index ? Math.max(MIN_WIDTH, size) : w)),
|
|
480
|
+
);
|
|
481
|
+
else
|
|
482
|
+
setHeights(current => {
|
|
483
|
+
const next = [...current];
|
|
484
|
+
next[index] = Math.max(cell, size);
|
|
485
|
+
return next;
|
|
486
|
+
});
|
|
487
|
+
}, release);
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
/* ---------- Keyboard and clipboard ---------- */
|
|
491
|
+
|
|
492
|
+
const key = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
493
|
+
if (edit || event.target !== table.current || !rows || !cols) return;
|
|
494
|
+
const mod = event.metaKey || event.ctrlKey;
|
|
495
|
+
const step: Record<string, Cell> = {
|
|
496
|
+
ArrowUp: { row: -1, col: 0 },
|
|
497
|
+
ArrowDown: { row: 1, col: 0 },
|
|
498
|
+
ArrowLeft: { row: 0, col: -1 },
|
|
499
|
+
ArrowRight: { row: 0, col: 1 },
|
|
500
|
+
};
|
|
501
|
+
const delta = step[event.key];
|
|
502
|
+
if (delta) {
|
|
503
|
+
event.preventDefault();
|
|
504
|
+
const from = event.shiftKey ? selection.focus : selection.anchor;
|
|
505
|
+
// With Ctrl or ⌘ the move runs to the grid's edge.
|
|
506
|
+
const to = mod
|
|
507
|
+
? {
|
|
508
|
+
row: delta.row ? (delta.row < 0 ? 0 : rows - 1) : from.row,
|
|
509
|
+
col: delta.col ? (delta.col < 0 ? 0 : cols - 1) : from.col,
|
|
510
|
+
}
|
|
511
|
+
: { row: from.row + delta.row, col: from.col + delta.col };
|
|
512
|
+
if (event.shiftKey) select(selection.anchor, to, true);
|
|
513
|
+
else select(to);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (event.key === 'Home' || event.key === 'End') {
|
|
517
|
+
event.preventDefault();
|
|
518
|
+
const col = event.key === 'Home' ? 0 : cols - 1;
|
|
519
|
+
const row = mod ? (event.key === 'Home' ? 0 : rows - 1) : active.row;
|
|
520
|
+
select({ row, col });
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (mod && event.key.toLowerCase() === 'a') {
|
|
524
|
+
event.preventDefault();
|
|
525
|
+
setSelection(
|
|
526
|
+
{ anchor: { row: 0, col: 0 }, focus: { row: rows - 1, col: cols - 1 } },
|
|
527
|
+
[],
|
|
528
|
+
);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (mod && event.key.toLowerCase() === 'z') {
|
|
532
|
+
event.preventDefault();
|
|
533
|
+
undo(event.shiftKey);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (mod && event.key.toLowerCase() === 'y') {
|
|
537
|
+
event.preventDefault();
|
|
538
|
+
undo(true);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
if (event.key === 'Escape' && multi) {
|
|
542
|
+
event.preventDefault();
|
|
543
|
+
select(active);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (!editable) return;
|
|
547
|
+
if (event.key === 'Enter' || event.key === 'F2') {
|
|
548
|
+
event.preventDefault();
|
|
549
|
+
startEdit('edit');
|
|
550
|
+
} else if (event.key === 'Delete' || event.key === 'Backspace') {
|
|
551
|
+
event.preventDefault();
|
|
552
|
+
// Every selected block is cleared, the added ones too.
|
|
553
|
+
write([...extra, range].flatMap(cellsOf).map(cell => ({ ...cell, value: '' })));
|
|
554
|
+
} else if (event.key.length === 1 && !mod && !event.altKey) {
|
|
555
|
+
// Typing over a cell replaces its value, starting with the key typed.
|
|
556
|
+
event.preventDefault();
|
|
557
|
+
startEdit('replace', event.key);
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const tsv = (block: DataGridRange) =>
|
|
562
|
+
Array.from({ length: block.bottom - block.top + 1 }, (_, r) =>
|
|
563
|
+
Array.from({ length: block.right - block.left + 1 }, (_, c) =>
|
|
564
|
+
valueAt(block.top + r, block.left + c),
|
|
565
|
+
).join('\t'),
|
|
566
|
+
).join('\n');
|
|
567
|
+
|
|
568
|
+
const copy = (event: ClipboardEvent<HTMLDivElement>, cut: boolean) => {
|
|
569
|
+
if (edit || event.target !== table.current) return;
|
|
570
|
+
event.preventDefault();
|
|
571
|
+
event.clipboardData.setData('text/plain', tsv(range));
|
|
572
|
+
if (cut && editable) write(cellsOf(range).map(cell => ({ ...cell, value: '' })));
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const paste = (event: ClipboardEvent<HTMLDivElement>) => {
|
|
576
|
+
if (edit || !editable || event.target !== table.current) return;
|
|
577
|
+
event.preventDefault();
|
|
578
|
+
const block = parseClipboard(event.clipboardData.getData('text/plain'));
|
|
579
|
+
const single = block.length === 1 && block[0]?.length === 1;
|
|
580
|
+
// One value fills the whole selection; a block lands with its corner on the selection's.
|
|
581
|
+
if (single) {
|
|
582
|
+
write(cellsOf(range).map(cell => ({ ...cell, value: block[0]?.[0] ?? '' })));
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
write(
|
|
586
|
+
block.flatMap((line, r) =>
|
|
587
|
+
line.map((value, c) => ({ row: range.top + r, col: range.left + c, value })),
|
|
588
|
+
),
|
|
589
|
+
);
|
|
590
|
+
select(
|
|
591
|
+
{ row: range.top, col: range.left },
|
|
592
|
+
{
|
|
593
|
+
row: range.top + block.length - 1,
|
|
594
|
+
col: range.left + Math.max(...block.map(line => line.length)) - 1,
|
|
595
|
+
},
|
|
596
|
+
);
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
const editKey = (event: KeyboardEvent<HTMLInputElement>) => {
|
|
600
|
+
if (!edit) return;
|
|
601
|
+
const moves: Record<string, Cell> = {
|
|
602
|
+
Enter: { row: event.shiftKey ? -1 : 1, col: 0 },
|
|
603
|
+
Tab: { row: 0, col: event.shiftKey ? -1 : 1 },
|
|
604
|
+
};
|
|
605
|
+
// Typed straight over a cell, the arrows leave it, as they would have without the edit;
|
|
606
|
+
// opened with Enter or F2 they move through the text.
|
|
607
|
+
if (edit.mode === 'replace') {
|
|
608
|
+
moves.ArrowUp = { row: -1, col: 0 };
|
|
609
|
+
moves.ArrowDown = { row: 1, col: 0 };
|
|
610
|
+
moves.ArrowLeft = { row: 0, col: -1 };
|
|
611
|
+
moves.ArrowRight = { row: 0, col: 1 };
|
|
612
|
+
}
|
|
613
|
+
const move = moves[event.key];
|
|
614
|
+
if (move) {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
endEdit(true, move);
|
|
617
|
+
} else if (event.key === 'Escape') {
|
|
618
|
+
event.preventDefault();
|
|
619
|
+
endEdit(false);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
/* ---------- Drawing ---------- */
|
|
624
|
+
|
|
625
|
+
const align = (col: number, value: string) =>
|
|
626
|
+
(columns?.[col]?.align ?? (looksNumeric(value) ? 'right' : 'left')) === 'right';
|
|
627
|
+
|
|
628
|
+
// The columns as drawn: their own widths, and when those end short of the frame, the last column
|
|
629
|
+
// takes the room that is left, as a table's name column does, so the grid always fills its frame
|
|
630
|
+
// and never leaves an empty band beside the last rule. Wider than the frame, it scrolls.
|
|
631
|
+
const shown = widths.slice(0, cols);
|
|
632
|
+
const own = ROW_HEAD + shown.reduce((sum, w) => sum + w, 0);
|
|
633
|
+
if (cols > 0 && room > own) shown[cols - 1] = (shown[cols - 1] ?? COLUMN_WIDTH) + room - own;
|
|
634
|
+
const total = ROW_HEAD + shown.reduce((sum, w) => sum + w, 0);
|
|
635
|
+
|
|
636
|
+
const headCell =
|
|
637
|
+
'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';
|
|
638
|
+
// A head over the selection darkens, and its edge facing the cells takes the accent, so the
|
|
639
|
+
// selected block is read from the heads as well as in the grid.
|
|
640
|
+
const headOn = 'bg-neutral-200 text-neutral-900';
|
|
641
|
+
const everything =
|
|
642
|
+
many &&
|
|
643
|
+
range.top === 0 &&
|
|
644
|
+
range.left === 0 &&
|
|
645
|
+
range.bottom === rows - 1 &&
|
|
646
|
+
range.right === cols - 1;
|
|
647
|
+
|
|
648
|
+
return (
|
|
649
|
+
<div
|
|
650
|
+
className={cn(
|
|
651
|
+
'group/grid flex min-h-0 flex-col overflow-hidden rounded-control border border-neutral-200 bg-neutral-50 shadow-card',
|
|
652
|
+
className,
|
|
653
|
+
)}
|
|
654
|
+
data-slot="data-grid"
|
|
655
|
+
{...props}
|
|
656
|
+
>
|
|
657
|
+
{toolbar}
|
|
658
|
+
<div
|
|
659
|
+
className="min-h-0 flex-1 overflow-auto"
|
|
660
|
+
data-slot="data-grid-scroller"
|
|
661
|
+
ref={scroller}
|
|
662
|
+
>
|
|
663
|
+
<div className="relative w-max" ref={grid}>
|
|
664
|
+
{/* biome-ignore lint/a11y/useSemanticElements: it is the table element already; the role adds the grid's keyboard. */}
|
|
665
|
+
<table
|
|
666
|
+
aria-activedescendant={
|
|
667
|
+
rows && cols ? `${id}-${active.row}-${active.col}` : undefined
|
|
668
|
+
}
|
|
669
|
+
aria-label={label}
|
|
670
|
+
aria-multiselectable
|
|
671
|
+
className="table-fixed border-separate border-spacing-0 text-xs outline-none"
|
|
672
|
+
data-slot="data-grid-table"
|
|
673
|
+
onCopy={event => copy(event, false)}
|
|
674
|
+
onCut={event => copy(event, true)}
|
|
675
|
+
onKeyDown={key}
|
|
676
|
+
onPaste={paste}
|
|
677
|
+
ref={table}
|
|
678
|
+
// biome-ignore lint/a11y/noNoninteractiveElementToInteractiveRole: the ARIA grid pattern on a real table, whose cells are worked on, not only read.
|
|
679
|
+
role="grid"
|
|
680
|
+
style={{ width: total }}
|
|
681
|
+
tabIndex={0}
|
|
682
|
+
>
|
|
683
|
+
<colgroup>
|
|
684
|
+
<col style={{ width: ROW_HEAD }} />
|
|
685
|
+
{shown.map((width, col) => (
|
|
686
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: columns are places, not items.
|
|
687
|
+
<col key={col} style={{ width }} />
|
|
688
|
+
))}
|
|
689
|
+
</colgroup>
|
|
690
|
+
<thead>
|
|
691
|
+
<tr className="h-cell">
|
|
692
|
+
{/* The corner selects every cell. */}
|
|
693
|
+
<th
|
|
694
|
+
aria-label="Select all"
|
|
695
|
+
className={cn(
|
|
696
|
+
headCell,
|
|
697
|
+
'sticky top-0 left-0 z-30 cursor-pointer',
|
|
698
|
+
everything && headOn,
|
|
699
|
+
)}
|
|
700
|
+
data-slot="data-grid-corner"
|
|
701
|
+
onPointerDown={event => {
|
|
702
|
+
event.preventDefault();
|
|
703
|
+
table.current?.focus({ preventScroll: true });
|
|
704
|
+
setSelection({
|
|
705
|
+
anchor: { row: 0, col: 0 },
|
|
706
|
+
focus: { row: rows - 1, col: cols - 1 },
|
|
707
|
+
});
|
|
708
|
+
}}
|
|
709
|
+
scope="col"
|
|
710
|
+
/>
|
|
711
|
+
{widths.slice(0, cols).map((_, col) => {
|
|
712
|
+
const name = columns?.[col]?.label;
|
|
713
|
+
const on = [...extra, range].some(
|
|
714
|
+
block => col >= block.left && col <= block.right,
|
|
715
|
+
);
|
|
716
|
+
return (
|
|
717
|
+
<th
|
|
718
|
+
className={cn(
|
|
719
|
+
headCell,
|
|
720
|
+
'sticky top-0 z-20 cursor-default px-3',
|
|
721
|
+
name
|
|
722
|
+
? columns?.[col]?.align === 'right'
|
|
723
|
+
? 'text-right'
|
|
724
|
+
: 'text-left'
|
|
725
|
+
: 'text-center',
|
|
726
|
+
on && [
|
|
727
|
+
headOn,
|
|
728
|
+
'shadow-[inset_0_-2px_0_var(--color-neutral-400)] group-focus-within/grid:shadow-[inset_0_-2px_0_var(--color-primary-500)]',
|
|
729
|
+
],
|
|
730
|
+
)}
|
|
731
|
+
data-col={col}
|
|
732
|
+
data-slot="data-grid-column"
|
|
733
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: columns are places, not items.
|
|
734
|
+
key={col}
|
|
735
|
+
onPointerDown={event => pressHead(event, 'col', col)}
|
|
736
|
+
scope="col"
|
|
737
|
+
>
|
|
738
|
+
<span className="block truncate">
|
|
739
|
+
{name ?? letter(col)}
|
|
740
|
+
</span>
|
|
741
|
+
<span
|
|
742
|
+
aria-hidden
|
|
743
|
+
// The last column's edge stays inside the grid, so it never makes the grid scroll.
|
|
744
|
+
className={cn(
|
|
745
|
+
'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',
|
|
746
|
+
col === cols - 1 ? 'right-0' : '-right-1',
|
|
747
|
+
)}
|
|
748
|
+
data-slot="data-grid-resize"
|
|
749
|
+
onDoubleClick={() =>
|
|
750
|
+
setWidths(current =>
|
|
751
|
+
current.map((w, i) =>
|
|
752
|
+
i === col
|
|
753
|
+
? (columns?.[col]?.width ??
|
|
754
|
+
COLUMN_WIDTH)
|
|
755
|
+
: w,
|
|
756
|
+
),
|
|
757
|
+
)
|
|
758
|
+
}
|
|
759
|
+
onPointerDown={event => resize(event, 'col', col)}
|
|
760
|
+
/>
|
|
761
|
+
</th>
|
|
762
|
+
);
|
|
763
|
+
})}
|
|
764
|
+
</tr>
|
|
765
|
+
</thead>
|
|
766
|
+
<tbody>
|
|
767
|
+
{values.slice(0, rows).map((line, row) => {
|
|
768
|
+
// The summary's rows, and its first, which takes the rule over it.
|
|
769
|
+
const summary = row >= rows - summaryRows;
|
|
770
|
+
const summaryStart = summaryRows > 0 && row === rows - summaryRows;
|
|
771
|
+
const rowOn = [...extra, range].some(
|
|
772
|
+
block => row >= block.top && row <= block.bottom,
|
|
773
|
+
);
|
|
774
|
+
return (
|
|
775
|
+
<tr
|
|
776
|
+
className="h-cell"
|
|
777
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: rows are places, not items.
|
|
778
|
+
key={row}
|
|
779
|
+
style={
|
|
780
|
+
heights[row] !== undefined
|
|
781
|
+
? { height: heights[row] }
|
|
782
|
+
: undefined
|
|
783
|
+
}
|
|
784
|
+
>
|
|
785
|
+
<th
|
|
786
|
+
className={cn(
|
|
787
|
+
headCell,
|
|
788
|
+
'sticky left-0 z-10 cursor-default text-center',
|
|
789
|
+
summaryStart &&
|
|
790
|
+
'shadow-[inset_0_1px_0_var(--color-neutral-400)]',
|
|
791
|
+
rowOn && [
|
|
792
|
+
headOn,
|
|
793
|
+
'shadow-[inset_-2px_0_0_var(--color-neutral-400)] group-focus-within/grid:shadow-[inset_-2px_0_0_var(--color-primary-500)]',
|
|
794
|
+
],
|
|
795
|
+
)}
|
|
796
|
+
data-row-head={row}
|
|
797
|
+
data-slot="data-grid-row"
|
|
798
|
+
onPointerDown={event => pressHead(event, 'row', row)}
|
|
799
|
+
scope="row"
|
|
800
|
+
>
|
|
801
|
+
{row + 1}
|
|
802
|
+
<span
|
|
803
|
+
aria-hidden
|
|
804
|
+
className={cn(
|
|
805
|
+
'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',
|
|
806
|
+
row === rows - 1 ? 'bottom-0' : '-bottom-1',
|
|
807
|
+
)}
|
|
808
|
+
data-slot="data-grid-resize"
|
|
809
|
+
onDoubleClick={() =>
|
|
810
|
+
setHeights(current => {
|
|
811
|
+
const next = [...current];
|
|
812
|
+
next[row] = undefined;
|
|
813
|
+
return next;
|
|
814
|
+
})
|
|
815
|
+
}
|
|
816
|
+
onPointerDown={event => resize(event, 'row', row)}
|
|
817
|
+
/>
|
|
818
|
+
</th>
|
|
819
|
+
{widths.slice(0, cols).map((_, col) => {
|
|
820
|
+
const value = line[col] ?? '';
|
|
821
|
+
const right = align(col, value);
|
|
822
|
+
const isActive =
|
|
823
|
+
row === active.row && col === active.col;
|
|
824
|
+
return (
|
|
825
|
+
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: a cell of a table whose role is grid is a gridcell, which takes aria-selected.
|
|
826
|
+
<td
|
|
827
|
+
aria-selected={selected(row, col)}
|
|
828
|
+
className={cn(
|
|
829
|
+
'cursor-cell select-none overflow-hidden text-ellipsis whitespace-nowrap border-neutral-200 border-r border-b px-3 text-neutral-900',
|
|
830
|
+
// The first column names the row, as a table's first column does: in 500.
|
|
831
|
+
col === 0 && 'font-medium',
|
|
832
|
+
summary && 'font-medium text-neutral-950',
|
|
833
|
+
// A shortfall in a total is the sheet's problem: in the error ink.
|
|
834
|
+
summary &&
|
|
835
|
+
/^\s*[-−]/.test(value) &&
|
|
836
|
+
'text-error-600',
|
|
837
|
+
summaryStart &&
|
|
838
|
+
'shadow-[inset_0_1px_0_var(--color-neutral-400)]',
|
|
839
|
+
'scroll-mt-cell scroll-ml-11',
|
|
840
|
+
right && 'text-right tabular-nums',
|
|
841
|
+
// The selected block is washed, the cell
|
|
842
|
+
// it started from left clear in its ring.
|
|
843
|
+
multi &&
|
|
844
|
+
!isActive &&
|
|
845
|
+
selected(row, col) &&
|
|
846
|
+
'bg-neutral-950/5 group-focus-within/grid:bg-primary-500/10',
|
|
847
|
+
)}
|
|
848
|
+
data-col={col}
|
|
849
|
+
data-row={row}
|
|
850
|
+
data-slot="data-grid-cell"
|
|
851
|
+
id={`${id}-${row}-${col}`}
|
|
852
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: cells are places, not items.
|
|
853
|
+
key={col}
|
|
854
|
+
onDoubleClick={() => startEdit('edit')}
|
|
855
|
+
onPointerDown={event =>
|
|
856
|
+
pressCell(event, { row, col })
|
|
857
|
+
}
|
|
858
|
+
>
|
|
859
|
+
{value}
|
|
860
|
+
</td>
|
|
861
|
+
);
|
|
862
|
+
})}
|
|
863
|
+
</tr>
|
|
864
|
+
);
|
|
865
|
+
})}
|
|
866
|
+
</tbody>
|
|
867
|
+
</table>
|
|
868
|
+
{/* The marks over the cells: the block, the cell it started from, the fill. */}
|
|
869
|
+
<div
|
|
870
|
+
aria-hidden
|
|
871
|
+
className="pointer-events-none absolute z-5 border border-neutral-500 group-focus-within/grid:border-primary-500"
|
|
872
|
+
data-slot="data-grid-range"
|
|
873
|
+
ref={rangeBox}
|
|
874
|
+
/>
|
|
875
|
+
<div
|
|
876
|
+
aria-hidden
|
|
877
|
+
className="pointer-events-none absolute z-6 border-2 border-neutral-500 group-focus-within/grid:border-primary-500"
|
|
878
|
+
data-slot="data-grid-active"
|
|
879
|
+
ref={activeBox}
|
|
880
|
+
/>
|
|
881
|
+
<div
|
|
882
|
+
aria-hidden
|
|
883
|
+
className="pointer-events-none absolute z-6 border border-primary-500 border-dashed bg-primary-500/5"
|
|
884
|
+
data-slot="data-grid-fill"
|
|
885
|
+
ref={fillBox}
|
|
886
|
+
/>
|
|
887
|
+
{/* The blocks added with Ctrl or ⌘, outlined as the one being made is. */}
|
|
888
|
+
{extra.map((_, i) => (
|
|
889
|
+
<div
|
|
890
|
+
aria-hidden
|
|
891
|
+
className="pointer-events-none absolute z-5 border border-neutral-500 group-focus-within/grid:border-primary-500"
|
|
892
|
+
data-slot="data-grid-extra"
|
|
893
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the blocks are places in a list that only grows.
|
|
894
|
+
key={i}
|
|
895
|
+
/>
|
|
896
|
+
))}
|
|
897
|
+
{/* A fill runs from one block; with several selected there is no handle. */}
|
|
898
|
+
{editable && !edit && extra.length === 0 && rows > 0 && cols > 0 && (
|
|
899
|
+
<FillHandle box={boxOf} onPointerDown={pressFill} range={range} />
|
|
900
|
+
)}
|
|
901
|
+
{edit && (
|
|
902
|
+
<div
|
|
903
|
+
className="absolute z-7 flex bg-neutral-50 outline-2 outline-primary-500 outline-solid -outline-offset-1"
|
|
904
|
+
data-slot="data-grid-editor"
|
|
905
|
+
ref={editor}
|
|
906
|
+
>
|
|
907
|
+
<input
|
|
908
|
+
aria-label={`${columns?.[edit.col]?.label ?? letter(edit.col)} ${edit.row + 1}`}
|
|
909
|
+
className={cn(
|
|
910
|
+
'h-full w-full min-w-0 bg-tint pr-3 pl-[calc(var(--spacing)*3+1px)] text-neutral-900 text-xs outline-none',
|
|
911
|
+
align(edit.col, edit.draft) && 'text-right tabular-nums',
|
|
912
|
+
edit.col === 0 && 'font-medium',
|
|
913
|
+
)}
|
|
914
|
+
onBlur={() => endEdit(true)}
|
|
915
|
+
onChange={event => setEdit({ ...edit, draft: event.target.value })}
|
|
916
|
+
onKeyDown={editKey}
|
|
917
|
+
ref={openField}
|
|
918
|
+
value={edit.draft}
|
|
919
|
+
/>
|
|
920
|
+
</div>
|
|
921
|
+
)}
|
|
922
|
+
</div>
|
|
923
|
+
</div>
|
|
924
|
+
{footer != null && (
|
|
925
|
+
<div
|
|
926
|
+
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"
|
|
927
|
+
data-slot="data-grid-footer"
|
|
928
|
+
>
|
|
929
|
+
{footer}
|
|
930
|
+
</div>
|
|
931
|
+
)}
|
|
932
|
+
</div>
|
|
933
|
+
);
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
/** The square at the selection's corner that drags a fill out of it. */
|
|
937
|
+
const FillHandle = ({
|
|
938
|
+
range,
|
|
939
|
+
box,
|
|
940
|
+
onPointerDown,
|
|
941
|
+
}: {
|
|
942
|
+
range: DataGridRange;
|
|
943
|
+
box: (
|
|
944
|
+
range: DataGridRange,
|
|
945
|
+
) => { left: number; top: number; width: number; height: number } | null;
|
|
946
|
+
onPointerDown: (event: PointerEvent<HTMLDivElement>) => void;
|
|
947
|
+
}) => {
|
|
948
|
+
const handle = useRef<HTMLDivElement>(null);
|
|
949
|
+
useLayoutEffect(() => {
|
|
950
|
+
const at = box(range);
|
|
951
|
+
if (!handle.current || !at) return;
|
|
952
|
+
handle.current.style.left = `${at.left + at.width - 4}px`;
|
|
953
|
+
handle.current.style.top = `${at.top + at.height - 4}px`;
|
|
954
|
+
});
|
|
955
|
+
return (
|
|
956
|
+
<div
|
|
957
|
+
aria-hidden
|
|
958
|
+
className="absolute z-8 size-2 cursor-crosshair touch-none border border-neutral-50 bg-neutral-500 group-focus-within/grid:bg-primary-500"
|
|
959
|
+
data-slot="data-grid-fill-handle"
|
|
960
|
+
onPointerDown={onPointerDown}
|
|
961
|
+
ref={handle}
|
|
962
|
+
title="Drag to fill"
|
|
963
|
+
/>
|
|
964
|
+
);
|
|
965
|
+
};
|