@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,272 @@
|
|
|
1
|
+
import { linearSrgbToOklch, type Oklch, oklchToHex, type RampStep } from './color';
|
|
2
|
+
import { type ThemeOptions, themeFontFamilies } from './create-theme';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* A theme a project already has, read into Continuum's choices: a Tailwind v4 `@theme` block, a
|
|
6
|
+
* shadcn/ui `globals.css`, or any stylesheet of custom properties in those names. What it reads:
|
|
7
|
+
* the primary colour, how grey the greys are, the font and the corners. Everything else stays
|
|
8
|
+
* Continuum's own, so the result is a starting point to adjust in the builder, not a copy.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type ImportedTheme = {
|
|
12
|
+
/** The choices read from it; empty when nothing was recognised. */
|
|
13
|
+
options: ThemeOptions;
|
|
14
|
+
/** What each choice was read from, in words, for saying what was taken: "primary: --color-brand-600". */
|
|
15
|
+
found: { option: keyof ThemeOptions; from: string }[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Every custom property and its first value: the light theme comes before `.dark` in these files. */
|
|
19
|
+
const declarations = (css: string) => {
|
|
20
|
+
const values = new Map<string, string>();
|
|
21
|
+
// Comments out, strings kept: a path such as '../views/*.blade.php' is not a comment's start.
|
|
22
|
+
const clean = css.replace(
|
|
23
|
+
/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")|\/\*[\s\S]*?\*\//g,
|
|
24
|
+
(_, text?: string) => text ?? '',
|
|
25
|
+
);
|
|
26
|
+
for (const match of clean.matchAll(/(--[\w-]+)\s*:\s*([^;{}]+);/g)) {
|
|
27
|
+
const [, name, value] = match;
|
|
28
|
+
if (name && value && !values.has(name)) values.set(name, value.trim());
|
|
29
|
+
}
|
|
30
|
+
return values;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A value with its `var()`s filled in from the same sheet, a few levels deep. */
|
|
34
|
+
const resolve = (value: string, values: Map<string, string>, depth = 0): string =>
|
|
35
|
+
depth > 8
|
|
36
|
+
? value
|
|
37
|
+
: value.replace(/var\(\s*(--[\w-]+)\s*(?:,\s*([^)]+))?\)/g, (_, name: string, fallback) => {
|
|
38
|
+
const next = values.get(name) ?? fallback;
|
|
39
|
+
return next ? resolve(next, values, depth + 1) : '';
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const toLinear = (channel: number) =>
|
|
43
|
+
channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
|
|
44
|
+
|
|
45
|
+
const fromRgb = (r: number, g: number, b: number): Oklch =>
|
|
46
|
+
linearSrgbToOklch([toLinear(r), toLinear(g), toLinear(b)]);
|
|
47
|
+
|
|
48
|
+
const hslToRgb = (h: number, s: number, l: number): [number, number, number] => {
|
|
49
|
+
const k = (n: number) => (n + h / 30) % 12;
|
|
50
|
+
const a = s * Math.min(l, 1 - l);
|
|
51
|
+
const f = (n: number) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
|
|
52
|
+
return [f(0), f(8), f(4)];
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** A number, a percentage of `whole` when it ends in %. */
|
|
56
|
+
const amount = (text: string, whole: number) =>
|
|
57
|
+
text.endsWith('%') ? (Number.parseFloat(text) / 100) * whole : Number.parseFloat(text);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A CSS colour as oklch: hex, rgb(), hsl(), oklch(), and shadcn's bare HSL ("222 47% 11%").
|
|
61
|
+
* Null for anything else (a keyword, a gradient, a colour-mix).
|
|
62
|
+
*/
|
|
63
|
+
export const parseCssColor = (input: string): Oklch | null => {
|
|
64
|
+
const value = input.trim().toLowerCase();
|
|
65
|
+
const hex = /^#([0-9a-f]{3,8})$/.exec(value)?.[1];
|
|
66
|
+
if (hex) {
|
|
67
|
+
const full =
|
|
68
|
+
hex.length <= 4 ? [...hex.slice(0, 3)].map(d => d + d).join('') : hex.slice(0, 6);
|
|
69
|
+
const n = Number.parseInt(full, 16);
|
|
70
|
+
return fromRgb(((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255);
|
|
71
|
+
}
|
|
72
|
+
const fn = /^(rgba?|hsla?|oklch)\(\s*([^)]*)\)$/.exec(value);
|
|
73
|
+
const bare = /^(-?[\d.]+)(?:deg)?\s+([\d.]+%)\s+([\d.]+%)$/.exec(value);
|
|
74
|
+
const parts = fn?.[2]
|
|
75
|
+
? fn[2]
|
|
76
|
+
.split(/[\s,/]+/)
|
|
77
|
+
.filter(Boolean)
|
|
78
|
+
.slice(0, 3)
|
|
79
|
+
: bare
|
|
80
|
+
? bare.slice(1, 4)
|
|
81
|
+
: null;
|
|
82
|
+
if (!parts || parts.length < 3) return null;
|
|
83
|
+
const [a = '', b = '', c = ''] = parts;
|
|
84
|
+
const kind = fn?.[1] ?? 'hsl';
|
|
85
|
+
if (kind.startsWith('rgb'))
|
|
86
|
+
return fromRgb(amount(a, 255) / 255, amount(b, 255) / 255, amount(c, 255) / 255);
|
|
87
|
+
if (kind.startsWith('hsl'))
|
|
88
|
+
return fromRgb(
|
|
89
|
+
...hslToRgb(
|
|
90
|
+
Number.parseFloat(a),
|
|
91
|
+
amount(b, 1) / (b.endsWith('%') ? 1 : 100),
|
|
92
|
+
amount(c, 1) / (c.endsWith('%') ? 1 : 100),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
// oklch(L C H): L as a share or a percentage, C as a number or a percentage of 0.4.
|
|
96
|
+
const l = amount(a, 1);
|
|
97
|
+
const color = { l: l > 1 ? l / 100 : l, c: amount(b, 0.4), h: Number.parseFloat(c) || 0 };
|
|
98
|
+
return Number.isNaN(color.l) || Number.isNaN(color.c) ? null : color;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** One length in px: px, rem and em (at 16px), or a bare number of px. */
|
|
102
|
+
const lengthPx = (value: string) => {
|
|
103
|
+
const match = /^(-?[\d.]+)(px|rem|em)?$/.exec(value.trim());
|
|
104
|
+
if (!match?.[1]) return null;
|
|
105
|
+
const n = Number.parseFloat(match[1]);
|
|
106
|
+
return match[2] === 'rem' || match[2] === 'em' ? n * 16 : n;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** A length in px, a `calc()` of lengths added and taken away included ("calc(0.5rem - 2px)"). */
|
|
110
|
+
const toPx = (value: string) => {
|
|
111
|
+
const inner = /^calc\((.*)\)$/.exec(value.trim())?.[1] ?? value;
|
|
112
|
+
// Terms and the signs between them: ["0.5rem", "-", "2px"].
|
|
113
|
+
const parts = inner.split(/\s+([+-])\s+/);
|
|
114
|
+
let total = 0;
|
|
115
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
116
|
+
const px = lengthPx(parts[i] ?? '');
|
|
117
|
+
if (px === null) return null;
|
|
118
|
+
total += parts[i - 1] === '-' ? -px : px;
|
|
119
|
+
}
|
|
120
|
+
return total;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** The colour a name holds, or null. */
|
|
124
|
+
const colorOf = (values: Map<string, string>, name: string) => {
|
|
125
|
+
const raw = values.get(name);
|
|
126
|
+
return raw ? parseCssColor(resolve(raw, values)) : null;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The ramp step a name stands for, followed through its `var()`s: shadcn's `--primary` is often
|
|
131
|
+
* `var(--color-primary-600)`, and that step is the product's own colour.
|
|
132
|
+
*/
|
|
133
|
+
const rampStepOf = (values: Map<string, string>, name: string) => {
|
|
134
|
+
let current = name;
|
|
135
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
136
|
+
const step = /^--color-[\w-]+-(\d{2,3})$/.exec(current)?.[1];
|
|
137
|
+
if (step) return { name: current, step };
|
|
138
|
+
const next = /^var\(\s*(--[\w-]+)\s*(?:,[^)]*)?\)$/.exec(
|
|
139
|
+
values.get(current)?.trim() ?? '',
|
|
140
|
+
)?.[1];
|
|
141
|
+
if (!next) return null;
|
|
142
|
+
current = next;
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** The semantic names a primary goes by, then the ramps a brand colour sits on. */
|
|
148
|
+
const semanticAccent = ['--color-primary', '--primary', '--color-brand', '--brand'];
|
|
149
|
+
const accentRamps = ['--color-primary', '--color-brand', '--color-accent'];
|
|
150
|
+
const greyRamps = [
|
|
151
|
+
'--color-neutral',
|
|
152
|
+
'--color-gray',
|
|
153
|
+
'--color-grey',
|
|
154
|
+
'--color-slate',
|
|
155
|
+
'--color-zinc',
|
|
156
|
+
'--color-stone',
|
|
157
|
+
'--color-mauve',
|
|
158
|
+
'--color-sage',
|
|
159
|
+
'--color-olive',
|
|
160
|
+
'--color-sand',
|
|
161
|
+
];
|
|
162
|
+
const allSteps = ['50', '100', '200', '300', '400', '500', '600', '700', '800', '900', '950'];
|
|
163
|
+
|
|
164
|
+
/** Continuum's control corner as drawn, which a radius scale of 1 means. */
|
|
165
|
+
const CONTROL_RADIUS = 12;
|
|
166
|
+
|
|
167
|
+
/** Below this chroma a colour reads as grey. */
|
|
168
|
+
const GREY = 0.04;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Reads a Tailwind or shadcn theme's stylesheet into Continuum's theme choices: the primary from
|
|
172
|
+
* its primary or brand colour (a grey one fills the action in neutral), the greys' hue and tint
|
|
173
|
+
* from its grey ramp, the font from `--font-sans`, and the corner scale from its radius.
|
|
174
|
+
*/
|
|
175
|
+
export const themeFromCss = (css: string): ImportedTheme => {
|
|
176
|
+
const values = declarations(css);
|
|
177
|
+
const options: ThemeOptions = {};
|
|
178
|
+
const found: ImportedTheme['found'] = [];
|
|
179
|
+
|
|
180
|
+
// The primary as the product names it, then a step of a brand ramp when that name is grey.
|
|
181
|
+
const named = semanticAccent
|
|
182
|
+
.map(name => ({ name, color: colorOf(values, name) }))
|
|
183
|
+
.find(item => item.color);
|
|
184
|
+
const fromRamp = () => {
|
|
185
|
+
for (const ramp of accentRamps)
|
|
186
|
+
for (const step of ['600', '500', '700', '400']) {
|
|
187
|
+
const color = colorOf(values, `${ramp}-${step}`);
|
|
188
|
+
if (color && color.c >= GREY) return { name: `${ramp}-${step}`, color };
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
};
|
|
192
|
+
const chromatic = named?.color && named.color.c >= GREY ? named : fromRamp();
|
|
193
|
+
if (chromatic?.color) {
|
|
194
|
+
options.accent = oklchToHex(chromatic.color);
|
|
195
|
+
options.action = named?.color && named.color.c < GREY ? 'neutral' : 'accent';
|
|
196
|
+
const step = rampStepOf(values, chromatic.name)?.step;
|
|
197
|
+
if (step && (allSteps as readonly string[]).includes(step))
|
|
198
|
+
options.accentStep = step as RampStep;
|
|
199
|
+
found.push({
|
|
200
|
+
option: 'accent',
|
|
201
|
+
from: rampStepOf(values, chromatic.name)?.name ?? chromatic.name,
|
|
202
|
+
});
|
|
203
|
+
if (options.action === 'neutral')
|
|
204
|
+
found.push({ option: 'action', from: `${named?.name}, a grey` });
|
|
205
|
+
} else if (named?.color) {
|
|
206
|
+
// A near-black primary and no brand colour, as shadcn's default: the action keeps to the greys.
|
|
207
|
+
options.action = 'neutral';
|
|
208
|
+
found.push({ option: 'action', from: `${named.name}, a grey` });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// The greys: the most tinted step of the grey ramp sets how grey they are, since many ramps
|
|
212
|
+
// keep their middle pure and tint only the dark end; shadcn's muted text without a ramp.
|
|
213
|
+
const greys = greyRamps
|
|
214
|
+
.map(ramp =>
|
|
215
|
+
allSteps
|
|
216
|
+
.map(step => ({
|
|
217
|
+
name: `${ramp}-${step}`,
|
|
218
|
+
color: colorOf(values, `${ramp}-${step}`),
|
|
219
|
+
}))
|
|
220
|
+
.filter(item => item.color),
|
|
221
|
+
)
|
|
222
|
+
.find(ramp => ramp.length >= 3);
|
|
223
|
+
const grey =
|
|
224
|
+
greys?.reduce((most, item) =>
|
|
225
|
+
(item.color?.c ?? 0) > (most.color?.c ?? 0) ? item : most,
|
|
226
|
+
) ??
|
|
227
|
+
['--color-muted-foreground', '--muted-foreground']
|
|
228
|
+
.map(name => ({ name, color: colorOf(values, name) }))
|
|
229
|
+
.find(item => item.color);
|
|
230
|
+
if (grey?.color) {
|
|
231
|
+
const tint = Math.min(0.03, Number(grey.color.c.toFixed(3)));
|
|
232
|
+
options.neutral =
|
|
233
|
+
tint < 0.002 ? { hue: 0, tint: 0 } : { hue: Math.round(grey.color.h), tint };
|
|
234
|
+
found.push({ option: 'neutral', from: greys ? grey.name.replace(/-\d+$/, '') : grey.name });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const fontName = [
|
|
238
|
+
'--font-sans',
|
|
239
|
+
'--font-family-sans',
|
|
240
|
+
'--font-body',
|
|
241
|
+
'--default-font-family',
|
|
242
|
+
].find(name => values.has(name));
|
|
243
|
+
const family = fontName
|
|
244
|
+
? resolve(values.get(fontName) ?? '', values)
|
|
245
|
+
.split(',')[0]
|
|
246
|
+
?.trim()
|
|
247
|
+
.replace(/^['"]|['"]$/g, '')
|
|
248
|
+
.replace(/\s+Variable$/i, '')
|
|
249
|
+
: undefined;
|
|
250
|
+
if (
|
|
251
|
+
fontName &&
|
|
252
|
+
family &&
|
|
253
|
+
!/^(ui-sans-serif|system-ui|sans-serif|-apple-system)$/i.test(family)
|
|
254
|
+
) {
|
|
255
|
+
const known = Object.entries(themeFontFamilies).find(
|
|
256
|
+
([, font]) => font.family.toLowerCase() === family.toLowerCase(),
|
|
257
|
+
)?.[0];
|
|
258
|
+
options.font = known ?? family;
|
|
259
|
+
found.push({ option: 'font', from: fontName });
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// A control's corner: Tailwind's md, or shadcn's --radius less the 2px its controls take off.
|
|
263
|
+
const radiusName = ['--radius-md', '--radius', '--radius-lg'].find(name => values.has(name));
|
|
264
|
+
const radiusPx = radiusName ? toPx(resolve(values.get(radiusName) ?? '', values)) : null;
|
|
265
|
+
if (radiusName && radiusPx !== null) {
|
|
266
|
+
const control = radiusName === '--radius' ? Math.max(0, radiusPx - 2) : radiusPx;
|
|
267
|
+
options.radius = Math.min(1.5, Math.round((control / CONTROL_RADIUS) * 20) / 20);
|
|
268
|
+
found.push({ option: 'radius', from: radiusName });
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return { options, found };
|
|
272
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
|
2
|
+
import { type CSSProperties, useCallback, useEffect, useState } from 'react';
|
|
3
|
+
import { cn } from './cn';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* A row that can outgrow its width (a dock, the top nav's sections) scrolls sideways with its
|
|
7
|
+
* scrollbar hidden, and says which way there is more: a chevron either side steps it along, the
|
|
8
|
+
* one at an end it has reached disabled rather than gone so the row does not shift, and an edge
|
|
9
|
+
* with more fades so the cut-off item reads as cut off rather than as the end. A row that fits
|
|
10
|
+
* has no chevrons at all.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const fade = 16;
|
|
14
|
+
|
|
15
|
+
export const useOverflowScroll = <T extends HTMLElement>() => {
|
|
16
|
+
const [node, setNode] = useState<T | null>(null);
|
|
17
|
+
const [edges, setEdges] = useState({ start: false, end: false, overflows: false });
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!node) return;
|
|
21
|
+
const update = () => {
|
|
22
|
+
const overflows = node.scrollWidth > node.clientWidth + 1;
|
|
23
|
+
const start = node.scrollLeft > 1;
|
|
24
|
+
const end = node.scrollLeft + node.clientWidth < node.scrollWidth - 1;
|
|
25
|
+
setEdges(current =>
|
|
26
|
+
current.start === start && current.end === end && current.overflows === overflows
|
|
27
|
+
? current
|
|
28
|
+
: { start, end, overflows },
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
update();
|
|
32
|
+
node.addEventListener('scroll', update, { passive: true });
|
|
33
|
+
// The row and its items both change size: a resize, a label, a font that loads late.
|
|
34
|
+
const observer = new ResizeObserver(update);
|
|
35
|
+
observer.observe(node);
|
|
36
|
+
for (const child of node.children) observer.observe(child);
|
|
37
|
+
return () => {
|
|
38
|
+
node.removeEventListener('scroll', update);
|
|
39
|
+
observer.disconnect();
|
|
40
|
+
};
|
|
41
|
+
}, [node]);
|
|
42
|
+
|
|
43
|
+
/** Steps most of a view along, so the item cut off at the edge ends up whole. */
|
|
44
|
+
const step = useCallback(
|
|
45
|
+
(direction: -1 | 1) =>
|
|
46
|
+
node?.scrollBy({ left: direction * node.clientWidth * 0.75, behavior: 'smooth' }),
|
|
47
|
+
[node],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/** Brings the selected item (matched by `selector`) into view, if it is out of it. */
|
|
51
|
+
const reveal = useCallback(
|
|
52
|
+
(selector: string) => {
|
|
53
|
+
const item = node?.querySelector<HTMLElement>(selector);
|
|
54
|
+
if (!node || !item) return;
|
|
55
|
+
const box = item.getBoundingClientRect();
|
|
56
|
+
const left = box.left - node.getBoundingClientRect().left + node.scrollLeft - fade;
|
|
57
|
+
const right = left + box.width + fade * 2;
|
|
58
|
+
if (left < node.scrollLeft) node.scrollTo({ left, behavior: 'smooth' });
|
|
59
|
+
else if (right > node.scrollLeft + node.clientWidth)
|
|
60
|
+
node.scrollTo({ left: right - node.clientWidth, behavior: 'smooth' });
|
|
61
|
+
},
|
|
62
|
+
[node],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const style: CSSProperties = {
|
|
66
|
+
maskImage: `linear-gradient(to right, ${edges.start ? 'transparent' : 'black'}, black ${fade}px, black calc(100% - ${fade}px), ${edges.end ? 'transparent' : 'black'})`,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return { ref: setNode, edges, step, reveal, style };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
type OverflowChevronProps = {
|
|
73
|
+
direction: -1 | 1;
|
|
74
|
+
/** At the end it points to, so there is nothing that way. */
|
|
75
|
+
disabled?: boolean;
|
|
76
|
+
onStep: (direction: -1 | 1) => void;
|
|
77
|
+
className?: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The pointer's way along the row. Out of the tab order: keyboard focus already scrolls the item
|
|
82
|
+
* it lands on into view.
|
|
83
|
+
*/
|
|
84
|
+
export const OverflowChevron = ({
|
|
85
|
+
direction,
|
|
86
|
+
disabled,
|
|
87
|
+
onStep,
|
|
88
|
+
className,
|
|
89
|
+
}: OverflowChevronProps) => {
|
|
90
|
+
const Icon = direction < 0 ? ChevronLeft : ChevronRight;
|
|
91
|
+
return (
|
|
92
|
+
<button
|
|
93
|
+
aria-label={direction < 0 ? 'Scroll back' : 'Scroll on'}
|
|
94
|
+
className={cn(
|
|
95
|
+
'grid shrink-0 cursor-pointer place-items-center text-neutral-600 transition-[background-color,color] duration-150 enabled:hover:bg-neutral-950/5 enabled:hover:text-neutral-950',
|
|
96
|
+
'disabled:cursor-default disabled:text-neutral-300',
|
|
97
|
+
className,
|
|
98
|
+
)}
|
|
99
|
+
data-slot="overflow-chevron"
|
|
100
|
+
disabled={disabled}
|
|
101
|
+
onClick={() => onStep(direction)}
|
|
102
|
+
tabIndex={-1}
|
|
103
|
+
type="button"
|
|
104
|
+
>
|
|
105
|
+
<Icon className="size-4" strokeWidth={2} />
|
|
106
|
+
</button>
|
|
107
|
+
);
|
|
108
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Toggle } from '@base-ui/react/toggle';
|
|
2
|
+
import type { HTMLProps } from 'react';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Base UI's Toggle is a pressed/unpressed button (aria-pressed), but a group that always keeps one
|
|
6
|
+
* value is a single choice, so each item is announced as a radio in a radiogroup.
|
|
7
|
+
* Otherwise a screen reader offers to "unpress" the active item, which useSingleValue then ignores.
|
|
8
|
+
*/
|
|
9
|
+
export const radioToggleRender = (
|
|
10
|
+
{ 'aria-pressed': _pressed, ...props }: HTMLProps<HTMLButtonElement>,
|
|
11
|
+
state: Toggle.State,
|
|
12
|
+
) => (
|
|
13
|
+
// biome-ignore lint/a11y/useSemanticElements: a styled segment with the toggle group's roving focus, which a native radio input can't carry.
|
|
14
|
+
<button {...props} aria-checked={state.pressed} role="radio" type="button" />
|
|
15
|
+
);
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { flushSync } from 'react-dom';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Dragging one of a run of items (a table's rows, or its columns) to another place in the run. The
|
|
7
|
+
* item lifts and follows the pointer along one axis, and the items it passes step aside by its size,
|
|
8
|
+
* so the gap it would drop into is always open. Plain DOM transforms while the pointer moves: nothing
|
|
9
|
+
* re-renders until the drop, and then the move is handed over in one go and the item settles into its
|
|
10
|
+
* place from where it was let go.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A list with one item moved from one place to another: what a drop hands over, applied. */
|
|
14
|
+
export const moveItem = <T>(list: readonly T[], from: number, to: number): T[] => {
|
|
15
|
+
const next = [...list];
|
|
16
|
+
const [item] = next.splice(from, 1);
|
|
17
|
+
if (item !== undefined) next.splice(to, 0, item);
|
|
18
|
+
return next;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Holds the pointer for a drag: one cursor over the whole page, and nothing under the pointer
|
|
23
|
+
* hovers, takes a cursor of its own or gets its text selected while the drag runs. Returns the
|
|
24
|
+
* release.
|
|
25
|
+
*/
|
|
26
|
+
export const holdPointer = (cursor: string) => {
|
|
27
|
+
document.getSelection()?.removeAllRanges();
|
|
28
|
+
const style = document.createElement('style');
|
|
29
|
+
style.dataset.continuumDrag = '';
|
|
30
|
+
style.textContent = `html { cursor: ${cursor} !important; user-select: none !important; -webkit-user-select: none !important; } body * { pointer-events: none !important; }`;
|
|
31
|
+
document.head.append(style);
|
|
32
|
+
return () => style.remove();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ReorderItem = {
|
|
36
|
+
/** What moves with the item: a row, or every cell of a column. */
|
|
37
|
+
nodes: HTMLElement[];
|
|
38
|
+
/** Where it starts and how long it is along the axis. */
|
|
39
|
+
start: number;
|
|
40
|
+
size: number;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type Options = {
|
|
44
|
+
axis: 'x' | 'y';
|
|
45
|
+
items: ReorderItem[];
|
|
46
|
+
index: number;
|
|
47
|
+
/** Where the pointer was when the drag began, along the axis. */
|
|
48
|
+
from: number;
|
|
49
|
+
/** Called once at the drop, inside a sync render, so the new order paints with the transforms gone. */
|
|
50
|
+
onMove: (from: number, to: number) => void;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const duration = 150;
|
|
54
|
+
const ease = `transform ${duration}ms cubic-bezier(0.215, 0.61, 0.355, 1)`;
|
|
55
|
+
|
|
56
|
+
/** Where the dragged item would land with its middle at `middle`. */
|
|
57
|
+
const targetOf = (items: ReorderItem[], index: number, middle: number) => {
|
|
58
|
+
let to = index;
|
|
59
|
+
items.forEach((item, i) => {
|
|
60
|
+
const centre = item.start + item.size / 2;
|
|
61
|
+
if (i > index && middle > centre) to = Math.max(to, i);
|
|
62
|
+
if (i < index && middle < centre) to = Math.min(to, i);
|
|
63
|
+
});
|
|
64
|
+
return to;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const translate = (axis: 'x' | 'y', shift: number) =>
|
|
68
|
+
shift === 0 ? '' : `${axis === 'x' ? 'translateX' : 'translateY'}(${shift}px)`;
|
|
69
|
+
|
|
70
|
+
const place = (options: Options, offset: number) => {
|
|
71
|
+
const { axis, items, index } = options;
|
|
72
|
+
const dragged = items[index];
|
|
73
|
+
if (!dragged) return index;
|
|
74
|
+
const first = items[0];
|
|
75
|
+
const last = items.at(-1);
|
|
76
|
+
// The item stays within the run: it can reach either end, never leave it.
|
|
77
|
+
const min = (first?.start ?? 0) - dragged.start;
|
|
78
|
+
const max = (last ? last.start + last.size : 0) - (dragged.start + dragged.size);
|
|
79
|
+
const clamped = Math.min(Math.max(offset, min), max);
|
|
80
|
+
const to = targetOf(items, index, dragged.start + dragged.size / 2 + clamped);
|
|
81
|
+
|
|
82
|
+
items.forEach((item, i) => {
|
|
83
|
+
let shift = 0;
|
|
84
|
+
if (i === index) shift = clamped;
|
|
85
|
+
else if (index < to && i > index && i <= to) shift = -dragged.size;
|
|
86
|
+
else if (index > to && i < index && i >= to) shift = dragged.size;
|
|
87
|
+
for (const node of item.nodes) {
|
|
88
|
+
node.style.transition = i === index ? 'none' : ease;
|
|
89
|
+
node.style.transform = translate(axis, shift);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return to;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Eases nodes from where they are drawn now to where they lie, then lets go of them. */
|
|
96
|
+
const settle = (nodes: HTMLElement[], axis: 'x' | 'y', offsets: number[], done: () => void) => {
|
|
97
|
+
nodes.forEach((node, i) => {
|
|
98
|
+
node.style.transition = 'none';
|
|
99
|
+
node.style.transform = translate(axis, offsets[i] ?? 0);
|
|
100
|
+
});
|
|
101
|
+
// Commit the start before the transition, so it runs from there.
|
|
102
|
+
void nodes[0]?.getBoundingClientRect();
|
|
103
|
+
for (const node of nodes) {
|
|
104
|
+
node.style.transition = ease;
|
|
105
|
+
node.style.transform = '';
|
|
106
|
+
}
|
|
107
|
+
window.setTimeout(() => {
|
|
108
|
+
for (const node of nodes) node.style.transition = '';
|
|
109
|
+
done();
|
|
110
|
+
}, duration);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Starts a drag from a pointer already down; ends at the pointer's release, or puts everything back
|
|
115
|
+
* on Escape. The drop calls `onMove`.
|
|
116
|
+
*/
|
|
117
|
+
export const startReorder = (options: Options) => {
|
|
118
|
+
const { axis, items, index, from, onMove } = options;
|
|
119
|
+
const dragged = items[index];
|
|
120
|
+
if (!dragged) return;
|
|
121
|
+
for (const node of dragged.nodes) node.dataset.dragging = '';
|
|
122
|
+
const release = holdPointer('grabbing');
|
|
123
|
+
let to = index;
|
|
124
|
+
const edge = (node: HTMLElement) => {
|
|
125
|
+
const box = node.getBoundingClientRect();
|
|
126
|
+
return axis === 'x' ? box.left : box.top;
|
|
127
|
+
};
|
|
128
|
+
const lift = () => {
|
|
129
|
+
for (const node of dragged.nodes) delete node.dataset.dragging;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const move = (event: PointerEvent) => {
|
|
133
|
+
to = place(options, (axis === 'x' ? event.clientX : event.clientY) - from);
|
|
134
|
+
};
|
|
135
|
+
const end = (drop: boolean) => {
|
|
136
|
+
window.removeEventListener('pointermove', move);
|
|
137
|
+
window.removeEventListener('pointerup', up);
|
|
138
|
+
window.removeEventListener('pointercancel', cancel);
|
|
139
|
+
window.removeEventListener('keydown', key, true);
|
|
140
|
+
release();
|
|
141
|
+
const others = items.filter((_, i) => i !== index).flatMap(item => item.nodes);
|
|
142
|
+
|
|
143
|
+
if (!drop || to === index) {
|
|
144
|
+
// Back where they came from, the dragged one and the ones that stepped aside alike.
|
|
145
|
+
const all = [...others, ...dragged.nodes];
|
|
146
|
+
for (const node of all) {
|
|
147
|
+
node.style.transition = ease;
|
|
148
|
+
node.style.transform = '';
|
|
149
|
+
}
|
|
150
|
+
window.setTimeout(() => {
|
|
151
|
+
for (const node of all) node.style.transition = '';
|
|
152
|
+
lift();
|
|
153
|
+
}, duration);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Where the dragged item is drawn as it is let go; the others already stand where the new
|
|
158
|
+
// order puts them, so they drop their shifts without a step.
|
|
159
|
+
const before = dragged.nodes.map(edge);
|
|
160
|
+
flushSync(() => onMove(index, to));
|
|
161
|
+
for (const node of [...others, ...dragged.nodes]) {
|
|
162
|
+
node.style.transition = 'none';
|
|
163
|
+
node.style.transform = '';
|
|
164
|
+
}
|
|
165
|
+
const offsets = dragged.nodes.map((node, i) => (before[i] ?? 0) - edge(node));
|
|
166
|
+
for (const node of others) node.style.transition = '';
|
|
167
|
+
settle(dragged.nodes, axis, offsets, lift);
|
|
168
|
+
};
|
|
169
|
+
const up = () => end(true);
|
|
170
|
+
const cancel = () => end(false);
|
|
171
|
+
const key = (event: KeyboardEvent) => {
|
|
172
|
+
if (event.key !== 'Escape') return;
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
event.stopPropagation();
|
|
175
|
+
end(false);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
window.addEventListener('pointermove', move);
|
|
179
|
+
window.addEventListener('pointerup', up);
|
|
180
|
+
window.addEventListener('pointercancel', cancel);
|
|
181
|
+
window.addEventListener('keydown', key, true);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A press that becomes a drag once it has moved `threshold` px, so a click on the same target (a
|
|
186
|
+
* sortable head's label) still clicks. A press that dragged swallows the click that follows it.
|
|
187
|
+
*/
|
|
188
|
+
export const pressToDrag = (
|
|
189
|
+
event: { clientX: number; clientY: number },
|
|
190
|
+
axis: 'x' | 'y',
|
|
191
|
+
begin: (from: number) => void,
|
|
192
|
+
threshold = 4,
|
|
193
|
+
) => {
|
|
194
|
+
const x = event.clientX;
|
|
195
|
+
const y = event.clientY;
|
|
196
|
+
const move = (next: PointerEvent) => {
|
|
197
|
+
const distance = axis === 'x' ? next.clientX - x : next.clientY - y;
|
|
198
|
+
if (Math.abs(distance) < threshold) return;
|
|
199
|
+
stop();
|
|
200
|
+
// The click this press ends in is the drop, not a sort. A click follows its pointerup in
|
|
201
|
+
// the same task, so the swallow goes right after it, also when no click comes.
|
|
202
|
+
const swallow = (click: MouseEvent) => {
|
|
203
|
+
click.preventDefault();
|
|
204
|
+
click.stopPropagation();
|
|
205
|
+
};
|
|
206
|
+
window.addEventListener('click', swallow, { capture: true, once: true });
|
|
207
|
+
window.addEventListener(
|
|
208
|
+
'pointerup',
|
|
209
|
+
() => setTimeout(() => window.removeEventListener('click', swallow, true), 0),
|
|
210
|
+
{ once: true },
|
|
211
|
+
);
|
|
212
|
+
begin(axis === 'x' ? x : y);
|
|
213
|
+
// The first step already counts.
|
|
214
|
+
window.dispatchEvent(new PointerEvent('pointermove', next));
|
|
215
|
+
};
|
|
216
|
+
const stop = () => {
|
|
217
|
+
window.removeEventListener('pointermove', move);
|
|
218
|
+
window.removeEventListener('pointerup', stop);
|
|
219
|
+
};
|
|
220
|
+
window.addEventListener('pointermove', move);
|
|
221
|
+
window.addEventListener('pointerup', stop);
|
|
222
|
+
};
|