@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,472 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { AlertCircle, Check, Loader2, Pencil, PencilLine } from "lucide-react";
|
|
4
|
+
import { cn } from "../lib/cn";
|
|
5
|
+
import { Tooltip } from "./tooltip";
|
|
6
|
+
import { useKitLabels } from "../i18n/kit-labels";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Sync state for a field backed by a database row.
|
|
10
|
+
*
|
|
11
|
+
* The four states a value can be in between the keyboard and the server:
|
|
12
|
+
*
|
|
13
|
+
* synced the draft equals what was last persisted
|
|
14
|
+
* edited the draft differs and nothing has been sent yet
|
|
15
|
+
* pending a save is in flight
|
|
16
|
+
* error the last save was rejected; `error` carries why
|
|
17
|
+
*
|
|
18
|
+
* `useFieldSync` is the engine and holds no opinion about markup;
|
|
19
|
+
* `FieldSyncIndicator` is the affordance and holds no state. They are separate
|
|
20
|
+
* because the state is useful without the dot — a form can disable its submit
|
|
21
|
+
* button while any field is `pending`, and a router can block navigation while
|
|
22
|
+
* any field is `edited`.
|
|
23
|
+
*/
|
|
24
|
+
export type FieldSyncState = "synced" | "edited" | "pending" | "error";
|
|
25
|
+
|
|
26
|
+
export interface FieldSyncLabels {
|
|
27
|
+
synced: string;
|
|
28
|
+
edited: string;
|
|
29
|
+
pending: string;
|
|
30
|
+
/** Shown when the failure carries no message of its own. */
|
|
31
|
+
error: string;
|
|
32
|
+
retry: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const DEFAULT_FIELD_SYNC_LABELS: FieldSyncLabels = {
|
|
36
|
+
synced: "Saved",
|
|
37
|
+
edited: "Unsaved changes",
|
|
38
|
+
pending: "Saving…",
|
|
39
|
+
error: "Could not save",
|
|
40
|
+
retry: "Retry",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function resolveFieldSyncLabels(labels?: Partial<FieldSyncLabels>): FieldSyncLabels {
|
|
44
|
+
return labels ? { ...DEFAULT_FIELD_SYNC_LABELS, ...labels } : DEFAULT_FIELD_SYNC_LABELS;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface UseFieldSyncOptions<T> {
|
|
48
|
+
/** What the database currently holds. Changing it re-seeds a CLEAN field. */
|
|
49
|
+
value: T;
|
|
50
|
+
/** Persist `next`. Reject — or throw — to put the field in `error`. */
|
|
51
|
+
onSave: (next: T) => Promise<void> | void;
|
|
52
|
+
/**
|
|
53
|
+
* Quiet period before an edit saves itself, in ms. Default `0`: no auto-save at
|
|
54
|
+
* all, so `save()` is the only way to persist — called on blur by `FieldSyncRow`,
|
|
55
|
+
* and on Enter by the caller. Saving after a pause in typing wrote half-finished
|
|
56
|
+
* values to the database whenever somebody stopped to think; pass a positive
|
|
57
|
+
* number only for a field where that is genuinely wanted.
|
|
58
|
+
*/
|
|
59
|
+
debounceMs?: number;
|
|
60
|
+
/** How to tell two values apart. Defaults to `Object.is`. */
|
|
61
|
+
equals?: (a: T, b: T) => boolean;
|
|
62
|
+
/** Called once per rejected save, with whatever `onSave` threw. */
|
|
63
|
+
onError?: (error: Error) => void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface UseFieldSyncReturn<T> {
|
|
67
|
+
/** The draft — bind this to the input, not the `value` option. */
|
|
68
|
+
value: T;
|
|
69
|
+
setValue: (next: T) => void;
|
|
70
|
+
state: FieldSyncState;
|
|
71
|
+
/** Non-null exactly when `state === "error"`. */
|
|
72
|
+
error: Error | null;
|
|
73
|
+
/** True whenever the draft differs from the last persisted value. */
|
|
74
|
+
dirty: boolean;
|
|
75
|
+
/** Persist now, cancelling any pending debounce. A no-op on a clean field. */
|
|
76
|
+
save: () => void;
|
|
77
|
+
/** Send the current draft again after a failure. */
|
|
78
|
+
retry: () => void;
|
|
79
|
+
/** Throw the draft away and go back to the last persisted value. */
|
|
80
|
+
reset: () => void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function toError(cause: unknown): Error {
|
|
84
|
+
if (cause instanceof Error) return cause;
|
|
85
|
+
// An EMPTY message for a rejection that carried none, not an English one: the
|
|
86
|
+
// indicator shows `error.message` when there is one and the translated
|
|
87
|
+
// `fieldSync.error` otherwise, so a fallback message here was an English sentence
|
|
88
|
+
// that outranked the app's own translation. The original value stays on `cause`
|
|
89
|
+
// for whoever logs it from `onError`.
|
|
90
|
+
return new Error(typeof cause === "string" ? cause : "", { cause });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Track one database-backed field: a local draft, a debounced save, and the state
|
|
95
|
+
* of the round trip.
|
|
96
|
+
*
|
|
97
|
+
* Four things this handles that a `useState` + `useEffect` in a page does not:
|
|
98
|
+
*
|
|
99
|
+
* 1. **An edit during a save is never lost.** A save in flight is not cancelled
|
|
100
|
+
* and not raced — when it settles, the draft is compared again and sent once
|
|
101
|
+
* more if it moved. Cancelling instead would drop a keystroke that arrived
|
|
102
|
+
* mid-flight; firing immediately would let two writes land out of order.
|
|
103
|
+
* 2. **The server value is only adopted when the field is clean.** An external
|
|
104
|
+
* refresh that overwrote what somebody was typing would be a data-loss bug, so
|
|
105
|
+
* a dirty draft wins and keeps winning until it is saved or reset.
|
|
106
|
+
* 3. **Nothing is set after unmount.** The save resolves into a component that
|
|
107
|
+
* may be gone — a row scrolled out of a virtualised table, a closed dialog.
|
|
108
|
+
* 4. **`onSave` is read through a ref**, so an inline arrow — which every call
|
|
109
|
+
* site will pass — does not restart the debounce on every render.
|
|
110
|
+
*/
|
|
111
|
+
export function useFieldSync<T>({
|
|
112
|
+
value,
|
|
113
|
+
onSave,
|
|
114
|
+
debounceMs = 0,
|
|
115
|
+
equals = Object.is,
|
|
116
|
+
onError,
|
|
117
|
+
}: UseFieldSyncOptions<T>): UseFieldSyncReturn<T> {
|
|
118
|
+
const [draft, setDraft] = useState<T>(value);
|
|
119
|
+
const [state, setState] = useState<FieldSyncState>("synced");
|
|
120
|
+
const [error, setError] = useState<Error | null>(null);
|
|
121
|
+
|
|
122
|
+
// The last value known to be on the server. Not state: it changes together with
|
|
123
|
+
// `state` inside the save, and a second render for it would be a wasted one.
|
|
124
|
+
const savedRef = useRef<T>(value);
|
|
125
|
+
const draftRef = useRef<T>(value);
|
|
126
|
+
const inFlightRef = useRef(false);
|
|
127
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
128
|
+
const mountedRef = useRef(true);
|
|
129
|
+
|
|
130
|
+
// Latest-ref for the callbacks and comparators, so a caller passing inline
|
|
131
|
+
// functions (all of them) does not invalidate the timer on every render.
|
|
132
|
+
const onSaveRef = useRef(onSave);
|
|
133
|
+
const onErrorRef = useRef(onError);
|
|
134
|
+
const equalsRef = useRef(equals);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
onSaveRef.current = onSave;
|
|
137
|
+
onErrorRef.current = onError;
|
|
138
|
+
equalsRef.current = equals;
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
mountedRef.current = true;
|
|
143
|
+
return () => {
|
|
144
|
+
mountedRef.current = false;
|
|
145
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
146
|
+
};
|
|
147
|
+
}, []);
|
|
148
|
+
|
|
149
|
+
const flush = useCallback(async () => {
|
|
150
|
+
if (inFlightRef.current) return; // the settle path below picks the new draft up
|
|
151
|
+
const next = draftRef.current;
|
|
152
|
+
if (equalsRef.current(next, savedRef.current)) return;
|
|
153
|
+
|
|
154
|
+
inFlightRef.current = true;
|
|
155
|
+
setState("pending");
|
|
156
|
+
setError(null);
|
|
157
|
+
try {
|
|
158
|
+
await onSaveRef.current(next);
|
|
159
|
+
savedRef.current = next;
|
|
160
|
+
inFlightRef.current = false;
|
|
161
|
+
if (!mountedRef.current) return;
|
|
162
|
+
// The draft may have moved while this was in the air. Compare against what is
|
|
163
|
+
// on screen NOW, not against what was sent.
|
|
164
|
+
if (equalsRef.current(draftRef.current, next)) {
|
|
165
|
+
setState("synced");
|
|
166
|
+
} else {
|
|
167
|
+
setState("edited");
|
|
168
|
+
void flush();
|
|
169
|
+
}
|
|
170
|
+
} catch (cause) {
|
|
171
|
+
inFlightRef.current = false;
|
|
172
|
+
const err = toError(cause);
|
|
173
|
+
onErrorRef.current?.(err);
|
|
174
|
+
if (!mountedRef.current) return;
|
|
175
|
+
setState("error");
|
|
176
|
+
setError(err);
|
|
177
|
+
}
|
|
178
|
+
}, []);
|
|
179
|
+
|
|
180
|
+
const setValue = useCallback(
|
|
181
|
+
(next: T) => {
|
|
182
|
+
draftRef.current = next;
|
|
183
|
+
setDraft(next);
|
|
184
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
185
|
+
|
|
186
|
+
if (equalsRef.current(next, savedRef.current)) {
|
|
187
|
+
// Typed back to what the server already has. Nothing to send, and an error
|
|
188
|
+
// from the previous attempt no longer describes anything.
|
|
189
|
+
setState(inFlightRef.current ? "pending" : "synced");
|
|
190
|
+
setError(null);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
setState("edited");
|
|
195
|
+
setError(null);
|
|
196
|
+
if (debounceMs > 0) timerRef.current = setTimeout(() => void flush(), debounceMs);
|
|
197
|
+
},
|
|
198
|
+
[debounceMs, flush],
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const save = useCallback(() => {
|
|
202
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
203
|
+
void flush();
|
|
204
|
+
}, [flush]);
|
|
205
|
+
|
|
206
|
+
const retry = save;
|
|
207
|
+
|
|
208
|
+
const reset = useCallback(() => {
|
|
209
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
210
|
+
draftRef.current = savedRef.current;
|
|
211
|
+
setDraft(savedRef.current);
|
|
212
|
+
setState(inFlightRef.current ? "pending" : "synced");
|
|
213
|
+
setError(null);
|
|
214
|
+
}, []);
|
|
215
|
+
|
|
216
|
+
// Anything that is not `synced` has something not yet on the server: `edited` and
|
|
217
|
+
// `error` obviously, and `pending` too — the write is in the air, not landed.
|
|
218
|
+
// Derived from state rather than by comparing against `savedRef` during render,
|
|
219
|
+
// because a ref read here would not re-render when the comparison's answer changes.
|
|
220
|
+
const dirty = state !== "synced";
|
|
221
|
+
|
|
222
|
+
// Adopt a new server value ONLY when there is nothing local to lose.
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
if (inFlightRef.current) return;
|
|
225
|
+
if (!equalsRef.current(draftRef.current, savedRef.current)) return; // dirty: keep it
|
|
226
|
+
if (equalsRef.current(value, savedRef.current)) return; // unchanged
|
|
227
|
+
savedRef.current = value;
|
|
228
|
+
draftRef.current = value;
|
|
229
|
+
setDraft(value);
|
|
230
|
+
setState("synced");
|
|
231
|
+
setError(null);
|
|
232
|
+
}, [value]);
|
|
233
|
+
|
|
234
|
+
return { value: draft, setValue, state, error, dirty, save, retry, reset };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const STATE_STYLE: Record<FieldSyncState, { color: string; Icon: typeof Check }> = {
|
|
238
|
+
synced: { color: "var(--status-synced)", Icon: Check },
|
|
239
|
+
edited: { color: "var(--status-edited)", Icon: Pencil },
|
|
240
|
+
pending: { color: "var(--status-pending)", Icon: Loader2 },
|
|
241
|
+
error: { color: "var(--status-error)", Icon: AlertCircle },
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
export interface FieldSyncIndicatorProps {
|
|
245
|
+
state: FieldSyncState;
|
|
246
|
+
/** The failure. Its message becomes the tooltip when `state === "error"`. */
|
|
247
|
+
error?: Error | null;
|
|
248
|
+
/** Override any of the five English defaults. */
|
|
249
|
+
labels?: Partial<FieldSyncLabels>;
|
|
250
|
+
/** Render the label beside the icon instead of only in the tooltip. */
|
|
251
|
+
showLabel?: boolean;
|
|
252
|
+
/** When given, the error state renders a retry button after the icon. */
|
|
253
|
+
onRetry?: () => void;
|
|
254
|
+
className?: string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The affordance for {@link useFieldSync}: a coloured icon, and the reason on hover.
|
|
259
|
+
*
|
|
260
|
+
* **Every state has its own icon, and that is not decoration.** The four colours
|
|
261
|
+
* include the green/red pair, which is the one combination this package's palette
|
|
262
|
+
* otherwise refuses (see the note in `tokens.css`) because red-green colour blindness
|
|
263
|
+
* affects roughly one man in twelve. The icon is what carries the meaning for them,
|
|
264
|
+
* and what carries it into a greyscale print or a screenshot. WCAG 1.4.1 is the same
|
|
265
|
+
* requirement stated formally.
|
|
266
|
+
*
|
|
267
|
+
* The state is also announced: the wrapper is a polite live region, so a screen
|
|
268
|
+
* reader hears "Saving…" then "Saved" without the user going looking. `error` is
|
|
269
|
+
* assertive instead — a failed write is worth interrupting for.
|
|
270
|
+
*/
|
|
271
|
+
export function FieldSyncIndicator({
|
|
272
|
+
state,
|
|
273
|
+
error,
|
|
274
|
+
labels,
|
|
275
|
+
showLabel = false,
|
|
276
|
+
onRetry,
|
|
277
|
+
className,
|
|
278
|
+
}: FieldSyncIndicatorProps) {
|
|
279
|
+
const l = useKitLabels("fieldSync", DEFAULT_FIELD_SYNC_LABELS, labels);
|
|
280
|
+
const { color, Icon } = STATE_STYLE[state];
|
|
281
|
+
const text =
|
|
282
|
+
state === "error" ? (error?.message?.trim() ? error.message : l.error) : l[state];
|
|
283
|
+
|
|
284
|
+
return (
|
|
285
|
+
<span
|
|
286
|
+
// The ICON carries the status colour; the words are `--text-secondary`, because
|
|
287
|
+
// the amber of `edited` is a graphic colour (3:1), not a text colour (4.5:1).
|
|
288
|
+
className={cn("inline-flex items-center gap-1.5 text-xs text-[var(--text-secondary)]", className)}
|
|
289
|
+
// Polite for the three ordinary states so save chatter does not interrupt
|
|
290
|
+
// typing; assertive for a failure, which the user has to know about now.
|
|
291
|
+
role={state === "error" ? "alert" : "status"}
|
|
292
|
+
aria-live={state === "error" ? "assertive" : "polite"}
|
|
293
|
+
>
|
|
294
|
+
<Tooltip label={showLabel ? undefined : text} redact={state === "error"}>
|
|
295
|
+
<span className="inline-flex items-center gap-1.5">
|
|
296
|
+
<Icon
|
|
297
|
+
aria-hidden
|
|
298
|
+
style={{ color }}
|
|
299
|
+
className={cn("size-3.5 shrink-0", state === "pending" && "animate-spin")}
|
|
300
|
+
/>
|
|
301
|
+
{/* Always in the DOM, so the live region has something to announce even
|
|
302
|
+
when the label is visually a tooltip. */}
|
|
303
|
+
<span className={showLabel ? undefined : "sr-only"}>{text}</span>
|
|
304
|
+
</span>
|
|
305
|
+
</Tooltip>
|
|
306
|
+
{state === "error" && onRetry && (
|
|
307
|
+
<button
|
|
308
|
+
type="button"
|
|
309
|
+
onClick={onRetry}
|
|
310
|
+
className="rounded underline underline-offset-2 hover:no-underline focus-visible:outline-2 focus-visible:outline-offset-2"
|
|
311
|
+
style={{ color }}
|
|
312
|
+
>
|
|
313
|
+
{l.retry}
|
|
314
|
+
</button>
|
|
315
|
+
)}
|
|
316
|
+
</span>
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The field's own frame, coloured by state — applied from the WRAPPER to whatever
|
|
322
|
+
* native control sits inside it, so {@link FieldSyncRow} works with any of the kit's
|
|
323
|
+
* inputs (and a caller's own) without each one growing a `syncState` prop.
|
|
324
|
+
*
|
|
325
|
+
* Border AND a 1px ring, for the reason {@link FIELD_INVALID} gives: a lone 1px
|
|
326
|
+
* border loses a vertical edge to sub-pixel spread at 125% display scaling. The
|
|
327
|
+
* `:focus` twins are what keep the state visible while the reader is typing —
|
|
328
|
+
* the base field class's `focus:` brand colour is `(0,2,0)` and would otherwise win
|
|
329
|
+
* over a descendant selector exactly when the field is in use.
|
|
330
|
+
*
|
|
331
|
+
* `synced` is green, but {@link FieldSyncRow} paints it only for a moment after a
|
|
332
|
+
* save lands — a form of eight permanently green boxes says nothing the absence of
|
|
333
|
+
* colour does not.
|
|
334
|
+
*
|
|
335
|
+
* Every class is a literal so Tailwind's scanner can see it.
|
|
336
|
+
*/
|
|
337
|
+
export const FIELD_SYNC_FRAME: Record<FieldSyncState, string> = {
|
|
338
|
+
synced:
|
|
339
|
+
"[&_:is(input,select,textarea)]:border-[var(--status-synced)] [&_:is(input,select,textarea)]:ring-1 [&_:is(input,select,textarea)]:ring-[var(--status-synced)] [&_:is(input,select,textarea):focus]:border-[var(--status-synced)] [&_:is(input,select,textarea):focus]:ring-[var(--status-synced)]",
|
|
340
|
+
edited:
|
|
341
|
+
"[&_:is(input,select,textarea)]:border-[var(--status-edited)] [&_:is(input,select,textarea)]:ring-1 [&_:is(input,select,textarea)]:ring-[var(--status-edited)] [&_:is(input,select,textarea):focus]:border-[var(--status-edited)] [&_:is(input,select,textarea):focus]:ring-[var(--status-edited)]",
|
|
342
|
+
pending:
|
|
343
|
+
"[&_:is(input,select,textarea)]:border-[var(--status-pending)] [&_:is(input,select,textarea)]:ring-1 [&_:is(input,select,textarea)]:ring-[var(--status-pending)] [&_:is(input,select,textarea):focus]:border-[var(--status-pending)] [&_:is(input,select,textarea):focus]:ring-[var(--status-pending)]",
|
|
344
|
+
error:
|
|
345
|
+
"[&_:is(input,select,textarea)]:border-[var(--status-error)] [&_:is(input,select,textarea)]:ring-1 [&_:is(input,select,textarea)]:ring-[var(--status-error)] [&_:is(input,select,textarea):focus]:border-[var(--status-error)] [&_:is(input,select,textarea):focus]:ring-[var(--status-error)]",
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** How long the green "saved" frame and check stay after a save lands, in ms. */
|
|
349
|
+
export const FIELD_SYNC_SAVED_MS = 1500;
|
|
350
|
+
|
|
351
|
+
export interface FieldSyncRowProps<T> extends Omit<FieldSyncIndicatorProps, "state" | "error" | "showLabel"> {
|
|
352
|
+
sync: UseFieldSyncReturn<T>;
|
|
353
|
+
children: ReactNode;
|
|
354
|
+
/** Call `sync.save()` when focus leaves the field. Default true — together with the
|
|
355
|
+
* hook's default `debounceMs: 0` this is "save on blur": nothing is written while
|
|
356
|
+
* the reader is still typing, however long they pause. */
|
|
357
|
+
saveOnBlur?: boolean;
|
|
358
|
+
/** How long the saved confirmation shows. Default {@link FIELD_SYNC_SAVED_MS}. */
|
|
359
|
+
savedMs?: number;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* A field wearing its sync state — as its FRAME COLOUR and an ICON AT THE END OF THE
|
|
364
|
+
* FIELD, so the field never changes size (a helper line appearing under it moved
|
|
365
|
+
* everything below it on every save):
|
|
366
|
+
*
|
|
367
|
+
* edited amber frame, pencil — changed, not yet saved
|
|
368
|
+
* pending blue frame, spinning circle — for as long as the save is in flight
|
|
369
|
+
* synced green frame, check — for {@link FIELD_SYNC_SAVED_MS} after a
|
|
370
|
+
* save lands, then an ordinary field again
|
|
371
|
+
* error red frame, alert mark — hover or focus the mark for the reason;
|
|
372
|
+
* click it to retry
|
|
373
|
+
*
|
|
374
|
+
* It saves when focus LEAVES the field (`saveOnBlur`), not after a pause in typing: a
|
|
375
|
+
* database write in the middle of a word is a write nobody asked for. Enter in a
|
|
376
|
+
* single-line field is the caller's to wire to `sync.save`.
|
|
377
|
+
*
|
|
378
|
+
* Colour is never the only signal: every state has its own icon, and the state text is
|
|
379
|
+
* always in the DOM as a live region — polite for the ordinary states, assertive for
|
|
380
|
+
* a failure. The input gets end padding for the icon whether or not one is showing,
|
|
381
|
+
* so the text never reflows when the state changes.
|
|
382
|
+
*/
|
|
383
|
+
export function FieldSyncRow<T>({
|
|
384
|
+
sync,
|
|
385
|
+
children,
|
|
386
|
+
className,
|
|
387
|
+
labels,
|
|
388
|
+
onRetry,
|
|
389
|
+
saveOnBlur = true,
|
|
390
|
+
savedMs = FIELD_SYNC_SAVED_MS,
|
|
391
|
+
}: FieldSyncRowProps<T>) {
|
|
392
|
+
const l = useKitLabels("fieldSync", DEFAULT_FIELD_SYNC_LABELS, labels);
|
|
393
|
+
const { state, error } = sync;
|
|
394
|
+
const isError = state === "error";
|
|
395
|
+
const text = isError ? (error?.message?.trim() ? error.message : l.error) : l[state];
|
|
396
|
+
const retry = onRetry ?? sync.retry;
|
|
397
|
+
|
|
398
|
+
// The saved confirmation: on only for the moment after a save lands. Keyed on the
|
|
399
|
+
// TRANSITION pending → synced, so a field that starts synced shows nothing.
|
|
400
|
+
const previous = useRef(state);
|
|
401
|
+
const [justSaved, setJustSaved] = useState(false);
|
|
402
|
+
useEffect(() => {
|
|
403
|
+
const was = previous.current;
|
|
404
|
+
previous.current = state;
|
|
405
|
+
if (state === "synced" && was === "pending") {
|
|
406
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect -- reacts to the save landing
|
|
407
|
+
setJustSaved(true);
|
|
408
|
+
const t = setTimeout(() => setJustSaved(false), savedMs);
|
|
409
|
+
return () => clearTimeout(t);
|
|
410
|
+
}
|
|
411
|
+
if (state !== "synced") setJustSaved(false);
|
|
412
|
+
}, [state, savedMs]);
|
|
413
|
+
const shown: FieldSyncState | null = state === "synced" ? (justSaved ? "synced" : null) : state;
|
|
414
|
+
|
|
415
|
+
return (
|
|
416
|
+
<div
|
|
417
|
+
data-field-sync={state}
|
|
418
|
+
className={cn(
|
|
419
|
+
// `relative` for the icon and for the sr-only live region's containing block.
|
|
420
|
+
"relative [&_:is(input,select,textarea)]:pe-9",
|
|
421
|
+
shown && FIELD_SYNC_FRAME[shown],
|
|
422
|
+
className,
|
|
423
|
+
)}
|
|
424
|
+
onBlur={
|
|
425
|
+
saveOnBlur
|
|
426
|
+
? (e) => {
|
|
427
|
+
// Focus moving between two controls INSIDE the row is not leaving it.
|
|
428
|
+
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) sync.save();
|
|
429
|
+
}
|
|
430
|
+
: undefined
|
|
431
|
+
}
|
|
432
|
+
>
|
|
433
|
+
{children}
|
|
434
|
+
<span className="pointer-events-none absolute inset-y-0 end-0 flex items-center pe-3">
|
|
435
|
+
{state === "edited" && (
|
|
436
|
+
<PencilLine aria-hidden className="size-4 text-[var(--status-edited)]" />
|
|
437
|
+
)}
|
|
438
|
+
{state === "pending" && (
|
|
439
|
+
<Loader2 aria-hidden className="size-4 animate-spin text-[var(--status-pending)] motion-reduce:animate-none" />
|
|
440
|
+
)}
|
|
441
|
+
{state === "synced" && (
|
|
442
|
+
<Check
|
|
443
|
+
aria-hidden
|
|
444
|
+
className={cn(
|
|
445
|
+
"size-4 text-[var(--status-synced)] transition-opacity duration-500 motion-reduce:transition-none",
|
|
446
|
+
justSaved ? "opacity-100" : "opacity-0",
|
|
447
|
+
)}
|
|
448
|
+
/>
|
|
449
|
+
)}
|
|
450
|
+
{isError && (
|
|
451
|
+
<Tooltip label={text} portal redact>
|
|
452
|
+
<button
|
|
453
|
+
type="button"
|
|
454
|
+
onClick={retry}
|
|
455
|
+
aria-label={l.retry}
|
|
456
|
+
className="pointer-events-auto flex rounded-full text-[var(--status-error)] focus-visible:outline-2 focus-visible:outline-offset-2"
|
|
457
|
+
>
|
|
458
|
+
<AlertCircle aria-hidden className="size-4" />
|
|
459
|
+
</button>
|
|
460
|
+
</Tooltip>
|
|
461
|
+
)}
|
|
462
|
+
</span>
|
|
463
|
+
<span
|
|
464
|
+
role={isError ? "alert" : "status"}
|
|
465
|
+
aria-live={isError ? "assertive" : "polite"}
|
|
466
|
+
className="sr-only"
|
|
467
|
+
>
|
|
468
|
+
{shown === null ? "" : text}
|
|
469
|
+
</span>
|
|
470
|
+
</div>
|
|
471
|
+
);
|
|
472
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import type { ComponentPropsWithoutRef } from "react";
|
|
3
|
+
import { Upload } from "lucide-react";
|
|
4
|
+
import { Button } from "./ui";
|
|
5
|
+
import { cn } from "../lib/cn";
|
|
6
|
+
import { useKitFileLabels } from "../i18n/kit-labels";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `onInvalid` is omitted from the `<div>` attributes and kept as this component's own.
|
|
10
|
+
* The DOM event of that name belongs to form validation and takes a `FormEvent`; this
|
|
11
|
+
* one takes the rejected `File`, and the kit's meaning is the one every caller already
|
|
12
|
+
* writes. Everything else a `<div>` takes reaches the root — which carries
|
|
13
|
+
* `role="button"`, so that is also where an `aria-label` or an `aria-describedby`
|
|
14
|
+
* belongs.
|
|
15
|
+
*/
|
|
16
|
+
export interface FileDropzoneProps extends Omit<ComponentPropsWithoutRef<"div">, "onInvalid"> {
|
|
17
|
+
file: File | null;
|
|
18
|
+
onFileSelected: (file: File) => void;
|
|
19
|
+
accept: string;
|
|
20
|
+
isValid: (file: File) => boolean;
|
|
21
|
+
invalidMessage: string;
|
|
22
|
+
/**
|
|
23
|
+
* How to surface {@link invalidMessage}. Defaults to a `sonner` toast — an
|
|
24
|
+
* optional peer, imported only when a file is actually rejected. Pass your own to
|
|
25
|
+
* route it somewhere else, or a no-op to silence it. The same escape hatch, for
|
|
26
|
+
* the same reason, as `useWizard`'s `onValidationFailed`.
|
|
27
|
+
*
|
|
28
|
+
* It exists so this module does not import `sonner` STATICALLY. `package.json`
|
|
29
|
+
* declares that peer `optional`, and the barrel re-exports this file — so a
|
|
30
|
+
* top-level `import { toast } from "sonner"` made the claim false for everybody:
|
|
31
|
+
* in an app that installed `@hb/ui` without sonner, `import { Button } from
|
|
32
|
+
* "@hb/ui"` failed to resolve and `tsc --noEmit` failed on the missing types,
|
|
33
|
+
* without a FileDropzone anywhere in it. Both current consumers happen to depend
|
|
34
|
+
* on sonner, which is why nothing broke and why the contradiction survived:
|
|
35
|
+
* `use-wizard.ts` had already answered the identical question the other way, in
|
|
36
|
+
* writing, two modules over.
|
|
37
|
+
*/
|
|
38
|
+
onInvalid?: (file: File) => void;
|
|
39
|
+
/** The dropzone's accessible name, and the instruction shown on it. A caller's own
|
|
40
|
+
* `aria-label` wins over it — see the root element below. */
|
|
41
|
+
dropLabel: string;
|
|
42
|
+
browseLabel: string;
|
|
43
|
+
emptyLabel: string;
|
|
44
|
+
hint: string;
|
|
45
|
+
/** Extra classes for the dropzone's root. */
|
|
46
|
+
className?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Drag-and-drop file picker shared by the import wizards (YNAB zip, CAMT xml).
|
|
50
|
+
* Validation failures surface `invalidMessage` (a `sonner` toast by default, see
|
|
51
|
+
* `onInvalid`); the chosen file is echoed with its size, otherwise `emptyLabel` +
|
|
52
|
+
* `hint` describe what to drop. */
|
|
53
|
+
export function FileDropzone({
|
|
54
|
+
file,
|
|
55
|
+
onFileSelected,
|
|
56
|
+
accept,
|
|
57
|
+
isValid,
|
|
58
|
+
invalidMessage,
|
|
59
|
+
onInvalid,
|
|
60
|
+
dropLabel,
|
|
61
|
+
browseLabel,
|
|
62
|
+
emptyLabel,
|
|
63
|
+
hint,
|
|
64
|
+
className,
|
|
65
|
+
"aria-label": ariaLabel,
|
|
66
|
+
...rest
|
|
67
|
+
}: FileDropzoneProps) {
|
|
68
|
+
const [dragOver, setDragOver] = useState(false);
|
|
69
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
70
|
+
// The size through the kit's `file.size` label (the provider's locale by default).
|
|
71
|
+
// This was `${n / 1024} KB`: ASCII digits, an English unit, and a binary kilobyte
|
|
72
|
+
// mislabelled as a decimal one, all in one template literal.
|
|
73
|
+
const fileText = useKitFileLabels();
|
|
74
|
+
|
|
75
|
+
const acceptFile = (f: File | undefined | null) => {
|
|
76
|
+
if (!f) return;
|
|
77
|
+
if (!isValid(f)) {
|
|
78
|
+
if (onInvalid) {
|
|
79
|
+
onInvalid(f);
|
|
80
|
+
} else {
|
|
81
|
+
// sonner is an optional peer: imported here, on the failure path only, so an
|
|
82
|
+
// app that never trips this never has to install it. Not awaited — nothing
|
|
83
|
+
// downstream depends on the toast having appeared, and the handlers that
|
|
84
|
+
// reach this are DOM events.
|
|
85
|
+
void import("sonner").then(({ toast }) => toast.error(invalidMessage));
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
onFileSelected(f);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<div
|
|
94
|
+
// `...rest` first: every handler below is the drop gesture itself, and the
|
|
95
|
+
// `role`/`tabIndex` pair is what makes this div operable by keyboard at all.
|
|
96
|
+
{...rest}
|
|
97
|
+
onDragEnter={(e) => {
|
|
98
|
+
e.preventDefault();
|
|
99
|
+
e.stopPropagation();
|
|
100
|
+
if (e.dataTransfer.types.includes("Files")) setDragOver(true);
|
|
101
|
+
}}
|
|
102
|
+
onDragOver={(e) => {
|
|
103
|
+
e.preventDefault();
|
|
104
|
+
e.stopPropagation();
|
|
105
|
+
e.dataTransfer.dropEffect = "copy";
|
|
106
|
+
if (!dragOver && e.dataTransfer.types.includes("Files")) setDragOver(true);
|
|
107
|
+
}}
|
|
108
|
+
onDragLeave={(e) => {
|
|
109
|
+
e.preventDefault();
|
|
110
|
+
e.stopPropagation();
|
|
111
|
+
if (e.currentTarget.contains(e.relatedTarget as Node)) return;
|
|
112
|
+
setDragOver(false);
|
|
113
|
+
}}
|
|
114
|
+
onDrop={(e) => {
|
|
115
|
+
e.preventDefault();
|
|
116
|
+
e.stopPropagation();
|
|
117
|
+
setDragOver(false);
|
|
118
|
+
acceptFile(e.dataTransfer.files?.[0]);
|
|
119
|
+
}}
|
|
120
|
+
onClick={() => fileInputRef.current?.click()}
|
|
121
|
+
onKeyDown={(e) => {
|
|
122
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
123
|
+
e.preventDefault();
|
|
124
|
+
fileInputRef.current?.click();
|
|
125
|
+
}
|
|
126
|
+
}}
|
|
127
|
+
role="button"
|
|
128
|
+
tabIndex={0}
|
|
129
|
+
// The DOM spelling wins over `dropLabel`, which stays the name for every caller
|
|
130
|
+
// that passes no `aria-label` — i.e. all of them today. Standardising on
|
|
131
|
+
// `aria-label` (audit §api-design) does not get to silently rename an existing
|
|
132
|
+
// required prop.
|
|
133
|
+
aria-label={ariaLabel ?? dropLabel}
|
|
134
|
+
className={cn(
|
|
135
|
+
// `relative` is load-bearing, not cosmetic. The file input below is `sr-only`,
|
|
136
|
+
// which Tailwind implements as `position: absolute` — so without a positioned
|
|
137
|
+
// ancestor its containing block is the INITIAL one, and it is laid out at its
|
|
138
|
+
// own offset from the top of the document. Nothing looks wrong, because the
|
|
139
|
+
// input is 1x1 and clipped; what breaks is the page height. On a long page
|
|
140
|
+
// every escaped sr-only element extends `documentElement.scrollHeight` to its
|
|
141
|
+
// own offset, which produces a second, whole-document scrollbar alongside the
|
|
142
|
+
// app shell's own — one that scrolls past the end of the content into nothing.
|
|
143
|
+
// Measured on the showcase: body 900px, document 47,919px.
|
|
144
|
+
"relative flex cursor-pointer flex-col items-center justify-center gap-2 rounded-md border-2 border-dashed px-4 py-6 text-center transition-colors",
|
|
145
|
+
// Three states out of two border tokens: the target rests on the plain
|
|
146
|
+
// hairline, hover pulls it to `--border-strong`, and a live drag keeps that
|
|
147
|
+
// border and adds the `--bg-active` wash on top — so "let go here" still reads
|
|
148
|
+
// one step louder than "you are over it".
|
|
149
|
+
dragOver
|
|
150
|
+
? "border-[var(--border-strong)] bg-[var(--bg-active)]"
|
|
151
|
+
: "border-[var(--border)] hover:border-[var(--border-strong)]",
|
|
152
|
+
className,
|
|
153
|
+
)}
|
|
154
|
+
>
|
|
155
|
+
<Upload className="size-5 text-[var(--text-muted)]" />
|
|
156
|
+
<div className="text-sm text-[var(--text-secondary)]">
|
|
157
|
+
{file ? <span className="font-medium">{file.name}</span> : emptyLabel}
|
|
158
|
+
</div>
|
|
159
|
+
<div className="text-xs text-[var(--text-muted)]">
|
|
160
|
+
{file ? fileText.size(file.size) : hint}
|
|
161
|
+
</div>
|
|
162
|
+
<Button
|
|
163
|
+
type="button"
|
|
164
|
+
variant="secondary"
|
|
165
|
+
onClick={(e) => {
|
|
166
|
+
e.stopPropagation();
|
|
167
|
+
fileInputRef.current?.click();
|
|
168
|
+
}}
|
|
169
|
+
>
|
|
170
|
+
{browseLabel}
|
|
171
|
+
</Button>
|
|
172
|
+
<input
|
|
173
|
+
ref={fileInputRef}
|
|
174
|
+
type="file"
|
|
175
|
+
accept={accept}
|
|
176
|
+
onChange={(e) => acceptFile(e.target.files?.[0])}
|
|
177
|
+
className="sr-only"
|
|
178
|
+
/>
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
}
|