@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,332 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
4
|
+
import { Check, CheckCheck, CircleAlert, Clock } from 'lucide-react';
|
|
5
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
6
|
+
import { cn } from '../lib/cn';
|
|
7
|
+
import { focusRing } from './button';
|
|
8
|
+
import { ChatTypingDots } from './chat-typing';
|
|
9
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from './tooltip';
|
|
10
|
+
|
|
11
|
+
// Both variants share one layout: a header row across the top, then two columns side by side, the words
|
|
12
|
+
// and the time with its tick at the foot of the second, 8 apart, 12 from the right and 10 from the
|
|
13
|
+
// bottom, so the words never run under it however long they get. 16 round the content.
|
|
14
|
+
export const chatMessageVariants = cva(
|
|
15
|
+
'grid min-w-0 grid-cols-[minmax(0,1fr)_auto] gap-x-2 gap-y-2 rounded-card py-4 pr-3 pl-4 text-neutral-950',
|
|
16
|
+
{
|
|
17
|
+
variants: {
|
|
18
|
+
variant: {
|
|
19
|
+
// The current user's words sit on the theme's tint, at the right and at most two thirds of
|
|
20
|
+
// the thread wide.
|
|
21
|
+
own: 'ml-auto w-fit max-w-2/3 bg-tint',
|
|
22
|
+
// Agents and bots read as a card on the surface, told apart by the hairline alone.
|
|
23
|
+
other: 'border border-neutral-200 bg-neutral-50',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
defaultVariants: {
|
|
27
|
+
variant: 'other',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export type ChatMessageStatus = 'sending' | 'sent' | 'delivered' | 'read' | 'failed';
|
|
33
|
+
|
|
34
|
+
const statuses = {
|
|
35
|
+
sending: { label: 'Sending', glyph: 'clock', className: 'text-neutral-500' },
|
|
36
|
+
sent: { label: 'Sent', glyph: 'tick', className: 'text-neutral-500' },
|
|
37
|
+
delivered: { label: 'Delivered', glyph: 'ticks', className: 'text-neutral-500' },
|
|
38
|
+
// Read is the same double tick, darkened: a quiet change, not a second accent colour.
|
|
39
|
+
read: { label: 'Read', glyph: 'ticks', className: 'text-neutral-950' },
|
|
40
|
+
failed: { label: 'Not sent', glyph: 'alert', className: 'text-error-500' },
|
|
41
|
+
} as const;
|
|
42
|
+
|
|
43
|
+
const glyphs = [
|
|
44
|
+
{ id: 'clock', icon: Clock },
|
|
45
|
+
{ id: 'tick', icon: Check },
|
|
46
|
+
{ id: 'ticks', icon: CheckCheck },
|
|
47
|
+
{ id: 'alert', icon: CircleAlert },
|
|
48
|
+
] as const;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The delivery tick. Every glyph sits stacked in one cell and only the current one shows, so a
|
|
52
|
+
* change of state crossfades (and settles from 80%) instead of snapping; read darkens in place.
|
|
53
|
+
*/
|
|
54
|
+
const ChatStatusIcon = ({ status, labelled }: { status: ChatMessageStatus; labelled: boolean }) => {
|
|
55
|
+
const state = statuses[status];
|
|
56
|
+
const className = cn(
|
|
57
|
+
'grid place-items-center transition-colors duration-150 *:col-start-1 *:row-start-1 *:transition-[opacity,scale] *:duration-200 *:ease-out-strong motion-reduce:transition-none motion-reduce:*:transition-none',
|
|
58
|
+
state.className,
|
|
59
|
+
);
|
|
60
|
+
const icons = glyphs.map(({ id, icon: Icon }) => (
|
|
61
|
+
<Icon
|
|
62
|
+
aria-hidden
|
|
63
|
+
className={cn('size-3', id !== state.glyph && 'scale-[0.8] opacity-0')}
|
|
64
|
+
key={id}
|
|
65
|
+
strokeWidth={2}
|
|
66
|
+
/>
|
|
67
|
+
));
|
|
68
|
+
|
|
69
|
+
return labelled ? (
|
|
70
|
+
<span
|
|
71
|
+
aria-label={state.label}
|
|
72
|
+
className={className}
|
|
73
|
+
data-slot="chat-message-status"
|
|
74
|
+
role="img"
|
|
75
|
+
>
|
|
76
|
+
{icons}
|
|
77
|
+
</span>
|
|
78
|
+
) : (
|
|
79
|
+
<span aria-hidden className={className} data-slot="chat-message-status">
|
|
80
|
+
{icons}
|
|
81
|
+
</span>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export type ChatMessageProps = Omit<ComponentProps<'article'>, 'title'> &
|
|
86
|
+
VariantProps<typeof chatMessageVariants> & {
|
|
87
|
+
/** Who wrote it, e.g. "@Support Bot". Own messages usually leave it out. */
|
|
88
|
+
author?: ReactNode;
|
|
89
|
+
/** Beside the author, usually a soft Badge: "AI", "Admin". */
|
|
90
|
+
badge?: ReactNode;
|
|
91
|
+
/** Before the author, usually a 24px round Avatar. */
|
|
92
|
+
avatar?: ReactNode;
|
|
93
|
+
/**
|
|
94
|
+
* At the header's right: facts about the message itself, as `ChatMessageStat`s, such as an AI
|
|
95
|
+
* reply's confidence, the files it read and whose profile it looked at. A dot goes between each.
|
|
96
|
+
*/
|
|
97
|
+
stats?: ReactNode;
|
|
98
|
+
/** When it was sent, as shown: "2:48 PM". */
|
|
99
|
+
time?: ReactNode;
|
|
100
|
+
/** Machine-readable `time`, set on the <time> element. */
|
|
101
|
+
dateTime?: string;
|
|
102
|
+
/**
|
|
103
|
+
* Delivery state, drawn as a tick after the time; it crossfades as it changes. On a message
|
|
104
|
+
* `status` means delivery; on `ChatAttachment` and `ChatReference` it is a badge node.
|
|
105
|
+
*/
|
|
106
|
+
status?: ChatMessageStatus;
|
|
107
|
+
/** Beside "Not sent" on a failed message: a Retry button that calls it. */
|
|
108
|
+
onRetry?: () => void;
|
|
109
|
+
retryLabel?: string;
|
|
110
|
+
/**
|
|
111
|
+
* The reply is still being written (an assistant thinking or streaming): three pulsing dots
|
|
112
|
+
* stand in for the words, or follow them while they stream in.
|
|
113
|
+
*/
|
|
114
|
+
pending?: boolean;
|
|
115
|
+
/** What a screen reader hears for a pending message with no words yet. */
|
|
116
|
+
pendingLabel?: string;
|
|
117
|
+
/**
|
|
118
|
+
* What goes with the message, under it at its width and on its side, 6 apart: a
|
|
119
|
+
* `ChatAttachments` row of files, a `ChatReference` to a record (an invoice), or both, files
|
|
120
|
+
* first. They read as one with the message, never as the thread's next item.
|
|
121
|
+
*/
|
|
122
|
+
attached?: ReactNode;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** One message: `own` for the current user on the tint, `other` for agents and bots as a card. */
|
|
126
|
+
export const ChatMessage = ({
|
|
127
|
+
variant,
|
|
128
|
+
author,
|
|
129
|
+
badge,
|
|
130
|
+
avatar,
|
|
131
|
+
stats,
|
|
132
|
+
time,
|
|
133
|
+
dateTime,
|
|
134
|
+
status,
|
|
135
|
+
onRetry,
|
|
136
|
+
retryLabel = 'Retry',
|
|
137
|
+
pending = false,
|
|
138
|
+
pendingLabel = 'Writing a reply',
|
|
139
|
+
attached,
|
|
140
|
+
className,
|
|
141
|
+
children,
|
|
142
|
+
...rest
|
|
143
|
+
}: ChatMessageProps) => {
|
|
144
|
+
// With something attached, the thread's entrance goes on the group, which is what it lays out.
|
|
145
|
+
const { 'data-enter': enter, ...props } = rest as typeof rest & { 'data-enter'?: string };
|
|
146
|
+
const grouped = attached != null && attached !== false;
|
|
147
|
+
const own = variant === 'own';
|
|
148
|
+
const state = status ? statuses[status] : undefined;
|
|
149
|
+
const failed = status === 'failed';
|
|
150
|
+
const hasHeader = author != null || badge != null || avatar != null || stats != null;
|
|
151
|
+
const hasBody = children != null && children !== false && children !== '';
|
|
152
|
+
const hasMeta = time != null || status != null;
|
|
153
|
+
// Only the time and its tick fit beside the words; anything more takes its own row.
|
|
154
|
+
const metaBeside = hasMeta && !failed;
|
|
155
|
+
|
|
156
|
+
const message = (
|
|
157
|
+
<article
|
|
158
|
+
aria-busy={pending || undefined}
|
|
159
|
+
className={cn(
|
|
160
|
+
chatMessageVariants({ variant }),
|
|
161
|
+
// In a group the group takes the side and the width; the message fills it.
|
|
162
|
+
grouped && 'ml-0 w-full max-w-none',
|
|
163
|
+
className,
|
|
164
|
+
)}
|
|
165
|
+
data-enter={grouped ? undefined : enter}
|
|
166
|
+
data-pending={pending || undefined}
|
|
167
|
+
data-slot="chat-message"
|
|
168
|
+
data-status={status}
|
|
169
|
+
data-variant={variant ?? 'other'}
|
|
170
|
+
{...props}
|
|
171
|
+
>
|
|
172
|
+
{hasHeader ? (
|
|
173
|
+
<header
|
|
174
|
+
className="col-span-2 flex min-w-0 items-center gap-2"
|
|
175
|
+
data-slot="chat-message-header"
|
|
176
|
+
>
|
|
177
|
+
{avatar}
|
|
178
|
+
{author != null && (
|
|
179
|
+
<span
|
|
180
|
+
className="min-w-0 truncate font-medium text-neutral-950 text-xs"
|
|
181
|
+
data-slot="chat-message-author"
|
|
182
|
+
>
|
|
183
|
+
{author}
|
|
184
|
+
</span>
|
|
185
|
+
)}
|
|
186
|
+
{badge}
|
|
187
|
+
{stats != null && (
|
|
188
|
+
<span
|
|
189
|
+
className="ml-auto flex shrink-0 items-center gap-1.5 pl-2 *:not-first:before:mr-1.5 *:not-first:before:text-neutral-400 *:not-first:before:content-['·']"
|
|
190
|
+
data-slot="chat-message-stats"
|
|
191
|
+
>
|
|
192
|
+
{stats}
|
|
193
|
+
</span>
|
|
194
|
+
)}
|
|
195
|
+
</header>
|
|
196
|
+
) : (
|
|
197
|
+
// Without a name the reader would hear the words but not whose they are.
|
|
198
|
+
own && <span className="sr-only">You:</span>
|
|
199
|
+
)}
|
|
200
|
+
<div
|
|
201
|
+
// Anywhere, so a run of emoji or a long id breaks inside the bubble instead of widening it.
|
|
202
|
+
className={cn(
|
|
203
|
+
'wrap-anywhere min-w-0 whitespace-pre-wrap text-xs',
|
|
204
|
+
// Words with nothing beside them (no time, or a failed footer on its own row) span both
|
|
205
|
+
// columns and keep 16 on the right.
|
|
206
|
+
!metaBeside && 'col-span-2 pr-1',
|
|
207
|
+
)}
|
|
208
|
+
data-slot="chat-message-body"
|
|
209
|
+
>
|
|
210
|
+
{children}
|
|
211
|
+
{pending && (
|
|
212
|
+
<>
|
|
213
|
+
<ChatTypingDots className={hasBody ? 'ml-1.5' : undefined} />
|
|
214
|
+
{!hasBody && <span className="sr-only">{pendingLabel}</span>}
|
|
215
|
+
</>
|
|
216
|
+
)}
|
|
217
|
+
</div>
|
|
218
|
+
{hasMeta && (
|
|
219
|
+
<footer
|
|
220
|
+
className={cn(
|
|
221
|
+
// The second column's foot, 10 from the bottom where the words keep 16.
|
|
222
|
+
'-mb-1.5 flex shrink-0 items-center justify-end gap-1 self-end text-neutral-600 text-xxs tabular-nums',
|
|
223
|
+
// "Not sent · Retry · time" is too wide to sit beside the words, so it takes its own row.
|
|
224
|
+
!metaBeside && 'col-span-2',
|
|
225
|
+
)}
|
|
226
|
+
data-slot="chat-message-meta"
|
|
227
|
+
>
|
|
228
|
+
{/* A failed message says so in words, so its icon is not named twice. */}
|
|
229
|
+
{status && <ChatStatusIcon labelled={!failed} status={status} />}
|
|
230
|
+
{failed && state && (
|
|
231
|
+
<span className="text-error-600" data-slot="chat-message-failed">
|
|
232
|
+
{state.label}
|
|
233
|
+
</span>
|
|
234
|
+
)}
|
|
235
|
+
{failed && onRetry && (
|
|
236
|
+
<>
|
|
237
|
+
<span aria-hidden>·</span>
|
|
238
|
+
<button
|
|
239
|
+
className={cn(
|
|
240
|
+
'cursor-pointer rounded-chip font-medium text-neutral-950 underline-offset-2 hover:underline',
|
|
241
|
+
focusRing,
|
|
242
|
+
)}
|
|
243
|
+
data-slot="chat-message-retry"
|
|
244
|
+
onClick={onRetry}
|
|
245
|
+
type="button"
|
|
246
|
+
>
|
|
247
|
+
{retryLabel}
|
|
248
|
+
</button>
|
|
249
|
+
</>
|
|
250
|
+
)}
|
|
251
|
+
{failed && time != null && <span aria-hidden>·</span>}
|
|
252
|
+
{time != null && (
|
|
253
|
+
<time data-slot="chat-message-time" dateTime={dateTime}>
|
|
254
|
+
{time}
|
|
255
|
+
</time>
|
|
256
|
+
)}
|
|
257
|
+
</footer>
|
|
258
|
+
)}
|
|
259
|
+
</article>
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
if (!grouped) return message;
|
|
263
|
+
|
|
264
|
+
return (
|
|
265
|
+
// The message and what goes with it read as one: the same side, the same width (the wider
|
|
266
|
+
// of the words and what is attached, up to two thirds for one's own), 6 apart.
|
|
267
|
+
<div
|
|
268
|
+
className={cn(
|
|
269
|
+
'flex min-w-0 flex-col gap-1.5',
|
|
270
|
+
own ? 'ml-auto w-fit max-w-2/3' : 'w-full',
|
|
271
|
+
)}
|
|
272
|
+
data-enter={enter}
|
|
273
|
+
data-slot="chat-message-group"
|
|
274
|
+
data-variant={variant ?? 'other'}
|
|
275
|
+
>
|
|
276
|
+
{message}
|
|
277
|
+
<div
|
|
278
|
+
className={cn(
|
|
279
|
+
'flex min-w-0 flex-col gap-1.5',
|
|
280
|
+
// Files keep to the message's side; a linked record spans the group and takes the
|
|
281
|
+
// message's corner, so the two read as one shape.
|
|
282
|
+
'*:data-[slot=chat-attachments]:ml-0 *:data-[slot=chat-attachments]:max-w-none',
|
|
283
|
+
'**:data-[variant=record]:rounded-card *:data-[slot=chat-reference]:rounded-card',
|
|
284
|
+
)}
|
|
285
|
+
data-slot="chat-message-attached"
|
|
286
|
+
>
|
|
287
|
+
{attached}
|
|
288
|
+
</div>
|
|
289
|
+
</div>
|
|
290
|
+
);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export type ChatMessageStatProps = Omit<ComponentProps<'span'>, 'children'> & {
|
|
294
|
+
/** What the figure is, read out before it and shown on hover: "Confidence", "Files read". */
|
|
295
|
+
label: string;
|
|
296
|
+
/** A 12 icon before the figure. */
|
|
297
|
+
icon?: ReactNode;
|
|
298
|
+
/** The figure: "90%", "2". Leave it out for an icon that says it all (a profile looked at). */
|
|
299
|
+
value?: ReactNode;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* One fact in a message's `stats`: a 12 icon and a figure, 11 `neutral-600` tabular. Quiet, as it is
|
|
304
|
+
* about the message rather than part of it; its tooltip names it, since the figure alone does not.
|
|
305
|
+
*/
|
|
306
|
+
export const ChatMessageStat = ({
|
|
307
|
+
label,
|
|
308
|
+
icon,
|
|
309
|
+
value,
|
|
310
|
+
className,
|
|
311
|
+
...props
|
|
312
|
+
}: ChatMessageStatProps) => (
|
|
313
|
+
<Tooltip>
|
|
314
|
+
<TooltipTrigger
|
|
315
|
+
render={
|
|
316
|
+
<span
|
|
317
|
+
{...props}
|
|
318
|
+
className={cn(
|
|
319
|
+
'inline-flex items-center gap-1 text-neutral-600 text-xxs tabular-nums [&_svg]:size-3 [&_svg]:shrink-0 [&_svg]:text-neutral-500',
|
|
320
|
+
className,
|
|
321
|
+
)}
|
|
322
|
+
data-slot="chat-message-stat"
|
|
323
|
+
/>
|
|
324
|
+
}
|
|
325
|
+
>
|
|
326
|
+
<span className="sr-only">{label}: </span>
|
|
327
|
+
{icon}
|
|
328
|
+
{value}
|
|
329
|
+
</TooltipTrigger>
|
|
330
|
+
<TooltipContent>{label}</TooltipContent>
|
|
331
|
+
</Tooltip>
|
|
332
|
+
);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { mergeProps } from '@base-ui/react/merge-props';
|
|
4
|
+
import { useRender } from '@base-ui/react/use-render';
|
|
5
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
6
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
7
|
+
import { cn } from '../lib/cn';
|
|
8
|
+
import { focusRing } from './button';
|
|
9
|
+
|
|
10
|
+
export const chatTokenVariants = cva(
|
|
11
|
+
// Inline in body copy as well as in a reference, so it breaks rather than overflow a narrow bubble.
|
|
12
|
+
'inline max-w-full rounded-chip px-1.5 py-0.5 font-mono font-normal text-xxs [box-decoration-break:clone] [overflow-wrap:anywhere]',
|
|
13
|
+
{
|
|
14
|
+
variants: {
|
|
15
|
+
tone: {
|
|
16
|
+
primary: 'bg-primary-500/20 text-primary-800',
|
|
17
|
+
neutral: 'bg-neutral-500/20 text-neutral-800',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
defaultVariants: {
|
|
21
|
+
// Neutral: a reference is not a state, and in an accent theme a primary chip outshouts the message.
|
|
22
|
+
tone: 'neutral',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export type ChatTokenProps = ComponentProps<'code'> & VariantProps<typeof chatTokenVariants>;
|
|
28
|
+
|
|
29
|
+
/** An identifier in mono on a tinted chip, such as an invoice or ticket number, inline in a message. */
|
|
30
|
+
export const ChatToken = ({ tone, className, ...props }: ChatTokenProps) => (
|
|
31
|
+
<code
|
|
32
|
+
className={cn(chatTokenVariants({ tone }), className)}
|
|
33
|
+
data-slot="chat-token"
|
|
34
|
+
{...props}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export type ChatReferenceProps = useRender.ComponentProps<'div'> & {
|
|
39
|
+
/** The identifier, shown as a ChatToken. */
|
|
40
|
+
token: ReactNode;
|
|
41
|
+
/** Trailing slot, usually a soft Badge: "Pending", "Paid". */
|
|
42
|
+
status?: ReactNode;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* An attachment or record dropped into the thread: its token, one fact and its status. Pass
|
|
47
|
+
* `render={<a href="…" />}` to make it open the record.
|
|
48
|
+
*/
|
|
49
|
+
export const ChatReference = ({
|
|
50
|
+
token,
|
|
51
|
+
status,
|
|
52
|
+
render,
|
|
53
|
+
ref,
|
|
54
|
+
className,
|
|
55
|
+
children,
|
|
56
|
+
...props
|
|
57
|
+
}: ChatReferenceProps) =>
|
|
58
|
+
useRender({
|
|
59
|
+
defaultTagName: 'div',
|
|
60
|
+
render,
|
|
61
|
+
ref,
|
|
62
|
+
state: { slot: 'chat-reference' },
|
|
63
|
+
props: mergeProps<'div'>(
|
|
64
|
+
{
|
|
65
|
+
className: cn(
|
|
66
|
+
// The field row's 38px, with the control's corner to match.
|
|
67
|
+
'flex min-h-control-xl min-w-0 items-center gap-3 rounded-control border border-neutral-200 bg-neutral-50 px-3 py-1.5 text-neutral-950 text-xs transition-[border-color] duration-150',
|
|
68
|
+
// Only a link or button reacts; a plain record stays still under the pointer.
|
|
69
|
+
'[a&]:hover:border-neutral-300 [button&]:hover:border-neutral-300',
|
|
70
|
+
focusRing,
|
|
71
|
+
className,
|
|
72
|
+
),
|
|
73
|
+
children: (
|
|
74
|
+
<>
|
|
75
|
+
<ChatToken className="min-w-0 truncate" data-slot="chat-reference-token">
|
|
76
|
+
{token}
|
|
77
|
+
</ChatToken>
|
|
78
|
+
{/* The fact is the figure the reader came for, so the long token gives way first. */}
|
|
79
|
+
{children != null && (
|
|
80
|
+
<span
|
|
81
|
+
className="ml-auto shrink-0 whitespace-nowrap tabular-nums"
|
|
82
|
+
data-slot="chat-reference-fact"
|
|
83
|
+
>
|
|
84
|
+
{children}
|
|
85
|
+
</span>
|
|
86
|
+
)}
|
|
87
|
+
{status != null && (
|
|
88
|
+
<span
|
|
89
|
+
className={cn('flex shrink-0', children == null && 'ml-auto')}
|
|
90
|
+
data-slot="chat-reference-status"
|
|
91
|
+
>
|
|
92
|
+
{status}
|
|
93
|
+
</span>
|
|
94
|
+
)}
|
|
95
|
+
</>
|
|
96
|
+
),
|
|
97
|
+
},
|
|
98
|
+
props,
|
|
99
|
+
),
|
|
100
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Private to the chat parts; not exported from the package.
|
|
2
|
+
import { Children, createContext, type MouseEvent, type ReactNode, type Ref } from 'react';
|
|
3
|
+
|
|
4
|
+
export const assignRef = <T>(ref: Ref<T> | undefined, node: T | null) => {
|
|
5
|
+
if (typeof ref === 'function') ref(node);
|
|
6
|
+
else if (ref) ref.current = node;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type SuggestionsContextValue = {
|
|
10
|
+
listId: string;
|
|
11
|
+
activeId: string | null;
|
|
12
|
+
setActiveId: (id: string) => void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const SuggestionsContext = createContext<SuggestionsContextValue | null>(null);
|
|
16
|
+
|
|
17
|
+
type AttachContextValue = {
|
|
18
|
+
/** Opens the composer's or prompt's file picker; null without `onAttach`. */
|
|
19
|
+
openFiles: (() => void) | null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Lets `MessageComposerAttachItem` open the file picker of the composer or prompt round it. */
|
|
23
|
+
export const AttachContext = createContext<AttachContextValue | null>(null);
|
|
24
|
+
|
|
25
|
+
export const hasContent = (node: ReactNode) =>
|
|
26
|
+
node != null && node !== false && Children.toArray(node).length > 0;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The box reads as one field: a press on its padding, its foot or between its buttons puts the
|
|
30
|
+
* caret in the text. Presses on anything interactive keep their own behaviour.
|
|
31
|
+
*/
|
|
32
|
+
export const focusTextOnPress = (
|
|
33
|
+
event: MouseEvent<HTMLElement>,
|
|
34
|
+
text: HTMLTextAreaElement | null,
|
|
35
|
+
disabled: boolean,
|
|
36
|
+
) => {
|
|
37
|
+
const target = event.target as HTMLElement;
|
|
38
|
+
if (disabled || target.closest('button, a, input, textarea, [role]')) return;
|
|
39
|
+
event.preventDefault();
|
|
40
|
+
text?.focus();
|
|
41
|
+
};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { type ComponentProps, type ReactNode, useContext, useId, useState } from 'react';
|
|
4
|
+
import { cn } from '../lib/cn';
|
|
5
|
+
import { hasContent, SuggestionsContext } from './chat-shared';
|
|
6
|
+
import { MenuItem, type MenuItemProps } from './menu-item';
|
|
7
|
+
|
|
8
|
+
export type ChatTriggerMatch = {
|
|
9
|
+
/** The character that started it: "#", "@". */
|
|
10
|
+
trigger: string;
|
|
11
|
+
/** What follows the trigger up to the caret: "a" for "#a". */
|
|
12
|
+
query: string;
|
|
13
|
+
/** Where the trigger stands in the text. */
|
|
14
|
+
start: number;
|
|
15
|
+
/** Where the query ends: the caret. */
|
|
16
|
+
end: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const blank = /\s/;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The reference being typed: the last `trigger` before the caret, at the start of the text or after
|
|
23
|
+
* a space, followed by no space. "Also, I noticed #a" gives `{ query: 'a', start: 16, end: 18 }`;
|
|
24
|
+
* "a#b" and "#a b" give nothing.
|
|
25
|
+
*/
|
|
26
|
+
export const findChatTrigger = (
|
|
27
|
+
text: string,
|
|
28
|
+
caret: number = text.length,
|
|
29
|
+
trigger = '#',
|
|
30
|
+
): ChatTriggerMatch | null => {
|
|
31
|
+
const before = text.slice(0, caret);
|
|
32
|
+
const start = before.lastIndexOf(trigger);
|
|
33
|
+
|
|
34
|
+
if (start === -1) return null;
|
|
35
|
+
if (start > 0 && !blank.test(before.charAt(start - 1))) return null;
|
|
36
|
+
const query = before.slice(start + trigger.length);
|
|
37
|
+
if (blank.test(query)) return null;
|
|
38
|
+
return { trigger, query, start, end: caret };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type UseChatTriggerOptions = {
|
|
42
|
+
/** The character that opens the suggestions; "#" by default. */
|
|
43
|
+
trigger?: string;
|
|
44
|
+
/** Where the caret is; the end of the text by default. */
|
|
45
|
+
caret?: number;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Watches the composer's draft for a reference being typed after `trigger` ("#a", "@ma"). `open`
|
|
50
|
+
* is whether to show the suggestions, `query` what to filter them by, `insert(token)` the draft with
|
|
51
|
+
* the typed reference replaced by the picked one and a space, and `dismiss` closes them (Escape)
|
|
52
|
+
* until a new trigger is typed.
|
|
53
|
+
*/
|
|
54
|
+
export const useChatTrigger = (
|
|
55
|
+
value: string,
|
|
56
|
+
{ trigger = '#', caret }: UseChatTriggerOptions = {},
|
|
57
|
+
) => {
|
|
58
|
+
const match = findChatTrigger(value, caret, trigger);
|
|
59
|
+
const [dismissed, setDismissed] = useState<number | null>(null);
|
|
60
|
+
|
|
61
|
+
// A dismissal holds for the trigger it closed; once that one is gone, the next opens again.
|
|
62
|
+
if (dismissed !== null && match?.start !== dismissed) setDismissed(null);
|
|
63
|
+
|
|
64
|
+
const open = match != null && match.start !== dismissed;
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
open,
|
|
68
|
+
match: open ? match : null,
|
|
69
|
+
query: open ? match.query : '',
|
|
70
|
+
dismiss: () => {
|
|
71
|
+
if (match) setDismissed(match.start);
|
|
72
|
+
},
|
|
73
|
+
insert: (token: string) => {
|
|
74
|
+
if (!match) return value;
|
|
75
|
+
const rest = value.slice(match.end).replace(/^\s+/, '');
|
|
76
|
+
return `${value.slice(0, match.start)}${token} ${rest}`;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type ChatSuggestionsProps = ComponentProps<'div'> & {
|
|
82
|
+
/** Heads the list: "Recent applications". Also the list's accessible name. */
|
|
83
|
+
title?: ReactNode;
|
|
84
|
+
/** Shown in place of the rows while there are none: "No applications match". */
|
|
85
|
+
empty?: ReactNode;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The records a reference being typed could mean, in the composer's `suggestions` slot: a card the
|
|
90
|
+
* box's width just above it, a title, then the rows. The composer's arrows move through them and
|
|
91
|
+
* Enter picks one, while the caret stays in the text.
|
|
92
|
+
*/
|
|
93
|
+
export const ChatSuggestions = ({
|
|
94
|
+
title,
|
|
95
|
+
empty,
|
|
96
|
+
className,
|
|
97
|
+
children,
|
|
98
|
+
...props
|
|
99
|
+
}: ChatSuggestionsProps) => {
|
|
100
|
+
const context = useContext(SuggestionsContext);
|
|
101
|
+
const titleId = useId();
|
|
102
|
+
const hasRows = hasContent(children);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<div
|
|
106
|
+
className={cn(
|
|
107
|
+
// The box's corner and width, so the two read as one shape stacked at every corner
|
|
108
|
+
// scale; it rises 4 as it opens.
|
|
109
|
+
'flex flex-col gap-1 rounded-[min(calc(var(--radius-item)+4px),calc(var(--radius-item)*100))] border border-neutral-200 bg-neutral-50 p-1 shadow-float',
|
|
110
|
+
'starting:translate-y-1 starting:opacity-0 transition-[opacity,translate] duration-150 ease-out-strong motion-reduce:transition-none',
|
|
111
|
+
className,
|
|
112
|
+
)}
|
|
113
|
+
data-slot="chat-suggestions"
|
|
114
|
+
{...props}
|
|
115
|
+
>
|
|
116
|
+
{title != null && (
|
|
117
|
+
<div
|
|
118
|
+
className="px-3 pt-1.5 pb-1 font-medium text-neutral-600 text-xxs"
|
|
119
|
+
data-slot="chat-suggestions-title"
|
|
120
|
+
id={titleId}
|
|
121
|
+
>
|
|
122
|
+
{title}
|
|
123
|
+
</div>
|
|
124
|
+
)}
|
|
125
|
+
<div
|
|
126
|
+
aria-labelledby={title != null ? titleId : undefined}
|
|
127
|
+
className="flex max-h-60 flex-col gap-1 overflow-y-auto"
|
|
128
|
+
data-slot="chat-suggestions-list"
|
|
129
|
+
id={context?.listId}
|
|
130
|
+
role="listbox"
|
|
131
|
+
>
|
|
132
|
+
{hasRows ? (
|
|
133
|
+
children
|
|
134
|
+
) : (
|
|
135
|
+
<div className="px-3 py-2.5 text-neutral-600 text-xs" role="presentation">
|
|
136
|
+
{empty ?? 'Nothing matches'}
|
|
137
|
+
</div>
|
|
138
|
+
)}
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export type ChatSuggestionProps = Omit<MenuItemProps, 'render' | 'onSelect'> & {
|
|
145
|
+
/** Picks it, by Enter in the composer or a click: insert the reference, link the record. */
|
|
146
|
+
onSelect: () => void;
|
|
147
|
+
disabled?: boolean;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* One row of `ChatSuggestions`, a popup row: a 16 icon in `leading`, the label, a quieter `meta`.
|
|
152
|
+
* The composer's active one takes the hover wash.
|
|
153
|
+
*/
|
|
154
|
+
export const ChatSuggestion = ({
|
|
155
|
+
onSelect,
|
|
156
|
+
disabled = false,
|
|
157
|
+
id: idProp,
|
|
158
|
+
className,
|
|
159
|
+
...props
|
|
160
|
+
}: ChatSuggestionProps) => {
|
|
161
|
+
const context = useContext(SuggestionsContext);
|
|
162
|
+
const autoId = useId();
|
|
163
|
+
const id = idProp ?? autoId;
|
|
164
|
+
const active = context?.activeId === id;
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<MenuItem
|
|
168
|
+
aria-disabled={disabled || undefined}
|
|
169
|
+
aria-selected={active}
|
|
170
|
+
// Concentric inside the panel: its corner (the item's and 4) less its 4 inset and 1 hairline.
|
|
171
|
+
className={cn('rounded-[max(0px,calc(var(--radius-item)-1px))]', className)}
|
|
172
|
+
data-disabled={disabled ? '' : undefined}
|
|
173
|
+
data-highlighted={active ? '' : undefined}
|
|
174
|
+
data-slot="chat-suggestion"
|
|
175
|
+
id={id}
|
|
176
|
+
// The caret stays in the text: a press on a row must not take focus from it.
|
|
177
|
+
onClick={() => {
|
|
178
|
+
if (!disabled) onSelect();
|
|
179
|
+
}}
|
|
180
|
+
onMouseDown={event => event.preventDefault()}
|
|
181
|
+
onMouseMove={() => {
|
|
182
|
+
if (!active && !disabled) context?.setActiveId(id);
|
|
183
|
+
}}
|
|
184
|
+
role="option"
|
|
185
|
+
{...props}
|
|
186
|
+
/>
|
|
187
|
+
);
|
|
188
|
+
};
|