@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,406 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
4
|
+
import {
|
|
5
|
+
type ClipboardEvent,
|
|
6
|
+
type ComponentProps,
|
|
7
|
+
type FormEvent,
|
|
8
|
+
type KeyboardEvent,
|
|
9
|
+
type MouseEvent,
|
|
10
|
+
type InputEvent as ReactInputEvent,
|
|
11
|
+
useEffect,
|
|
12
|
+
useLayoutEffect,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import { cn } from '../lib/cn';
|
|
17
|
+
import { useFieldControl } from './field';
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* One box per character. Boxes take the Input's border, radius and states so a
|
|
21
|
+
* code field sits beside ordinary inputs without looking like another family;
|
|
22
|
+
* md is the input's measured 38px, lg a step up for a code that is the page's
|
|
23
|
+
* only task (verify email, two-factor).
|
|
24
|
+
*/
|
|
25
|
+
export const codeInputSlotVariants = cva(
|
|
26
|
+
[
|
|
27
|
+
// grow: a row given a width (w-full) shares it out; in the default w-fit row each box keeps its own.
|
|
28
|
+
// min-w-0: on a parent narrower than the row, the boxes shrink to fit rather than spill.
|
|
29
|
+
'relative flex min-w-0 grow items-center justify-center border bg-neutral-50 font-medium tabular-nums shadow-control',
|
|
30
|
+
'transition-[border-color,color] duration-150',
|
|
31
|
+
],
|
|
32
|
+
{
|
|
33
|
+
variants: {
|
|
34
|
+
size: {
|
|
35
|
+
md: 'h-control-lg w-control-lg rounded-control text-sm',
|
|
36
|
+
lg: 'h-bar w-control-xl rounded-control text-lg',
|
|
37
|
+
},
|
|
38
|
+
invalid: {
|
|
39
|
+
true: 'border-error-500 text-error-600',
|
|
40
|
+
false: '',
|
|
41
|
+
},
|
|
42
|
+
disabled: {
|
|
43
|
+
// The row is dimmed to half strength, as every disabled control is; the box keeps its colours.
|
|
44
|
+
true: 'border-neutral-200 text-neutral-900',
|
|
45
|
+
false: '',
|
|
46
|
+
},
|
|
47
|
+
active: {
|
|
48
|
+
// The real input is focused, so the box it writes into carries the ring.
|
|
49
|
+
true: 'outline-solid outline-2 outline-focus outline-offset-2',
|
|
50
|
+
false: '',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
compoundVariants: [
|
|
54
|
+
{
|
|
55
|
+
invalid: false,
|
|
56
|
+
disabled: false,
|
|
57
|
+
className:
|
|
58
|
+
'border-neutral-200 text-neutral-900 group-hover/code-input:border-neutral-300',
|
|
59
|
+
},
|
|
60
|
+
{ invalid: false, disabled: false, active: true, className: 'border-neutral-300' },
|
|
61
|
+
],
|
|
62
|
+
defaultVariants: {
|
|
63
|
+
size: 'md',
|
|
64
|
+
invalid: false,
|
|
65
|
+
disabled: false,
|
|
66
|
+
active: false,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const gapBySize = { md: 'gap-2', lg: 'gap-2.5' } as const;
|
|
72
|
+
|
|
73
|
+
const charsets = {
|
|
74
|
+
numeric: { test: /\d/, pattern: '\\d', inputMode: 'numeric' },
|
|
75
|
+
alphanumeric: { test: /[a-z\d]/i, pattern: '[A-Za-z0-9]', inputMode: 'text' },
|
|
76
|
+
} as const;
|
|
77
|
+
|
|
78
|
+
/*
|
|
79
|
+
* A short horizontal shake, kept under the 250ms ceiling. It runs through the Web
|
|
80
|
+
* Animations API so it needs no keyframes in the stylesheet and can be replayed.
|
|
81
|
+
*/
|
|
82
|
+
const shakeKeyframes: Keyframe[] = [
|
|
83
|
+
{ transform: 'translateX(0)' },
|
|
84
|
+
{ transform: 'translateX(-4px)' },
|
|
85
|
+
{ transform: 'translateX(4px)' },
|
|
86
|
+
{ transform: 'translateX(-3px)' },
|
|
87
|
+
{ transform: 'translateX(2px)' },
|
|
88
|
+
{ transform: 'translateX(0)' },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
export type CodeInputProps = Omit<
|
|
92
|
+
ComponentProps<'input'>,
|
|
93
|
+
'size' | 'value' | 'defaultValue' | 'onChange' | 'maxLength' | 'type' | 'pattern' | 'inputMode'
|
|
94
|
+
> &
|
|
95
|
+
Pick<VariantProps<typeof codeInputSlotVariants>, 'size'> & {
|
|
96
|
+
/** Number of characters; 6 by default. */
|
|
97
|
+
length?: number;
|
|
98
|
+
value?: string;
|
|
99
|
+
defaultValue?: string;
|
|
100
|
+
onValueChange?: (value: string) => void;
|
|
101
|
+
/** Called once the last box is filled by the user, with the full code. */
|
|
102
|
+
onComplete?: (value: string) => void;
|
|
103
|
+
/** Which characters are accepted; letters are upper-cased. */
|
|
104
|
+
charset?: keyof typeof charsets;
|
|
105
|
+
/** Shake the boxes once when the field turns invalid. Skipped under reduced motion. */
|
|
106
|
+
shake?: boolean;
|
|
107
|
+
/** Show dots instead of the characters, for codes that should not be read over a shoulder. */
|
|
108
|
+
mask?: boolean;
|
|
109
|
+
/** Classes for the native input that drives the boxes; `className` goes on the row. */
|
|
110
|
+
inputClassName?: string;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/** A one-time-code field: N boxes driven by one real input, so paste, SMS autofill and backspace work. */
|
|
114
|
+
export const CodeInput = ({
|
|
115
|
+
length = 6,
|
|
116
|
+
value: valueProp,
|
|
117
|
+
defaultValue,
|
|
118
|
+
onValueChange,
|
|
119
|
+
onComplete,
|
|
120
|
+
charset = 'numeric',
|
|
121
|
+
shake = false,
|
|
122
|
+
mask = false,
|
|
123
|
+
size = 'md',
|
|
124
|
+
className,
|
|
125
|
+
inputClassName,
|
|
126
|
+
id,
|
|
127
|
+
disabled,
|
|
128
|
+
'aria-describedby': describedBy,
|
|
129
|
+
'aria-invalid': ariaInvalid,
|
|
130
|
+
onFocus,
|
|
131
|
+
onBlur,
|
|
132
|
+
onKeyDown,
|
|
133
|
+
onPaste,
|
|
134
|
+
onSelect,
|
|
135
|
+
onMouseDown,
|
|
136
|
+
onBeforeInput,
|
|
137
|
+
ref,
|
|
138
|
+
...props
|
|
139
|
+
}: CodeInputProps) => {
|
|
140
|
+
const { invalid, ...control } = useFieldControl({
|
|
141
|
+
id,
|
|
142
|
+
disabled,
|
|
143
|
+
'aria-describedby': describedBy,
|
|
144
|
+
'aria-invalid': ariaInvalid,
|
|
145
|
+
});
|
|
146
|
+
const isDisabled = Boolean(control.disabled);
|
|
147
|
+
const set = charsets[charset];
|
|
148
|
+
|
|
149
|
+
const sanitise = (raw: string) => {
|
|
150
|
+
let next = '';
|
|
151
|
+
for (const char of raw) {
|
|
152
|
+
if (set.test.test(char)) next += char;
|
|
153
|
+
if (next.length === length) break;
|
|
154
|
+
}
|
|
155
|
+
return charset === 'alphanumeric' ? next.toUpperCase() : next;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const [uncontrolled, setUncontrolled] = useState(() => sanitise(defaultValue ?? ''));
|
|
159
|
+
const value = valueProp === undefined ? uncontrolled : sanitise(valueProp);
|
|
160
|
+
|
|
161
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
162
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
163
|
+
const slotRefs = useRef<(HTMLDivElement | null)[]>([]);
|
|
164
|
+
// Where the caret should land after the next render, once React has written the value.
|
|
165
|
+
const pendingCaret = useRef<number | null>(null);
|
|
166
|
+
const [focused, setFocused] = useState(false);
|
|
167
|
+
const [selection, setSelection] = useState<[number, number]>([0, 0]);
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* Overwrite mode: a collapsed caret in front of a character becomes a
|
|
171
|
+
* selection of that character, so typing replaces the box rather than
|
|
172
|
+
* pushing the rest of the code to the right.
|
|
173
|
+
*/
|
|
174
|
+
const placeCaret = (index: number, current = value) => {
|
|
175
|
+
const input = inputRef.current;
|
|
176
|
+
if (!input) return;
|
|
177
|
+
const start = Math.max(0, Math.min(index, current.length, length - 1));
|
|
178
|
+
const end = start < current.length ? start + 1 : start;
|
|
179
|
+
input.setSelectionRange(start, end);
|
|
180
|
+
setSelection([start, end]);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const commit = (next: string, caret: number) => {
|
|
184
|
+
pendingCaret.current = caret;
|
|
185
|
+
if (next === value) {
|
|
186
|
+
// Nothing re-renders, and React writes the old value back after this event, so wait a frame.
|
|
187
|
+
requestAnimationFrame(() => placeCaret(caret));
|
|
188
|
+
pendingCaret.current = null;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (valueProp === undefined) setUncontrolled(next);
|
|
192
|
+
onValueChange?.(next);
|
|
193
|
+
if (next.length === length) onComplete?.(next);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
useLayoutEffect(() => {
|
|
197
|
+
if (pendingCaret.current === null || document.activeElement !== inputRef.current) return;
|
|
198
|
+
placeCaret(pendingCaret.current);
|
|
199
|
+
pendingCaret.current = null;
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Shake on the change into invalid only, not on every render while it stays so.
|
|
203
|
+
const wasInvalid = useRef(invalid);
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
const row = rowRef.current;
|
|
206
|
+
if (shake && invalid && !wasInvalid.current && row?.animate) {
|
|
207
|
+
const reduced = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
208
|
+
if (!reduced) row.animate(shakeKeyframes, { duration: 240, easing: 'ease-out' });
|
|
209
|
+
}
|
|
210
|
+
wasInvalid.current = invalid;
|
|
211
|
+
}, [invalid, shake]);
|
|
212
|
+
|
|
213
|
+
const handleChange = (event: FormEvent<HTMLInputElement>) => {
|
|
214
|
+
// Typing, autofill and IME all arrive here; the raw value may hold spaces or dashes.
|
|
215
|
+
const target = event.currentTarget;
|
|
216
|
+
const next = sanitise(target.value);
|
|
217
|
+
const caret = Math.min(target.selectionStart ?? next.length, next.length);
|
|
218
|
+
commit(next, next.length === length && caret >= length ? length - 1 : caret);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const handleBeforeInput = (event: ReactInputEvent<HTMLInputElement>) => {
|
|
222
|
+
onBeforeInput?.(event);
|
|
223
|
+
const data = event.data;
|
|
224
|
+
// Refuse a stray letter before it reaches the value, so the caret never jumps.
|
|
225
|
+
if (data && ![...data].some(char => set.test.test(char))) event.preventDefault();
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const handlePaste = (event: ClipboardEvent<HTMLInputElement>) => {
|
|
229
|
+
onPaste?.(event);
|
|
230
|
+
if (event.defaultPrevented) return;
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
const pasted = sanitise(event.clipboardData.getData('text'));
|
|
233
|
+
if (!pasted) return;
|
|
234
|
+
const [start, end] = selection;
|
|
235
|
+
// A whole code replaces what is there; a fragment goes in at the caret.
|
|
236
|
+
const next =
|
|
237
|
+
pasted.length >= length
|
|
238
|
+
? pasted
|
|
239
|
+
: sanitise(value.slice(0, start) + pasted + value.slice(end));
|
|
240
|
+
commit(next, Math.min(start + pasted.length, length - 1));
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
|
244
|
+
onKeyDown?.(event);
|
|
245
|
+
if (event.defaultPrevented || event.metaKey || event.ctrlKey || event.altKey) return;
|
|
246
|
+
const [start, end] = selection;
|
|
247
|
+
const single = end - start <= 1;
|
|
248
|
+
|
|
249
|
+
if (event.key === 'ArrowLeft' && !event.shiftKey) {
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
placeCaret(start - 1);
|
|
252
|
+
} else if (event.key === 'ArrowRight' && !event.shiftKey) {
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
placeCaret(start + 1);
|
|
255
|
+
} else if ((event.key === 'ArrowUp' || event.key === 'Home') && !event.shiftKey) {
|
|
256
|
+
event.preventDefault();
|
|
257
|
+
placeCaret(0);
|
|
258
|
+
} else if ((event.key === 'ArrowDown' || event.key === 'End') && !event.shiftKey) {
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
placeCaret(value.length);
|
|
261
|
+
} else if (event.key === 'Backspace' && single) {
|
|
262
|
+
/*
|
|
263
|
+
* Backspace clears the box under the caret and steps back; on an
|
|
264
|
+
* empty box it clears the one before, as the boxes suggest.
|
|
265
|
+
*/
|
|
266
|
+
event.preventDefault();
|
|
267
|
+
const index = start < value.length ? start : start - 1;
|
|
268
|
+
if (index < 0) return;
|
|
269
|
+
commit(value.slice(0, index) + value.slice(index + 1), Math.max(0, start - 1));
|
|
270
|
+
} else if (event.key === 'Delete' && single) {
|
|
271
|
+
event.preventDefault();
|
|
272
|
+
if (start >= value.length) return;
|
|
273
|
+
commit(value.slice(0, start) + value.slice(start + 1), start);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const handleSelect = (event: FormEvent<HTMLInputElement>) => {
|
|
278
|
+
onSelect?.(event);
|
|
279
|
+
const input = event.currentTarget;
|
|
280
|
+
const start = input.selectionStart ?? 0;
|
|
281
|
+
const end = input.selectionEnd ?? start;
|
|
282
|
+
if (start === end && start < value.length) {
|
|
283
|
+
input.setSelectionRange(start, start + 1);
|
|
284
|
+
setSelection([start, start + 1]);
|
|
285
|
+
} else {
|
|
286
|
+
setSelection([start, end]);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/*
|
|
291
|
+
* The input's own characters are invisible and do not line up with the boxes,
|
|
292
|
+
* so a click is mapped to the box under the pointer. It cannot land past the
|
|
293
|
+
* first empty box: a code has no gaps.
|
|
294
|
+
*/
|
|
295
|
+
const handleMouseDown = (event: MouseEvent<HTMLInputElement>) => {
|
|
296
|
+
onMouseDown?.(event);
|
|
297
|
+
if (event.defaultPrevented || isDisabled || event.button !== 0) return;
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
const hit = slotRefs.current.findIndex(slot => {
|
|
300
|
+
const rect = slot?.getBoundingClientRect();
|
|
301
|
+
return rect ? event.clientX <= rect.right : false;
|
|
302
|
+
});
|
|
303
|
+
inputRef.current?.focus();
|
|
304
|
+
placeCaret(hit === -1 ? length - 1 : hit);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const [selStart, selEnd] = selection;
|
|
308
|
+
// With the code full and the caret parked after it, the last box stays the active one.
|
|
309
|
+
const activeStart = selStart === selEnd && selStart >= length ? length - 1 : selStart;
|
|
310
|
+
const activeEnd = Math.max(selEnd, activeStart + 1);
|
|
311
|
+
|
|
312
|
+
return (
|
|
313
|
+
<div
|
|
314
|
+
className={cn(
|
|
315
|
+
'group/code-input relative inline-flex w-fit max-w-full',
|
|
316
|
+
gapBySize[size ?? 'md'],
|
|
317
|
+
isDisabled && 'cursor-not-allowed opacity-50',
|
|
318
|
+
className,
|
|
319
|
+
)}
|
|
320
|
+
data-complete={value.length === length || undefined}
|
|
321
|
+
data-disabled={isDisabled || undefined}
|
|
322
|
+
data-invalid={invalid || undefined}
|
|
323
|
+
data-slot="code-input"
|
|
324
|
+
ref={rowRef}
|
|
325
|
+
>
|
|
326
|
+
{Array.from({ length }, (_, index) => {
|
|
327
|
+
const char = value[index];
|
|
328
|
+
const active = focused && !isDisabled && index >= activeStart && index < activeEnd;
|
|
329
|
+
// The empty box the next character goes into shows a caret.
|
|
330
|
+
const caret = active && char === undefined && selStart === selEnd;
|
|
331
|
+
|
|
332
|
+
return (
|
|
333
|
+
<div
|
|
334
|
+
aria-hidden
|
|
335
|
+
className={codeInputSlotVariants({
|
|
336
|
+
size,
|
|
337
|
+
invalid,
|
|
338
|
+
disabled: isDisabled,
|
|
339
|
+
active,
|
|
340
|
+
})}
|
|
341
|
+
data-active={active || undefined}
|
|
342
|
+
data-filled={char !== undefined || undefined}
|
|
343
|
+
data-slot="code-input-slot"
|
|
344
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the boxes are positions, not items.
|
|
345
|
+
key={index}
|
|
346
|
+
ref={node => {
|
|
347
|
+
slotRefs.current[index] = node;
|
|
348
|
+
}}
|
|
349
|
+
>
|
|
350
|
+
{char !== undefined && (mask ? '•' : char)}
|
|
351
|
+
{caret && (
|
|
352
|
+
<span
|
|
353
|
+
className="h-4 w-px animate-pulse bg-neutral-900 motion-reduce:animate-none"
|
|
354
|
+
data-slot="code-input-caret"
|
|
355
|
+
/>
|
|
356
|
+
)}
|
|
357
|
+
</div>
|
|
358
|
+
);
|
|
359
|
+
})}
|
|
360
|
+
<input
|
|
361
|
+
autoComplete="one-time-code"
|
|
362
|
+
autoCorrect="off"
|
|
363
|
+
className={cn(
|
|
364
|
+
// Covers the row so every box is a click target, but draws nothing itself.
|
|
365
|
+
'absolute inset-0 h-full w-full bg-transparent text-transparent caret-transparent outline-none selection:bg-transparent',
|
|
366
|
+
// 16px keeps iOS Safari from zooming in when the field takes focus.
|
|
367
|
+
'text-[16px]',
|
|
368
|
+
'disabled:cursor-not-allowed',
|
|
369
|
+
inputClassName,
|
|
370
|
+
)}
|
|
371
|
+
data-slot="code-input-control"
|
|
372
|
+
inputMode={set.inputMode}
|
|
373
|
+
// No maxLength: the browser would cut an autofilled "123-456" to "123-45" before
|
|
374
|
+
// sanitise drops the dash; sanitise caps the length itself.
|
|
375
|
+
onBeforeInput={handleBeforeInput}
|
|
376
|
+
onBlur={event => {
|
|
377
|
+
setFocused(false);
|
|
378
|
+
onBlur?.(event);
|
|
379
|
+
}}
|
|
380
|
+
onChange={handleChange}
|
|
381
|
+
onFocus={event => {
|
|
382
|
+
setFocused(true);
|
|
383
|
+
// Keyboard focus lands on the first empty box, where the code continues.
|
|
384
|
+
placeCaret(value.length);
|
|
385
|
+
onFocus?.(event);
|
|
386
|
+
}}
|
|
387
|
+
onKeyDown={handleKeyDown}
|
|
388
|
+
onMouseDown={handleMouseDown}
|
|
389
|
+
onPaste={handlePaste}
|
|
390
|
+
onSelect={handleSelect}
|
|
391
|
+
pattern={`${set.pattern}{${length}}`}
|
|
392
|
+
ref={node => {
|
|
393
|
+
inputRef.current = node;
|
|
394
|
+
if (typeof ref === 'function') return ref(node);
|
|
395
|
+
if (ref) ref.current = node;
|
|
396
|
+
}}
|
|
397
|
+
spellCheck={false}
|
|
398
|
+
// A masked code is a password to assistive tech too, so a screen reader does not read it out.
|
|
399
|
+
type={mask ? 'password' : 'text'}
|
|
400
|
+
value={value}
|
|
401
|
+
{...control}
|
|
402
|
+
{...props}
|
|
403
|
+
/>
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
};
|