@eifi1/ui-kit 0.4.0 → 0.5.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 +32 -0
- package/README.md +498 -57
- package/dist/chart.d.ts +10 -0
- package/dist/chart.js +10 -0
- package/dist/chart.js.map +1 -0
- package/dist/components/account-settings.d.ts +11 -0
- package/dist/components/account-settings.js +19 -12
- package/dist/components/account-settings.js.map +1 -1
- package/dist/components/alert-banner.d.ts +10 -6
- package/dist/components/alert-banner.js +20 -16
- package/dist/components/alert-banner.js.map +1 -1
- package/dist/components/amount-input.d.ts +30 -4
- package/dist/components/amount-input.js +36 -14
- package/dist/components/amount-input.js.map +1 -1
- package/dist/components/calculator.d.ts +24 -7
- package/dist/components/calculator.js +37 -20
- package/dist/components/calculator.js.map +1 -1
- package/dist/components/chart-zoom.d.ts +156 -0
- package/dist/components/chart-zoom.js +262 -0
- package/dist/components/chart-zoom.js.map +1 -0
- package/dist/components/chart.d.ts +28 -3
- package/dist/components/chart.js +71 -32
- package/dist/components/chart.js.map +1 -1
- package/dist/components/checkbox.d.ts +51 -0
- package/dist/components/checkbox.js +123 -0
- package/dist/components/checkbox.js.map +1 -0
- package/dist/components/chip.d.ts +128 -0
- package/dist/components/chip.js +357 -0
- package/dist/components/chip.js.map +1 -0
- package/dist/components/choice-parts.d.ts +24 -0
- package/dist/components/choice-parts.js +18 -0
- package/dist/components/choice-parts.js.map +1 -0
- package/dist/components/combobox-core.d.ts +52 -9
- package/dist/components/combobox-core.js +91 -20
- package/dist/components/combobox-core.js.map +1 -1
- package/dist/components/combobox.d.ts +43 -33
- package/dist/components/combobox.js +409 -340
- package/dist/components/combobox.js.map +1 -1
- package/dist/components/currency-select.d.ts +48 -8
- package/dist/components/currency-select.js +107 -65
- package/dist/components/currency-select.js.map +1 -1
- package/dist/components/data-table-filter-popover.d.ts +15 -3
- package/dist/components/data-table-filter-popover.js +23 -17
- package/dist/components/data-table-filter-popover.js.map +1 -1
- package/dist/components/data-table-filters.d.ts +1 -1
- package/dist/components/data-table-filters.js +3 -2
- package/dist/components/data-table-filters.js.map +1 -1
- package/dist/components/data-table-labels.d.ts +53 -2
- package/dist/components/data-table-labels.js +31 -2
- package/dist/components/data-table-labels.js.map +1 -1
- package/dist/components/data-table-pagination.d.ts +3 -1
- package/dist/components/data-table-pagination.js +16 -10
- package/dist/components/data-table-pagination.js.map +1 -1
- package/dist/components/data-table-sort.js +1 -0
- package/dist/components/data-table-sort.js.map +1 -1
- package/dist/components/data-table.d.ts +1 -1
- package/dist/components/data-table.js +201 -85
- package/dist/components/data-table.js.map +1 -1
- package/dist/components/date-picker.d.ts +25 -9
- package/dist/components/date-picker.js +232 -94
- package/dist/components/date-picker.js.map +1 -1
- package/dist/components/dropdown.d.ts +96 -20
- package/dist/components/dropdown.js +77 -33
- package/dist/components/dropdown.js.map +1 -1
- package/dist/components/entity-combobox.d.ts +5 -4
- package/dist/components/entity-combobox.js +107 -76
- package/dist/components/entity-combobox.js.map +1 -1
- package/dist/components/facing-pair.d.ts +36 -0
- package/dist/components/facing-pair.js +40 -0
- package/dist/components/facing-pair.js.map +1 -0
- package/dist/components/field-sync.d.ts +164 -0
- package/dist/components/field-sync.js +264 -0
- package/dist/components/field-sync.js.map +1 -0
- package/dist/components/file-dropzone.d.ts +38 -6
- package/dist/components/file-dropzone.js +34 -9
- package/dist/components/file-dropzone.js.map +1 -1
- package/dist/components/full-bleed-dialog.d.ts +37 -26
- package/dist/components/full-bleed-dialog.js +20 -5
- package/dist/components/full-bleed-dialog.js.map +1 -1
- package/dist/components/grouped-picker.d.ts +21 -10
- package/dist/components/grouped-picker.js +66 -52
- package/dist/components/grouped-picker.js.map +1 -1
- package/dist/components/hover-menu.d.ts +20 -4
- package/dist/components/hover-menu.js +15 -4
- package/dist/components/hover-menu.js.map +1 -1
- package/dist/components/mini-calendar.d.ts +102 -14
- package/dist/components/mini-calendar.js +256 -70
- package/dist/components/mini-calendar.js.map +1 -1
- package/dist/components/modal.d.ts +9 -3
- package/dist/components/modal.js +34 -45
- package/dist/components/modal.js.map +1 -1
- package/dist/components/month-picker.d.ts +79 -0
- package/dist/components/month-picker.js +342 -0
- package/dist/components/month-picker.js.map +1 -0
- package/dist/components/multi-entity-combobox.d.ts +7 -6
- package/dist/components/multi-entity-combobox.js +112 -72
- package/dist/components/multi-entity-combobox.js.map +1 -1
- package/dist/components/multi-select.d.ts +13 -11
- package/dist/components/multi-select.js +161 -84
- package/dist/components/multi-select.js.map +1 -1
- package/dist/components/number-field.d.ts +92 -0
- package/dist/components/number-field.js +111 -0
- package/dist/components/number-field.js.map +1 -0
- package/dist/components/number-input.d.ts +28 -2
- package/dist/components/number-input.js +12 -2
- package/dist/components/number-input.js.map +1 -1
- package/dist/components/numpad-sheet.d.ts +32 -4
- package/dist/components/numpad-sheet.js +30 -13
- package/dist/components/numpad-sheet.js.map +1 -1
- package/dist/components/page-contents.d.ts +90 -0
- package/dist/components/page-contents.js +170 -0
- package/dist/components/page-contents.js.map +1 -0
- package/dist/components/password-strength.d.ts +106 -0
- package/dist/components/password-strength.js +132 -0
- package/dist/components/password-strength.js.map +1 -0
- package/dist/components/picker-sheet.d.ts +23 -15
- package/dist/components/picker-sheet.js +34 -14
- package/dist/components/picker-sheet.js.map +1 -1
- package/dist/components/popover.d.ts +62 -4
- package/dist/components/popover.js +52 -2
- package/dist/components/popover.js.map +1 -1
- package/dist/components/search-field.d.ts +25 -7
- package/dist/components/search-field.js +7 -5
- package/dist/components/search-field.js.map +1 -1
- package/dist/components/series-chart-labels.d.ts +14 -0
- package/dist/components/series-chart-labels.js +11 -0
- package/dist/components/series-chart-labels.js.map +1 -0
- package/dist/components/series-chart.d.ts +207 -0
- package/dist/components/series-chart.js +223 -0
- package/dist/components/series-chart.js.map +1 -0
- package/dist/components/settings-fields.d.ts +14 -4
- package/dist/components/settings-fields.js +19 -8
- package/dist/components/settings-fields.js.map +1 -1
- package/dist/components/signature-pad.d.ts +119 -0
- package/dist/components/signature-pad.js +405 -0
- package/dist/components/signature-pad.js.map +1 -0
- package/dist/components/slider.d.ts +80 -0
- package/dist/components/slider.js +155 -0
- package/dist/components/slider.js.map +1 -0
- package/dist/components/sparkline.d.ts +93 -0
- package/dist/components/sparkline.js +205 -0
- package/dist/components/sparkline.js.map +1 -0
- package/dist/components/stat-tile.d.ts +151 -0
- package/dist/components/stat-tile.js +236 -0
- package/dist/components/stat-tile.js.map +1 -0
- package/dist/components/swipeable-row.d.ts +25 -2
- package/dist/components/swipeable-row.js +31 -0
- package/dist/components/swipeable-row.js.map +1 -1
- package/dist/components/switch.d.ts +60 -0
- package/dist/components/switch.js +81 -0
- package/dist/components/switch.js.map +1 -0
- package/dist/components/time-input.d.ts +58 -0
- package/dist/components/time-input.js +50 -0
- package/dist/components/time-input.js.map +1 -0
- package/dist/components/toggle-group.d.ts +18 -3
- package/dist/components/toggle-group.js +10 -6
- package/dist/components/toggle-group.js.map +1 -1
- package/dist/components/toggle-legend.d.ts +115 -0
- package/dist/components/toggle-legend.js +114 -0
- package/dist/components/toggle-legend.js.map +1 -0
- package/dist/components/tooltip.d.ts +40 -11
- package/dist/components/tooltip.js +77 -20
- package/dist/components/tooltip.js.map +1 -1
- package/dist/components/treemap.d.ts +132 -0
- package/dist/components/treemap.js +179 -0
- package/dist/components/treemap.js.map +1 -0
- package/dist/components/ui.d.ts +159 -46
- package/dist/components/ui.js +220 -115
- package/dist/components/ui.js.map +1 -1
- package/dist/components/use-mobile-reveal.js +1 -0
- package/dist/components/use-mobile-reveal.js.map +1 -1
- package/dist/components/use-table-state.d.ts +1 -1
- package/dist/components/use-table-state.js +38 -17
- package/dist/components/use-table-state.js.map +1 -1
- package/dist/components/user-avatar.d.ts +15 -4
- package/dist/components/user-avatar.js +5 -4
- package/dist/components/user-avatar.js.map +1 -1
- package/dist/components/wizard-stepper.js +3 -2
- package/dist/components/wizard-stepper.js.map +1 -1
- package/dist/{data-table-filters-aXyf0Xub.d.ts → data-table-filters-DM1auUfH.d.ts} +27 -5
- package/dist/data-table.d.ts +7 -0
- package/dist/data-table.js +12 -0
- package/dist/data-table.js.map +1 -0
- package/dist/feedback/feedback-attachment.js +12 -11
- package/dist/feedback/feedback-attachment.js.map +1 -1
- package/dist/feedback/feedback-dialog.js +2 -1
- package/dist/feedback/feedback-dialog.js.map +1 -1
- package/dist/feedback/feedback-inbox.d.ts +33 -7
- package/dist/feedback/feedback-inbox.js +49 -37
- package/dist/feedback/feedback-inbox.js.map +1 -1
- package/dist/feedback.d.ts +5 -0
- package/dist/feedback.js +5 -0
- package/dist/feedback.js.map +1 -0
- package/dist/hooks/use-anchored-panel.js +1 -0
- package/dist/hooks/use-anchored-panel.js.map +1 -1
- package/dist/hooks/use-anchored-rect.js +1 -0
- package/dist/hooks/use-anchored-rect.js.map +1 -1
- package/dist/hooks/use-announce.d.ts +46 -0
- package/dist/hooks/use-announce.js +37 -0
- package/dist/hooks/use-announce.js.map +1 -0
- package/dist/hooks/use-body-scroll-lock.js +1 -0
- package/dist/hooks/use-body-scroll-lock.js.map +1 -1
- package/dist/hooks/use-close-transition.js +1 -0
- package/dist/hooks/use-close-transition.js.map +1 -1
- package/dist/hooks/use-dismiss.js +1 -0
- package/dist/hooks/use-dismiss.js.map +1 -1
- package/dist/hooks/use-focus-trap.d.ts +16 -0
- package/dist/hooks/use-focus-trap.js +67 -0
- package/dist/hooks/use-focus-trap.js.map +1 -0
- package/dist/hooks/use-media-query.js +1 -0
- package/dist/hooks/use-media-query.js.map +1 -1
- package/dist/hooks/use-overlay-history.js +27 -1
- package/dist/hooks/use-overlay-history.js.map +1 -1
- package/dist/hooks/use-row-swipe.js +1 -0
- package/dist/hooks/use-row-swipe.js.map +1 -1
- package/dist/i18n/defaults.d.ts +31 -0
- package/dist/i18n/defaults.js +63 -0
- package/dist/i18n/defaults.js.map +1 -0
- package/dist/i18n/kit-labels.d.ts +250 -0
- package/dist/i18n/kit-labels.js +184 -0
- package/dist/i18n/kit-labels.js.map +1 -0
- package/dist/index.d.ts +47 -22
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/calc.js +1 -0
- package/dist/lib/calc.js.map +1 -1
- package/dist/lib/cn.js +1 -0
- package/dist/lib/cn.js.map +1 -1
- package/dist/lib/dates.js +1 -0
- package/dist/lib/dates.js.map +1 -1
- package/dist/lib/logger.js +1 -0
- package/dist/lib/logger.js.map +1 -1
- package/dist/lib/safe-storage.js +1 -0
- package/dist/lib/safe-storage.js.map +1 -1
- package/dist/search/command-palette.d.ts +11 -1
- package/dist/search/command-palette.js +21 -15
- package/dist/search/command-palette.js.map +1 -1
- package/dist/search.d.ts +2 -0
- package/dist/search.js +3 -0
- package/dist/search.js.map +1 -0
- package/dist/shell/app-shell.d.ts +33 -4
- package/dist/shell/app-shell.js +275 -78
- package/dist/shell/app-shell.js.map +1 -1
- package/dist/shell/option-switcher-menu.js +3 -2
- package/dist/shell/option-switcher-menu.js.map +1 -1
- package/dist/shell/role-switcher.d.ts +6 -0
- package/dist/shell/role-switcher.js +7 -4
- package/dist/shell/role-switcher.js.map +1 -1
- package/dist/shell/top-bar.d.ts +15 -7
- package/dist/shell/top-bar.js +3 -5
- package/dist/shell/top-bar.js.map +1 -1
- package/dist/shell/topbar-action-menu.js +3 -2
- package/dist/shell/topbar-action-menu.js.map +1 -1
- package/dist/shell/topbar-controls.d.ts +5 -2
- package/dist/shell/topbar-controls.js +20 -15
- package/dist/shell/topbar-controls.js.map +1 -1
- package/dist/shell.d.ts +13 -0
- package/dist/shell.js +8 -0
- package/dist/shell.js.map +1 -0
- package/dist/theme/chart-palette.d.ts +19 -12
- package/dist/theme/chart-palette.js +24 -30
- package/dist/theme/chart-palette.js.map +1 -1
- package/dist/theme/color.d.ts +78 -0
- package/dist/theme/color.js +148 -0
- package/dist/theme/color.js.map +1 -0
- package/dist/theme/palette-derive.d.ts +140 -0
- package/dist/theme/palette-derive.js +376 -0
- package/dist/theme/palette-derive.js.map +1 -0
- package/dist/theme/palette-presets.d.ts +27 -5
- package/dist/theme/palette-presets.js +231 -33
- package/dist/theme/palette-presets.js.map +1 -1
- package/dist/theme/palette-store.js +1 -0
- package/dist/theme/palette-store.js.map +1 -1
- package/dist/theme/theme-store.js +1 -0
- package/dist/theme/theme-store.js.map +1 -1
- package/dist/tour/tour.d.ts +2 -1
- package/dist/tour/tour.js +15 -9
- package/dist/tour/tour.js.map +1 -1
- package/dist/tour.d.ts +2 -0
- package/dist/tour.js +3 -0
- package/dist/tour.js.map +1 -0
- package/dist/wizard/stepper-nav.js +10 -8
- package/dist/wizard/stepper-nav.js.map +1 -1
- package/dist/wizard/types.d.ts +18 -2
- package/dist/wizard/types.js +4 -1
- package/dist/wizard/types.js.map +1 -1
- package/dist/wizard/use-wizard.js +12 -4
- package/dist/wizard/use-wizard.js.map +1 -1
- package/dist/wizard/validation.js +1 -0
- package/dist/wizard/validation.js.map +1 -1
- package/dist/wizard/wizard-context.js +1 -0
- package/dist/wizard/wizard-context.js.map +1 -1
- package/dist/wizard/wizard-step.js +1 -0
- package/dist/wizard/wizard-step.js.map +1 -1
- package/dist/wizard/wizard-summary.js +5 -3
- package/dist/wizard/wizard-summary.js.map +1 -1
- package/dist/wizard.d.ts +8 -0
- package/dist/wizard.js +9 -0
- package/dist/wizard.js.map +1 -0
- package/package.json +67 -14
- package/src/chart.ts +22 -0
- package/src/components/account-settings.tsx +275 -0
- package/src/components/alert-banner.tsx +75 -0
- package/src/components/amount-input.tsx +471 -0
- package/src/components/calculator.tsx +252 -0
- package/src/components/chart-zoom.tsx +499 -0
- package/src/components/chart.tsx +363 -0
- package/src/components/checkbox.tsx +222 -0
- package/src/components/chip.tsx +570 -0
- package/src/components/choice-parts.ts +33 -0
- package/src/components/combobox-core.tsx +540 -0
- package/src/components/combobox.tsx +1026 -0
- package/src/components/currency-select.tsx +269 -0
- package/src/components/data-table-filter-popover.tsx +252 -0
- package/src/components/data-table-filters.ts +176 -0
- package/src/components/data-table-labels.ts +190 -0
- package/src/components/data-table-pagination.tsx +126 -0
- package/src/components/data-table-sort.ts +70 -0
- package/src/components/data-table.tsx +1740 -0
- package/src/components/date-picker.tsx +631 -0
- package/src/components/dropdown.tsx +341 -0
- package/src/components/entity-combobox.tsx +215 -0
- package/src/components/facing-pair.ts +86 -0
- package/src/components/field-sync.tsx +472 -0
- package/src/components/file-dropzone.tsx +181 -0
- package/src/components/full-bleed-dialog.tsx +179 -0
- package/src/components/grouped-picker.tsx +157 -0
- package/src/components/hover-menu.tsx +183 -0
- package/src/components/mini-calendar.tsx +554 -0
- package/src/components/modal.tsx +287 -0
- package/src/components/month-picker.tsx +549 -0
- package/src/components/multi-entity-combobox.tsx +226 -0
- package/src/components/multi-select.tsx +303 -0
- package/src/components/number-field.tsx +225 -0
- package/src/components/number-input.tsx +211 -0
- package/src/components/numpad-sheet.tsx +264 -0
- package/src/components/page-contents.tsx +289 -0
- package/src/components/password-strength.tsx +258 -0
- package/src/components/picker-sheet.tsx +251 -0
- package/src/components/popover.tsx +195 -0
- package/src/components/search-field.tsx +146 -0
- package/src/components/series-chart-labels.ts +26 -0
- package/src/components/series-chart.tsx +481 -0
- package/src/components/settings-fields.tsx +68 -0
- package/src/components/signature-pad.tsx +630 -0
- package/src/components/slider.tsx +313 -0
- package/src/components/sparkline.tsx +329 -0
- package/src/components/stat-tile.tsx +448 -0
- package/src/components/swipeable-row.tsx +271 -0
- package/src/components/switch.tsx +170 -0
- package/src/components/time-input.tsx +107 -0
- package/src/components/toggle-group.tsx +153 -0
- package/src/components/toggle-legend.tsx +229 -0
- package/src/components/tooltip.tsx +477 -0
- package/src/components/treemap.tsx +390 -0
- package/src/components/ui.tsx +1284 -0
- package/src/components/use-mobile-reveal.ts +54 -0
- package/src/components/use-table-state.ts +295 -0
- package/src/components/user-avatar.tsx +61 -0
- package/src/components/wizard-stepper.tsx +37 -0
- package/src/data-table.ts +19 -0
- package/src/feedback/feedback-attachment.tsx +246 -0
- package/src/feedback/feedback-dialog.tsx +209 -0
- package/src/feedback/feedback-inbox.tsx +615 -0
- package/src/feedback.ts +8 -0
- package/src/hooks/use-anchored-panel.ts +169 -0
- package/src/hooks/use-anchored-rect.ts +78 -0
- package/src/hooks/use-announce.ts +85 -0
- package/src/hooks/use-body-scroll-lock.ts +94 -0
- package/src/hooks/use-close-transition.ts +99 -0
- package/src/hooks/use-dismiss.ts +77 -0
- package/src/hooks/use-focus-trap.ts +160 -0
- package/src/hooks/use-media-query.ts +23 -0
- package/src/hooks/use-overlay-history.ts +345 -0
- package/src/hooks/use-row-swipe.ts +212 -0
- package/src/i18n/defaults.ts +68 -0
- package/src/i18n/kit-labels.tsx +447 -0
- package/src/index.ts +169 -0
- package/src/lib/calc.ts +309 -0
- package/src/lib/cn.ts +5 -0
- package/src/lib/dates.ts +187 -0
- package/src/lib/logger.ts +73 -0
- package/src/lib/safe-storage.ts +37 -0
- package/src/search/command-palette.tsx +342 -0
- package/src/search.ts +3 -0
- package/src/shell/app-shell.tsx +772 -0
- package/src/shell/option-switcher-menu.tsx +72 -0
- package/src/shell/role-switcher.tsx +46 -0
- package/src/shell/top-bar.tsx +39 -0
- package/src/shell/topbar-action-menu.tsx +91 -0
- package/src/shell/topbar-controls.tsx +229 -0
- package/src/shell.ts +10 -0
- package/src/theme/chart-palette.ts +145 -0
- package/src/theme/color.ts +266 -0
- package/src/theme/palette-derive.ts +681 -0
- package/src/theme/palette-presets.ts +760 -0
- package/src/theme/palette-store.ts +94 -0
- package/src/theme/theme-store.ts +145 -0
- package/src/tour/tour.tsx +586 -0
- package/src/tour.ts +3 -0
- package/src/vite-env.d.ts +26 -0
- package/src/wizard/stepper-nav.tsx +221 -0
- package/src/wizard/types.ts +178 -0
- package/src/wizard/use-wizard.ts +345 -0
- package/src/wizard/validation.ts +37 -0
- package/src/wizard/wizard-context.tsx +29 -0
- package/src/wizard/wizard-step.tsx +14 -0
- package/src/wizard/wizard-summary.tsx +69 -0
- package/src/wizard.ts +14 -0
- package/tokens.css +222 -20
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { forwardRef, useId, useRef, useState } from "react";
|
|
2
|
+
import type { ComponentPropsWithoutRef, KeyboardEvent, ReactNode } from "react";
|
|
3
|
+
import { X } from "lucide-react";
|
|
4
|
+
import type { LucideIcon } from "lucide-react";
|
|
5
|
+
import { cn } from "../lib/cn";
|
|
6
|
+
import { FIELD_INVALID } from "./ui";
|
|
7
|
+
import { DEFAULT_COMMON_LABELS, useKitLabels, useKitLocale } from "../i18n/kit-labels";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A chip: a compact pill carrying one value.
|
|
11
|
+
*
|
|
12
|
+
* Three jobs, deliberately one component. A chip can be inert (a tag on a row), a link
|
|
13
|
+
* (a jump to a section), or a toggle (a filter that is on or off) — and the difference is
|
|
14
|
+
* which prop you pass, not which component you pick. Keeping them together is what stops
|
|
15
|
+
* an app growing three near-identical pills that drift apart; it is the shape this kit
|
|
16
|
+
* already uses for `Button`, which is a link when it is given an href.
|
|
17
|
+
*
|
|
18
|
+
* A chip is not a `Button`. It is smaller, it is rounded to a pill, and it reads as a
|
|
19
|
+
* VALUE rather than as an action — which matters because a row of eight buttons says
|
|
20
|
+
* "choose one of eight things to do" and a row of eight chips says "here are eight
|
|
21
|
+
* things". Using `Button` for both is the mistake this exists to prevent.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type ChipTone = "neutral" | "brand" | "danger" | "warning" | "success" | "info";
|
|
25
|
+
export type ChipSize = "sm" | "md";
|
|
26
|
+
|
|
27
|
+
const TONE: Record<ChipTone, { idle: string; selected: string }> = {
|
|
28
|
+
neutral: {
|
|
29
|
+
idle: "border-[var(--border)] bg-[var(--bg-surface-2)] text-[var(--text-secondary)]",
|
|
30
|
+
selected: "border-[var(--border-strong)] bg-[var(--bg-active)] text-[var(--text-primary)]",
|
|
31
|
+
},
|
|
32
|
+
brand: {
|
|
33
|
+
idle: "border-[var(--border)] bg-[var(--brand-bg)] text-[var(--brand-muted)]",
|
|
34
|
+
selected: "border-[var(--brand)] bg-[var(--brand)] text-[var(--brand-contrast)]",
|
|
35
|
+
},
|
|
36
|
+
danger: {
|
|
37
|
+
idle: "border-[var(--danger-border)] bg-[var(--danger-bg)] text-[var(--danger)]",
|
|
38
|
+
selected: "border-[var(--danger)] bg-[var(--danger)] text-[var(--danger-contrast)]",
|
|
39
|
+
},
|
|
40
|
+
warning: {
|
|
41
|
+
idle: "border-[var(--warning-border)] bg-[var(--warning-bg)] text-[var(--warning)]",
|
|
42
|
+
selected: "border-[var(--warning)] bg-[var(--warning-bg)] text-[var(--warning)]",
|
|
43
|
+
},
|
|
44
|
+
success: {
|
|
45
|
+
idle: "border-[var(--success-border)] bg-[var(--success-bg)] text-[var(--success)]",
|
|
46
|
+
selected: "border-[var(--success)] bg-[var(--success-bg)] text-[var(--success)]",
|
|
47
|
+
},
|
|
48
|
+
info: {
|
|
49
|
+
idle: "border-[var(--info-border)] bg-[var(--info-bg)] text-[var(--info)]",
|
|
50
|
+
selected: "border-[var(--info)] bg-[var(--info-bg)] text-[var(--info)]",
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const SIZE: Record<ChipSize, { body: string; icon: string; remove: string }> = {
|
|
55
|
+
sm: { body: "gap-1 px-2 py-0.5 text-xs", icon: "size-3", remove: "size-3" },
|
|
56
|
+
md: { body: "gap-1.5 px-2.5 py-1 text-sm", icon: "size-3.5", remove: "size-3.5" },
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const CHIP_BASE =
|
|
60
|
+
"inline-flex max-w-full items-center rounded-full border transition-colors " +
|
|
61
|
+
// `focus-visible`, not `focus`: a chip commonly receives focus programmatically (the
|
|
62
|
+
// ChipInput moves focus onto one after a removal) and a ring that appears on a
|
|
63
|
+
// pointer click reads as a stuck selection.
|
|
64
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--brand)] focus-visible:ring-offset-1 focus-visible:ring-offset-[var(--bg-surface)]";
|
|
65
|
+
|
|
66
|
+
export interface ChipProps {
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
tone?: ChipTone;
|
|
69
|
+
size?: ChipSize;
|
|
70
|
+
icon?: LucideIcon;
|
|
71
|
+
/** Marks the chip as the current one — `aria-current` on a link, `aria-pressed` on a toggle. */
|
|
72
|
+
selected?: boolean;
|
|
73
|
+
/** Renders the chip as a link. Mutually exclusive with `onClick`. */
|
|
74
|
+
href?: string;
|
|
75
|
+
/** Renders the chip as a toggle button. Mutually exclusive with `href`. */
|
|
76
|
+
onClick?: () => void;
|
|
77
|
+
/** Renders a dismiss affordance. Works alongside `href`/`onClick` — see the note below. */
|
|
78
|
+
onRemove?: () => void;
|
|
79
|
+
/** Accessible name for the dismiss button. Default: `common.remove` from the
|
|
80
|
+
* {@link UiKitProvider}, else "Remove". */
|
|
81
|
+
removeLabel?: string;
|
|
82
|
+
disabled?: boolean;
|
|
83
|
+
className?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Roving focus, for a container that manages a row of chips (see {@link ChipInput}).
|
|
86
|
+
* A chip with no `href`/`onClick` is inert and unfocusable by default; give it a
|
|
87
|
+
* `tabIndex` and a `role` and it becomes the thing arrow keys move between.
|
|
88
|
+
*/
|
|
89
|
+
tabIndex?: number;
|
|
90
|
+
role?: string;
|
|
91
|
+
onKeyDown?: (e: KeyboardEvent<HTMLElement>) => void;
|
|
92
|
+
onFocus?: () => void;
|
|
93
|
+
/** Accessible name, when the visible text is not the whole story. */
|
|
94
|
+
"aria-label"?: string;
|
|
95
|
+
/** Escape hatch for a test id, a `data-tour` anchor, or an `aria-describedby`. */
|
|
96
|
+
[key: `data-${string}`]: unknown;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Note on `onRemove` with `href`/`onClick`: a button cannot be nested inside a button or
|
|
101
|
+
* a link, so when both are present the chip renders a wrapper holding TWO siblings — the
|
|
102
|
+
* interactive body and the dismiss control. That is why the outer element is not always
|
|
103
|
+
* the interactive one, and why the focus ring is drawn on the body rather than the
|
|
104
|
+
* wrapper.
|
|
105
|
+
*/
|
|
106
|
+
export const Chip = forwardRef<HTMLElement, ChipProps>(function Chip(
|
|
107
|
+
{
|
|
108
|
+
children,
|
|
109
|
+
tone = "neutral",
|
|
110
|
+
size = "md",
|
|
111
|
+
icon: Icon,
|
|
112
|
+
selected = false,
|
|
113
|
+
href,
|
|
114
|
+
onClick,
|
|
115
|
+
onRemove,
|
|
116
|
+
removeLabel,
|
|
117
|
+
disabled = false,
|
|
118
|
+
className,
|
|
119
|
+
tabIndex,
|
|
120
|
+
role,
|
|
121
|
+
onKeyDown,
|
|
122
|
+
onFocus,
|
|
123
|
+
...rest
|
|
124
|
+
},
|
|
125
|
+
ref,
|
|
126
|
+
) {
|
|
127
|
+
const common = useKitLabels("common", DEFAULT_COMMON_LABELS, { remove: removeLabel });
|
|
128
|
+
const s = SIZE[size];
|
|
129
|
+
const palette = TONE[tone];
|
|
130
|
+
const interactive = !!href || !!onClick;
|
|
131
|
+
const look = cn(
|
|
132
|
+
CHIP_BASE,
|
|
133
|
+
s.body,
|
|
134
|
+
selected ? palette.selected : palette.idle,
|
|
135
|
+
interactive && !disabled && "cursor-pointer hover:brightness-[0.97] dark:hover:brightness-110",
|
|
136
|
+
disabled && "cursor-default opacity-50",
|
|
137
|
+
className,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const body = (
|
|
141
|
+
<>
|
|
142
|
+
{Icon && <Icon className={cn(s.icon, "shrink-0")} aria-hidden />}
|
|
143
|
+
<span className="min-w-0 truncate">{children}</span>
|
|
144
|
+
</>
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const remove = onRemove ? (
|
|
148
|
+
<button
|
|
149
|
+
type="button"
|
|
150
|
+
// The name carries WHAT is being removed, not just "Remove" — a screen-reader user
|
|
151
|
+
// tabbing a row of eight dismiss buttons otherwise hears the same word eight times
|
|
152
|
+
// with no way to tell which one they are on.
|
|
153
|
+
// Composed through `common.fieldValue` because the punctuation between the two
|
|
154
|
+
// halves is the language's, not ours. A chip whose content is not plain text
|
|
155
|
+
// has no value to name, and gets the bare verb rather than a dangling colon.
|
|
156
|
+
aria-label={
|
|
157
|
+
typeof children === "string" && children
|
|
158
|
+
? common.fieldValue(common.remove, children)
|
|
159
|
+
: common.remove
|
|
160
|
+
}
|
|
161
|
+
onClick={(e) => {
|
|
162
|
+
// The chip may itself be a link; removing it must not also follow it.
|
|
163
|
+
e.preventDefault();
|
|
164
|
+
e.stopPropagation();
|
|
165
|
+
onRemove();
|
|
166
|
+
}}
|
|
167
|
+
disabled={disabled}
|
|
168
|
+
className={cn(
|
|
169
|
+
"-mr-0.5 ml-0.5 shrink-0 rounded-full p-0.5 transition-colors",
|
|
170
|
+
"hover:bg-[var(--bg-active)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--brand)]",
|
|
171
|
+
disabled && "pointer-events-none",
|
|
172
|
+
)}
|
|
173
|
+
>
|
|
174
|
+
<X className={s.remove} aria-hidden />
|
|
175
|
+
</button>
|
|
176
|
+
) : null;
|
|
177
|
+
|
|
178
|
+
// ── The three shapes.
|
|
179
|
+
if (href && !disabled) {
|
|
180
|
+
const link = (
|
|
181
|
+
<a
|
|
182
|
+
ref={ref as React.Ref<HTMLAnchorElement>}
|
|
183
|
+
href={href}
|
|
184
|
+
aria-current={selected ? "true" : undefined}
|
|
185
|
+
className={cn(look, remove && "pr-1.5")}
|
|
186
|
+
{...rest}
|
|
187
|
+
>
|
|
188
|
+
{body}
|
|
189
|
+
{!remove && null}
|
|
190
|
+
</a>
|
|
191
|
+
);
|
|
192
|
+
return remove ? (
|
|
193
|
+
<span className="inline-flex items-center">
|
|
194
|
+
{link}
|
|
195
|
+
{remove}
|
|
196
|
+
</span>
|
|
197
|
+
) : (
|
|
198
|
+
link
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (onClick) {
|
|
203
|
+
const button = (
|
|
204
|
+
<button
|
|
205
|
+
ref={ref as React.Ref<HTMLButtonElement>}
|
|
206
|
+
type="button"
|
|
207
|
+
onClick={onClick}
|
|
208
|
+
disabled={disabled}
|
|
209
|
+
aria-pressed={selected}
|
|
210
|
+
className={cn(look, remove && "pr-1.5")}
|
|
211
|
+
{...rest}
|
|
212
|
+
>
|
|
213
|
+
{body}
|
|
214
|
+
</button>
|
|
215
|
+
);
|
|
216
|
+
return remove ? (
|
|
217
|
+
<span className="inline-flex items-center">
|
|
218
|
+
{button}
|
|
219
|
+
{remove}
|
|
220
|
+
</span>
|
|
221
|
+
) : (
|
|
222
|
+
button
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<span
|
|
228
|
+
ref={ref as React.Ref<HTMLSpanElement>}
|
|
229
|
+
className={look}
|
|
230
|
+
tabIndex={tabIndex}
|
|
231
|
+
role={role}
|
|
232
|
+
onKeyDown={onKeyDown}
|
|
233
|
+
onFocus={onFocus}
|
|
234
|
+
{...rest}
|
|
235
|
+
>
|
|
236
|
+
{body}
|
|
237
|
+
{remove}
|
|
238
|
+
</span>
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
/* ── ChipInput ────────────────────────────────────────────────────────────── */
|
|
243
|
+
|
|
244
|
+
export interface ChipInputLabels {
|
|
245
|
+
/** Announced after a value is added. Receives the value. */
|
|
246
|
+
added: (value: string) => string;
|
|
247
|
+
/** Announced after a value is removed. Receives the value. */
|
|
248
|
+
removed: (value: string) => string;
|
|
249
|
+
/** Accessible name for each chip's dismiss button. */
|
|
250
|
+
remove: string;
|
|
251
|
+
/** Shown when `max` is reached. Receives the limit. */
|
|
252
|
+
atLimit: (max: number) => string;
|
|
253
|
+
/** Shown when a duplicate is rejected. Receives the value. */
|
|
254
|
+
duplicate: (value: string) => string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export const DEFAULT_CHIP_INPUT_LABELS: ChipInputLabels = {
|
|
258
|
+
added: (v) => `${v} added`,
|
|
259
|
+
removed: (v) => `${v} removed`,
|
|
260
|
+
remove: "Remove",
|
|
261
|
+
atLimit: (max) => `Limit of ${max} reached`,
|
|
262
|
+
duplicate: (v) => `${v} is already in the list`,
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export function resolveChipInputLabels(labels?: Partial<ChipInputLabels>): ChipInputLabels {
|
|
266
|
+
return labels ? { ...DEFAULT_CHIP_INPUT_LABELS, ...labels } : DEFAULT_CHIP_INPUT_LABELS;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* `onChange` is the field's own — the whole LIST, not a DOM event — so the wrapper
|
|
271
|
+
* div's `onChange` is omitted rather than shadowed. `placeholder` is likewise this
|
|
272
|
+
* component's, forwarded to the inner `<input>`; the div has none of its own.
|
|
273
|
+
*/
|
|
274
|
+
export interface ChipInputProps
|
|
275
|
+
extends Omit<ComponentPropsWithoutRef<"div">, "onChange" | "placeholder" | "children"> {
|
|
276
|
+
value: string[];
|
|
277
|
+
onChange: (next: string[]) => void;
|
|
278
|
+
placeholder?: string;
|
|
279
|
+
/** Visible label, rendered above the field. Without one, name the field with
|
|
280
|
+
* `aria-label`. */
|
|
281
|
+
label?: string;
|
|
282
|
+
/**
|
|
283
|
+
* @deprecated Pass `aria-label` instead — the DOM spelling the rest of this kit
|
|
284
|
+
* now uses. Still honoured, and still second to `aria-label` when both are given.
|
|
285
|
+
*/
|
|
286
|
+
ariaLabel?: string;
|
|
287
|
+
/** Reject or rewrite a value on commit. Return a string to reject WITH that message. */
|
|
288
|
+
validate?: (raw: string) => string | null;
|
|
289
|
+
/** Characters that commit the current text, besides Enter. Default: comma. */
|
|
290
|
+
separators?: string[];
|
|
291
|
+
allowDuplicates?: boolean;
|
|
292
|
+
max?: number;
|
|
293
|
+
tone?: ChipTone;
|
|
294
|
+
size?: ChipSize;
|
|
295
|
+
disabled?: boolean;
|
|
296
|
+
invalid?: boolean;
|
|
297
|
+
/** Error text. Wires `aria-describedby` and implies `invalid`. */
|
|
298
|
+
error?: ReactNode;
|
|
299
|
+
labels?: Partial<ChipInputLabels>;
|
|
300
|
+
className?: string;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* A field whose value is a LIST, entered as chips.
|
|
305
|
+
*
|
|
306
|
+
* The keyboard model is the whole component; the pills are the easy part. Per the WAI
|
|
307
|
+
* pattern for a "tag" input:
|
|
308
|
+
*
|
|
309
|
+
* Enter, or any separator commit the typed text
|
|
310
|
+
* Backspace on empty text move focus to the last chip (does NOT delete it)
|
|
311
|
+
* ← → while on a chip move between chips
|
|
312
|
+
* Backspace / Delete remove the focused chip, focus its neighbour
|
|
313
|
+
* Escape clear what is typed but keep the committed chips
|
|
314
|
+
* paste splits on the separators, so a pasted CSV becomes chips
|
|
315
|
+
*
|
|
316
|
+
* The Backspace-on-empty behaviour is the one worth arguing about. Deleting immediately
|
|
317
|
+
* is more common and is a trap: it is a destructive action with no feedback on a key
|
|
318
|
+
* people hit by reflex, and the value it destroys may have taken a while to type. Moving
|
|
319
|
+
* focus first makes the second Backspace a deliberate one, and it is what lets a
|
|
320
|
+
* keyboard user reach a chip at all.
|
|
321
|
+
*/
|
|
322
|
+
export function ChipInput({
|
|
323
|
+
value,
|
|
324
|
+
onChange,
|
|
325
|
+
placeholder,
|
|
326
|
+
label,
|
|
327
|
+
ariaLabel,
|
|
328
|
+
validate,
|
|
329
|
+
separators = [","],
|
|
330
|
+
allowDuplicates = false,
|
|
331
|
+
max,
|
|
332
|
+
tone = "neutral",
|
|
333
|
+
size = "md",
|
|
334
|
+
disabled = false,
|
|
335
|
+
invalid = false,
|
|
336
|
+
error,
|
|
337
|
+
labels,
|
|
338
|
+
className,
|
|
339
|
+
"aria-label": ariaLabelAttr,
|
|
340
|
+
...rest
|
|
341
|
+
}: ChipInputProps) {
|
|
342
|
+
// `labels` over the provider's `chipInput` over English — the same order
|
|
343
|
+
// `resolveChipInputLabels` gives outside a provider.
|
|
344
|
+
const text = useKitLabels("chipInput", DEFAULT_CHIP_INPUT_LABELS, labels);
|
|
345
|
+
// A batch of pasted values is announced as one list, joined the way the language
|
|
346
|
+
// joins a list in a sentence ("a, b and c added") — hence the long conjunction,
|
|
347
|
+
// where a trigger's summary would use the narrow one.
|
|
348
|
+
const locale = useKitLocale();
|
|
349
|
+
const [draft, setDraft] = useState("");
|
|
350
|
+
const [message, setMessage] = useState("");
|
|
351
|
+
const [focusedChip, setFocusedChip] = useState<number | null>(null);
|
|
352
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
353
|
+
const chipRefs = useRef<Array<HTMLElement | null>>([]);
|
|
354
|
+
const id = useId();
|
|
355
|
+
const listId = `${id}-list`;
|
|
356
|
+
const errorId = `${id}-error`;
|
|
357
|
+
const isInvalid = invalid || !!error;
|
|
358
|
+
|
|
359
|
+
const focusChip = (index: number | null) => {
|
|
360
|
+
setFocusedChip(index);
|
|
361
|
+
if (index === null) inputRef.current?.focus();
|
|
362
|
+
else chipRefs.current[index]?.focus();
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Try to add every candidate, as ONE change.
|
|
367
|
+
*
|
|
368
|
+
* Batched rather than looped for a reason that bites immediately: `commit` closes over
|
|
369
|
+
* `value`, so calling it once per pasted item makes every call compute its result from
|
|
370
|
+
* the list as it was BEFORE the paste, and each `onChange` overwrites the last. A
|
|
371
|
+
* pasted "one,two,three" left only "three". Accumulating locally and emitting once is
|
|
372
|
+
* also the correct behaviour for an undo stack — a paste is one action.
|
|
373
|
+
*/
|
|
374
|
+
const commitMany = (raws: string[]): { added: string[]; rejection: string | null } => {
|
|
375
|
+
const next = [...value];
|
|
376
|
+
const added: string[] = [];
|
|
377
|
+
let rejection: string | null = null;
|
|
378
|
+
|
|
379
|
+
for (const raw of raws) {
|
|
380
|
+
const candidate = raw.trim();
|
|
381
|
+
if (!candidate) continue;
|
|
382
|
+
if (max !== undefined && next.length >= max) {
|
|
383
|
+
rejection ??= text.atLimit(max);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
if (!allowDuplicates && next.includes(candidate)) {
|
|
387
|
+
rejection ??= text.duplicate(candidate);
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
const invalidReason = validate?.(candidate);
|
|
391
|
+
if (invalidReason) {
|
|
392
|
+
rejection ??= invalidReason;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
next.push(candidate);
|
|
396
|
+
added.push(candidate);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (added.length) onChange(next);
|
|
400
|
+
// A rejection is the more useful thing to say: "three added" is obvious from the
|
|
401
|
+
// chips appearing, whereas "already in the list" is the only sign of what did not.
|
|
402
|
+
const addedList = new Intl.ListFormat(locale, { type: "conjunction", style: "long" });
|
|
403
|
+
setMessage(rejection ?? (added.length ? text.added(addedList.format(added)) : ""));
|
|
404
|
+
return { added, rejection };
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const commit = (raw: string): boolean => commitMany([raw]).added.length > 0;
|
|
408
|
+
|
|
409
|
+
const removeAt = (index: number) => {
|
|
410
|
+
const removed = value[index];
|
|
411
|
+
onChange(value.filter((_, i) => i !== index));
|
|
412
|
+
setMessage(text.removed(removed));
|
|
413
|
+
// Focus the neighbour that takes its place, or the input when the list empties —
|
|
414
|
+
// dropping focus to <body> here is how a keyboard user loses the field entirely.
|
|
415
|
+
const next = index >= value.length - 1 ? null : index;
|
|
416
|
+
requestAnimationFrame(() => focusChip(value.length - 1 === 0 ? null : next));
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const onInputKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
420
|
+
if (e.key === "Enter") {
|
|
421
|
+
// Only when there is something to commit: an empty Enter belongs to the form.
|
|
422
|
+
if (draft.trim()) {
|
|
423
|
+
e.preventDefault();
|
|
424
|
+
if (commit(draft)) setDraft("");
|
|
425
|
+
}
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (separators.includes(e.key)) {
|
|
429
|
+
e.preventDefault();
|
|
430
|
+
if (commit(draft)) setDraft("");
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (e.key === "Escape" && draft) {
|
|
434
|
+
e.preventDefault();
|
|
435
|
+
setDraft("");
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (e.key === "Backspace" && !draft && value.length) {
|
|
439
|
+
e.preventDefault();
|
|
440
|
+
focusChip(value.length - 1);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const onChipKeyDown = (e: KeyboardEvent<HTMLElement>, index: number) => {
|
|
445
|
+
if (e.key === "Backspace" || e.key === "Delete") {
|
|
446
|
+
e.preventDefault();
|
|
447
|
+
removeAt(index);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (e.key === "ArrowLeft" && index > 0) {
|
|
451
|
+
e.preventDefault();
|
|
452
|
+
focusChip(index - 1);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (e.key === "ArrowRight") {
|
|
456
|
+
e.preventDefault();
|
|
457
|
+
focusChip(index === value.length - 1 ? null : index + 1);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (e.key === "Escape") {
|
|
461
|
+
e.preventDefault();
|
|
462
|
+
focusChip(null);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
return (
|
|
467
|
+
// The rest lands on the OUTER box — the thing a `data-tour` step or a test id
|
|
468
|
+
// wants to point at — while the accessible name below goes on the `<input>`,
|
|
469
|
+
// because that is what a screen-reader user actually lands in. Spreading the two
|
|
470
|
+
// to the same element would have named a decorative wrapper and left the field
|
|
471
|
+
// anonymous.
|
|
472
|
+
<div {...rest} className={cn("space-y-1", className)}>
|
|
473
|
+
{label && (
|
|
474
|
+
<label htmlFor={`${id}-input`} className="block text-sm text-[var(--text-secondary)]">
|
|
475
|
+
{label}
|
|
476
|
+
</label>
|
|
477
|
+
)}
|
|
478
|
+
<div
|
|
479
|
+
// `relative` so the live region below can be `sr-only` without escaping to the
|
|
480
|
+
// initial containing block and inflating the document height.
|
|
481
|
+
className={cn(
|
|
482
|
+
"relative flex flex-wrap items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--bg-surface)] px-2 py-1.5 shadow-sm",
|
|
483
|
+
"focus-within:border-[var(--brand)] focus-within:ring-1 focus-within:ring-[var(--brand)]",
|
|
484
|
+
disabled && "cursor-default bg-[var(--bg-surface-2)]",
|
|
485
|
+
isInvalid && FIELD_INVALID,
|
|
486
|
+
)}
|
|
487
|
+
onClick={() => !disabled && inputRef.current?.focus()}
|
|
488
|
+
>
|
|
489
|
+
{/* `display: contents` on the wrappers so the chips participate in the field's
|
|
490
|
+
own flex wrap rather than forming a nested row. The list semantics are
|
|
491
|
+
restored with explicit roles, because `contents` removes the ul/li boxes from
|
|
492
|
+
the accessibility tree in several engines. */}
|
|
493
|
+
{/* eslint-disable-next-line jsx-a11y/no-redundant-roles -- NOT redundant here: the
|
|
494
|
+
`contents` display below removes the ul and li boxes from the box tree, and
|
|
495
|
+
several engines drop the list semantics with them. The explicit role is what
|
|
496
|
+
puts "list, 3 items" back. */}
|
|
497
|
+
<ul id={listId} role="list" className="contents">
|
|
498
|
+
{value.map((v, i) => (
|
|
499
|
+
<li key={`${v}-${i}`} role="presentation" className="contents">
|
|
500
|
+
<Chip
|
|
501
|
+
ref={(el: HTMLElement | null) => {
|
|
502
|
+
chipRefs.current[i] = el;
|
|
503
|
+
}}
|
|
504
|
+
tone={tone}
|
|
505
|
+
size={size}
|
|
506
|
+
disabled={disabled}
|
|
507
|
+
removeLabel={text.remove}
|
|
508
|
+
onRemove={disabled ? undefined : () => removeAt(i)}
|
|
509
|
+
// A ROVING tabIndex: only the focused chip is tabbable, so Tab leaves
|
|
510
|
+
// the field instead of walking through every value in it — the reason a
|
|
511
|
+
// list of twenty tags is otherwise a twenty-stop detour for a keyboard.
|
|
512
|
+
tabIndex={focusedChip === i ? 0 : -1}
|
|
513
|
+
role="listitem"
|
|
514
|
+
aria-label={v}
|
|
515
|
+
onKeyDown={(e) => onChipKeyDown(e, i)}
|
|
516
|
+
onFocus={() => setFocusedChip(i)}
|
|
517
|
+
data-chip-index={i}
|
|
518
|
+
>
|
|
519
|
+
{v}
|
|
520
|
+
</Chip>
|
|
521
|
+
</li>
|
|
522
|
+
))}
|
|
523
|
+
</ul>
|
|
524
|
+
<input
|
|
525
|
+
id={`${id}-input`}
|
|
526
|
+
ref={inputRef}
|
|
527
|
+
value={draft}
|
|
528
|
+
onChange={(e) => {
|
|
529
|
+
setDraft(e.target.value);
|
|
530
|
+
setMessage("");
|
|
531
|
+
}}
|
|
532
|
+
onKeyDown={onInputKeyDown}
|
|
533
|
+
onBlur={() => {
|
|
534
|
+
// Commit on blur: a value typed and then clicked away from is a value the
|
|
535
|
+
// user meant to add, and losing it silently is the commonest complaint
|
|
536
|
+
// about this pattern.
|
|
537
|
+
if (draft.trim() && commit(draft)) setDraft("");
|
|
538
|
+
}}
|
|
539
|
+
onPaste={(e) => {
|
|
540
|
+
const pasted = e.clipboardData.getData("text");
|
|
541
|
+
const pattern = new RegExp(`[${separators.map((s) => `\\${s}`).join("")}\n\r]`);
|
|
542
|
+
if (!pattern.test(pasted)) return;
|
|
543
|
+
e.preventDefault();
|
|
544
|
+
if (commitMany(pasted.split(pattern)).added.length) setDraft("");
|
|
545
|
+
}}
|
|
546
|
+
placeholder={value.length ? undefined : placeholder}
|
|
547
|
+
disabled={disabled}
|
|
548
|
+
// A visible `label` is already wired through `htmlFor`, so adding a name
|
|
549
|
+
// here would give the field two. Failing that, the DOM spelling wins and
|
|
550
|
+
// the deprecated `ariaLabel` is the fallback.
|
|
551
|
+
aria-label={label ? undefined : (ariaLabelAttr ?? ariaLabel)}
|
|
552
|
+
aria-describedby={error ? errorId : undefined}
|
|
553
|
+
aria-invalid={isInvalid || undefined}
|
|
554
|
+
className="min-w-[6rem] flex-1 bg-transparent py-0.5 text-sm text-[var(--text-primary)] outline-none placeholder:text-[var(--text-placeholder)] disabled:cursor-default"
|
|
555
|
+
/>
|
|
556
|
+
{/* Additions, removals and rejections move no focus, so nothing would announce
|
|
557
|
+
them. `sr-only-fixed` rather than `sr-only`: this sits inside a consumer's
|
|
558
|
+
markup and must not extend the document height. */}
|
|
559
|
+
<span role="status" aria-live="polite" aria-atomic className="sr-only-fixed">
|
|
560
|
+
{message}
|
|
561
|
+
</span>
|
|
562
|
+
</div>
|
|
563
|
+
{error && (
|
|
564
|
+
<p id={errorId} className="text-xs text-[var(--danger)]">
|
|
565
|
+
{error}
|
|
566
|
+
</p>
|
|
567
|
+
)}
|
|
568
|
+
</div>
|
|
569
|
+
);
|
|
570
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ReactNode, Ref } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Small pieces the native choice controls (`Checkbox`, `Switch`) share. Private to
|
|
5
|
+
* the package — not re-exported from the barrel.
|
|
6
|
+
*
|
|
7
|
+
* `mergeDescribedBy` and `hasMessage` restate two rules ui.tsx's `useFieldError`
|
|
8
|
+
* keeps for Input/Select/Textarea; that hook is module-private and shaped around a
|
|
9
|
+
* single error node, while these controls also describe themselves with a
|
|
10
|
+
* `description`, so they compose the ids themselves under the same rules.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Ids to describe the control by, merged in reading order: whatever the caller
|
|
14
|
+
* already pointed at (standing advice), then the description, then the error (the
|
|
15
|
+
* news). Merge, never replace — the rule ui.tsx's `useFieldError` keeps for Input.
|
|
16
|
+
* An empty list is `undefined`, never `""`: a dangling attribute describes the
|
|
17
|
+
* control as nothing at all. */
|
|
18
|
+
export function mergeDescribedBy(...ids: Array<string | false | null | undefined>): string | undefined {
|
|
19
|
+
const list = ids.filter(Boolean);
|
|
20
|
+
return list.length ? list.join(" ") : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `null`, `false` and `""` are what `touched && errors.x` evaluates to on the happy
|
|
24
|
+
* path. None of them is a message. */
|
|
25
|
+
export function hasMessage(node: ReactNode): boolean {
|
|
26
|
+
return node !== undefined && node !== null && node !== false && node !== "";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Point a forwarded ref and a local one at the same node. */
|
|
30
|
+
export function assignRef<T>(ref: Ref<T> | undefined, node: T | null) {
|
|
31
|
+
if (typeof ref === "function") ref(node);
|
|
32
|
+
else if (ref) (ref as { current: T | null }).current = node;
|
|
33
|
+
}
|