@brightlocal/ui-components 3.2.0 → 3.4.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/AI_USAGE.md +10 -0
- package/README.md +3 -1
- package/component-meta.json +1458 -380
- package/deprecations.json +44 -0
- package/dist/_virtual/_rolldown/runtime.cjs +1 -0
- package/dist/accordion.cjs +1 -1
- package/dist/accordion.js +73 -102
- package/dist/alert-dialog.cjs +1 -1
- package/dist/alert-dialog.d.ts +6 -4
- package/dist/alert-dialog.d.ts.map +1 -1
- package/dist/alert-dialog.js +85 -133
- package/dist/alert.cjs +1 -1
- package/dist/alert.d.ts +4 -4
- package/dist/alert.d.ts.map +1 -1
- package/dist/alert.js +106 -168
- package/dist/aspect-ratio.cjs +1 -1
- package/dist/aspect-ratio.js +13 -16
- package/dist/avatar.cjs +1 -1
- package/dist/avatar.js +30 -45
- package/dist/badge.cjs +1 -1
- package/dist/badge.d.ts +23 -1
- package/dist/badge.d.ts.map +1 -1
- package/dist/badge.js +90 -91
- package/dist/banner.cjs +1 -1
- package/dist/banner.js +102 -148
- package/dist/blocks/empty-state.cjs +1 -1
- package/dist/blocks/empty-state.d.ts +20 -3
- package/dist/blocks/empty-state.d.ts.map +1 -1
- package/dist/blocks/empty-state.js +73 -140
- package/dist/blocks/input-list.cjs +2 -2
- package/dist/blocks/input-list.d.ts +2 -2
- package/dist/blocks/input-list.d.ts.map +1 -1
- package/dist/blocks/input-list.js +138 -178
- package/dist/blocks/progress-state.cjs +1 -1
- package/dist/blocks/progress-state.js +223 -347
- package/dist/blocks/sidebar.cjs +1 -1
- package/dist/blocks/sidebar.d.ts +11 -6
- package/dist/blocks/sidebar.d.ts.map +1 -1
- package/dist/blocks/sidebar.js +763 -1092
- package/dist/brand-illustration-motif.cjs +1 -1
- package/dist/brand-illustration-motif.js +40 -61
- package/dist/breadcrumb.cjs +1 -1
- package/dist/breadcrumb.js +62 -102
- package/dist/button-group.cjs +1 -1
- package/dist/button-group.js +23 -31
- package/dist/button.cjs +1 -1
- package/dist/button.d.ts +4 -2
- package/dist/button.d.ts.map +1 -1
- package/dist/button.js +149 -189
- package/dist/calendar.cjs +1 -1
- package/dist/calendar.d.ts +87 -3
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +116 -173
- package/dist/callout.cjs +1 -1
- package/dist/callout.js +73 -96
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +10 -4
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +233 -316
- package/dist/carousel.cjs +1 -1
- package/dist/carousel.d.ts +3 -3
- package/dist/carousel.d.ts.map +1 -1
- package/dist/carousel.js +174 -268
- package/dist/centred-layout.cjs +1 -1
- package/dist/centred-layout.js +27 -48
- package/dist/chart-empty.cjs +1 -1
- package/dist/chart-empty.js +18 -27
- package/dist/chart.cjs +7 -7
- package/dist/chart.d.ts +23 -13
- package/dist/chart.d.ts.map +1 -1
- package/dist/chart.js +445 -651
- package/dist/checkbox.cjs +1 -1
- package/dist/checkbox.js +36 -69
- package/dist/chip.cjs +1 -1
- package/dist/chip.js +116 -173
- package/dist/collapsible.cjs +1 -1
- package/dist/collapsible.d.ts.map +1 -1
- package/dist/collapsible.js +35 -41
- package/dist/combobox.cjs +3 -3
- package/dist/combobox.d.ts +14 -4
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +439 -638
- package/dist/command.cjs +1 -1
- package/dist/command.d.ts.map +1 -1
- package/dist/command.js +273 -420
- package/dist/context-menu.cjs +1 -1
- package/dist/context-menu.d.ts +5 -5
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js +121 -191
- package/dist/data-table.cjs +1 -1
- package/dist/data-table.d.ts +280 -129
- package/dist/data-table.d.ts.map +1 -1
- package/dist/data-table.js +687 -948
- package/dist/date-picker.cjs +1 -1
- package/dist/date-picker.d.ts +15 -4
- package/dist/date-picker.d.ts.map +1 -1
- package/dist/date-picker.js +156 -231
- package/dist/delta.cjs +1 -0
- package/dist/delta.d.ts +79 -0
- package/dist/delta.d.ts.map +1 -0
- package/dist/delta.js +62 -0
- package/dist/description-list.cjs +1 -0
- package/dist/description-list.d.ts +142 -0
- package/dist/description-list.d.ts.map +1 -0
- package/dist/description-list.js +173 -0
- package/dist/description.cjs +1 -1
- package/dist/description.js +11 -20
- package/dist/dialog.cjs +1 -1
- package/dist/dialog.d.ts +1 -1
- package/dist/dialog.d.ts.map +1 -1
- package/dist/dialog.js +76 -116
- package/dist/drawer.cjs +1 -1
- package/dist/drawer.d.ts +11 -11
- package/dist/drawer.d.ts.map +1 -1
- package/dist/drawer.js +76 -134
- package/dist/dropdown-menu.cjs +1 -1
- package/dist/dropdown-menu.d.ts +6 -6
- package/dist/dropdown-menu.d.ts.map +1 -1
- package/dist/dropdown-menu.js +179 -300
- package/dist/field.cjs +1 -1
- package/dist/field.d.ts +9 -9
- package/dist/field.d.ts.map +1 -1
- package/dist/field.js +166 -286
- package/dist/global-layout.cjs +1 -1
- package/dist/global-layout.js +203 -342
- package/dist/grid.cjs +1 -1
- package/dist/grid.js +16 -29
- package/dist/header.cjs +1 -1
- package/dist/header.js +21 -38
- package/dist/height-transition.cjs +1 -1
- package/dist/height-transition.js +38 -55
- package/dist/hooks/use-breakpoint.cjs +1 -1
- package/dist/hooks/use-breakpoint.js +9 -9
- package/dist/hooks/use-debounce.cjs +1 -1
- package/dist/hooks/use-debounce.js +12 -11
- package/dist/hooks/use-delayed-pending.cjs +1 -1
- package/dist/hooks/use-delayed-pending.js +8 -9
- package/dist/hooks/use-filter-search-params.cjs +1 -0
- package/dist/hooks/use-filter-search-params.d.ts +93 -0
- package/dist/hooks/use-filter-search-params.d.ts.map +1 -0
- package/dist/hooks/use-filter-search-params.js +122 -0
- package/dist/hooks/use-mobile.cjs +1 -1
- package/dist/hooks/use-mobile.js +6 -6
- package/dist/hooks/use-overlay-pointer-lock.cjs +1 -1
- package/dist/hooks/use-overlay-pointer-lock.js +8 -9
- package/dist/hooks/use-prefers-reduced-motion.cjs +1 -1
- package/dist/hooks/use-prefers-reduced-motion.js +5 -8
- package/dist/hover-card.cjs +1 -1
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.d.ts.map +1 -1
- package/dist/hover-card.js +30 -62
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +658 -614
- package/dist/info-tooltip.cjs +1 -1
- package/dist/info-tooltip.js +22 -36
- package/dist/input-chip.cjs +1 -1
- package/dist/input-chip.d.ts +32 -3
- package/dist/input-chip.d.ts.map +1 -1
- package/dist/input-chip.js +181 -230
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.js +174 -221
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.d.ts +3 -3
- package/dist/input-otp.d.ts.map +1 -1
- package/dist/input-otp.js +102 -146
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +12 -16
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +139 -171
- package/dist/input.cjs +1 -1
- package/dist/input.d.ts.map +1 -1
- package/dist/input.js +51 -73
- package/dist/label.cjs +1 -1
- package/dist/label.js +20 -38
- package/dist/lib/block-illustration.cjs +1 -1
- package/dist/lib/block-illustration.d.ts +1 -1
- package/dist/lib/block-illustration.d.ts.map +1 -1
- package/dist/lib/block-illustration.js +16 -18
- package/dist/lib/compose-event-handlers.cjs +1 -0
- package/dist/lib/compose-event-handlers.d.ts +9 -0
- package/dist/lib/compose-event-handlers.d.ts.map +1 -0
- package/dist/lib/compose-event-handlers.js +8 -0
- package/dist/lib/download-file.cjs +1 -0
- package/dist/lib/download-file.d.ts +14 -0
- package/dist/lib/download-file.d.ts.map +1 -0
- package/dist/lib/download-file.js +40 -0
- package/dist/lib/empty-canvas.cjs +1 -1
- package/dist/lib/empty-canvas.js +28 -56
- package/dist/lib/empty-chip.cjs +1 -1
- package/dist/lib/empty-chip.d.ts +14 -3
- package/dist/lib/empty-chip.d.ts.map +1 -1
- package/dist/lib/empty-chip.js +15 -11
- package/dist/lib/field-aria.cjs +1 -0
- package/dist/lib/field-aria.d.ts +34 -0
- package/dist/lib/field-aria.d.ts.map +1 -0
- package/dist/lib/field-aria.js +52 -0
- package/dist/lib/image-file-info.cjs +1 -0
- package/dist/lib/image-file-info.d.ts +25 -0
- package/dist/lib/image-file-info.d.ts.map +1 -0
- package/dist/lib/image-file-info.js +67 -0
- package/dist/lib/media-viewer-content.cjs +1 -0
- package/dist/lib/media-viewer-content.d.ts +102 -0
- package/dist/lib/media-viewer-content.d.ts.map +1 -0
- package/dist/lib/media-viewer-content.js +650 -0
- package/dist/lib/media-viewer-internal.cjs +1 -0
- package/dist/lib/media-viewer-internal.d.ts +98 -0
- package/dist/lib/media-viewer-internal.d.ts.map +1 -0
- package/dist/lib/media-viewer-internal.js +61 -0
- package/dist/lib/media-viewer-motion.cjs +1 -0
- package/dist/lib/media-viewer-motion.d.ts +40 -0
- package/dist/lib/media-viewer-motion.d.ts.map +1 -0
- package/dist/lib/media-viewer-motion.js +37 -0
- package/dist/lib/motion.cjs +1 -1
- package/dist/lib/motion.d.ts +8 -0
- package/dist/lib/motion.d.ts.map +1 -1
- package/dist/lib/motion.js +14 -8
- package/dist/lib/optical-center.cjs +1 -1
- package/dist/lib/optical-center.d.ts +1 -1
- package/dist/lib/optical-center.d.ts.map +1 -1
- package/dist/lib/optical-center.js +12 -5
- package/dist/lib/spring-settle.cjs +1 -1
- package/dist/lib/spring-settle.js +1 -1
- package/dist/lib/table-internal.cjs +1 -0
- package/dist/lib/table-internal.d.ts +62 -0
- package/dist/lib/table-internal.d.ts.map +1 -0
- package/dist/lib/table-internal.js +18 -0
- package/dist/lib/use-controllable.cjs +1 -0
- package/dist/lib/use-controllable.d.ts +12 -0
- package/dist/lib/use-controllable.d.ts.map +1 -0
- package/dist/lib/use-controllable.js +15 -0
- package/dist/lib/use-dev-warning.cjs +1 -1
- package/dist/lib/utils.cjs +1 -1
- package/dist/lib/utils.js +26 -33
- package/dist/link.cjs +1 -1
- package/dist/link.d.ts +1 -1
- package/dist/link.js +78 -124
- package/dist/list.cjs +1 -1
- package/dist/list.d.ts +224 -39
- package/dist/list.d.ts.map +1 -1
- package/dist/list.js +323 -171
- package/dist/loading.cjs +1 -1
- package/dist/loading.js +124 -151
- package/dist/logo-mark.cjs +1 -0
- package/dist/logo-mark.d.ts +79 -0
- package/dist/logo-mark.d.ts.map +1 -0
- package/dist/logo-mark.js +219 -0
- package/dist/logo.cjs +1 -1
- package/dist/logo.d.ts +1 -1
- package/dist/logo.d.ts.map +1 -1
- package/dist/logo.js +51 -87
- package/dist/map.cjs +1 -1
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +390 -586
- package/dist/media-empty.cjs +1 -1
- package/dist/media-empty.js +27 -42
- package/dist/media-viewer.cjs +1 -0
- package/dist/media-viewer.d.ts +224 -0
- package/dist/media-viewer.d.ts.map +1 -0
- package/dist/media-viewer.js +295 -0
- package/dist/menubar.cjs +1 -1
- package/dist/menubar.d.ts +14 -14
- package/dist/menubar.d.ts.map +1 -1
- package/dist/menubar.js +130 -267
- package/dist/multi-select-filter.cjs +1 -1
- package/dist/multi-select-filter.d.ts +9 -1
- package/dist/multi-select-filter.d.ts.map +1 -1
- package/dist/multi-select-filter.js +357 -446
- package/dist/navigation-menu.cjs +1 -1
- package/dist/navigation-menu.js +108 -172
- package/dist/pagination.cjs +1 -1
- package/dist/pagination.d.ts +36 -12
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +208 -281
- package/dist/popover.cjs +1 -1
- package/dist/popover.d.ts +1 -1
- package/dist/popover.d.ts.map +1 -1
- package/dist/popover.js +34 -45
- package/dist/progress.cjs +1 -1
- package/dist/progress.d.ts +17 -2
- package/dist/progress.d.ts.map +1 -1
- package/dist/progress.js +116 -162
- package/dist/radio-group.cjs +1 -1
- package/dist/radio-group.js +90 -146
- package/dist/rating.cjs +1 -1
- package/dist/rating.d.ts +80 -13
- package/dist/rating.d.ts.map +1 -1
- package/dist/rating.js +70 -65
- package/dist/relative-date.cjs +1 -1
- package/dist/relative-date.d.ts.map +1 -1
- package/dist/relative-date.js +73 -89
- package/dist/resizable.cjs +1 -1
- package/dist/resizable.d.ts +39 -23
- package/dist/resizable.d.ts.map +1 -1
- package/dist/resizable.js +42 -44
- package/dist/result-count.cjs +1 -0
- package/dist/result-count.d.ts +110 -0
- package/dist/result-count.d.ts.map +1 -0
- package/dist/result-count.js +49 -0
- package/dist/route-progress.cjs +1 -1
- package/dist/route-progress.js +59 -102
- package/dist/scroll-area.cjs +1 -1
- package/dist/scroll-area.d.ts +9 -1
- package/dist/scroll-area.d.ts.map +1 -1
- package/dist/scroll-area.js +44 -79
- package/dist/select.cjs +2 -2
- package/dist/select.d.ts +10 -10
- package/dist/select.d.ts.map +1 -1
- package/dist/select.js +118 -238
- package/dist/separator.cjs +1 -1
- package/dist/separator.js +90 -78
- package/dist/sheet.cjs +1 -1
- package/dist/sheet.d.ts +8 -5
- package/dist/sheet.d.ts.map +1 -1
- package/dist/sheet.js +250 -317
- package/dist/single-select-filter.cjs +1 -1
- package/dist/single-select-filter.d.ts +32 -0
- package/dist/single-select-filter.d.ts.map +1 -1
- package/dist/single-select-filter.js +201 -203
- package/dist/skeleton.cjs +1 -1
- package/dist/skeleton.js +24 -38
- package/dist/slider.cjs +1 -1
- package/dist/slider.js +51 -81
- package/dist/sonner.cjs +1 -1
- package/dist/sonner.d.ts +1 -1
- package/dist/sonner.d.ts.map +1 -1
- package/dist/sonner.js +101 -125
- package/dist/spinner.cjs +1 -1
- package/dist/spinner.js +22 -31
- package/dist/split-layout.cjs +1 -1
- package/dist/split-layout.js +52 -101
- package/dist/status.cjs +1 -0
- package/dist/status.d.ts +51 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +60 -0
- package/dist/stepper.cjs +1 -1
- package/dist/stepper.js +184 -316
- package/dist/switch.cjs +1 -1
- package/dist/switch.js +17 -32
- package/dist/table.cjs +1 -1
- package/dist/table.d.ts +103 -30
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +170 -209
- package/dist/tabs.cjs +1 -1
- package/dist/tabs.d.ts.map +1 -1
- package/dist/tabs.js +102 -147
- package/dist/text-reveal-motion.cjs +1 -1
- package/dist/text-reveal-motion.js +307 -363
- package/dist/textarea.cjs +1 -1
- package/dist/textarea.d.ts.map +1 -1
- package/dist/textarea.js +22 -35
- package/dist/toggle-group.cjs +1 -1
- package/dist/toggle-group.js +43 -62
- package/dist/toggle.cjs +1 -1
- package/dist/toggle.js +34 -46
- package/dist/tooltip.cjs +1 -1
- package/dist/tooltip.d.ts +2 -2
- package/dist/tooltip.d.ts.map +1 -1
- package/dist/tooltip.js +157 -249
- package/dist/typography-heading.cjs +1 -1
- package/dist/typography-heading.js +25 -61
- package/dist/typography-text.cjs +1 -1
- package/dist/typography-text.js +27 -57
- package/dist/typography.cjs +1 -1
- package/dist/typography.js +132 -340
- package/package.json +47 -47
- package/src/blocks/empty-state.tsx +34 -18
- package/src/blocks/input-list.tsx +14 -9
- package/src/blocks/sidebar.tsx +86 -31
- package/src/components/accordion.tsx +1 -1
- package/src/components/alert-dialog.tsx +4 -2
- package/src/components/badge.tsx +127 -15
- package/src/components/breadcrumb.tsx +1 -1
- package/src/components/button.tsx +8 -6
- package/src/components/calendar.tsx +155 -9
- package/src/components/card.tsx +63 -10
- package/src/components/carousel.tsx +4 -1
- package/src/components/chart.tsx +151 -17
- package/src/components/checkbox.tsx +1 -1
- package/src/components/collapsible.tsx +4 -1
- package/src/components/combobox.tsx +40 -7
- package/src/components/command.tsx +9 -10
- package/src/components/context-menu.tsx +4 -4
- package/src/components/data-table.tsx +619 -237
- package/src/components/date-picker.tsx +31 -4
- package/src/components/delta.tsx +217 -0
- package/src/components/description-list.tsx +445 -0
- package/src/components/drawer.tsx +5 -5
- package/src/components/dropdown-menu.tsx +4 -4
- package/src/components/field.tsx +36 -15
- package/src/components/global-layout.tsx +1 -1
- package/src/components/input-chip.tsx +51 -7
- package/src/components/input-group.tsx +1 -1
- package/src/components/input-password.tsx +15 -19
- package/src/components/input.tsx +10 -1
- package/src/components/list.tsx +695 -72
- package/src/components/logo-mark.tsx +345 -0
- package/src/components/map.tsx +18 -20
- package/src/components/media-viewer.tsx +908 -0
- package/src/components/multi-select-filter.tsx +255 -121
- package/src/components/navigation-menu.tsx +2 -2
- package/src/components/pagination.tsx +86 -26
- package/src/components/progress.tsx +91 -23
- package/src/components/rating.tsx +200 -74
- package/src/components/relative-date.tsx +19 -11
- package/src/components/resizable.tsx +86 -33
- package/src/components/result-count.tsx +253 -0
- package/src/components/scroll-area.tsx +31 -5
- package/src/components/select.tsx +2 -2
- package/src/components/sheet.tsx +14 -4
- package/src/components/single-select-filter.tsx +136 -41
- package/src/components/slider.tsx +2 -2
- package/src/components/status.tsx +162 -0
- package/src/components/switch.tsx +1 -1
- package/src/components/table.tsx +278 -106
- package/src/components/tabs.tsx +1 -0
- package/src/components/textarea.tsx +20 -2
- package/src/components/toggle.tsx +2 -2
- package/src/components/tooltip.tsx +11 -20
- package/src/hooks/use-filter-search-params.ts +302 -0
- package/src/index.ts +7 -0
- package/src/lib/compose-event-handlers.ts +14 -0
- package/src/lib/download-file.ts +61 -0
- package/src/lib/empty-chip.tsx +52 -0
- package/src/lib/field-aria.tsx +97 -0
- package/src/lib/image-file-info.ts +102 -0
- package/src/lib/media-viewer-content.tsx +1587 -0
- package/src/lib/media-viewer-internal.tsx +191 -0
- package/src/lib/media-viewer-motion.ts +120 -0
- package/src/lib/motion.ts +9 -0
- package/src/lib/table-internal.ts +74 -0
- package/src/lib/use-controllable.ts +43 -0
- package/src/lib/empty-chip.ts +0 -24
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How one key reads from and writes to the query string. Build these with
|
|
7
|
+
* `filterParam` rather than by hand.
|
|
8
|
+
*/
|
|
9
|
+
export interface FilterParamCodec<T> {
|
|
10
|
+
/** The value when the key is absent from the link. */
|
|
11
|
+
default: T;
|
|
12
|
+
/** Reads the raw string. Anything unknown comes back as the default. */
|
|
13
|
+
parse(raw: string): T;
|
|
14
|
+
/** Writes the value. The key is dropped when this equals the default's. */
|
|
15
|
+
serialize(value: T): string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/* Method signatures above on purpose: they check bivariantly, so a
|
|
19
|
+
`FilterParamCodec<"a" | "b">` satisfies the `FilterParamCodec<unknown>`
|
|
20
|
+
schema constraint and the hook can infer each key's type. */
|
|
21
|
+
|
|
22
|
+
export type FilterParamSchema = Record<string, FilterParamCodec<unknown>>;
|
|
23
|
+
|
|
24
|
+
/** The decoded values of a schema. */
|
|
25
|
+
export type FilterParamValues<S extends FilterParamSchema> = {
|
|
26
|
+
[K in keyof S]: S[K] extends FilterParamCodec<infer T> ? T : never;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface UseFilterSearchParamsOptions<S extends FilterParamSchema> {
|
|
30
|
+
/**
|
|
31
|
+
* Keys that go back to their default whenever another key changes — the
|
|
32
|
+
* pattern's "any filter, search or sort change goes back to page 1".
|
|
33
|
+
* @default ["page"] when the schema has a `page` key, otherwise none
|
|
34
|
+
*/
|
|
35
|
+
resetOnChange?: (keyof S)[];
|
|
36
|
+
/**
|
|
37
|
+
* `"replace"` rewrites the current history entry so filtering doesn't fill
|
|
38
|
+
* the back button; `"push"` adds one per change.
|
|
39
|
+
* @default "replace"
|
|
40
|
+
*/
|
|
41
|
+
history?: "push" | "replace";
|
|
42
|
+
/**
|
|
43
|
+
* Hand the write to a router instead of `window.history` — React Router's
|
|
44
|
+
* `navigate({ search }, { replace })`, Next's `router.replace(...)`. Pair
|
|
45
|
+
* it with `search` when the router commits the address asynchronously.
|
|
46
|
+
*/
|
|
47
|
+
navigate?: (search: string, options: { replace: boolean }) => void;
|
|
48
|
+
/**
|
|
49
|
+
* The query string to read when a router owns it — React Router's
|
|
50
|
+
* `useLocation().search`, Next's `useSearchParams().toString()`; with or
|
|
51
|
+
* without the leading `?`. Without it the hook reads `window.location`,
|
|
52
|
+
* which is current as long as the URL is written through `window.history`.
|
|
53
|
+
* A router that commits the address asynchronously (a data router with
|
|
54
|
+
* loaders, Next's app router) updates `window.location` only after
|
|
55
|
+
* `navigate` returns, so pass its search here to stay in step with it.
|
|
56
|
+
*/
|
|
57
|
+
search?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface UseFilterSearchParamsResult<S extends FilterParamSchema> {
|
|
61
|
+
/** The current values, decoded from the link. */
|
|
62
|
+
values: FilterParamValues<S>;
|
|
63
|
+
/**
|
|
64
|
+
* Change one or more keys in a single write. Keys in `resetOnChange` go
|
|
65
|
+
* back to default; a key set to `undefined` goes back to its default too.
|
|
66
|
+
*/
|
|
67
|
+
set: (patch: Partial<FilterParamValues<S>>) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Put the given keys — or every key — back to their default. Keys in
|
|
70
|
+
* `resetOnChange` go back too, so clearing a filter also returns to page 1.
|
|
71
|
+
*/
|
|
72
|
+
reset: (keys?: (keyof S)[]) => void;
|
|
73
|
+
/** The keys that are off their default: "Clear filters" shows while any filter key is here. */
|
|
74
|
+
changed: (keyof S)[];
|
|
75
|
+
/** The query string as it stands (with the leading `?`, or empty), for building links. */
|
|
76
|
+
search: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const CHANGE_EVENT = "ds:filter-search-params";
|
|
80
|
+
|
|
81
|
+
function subscribe(onChange: () => void) {
|
|
82
|
+
window.addEventListener("popstate", onChange);
|
|
83
|
+
window.addEventListener(CHANGE_EVENT, onChange);
|
|
84
|
+
return () => {
|
|
85
|
+
window.removeEventListener("popstate", onChange);
|
|
86
|
+
window.removeEventListener(CHANGE_EVENT, onChange);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const getSnapshot = () => window.location.search;
|
|
91
|
+
const getServerSnapshot = () => "";
|
|
92
|
+
|
|
93
|
+
/* A router that commits the address asynchronously leaves a gap between
|
|
94
|
+
`navigate` returning and the search updating. Two writes inside that gap
|
|
95
|
+
must compose — "status=pending", then "q=cafe", not "q=cafe" alone — so
|
|
96
|
+
the last write is remembered module-wide (every instance on the page
|
|
97
|
+
shares one address): while the committed search still equals what that
|
|
98
|
+
write started from, the next write starts from what it produced. */
|
|
99
|
+
let pendingWrite: { from: string; to: string } | null = null;
|
|
100
|
+
|
|
101
|
+
/* `useLocation().search` carries the `?`, `useSearchParams().toString()`
|
|
102
|
+
doesn't; both read as the same query string. */
|
|
103
|
+
function withQuestionMark(search: string | undefined): string | undefined {
|
|
104
|
+
if (search === undefined) return undefined;
|
|
105
|
+
if (search === "" || search.startsWith("?")) return search;
|
|
106
|
+
return `?${search}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function decode<S extends FilterParamSchema>(
|
|
110
|
+
schema: S,
|
|
111
|
+
search: string
|
|
112
|
+
): FilterParamValues<S> {
|
|
113
|
+
const params = new URLSearchParams(search);
|
|
114
|
+
const values = {} as Record<string, unknown>;
|
|
115
|
+
for (const key of Object.keys(schema)) {
|
|
116
|
+
const codec = codecOf(schema, key);
|
|
117
|
+
const raw = params.get(key);
|
|
118
|
+
values[key] = raw === null ? codec.default : codec.parse(raw);
|
|
119
|
+
}
|
|
120
|
+
return values as FilterParamValues<S>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* Index access is typed as possibly undefined; every key here comes from the schema itself. */
|
|
124
|
+
function codecOf<S extends FilterParamSchema>(
|
|
125
|
+
schema: S,
|
|
126
|
+
key: keyof S
|
|
127
|
+
): FilterParamCodec<unknown> {
|
|
128
|
+
return schema[key as string] as FilterParamCodec<unknown>;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isDefault<T>(codec: FilterParamCodec<T>, value: T): boolean {
|
|
132
|
+
return codec.serialize(value) === codec.serialize(codec.default);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Keeps a section's filters, search, sort and page in the link, so sharing the
|
|
137
|
+
* address shares the view and the back button works. Defaults stay out of the
|
|
138
|
+
* address; keys the schema doesn't name (a router's own, `utm_*`) are left
|
|
139
|
+
* untouched; any change to one key resets the `page` key, as the Filtering
|
|
140
|
+
* pattern asks. Layout and density belong in local storage, the selection
|
|
141
|
+
* nowhere — don't put them here.
|
|
142
|
+
*
|
|
143
|
+
* @param schema - One `filterParam` codec per key, defined outside the component.
|
|
144
|
+
* @param options - Which keys reset on change, replace vs push, and a router's navigate and search.
|
|
145
|
+
*/
|
|
146
|
+
export function useFilterSearchParams<S extends FilterParamSchema>(
|
|
147
|
+
schema: S,
|
|
148
|
+
options: UseFilterSearchParamsOptions<S> = {}
|
|
149
|
+
): UseFilterSearchParamsResult<S> {
|
|
150
|
+
const storeSearch = React.useSyncExternalStore(
|
|
151
|
+
subscribe,
|
|
152
|
+
getSnapshot,
|
|
153
|
+
getServerSnapshot
|
|
154
|
+
);
|
|
155
|
+
const search = withQuestionMark(options.search) ?? storeSearch;
|
|
156
|
+
|
|
157
|
+
const values = React.useMemo(() => decode(schema, search), [schema, search]);
|
|
158
|
+
|
|
159
|
+
const changed = React.useMemo(
|
|
160
|
+
() =>
|
|
161
|
+
(Object.keys(schema) as (keyof S)[]).filter(
|
|
162
|
+
(key) => !isDefault(codecOf(schema, key), values[key] as unknown)
|
|
163
|
+
),
|
|
164
|
+
[schema, values]
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
/* The writers read the schema and options through a ref, so their identity
|
|
168
|
+
is stable even when a consumer passes `options` inline. Updated after
|
|
169
|
+
commit; writes only ever happen from event handlers, which run later. */
|
|
170
|
+
const latest = React.useRef({ schema, options });
|
|
171
|
+
React.useEffect(() => {
|
|
172
|
+
latest.current = { schema, options };
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const write = React.useCallback((patch: Record<string, unknown>) => {
|
|
176
|
+
const { schema, options } = latest.current;
|
|
177
|
+
const committed =
|
|
178
|
+
withQuestionMark(options.search) ?? window.location.search;
|
|
179
|
+
const current =
|
|
180
|
+
pendingWrite && pendingWrite.from === committed
|
|
181
|
+
? pendingWrite.to
|
|
182
|
+
: committed;
|
|
183
|
+
const params = new URLSearchParams(current);
|
|
184
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
185
|
+
const codec = schema[key] as FilterParamCodec<unknown> | undefined;
|
|
186
|
+
if (!codec) continue;
|
|
187
|
+
// `Partial` admits `undefined`; it means "back to the default", never
|
|
188
|
+
// the string "undefined" in the address.
|
|
189
|
+
const resolved = value === undefined ? codec.default : value;
|
|
190
|
+
if (isDefault(codec, resolved)) params.delete(key);
|
|
191
|
+
else params.set(key, codec.serialize(resolved));
|
|
192
|
+
}
|
|
193
|
+
const query = params.toString();
|
|
194
|
+
const next = query ? `?${query}` : "";
|
|
195
|
+
if (next === current) return;
|
|
196
|
+
pendingWrite = { from: committed, to: next };
|
|
197
|
+
|
|
198
|
+
const replace = (options.history ?? "replace") === "replace";
|
|
199
|
+
if (options.navigate) {
|
|
200
|
+
options.navigate(next, { replace });
|
|
201
|
+
} else {
|
|
202
|
+
const url = `${window.location.pathname}${next}${window.location.hash}`;
|
|
203
|
+
if (replace) window.history.replaceState(window.history.state, "", url);
|
|
204
|
+
else window.history.pushState(window.history.state, "", url);
|
|
205
|
+
}
|
|
206
|
+
window.dispatchEvent(new Event(CHANGE_EVENT));
|
|
207
|
+
}, []);
|
|
208
|
+
|
|
209
|
+
/* Any change to a key outside `resetOnChange` sends those keys back to
|
|
210
|
+
their default — the pattern's "back to page 1". Shared by `set` and
|
|
211
|
+
`reset`, so clearing a filter leaves page 3 as surely as picking one. */
|
|
212
|
+
const writeWithResets = React.useCallback(
|
|
213
|
+
(patch: Record<string, unknown>) => {
|
|
214
|
+
const { schema, options } = latest.current;
|
|
215
|
+
const resetKeys =
|
|
216
|
+
options.resetOnChange ??
|
|
217
|
+
(("page" in schema ? ["page"] : []) as (keyof S)[]);
|
|
218
|
+
const full: Record<string, unknown> = { ...patch };
|
|
219
|
+
const changesSomethingElse = Object.keys(patch).some(
|
|
220
|
+
(key) => !resetKeys.includes(key as keyof S)
|
|
221
|
+
);
|
|
222
|
+
if (changesSomethingElse) {
|
|
223
|
+
for (const key of resetKeys) {
|
|
224
|
+
if (!(key in full))
|
|
225
|
+
full[key as string] = codecOf(schema, key).default;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
write(full);
|
|
229
|
+
},
|
|
230
|
+
[write]
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const set = React.useCallback(
|
|
234
|
+
(patch: Partial<FilterParamValues<S>>) => writeWithResets(patch),
|
|
235
|
+
[writeWithResets]
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const reset = React.useCallback(
|
|
239
|
+
(keys?: (keyof S)[]) => {
|
|
240
|
+
const { schema } = latest.current;
|
|
241
|
+
const full: Record<string, unknown> = {};
|
|
242
|
+
for (const key of keys ?? (Object.keys(schema) as (keyof S)[])) {
|
|
243
|
+
full[key as string] = codecOf(schema, key).default;
|
|
244
|
+
}
|
|
245
|
+
writeWithResets(full);
|
|
246
|
+
},
|
|
247
|
+
[writeWithResets]
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return { values, set, reset, changed, search };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Codecs for the usual keys. Each takes the default — what an absent key means. */
|
|
254
|
+
export const filterParam = {
|
|
255
|
+
/** Free text: the search box. */
|
|
256
|
+
string(fallback = ""): FilterParamCodec<string> {
|
|
257
|
+
return { default: fallback, parse: (raw) => raw, serialize: (v) => v };
|
|
258
|
+
},
|
|
259
|
+
/** One of a fixed set: a single-select filter, a sort column, a view. Unknown reads as the default. */
|
|
260
|
+
oneOf<T extends string>(
|
|
261
|
+
options: readonly T[],
|
|
262
|
+
fallback: T
|
|
263
|
+
): FilterParamCodec<T> {
|
|
264
|
+
return {
|
|
265
|
+
default: fallback,
|
|
266
|
+
parse: (raw) => options.find((o) => o === raw) ?? fallback,
|
|
267
|
+
serialize: (v) => v,
|
|
268
|
+
};
|
|
269
|
+
},
|
|
270
|
+
/** Several of a fixed set, comma-joined: a multi-select filter. Unknown entries are dropped. */
|
|
271
|
+
list<T extends string>(
|
|
272
|
+
options: readonly T[],
|
|
273
|
+
fallback: T[] = []
|
|
274
|
+
): FilterParamCodec<T[]> {
|
|
275
|
+
return {
|
|
276
|
+
default: fallback,
|
|
277
|
+
parse: (raw) =>
|
|
278
|
+
raw.split(",").filter((part): part is T => options.includes(part as T)),
|
|
279
|
+
serialize: (v) => v.join(","),
|
|
280
|
+
};
|
|
281
|
+
},
|
|
282
|
+
/** A whole number at or above `min`: the page. Anything else — `2.7`, `2junk`, `-1` — reads as the default. */
|
|
283
|
+
number(fallback: number, min = 1): FilterParamCodec<number> {
|
|
284
|
+
return {
|
|
285
|
+
default: fallback,
|
|
286
|
+
parse: (raw) => {
|
|
287
|
+
if (!/^\d+$/.test(raw)) return fallback;
|
|
288
|
+
const n = Number(raw);
|
|
289
|
+
return Number.isSafeInteger(n) && n >= min ? n : fallback;
|
|
290
|
+
},
|
|
291
|
+
serialize: (v) => String(v),
|
|
292
|
+
};
|
|
293
|
+
},
|
|
294
|
+
/** A flag: present as `1`, absent otherwise. */
|
|
295
|
+
boolean(fallback = false): FilterParamCodec<boolean> {
|
|
296
|
+
return {
|
|
297
|
+
default: fallback,
|
|
298
|
+
parse: (raw) => raw === "1" || raw === "true",
|
|
299
|
+
serialize: (v) => (v ? "1" : "0"),
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -27,7 +27,9 @@ export * from "./components/command";
|
|
|
27
27
|
export * from "./components/data-table";
|
|
28
28
|
export * from "./components/date-picker";
|
|
29
29
|
export * from "./components/context-menu";
|
|
30
|
+
export * from "./components/delta";
|
|
30
31
|
export * from "./components/description";
|
|
32
|
+
export * from "./components/description-list";
|
|
31
33
|
export * from "./components/dialog";
|
|
32
34
|
export * from "./components/drawer";
|
|
33
35
|
export * from "./components/dropdown-menu";
|
|
@@ -49,6 +51,7 @@ export * from "./components/loading";
|
|
|
49
51
|
export * from "./components/logo";
|
|
50
52
|
export * from "./components/map";
|
|
51
53
|
export * from "./components/media-empty";
|
|
54
|
+
export * from "./components/media-viewer";
|
|
52
55
|
export * from "./components/menubar";
|
|
53
56
|
export * from "./components/multi-select-filter";
|
|
54
57
|
export * from "./components/navigation-menu";
|
|
@@ -60,6 +63,7 @@ export * from "./components/radio-group";
|
|
|
60
63
|
export * from "./components/rating";
|
|
61
64
|
export * from "./components/relative-date";
|
|
62
65
|
export * from "./components/resizable";
|
|
66
|
+
export * from "./components/result-count";
|
|
63
67
|
export * from "./components/route-progress";
|
|
64
68
|
export * from "./components/scroll-area";
|
|
65
69
|
export * from "./components/select";
|
|
@@ -69,8 +73,10 @@ export * from "./components/single-select-filter";
|
|
|
69
73
|
export * from "./components/skeleton";
|
|
70
74
|
export * from "./components/slider";
|
|
71
75
|
export * from "./components/sonner";
|
|
76
|
+
export * from "./components/logo-mark";
|
|
72
77
|
export * from "./components/spinner";
|
|
73
78
|
export * from "./components/split-layout";
|
|
79
|
+
export * from "./components/status";
|
|
74
80
|
export * from "./components/stepper";
|
|
75
81
|
export * from "./components/switch";
|
|
76
82
|
export * from "./components/table";
|
|
@@ -93,6 +99,7 @@ export * from "./blocks/sidebar";
|
|
|
93
99
|
// Hooks
|
|
94
100
|
export * from "./hooks/use-debounce";
|
|
95
101
|
export * from "./hooks/use-delayed-pending";
|
|
102
|
+
export * from "./hooks/use-filter-search-params";
|
|
96
103
|
export * from "./hooks/use-breakpoint";
|
|
97
104
|
export * from "./hooks/use-mobile";
|
|
98
105
|
export * from "./hooks/use-overlay-pointer-lock";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls the consumer's handler first, then ours — unless the consumer called
|
|
3
|
+
* `preventDefault()`. Mirrors Radix's `composeEventHandlers` without adding
|
|
4
|
+
* `@radix-ui/primitive` as a direct dependency.
|
|
5
|
+
*/
|
|
6
|
+
export function composeEventHandlers<E extends { defaultPrevented: boolean }>(
|
|
7
|
+
theirs: ((event: E) => void) | undefined,
|
|
8
|
+
ours: (event: E) => void
|
|
9
|
+
) {
|
|
10
|
+
return (event: E) => {
|
|
11
|
+
theirs?.(event);
|
|
12
|
+
if (!event.defaultPrevented) ours(event);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const EXTENSIONS: Record<string, string> = {
|
|
2
|
+
"image/jpeg": "jpg",
|
|
3
|
+
"image/png": "png",
|
|
4
|
+
"image/webp": "webp",
|
|
5
|
+
"image/gif": "gif",
|
|
6
|
+
"image/avif": "avif",
|
|
7
|
+
"image/svg+xml": "svg",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A file name for `src`: its last path segment, with an extension from the
|
|
12
|
+
* file's type when the URL has none (an image CDN path often does not).
|
|
13
|
+
*/
|
|
14
|
+
export function fileNameFor(src: string, type = ""): string {
|
|
15
|
+
const fromType = EXTENSIONS[type.split(";")[0]?.trim() ?? ""];
|
|
16
|
+
if (src.startsWith("data:")) {
|
|
17
|
+
const mime = src.slice(5, src.indexOf(";"));
|
|
18
|
+
return `image.${EXTENSIONS[mime] ?? fromType ?? "png"}`;
|
|
19
|
+
}
|
|
20
|
+
let name = "image";
|
|
21
|
+
try {
|
|
22
|
+
const path = new URL(src, window.location.href).pathname;
|
|
23
|
+
name = decodeURIComponent(path.split("/").filter(Boolean).pop() ?? name);
|
|
24
|
+
} catch {
|
|
25
|
+
/* keep the fallback name */
|
|
26
|
+
}
|
|
27
|
+
return /\.[a-z0-9]{2,5}$/i.test(name) ? name : `${name}.${fromType ?? "jpg"}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function save(href: string, name: string, newTab = false) {
|
|
31
|
+
const link = document.createElement("a");
|
|
32
|
+
link.href = href;
|
|
33
|
+
link.download = name;
|
|
34
|
+
link.rel = "noopener";
|
|
35
|
+
/* A browser ignores `download` across origins and follows the link
|
|
36
|
+
instead — in a new tab, so the page (and any unsaved edits) stays. */
|
|
37
|
+
if (newTab) link.target = "_blank";
|
|
38
|
+
document.body.appendChild(link);
|
|
39
|
+
link.click();
|
|
40
|
+
link.remove();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Saves the file at `src` to the reader's device. The file is fetched and
|
|
45
|
+
* saved from a blob, so a cross-origin image downloads rather than opening
|
|
46
|
+
* (`<a download>` alone is ignored across origins). If the fetch fails — the
|
|
47
|
+
* host sends no CORS headers — it falls back to a plain download link, which
|
|
48
|
+
* still saves same-origin files and opens a cross-origin one in a new tab.
|
|
49
|
+
*/
|
|
50
|
+
export async function downloadFile(src: string): Promise<void> {
|
|
51
|
+
try {
|
|
52
|
+
const response = await fetch(src, { mode: "cors" });
|
|
53
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
54
|
+
const blob = await response.blob();
|
|
55
|
+
const url = URL.createObjectURL(blob);
|
|
56
|
+
save(url, fileNameFor(src, blob.type));
|
|
57
|
+
window.setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
58
|
+
} catch {
|
|
59
|
+
save(src, fileNameFor(src), true);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
4
|
+
import { LogoMark, type LogoMarkVariant } from "../components/logo-mark";
|
|
5
|
+
|
|
6
|
+
/* -------------------------------------------------------------------------------------------------
|
|
7
|
+
* Empty chip (internal)
|
|
8
|
+
*
|
|
9
|
+
* The icon chip every "nothing here" moment shares — EmptyState at card
|
|
10
|
+
* scope, and CommandEmpty / ComboboxEmpty at popover scope. It is the DS
|
|
11
|
+
* LogoMark at lg (a 40px rounded square) with the glyph in muted ink, so the
|
|
12
|
+
* empty chip and every other icon mark in the product share one shape
|
|
13
|
+
* (DS-1208). It lives once so the two scopes cannot drift: the popover form
|
|
14
|
+
* used to be hand-rolled in consumer code with the chip's classes copied in.
|
|
15
|
+
*
|
|
16
|
+
* `variant` is LogoMark's: `filled` (the default) sits on a white surface;
|
|
17
|
+
* on a `bg-muted` panel the fill disappears, so take `border` or
|
|
18
|
+
* `transparent` there.
|
|
19
|
+
*
|
|
20
|
+
* A canvas drops the chip — but a canvas is a different component, so there
|
|
21
|
+
* is no variant for that here.
|
|
22
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
23
|
+
|
|
24
|
+
interface EmptyChipProps {
|
|
25
|
+
dataHook: string;
|
|
26
|
+
/** The glyph. Decorative — the chip is aria-hidden. */
|
|
27
|
+
icon: React.ReactNode;
|
|
28
|
+
/** @default "filled" */
|
|
29
|
+
variant?: LogoMarkVariant;
|
|
30
|
+
/** Overrides LogoMark's own `logo-mark` slot, so each host keeps its name. */
|
|
31
|
+
slot: string;
|
|
32
|
+
className?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function EmptyChip({
|
|
36
|
+
dataHook,
|
|
37
|
+
icon,
|
|
38
|
+
variant = "filled",
|
|
39
|
+
slot,
|
|
40
|
+
className,
|
|
41
|
+
}: EmptyChipProps) {
|
|
42
|
+
return (
|
|
43
|
+
<LogoMark
|
|
44
|
+
className={cn("text-muted-foreground", className)}
|
|
45
|
+
data-slot={slot}
|
|
46
|
+
dataHook={dataHook}
|
|
47
|
+
name={icon}
|
|
48
|
+
size="lg"
|
|
49
|
+
variant={variant}
|
|
50
|
+
/>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wires a Field's hint and error message to the control inside it.
|
|
7
|
+
*
|
|
8
|
+
* `Field` provides the context. `FieldDescription` and `FieldError` register
|
|
9
|
+
* the id they render while they are on screen. Text controls (`Input`,
|
|
10
|
+
* `Textarea`) read the registered ids and point `aria-describedby` at them, so
|
|
11
|
+
* a screen reader hears the message together with the field. Outside a Field
|
|
12
|
+
* the hooks do nothing and a control behaves exactly as before.
|
|
13
|
+
*
|
|
14
|
+
* Registration happens in an effect, after hydration. The hint and error sit
|
|
15
|
+
* after the control in the markup, so the control cannot know about them while
|
|
16
|
+
* it renders on the server. Server-rendered markup therefore has no wired
|
|
17
|
+
* `aria-describedby` until hydration; a consumer that needs it before that
|
|
18
|
+
* passes `id` on the message and `aria-describedby` on the control by hand.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
type FieldAriaKind = "description" | "error";
|
|
22
|
+
|
|
23
|
+
type FieldAriaIds = Record<FieldAriaKind, string[]>;
|
|
24
|
+
|
|
25
|
+
interface FieldAriaContextValue {
|
|
26
|
+
/** Ids of the hint and error elements currently rendered in the Field. */
|
|
27
|
+
ids: FieldAriaIds;
|
|
28
|
+
/** Registers an id for as long as the element is rendered. Returns cleanup. */
|
|
29
|
+
register: (kind: FieldAriaKind, id: string) => () => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const EMPTY_IDS: FieldAriaIds = { description: [], error: [] };
|
|
33
|
+
|
|
34
|
+
const FieldAriaContext = React.createContext<FieldAriaContextValue | null>(
|
|
35
|
+
null
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
function FieldAriaProvider({ children }: { children: React.ReactNode }) {
|
|
39
|
+
const [ids, setIds] = React.useState<FieldAriaIds>(EMPTY_IDS);
|
|
40
|
+
|
|
41
|
+
const register = React.useCallback((kind: FieldAriaKind, id: string) => {
|
|
42
|
+
setIds((prev) =>
|
|
43
|
+
prev[kind].includes(id) ? prev : { ...prev, [kind]: [...prev[kind], id] }
|
|
44
|
+
);
|
|
45
|
+
return () => {
|
|
46
|
+
setIds((prev) =>
|
|
47
|
+
prev[kind].includes(id)
|
|
48
|
+
? { ...prev, [kind]: prev[kind].filter((item) => item !== id) }
|
|
49
|
+
: prev
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
const value = React.useMemo(() => ({ ids, register }), [ids, register]);
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<FieldAriaContext.Provider value={value}>
|
|
58
|
+
{children}
|
|
59
|
+
</FieldAriaContext.Provider>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Registers `id` as the Field's hint or error element while `present` is true.
|
|
65
|
+
* A no-op outside a Field.
|
|
66
|
+
*/
|
|
67
|
+
function useFieldAriaRegistration(
|
|
68
|
+
kind: FieldAriaKind,
|
|
69
|
+
id: string,
|
|
70
|
+
present: boolean
|
|
71
|
+
) {
|
|
72
|
+
const register = React.useContext(FieldAriaContext)?.register;
|
|
73
|
+
|
|
74
|
+
React.useEffect(() => {
|
|
75
|
+
if (!register || !present) return;
|
|
76
|
+
return register(kind, id);
|
|
77
|
+
}, [register, kind, id, present]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Returns the `aria-describedby` value for a control: the Field's error, then
|
|
82
|
+
* its hint, then whatever the consumer passed. Ids are de-duplicated, so a
|
|
83
|
+
* consumer that already wires the same id by hand is unaffected. Outside a
|
|
84
|
+
* Field it returns `describedBy` unchanged.
|
|
85
|
+
*/
|
|
86
|
+
function useFieldDescribedBy(describedBy?: string): string | undefined {
|
|
87
|
+
const context = React.useContext(FieldAriaContext);
|
|
88
|
+
if (!context) return describedBy;
|
|
89
|
+
|
|
90
|
+
const own = describedBy?.split(/\s+/).filter(Boolean) ?? [];
|
|
91
|
+
const merged = [
|
|
92
|
+
...new Set([...context.ids.error, ...context.ids.description, ...own]),
|
|
93
|
+
];
|
|
94
|
+
return merged.length > 0 ? merged.join(" ") : undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export { FieldAriaProvider, useFieldAriaRegistration, useFieldDescribedBy };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
/** What the view knows about an image file. */
|
|
4
|
+
export interface ImageFileInfo {
|
|
5
|
+
/** MIME type, from the response — e.g. "image/jpeg". */
|
|
6
|
+
type?: string;
|
|
7
|
+
/** File size in bytes, from the response. */
|
|
8
|
+
bytes?: number;
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const FORMATS: Record<string, string> = {
|
|
14
|
+
"image/jpeg": "JPEG",
|
|
15
|
+
"image/png": "PNG",
|
|
16
|
+
"image/webp": "WebP",
|
|
17
|
+
"image/gif": "GIF",
|
|
18
|
+
"image/avif": "AVIF",
|
|
19
|
+
"image/svg+xml": "SVG",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function formatOf(src: string, type?: string): string | undefined {
|
|
23
|
+
const fromType = type ? FORMATS[type.split(";")[0]?.trim() ?? ""] : undefined;
|
|
24
|
+
if (fromType) return fromType;
|
|
25
|
+
if (src.startsWith("data:")) return FORMATS[src.slice(5, src.indexOf(";"))];
|
|
26
|
+
try {
|
|
27
|
+
const path = new URL(src, window.location.href).pathname;
|
|
28
|
+
const ext = /\.([a-z0-9]{2,5})$/i.exec(path)?.[1];
|
|
29
|
+
if (ext) return ext.toLowerCase() === "jpg" ? "JPEG" : ext.toUpperCase();
|
|
30
|
+
} catch {
|
|
31
|
+
/* no extension to read */
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatBytes(bytes: number): string {
|
|
37
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
38
|
+
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
|
39
|
+
return `${(bytes / (1024 * 1024)).toFixed(1).replace(/\.0$/, "")} MB`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* One line for the details tooltip: "JPEG · 1600 × 1067 px · 245 KB". Only
|
|
44
|
+
* what is known; a dash while nothing is.
|
|
45
|
+
*/
|
|
46
|
+
export function describeImageFile(
|
|
47
|
+
info: (ImageFileInfo & { src: string }) | undefined
|
|
48
|
+
): string {
|
|
49
|
+
if (!info) return "—";
|
|
50
|
+
const parts = [
|
|
51
|
+
formatOf(info.src, info.type),
|
|
52
|
+
info.width && info.height ? `${info.width} × ${info.height} px` : undefined,
|
|
53
|
+
info.bytes ? formatBytes(info.bytes) : undefined,
|
|
54
|
+
].filter(Boolean);
|
|
55
|
+
return parts.length ? parts.join(" · ") : "—";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Looks up the file at `src` while `enabled`: its size in pixels from the
|
|
60
|
+
* image itself, its type and weight from a HEAD request (left out when the
|
|
61
|
+
* host does not allow it). Each file is looked up once.
|
|
62
|
+
*/
|
|
63
|
+
export function useImageFileInfo(
|
|
64
|
+
src: string | undefined,
|
|
65
|
+
enabled: boolean
|
|
66
|
+
): (ImageFileInfo & { src: string }) | undefined {
|
|
67
|
+
const [known, setKnown] = React.useState<ReadonlyMap<string, ImageFileInfo>>(
|
|
68
|
+
() => new Map()
|
|
69
|
+
);
|
|
70
|
+
const askedRef = React.useRef(new Set<string>());
|
|
71
|
+
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
if (!enabled || !src || askedRef.current.has(src)) return;
|
|
74
|
+
askedRef.current.add(src);
|
|
75
|
+
const note = (patch: ImageFileInfo) =>
|
|
76
|
+
setKnown((prev) =>
|
|
77
|
+
new Map(prev).set(src, { ...prev.get(src), ...patch })
|
|
78
|
+
);
|
|
79
|
+
const image = new Image();
|
|
80
|
+
image.src = src;
|
|
81
|
+
image
|
|
82
|
+
.decode()
|
|
83
|
+
.then(() =>
|
|
84
|
+
note({ width: image.naturalWidth, height: image.naturalHeight })
|
|
85
|
+
)
|
|
86
|
+
.catch(() => {});
|
|
87
|
+
if (src.startsWith("data:")) return;
|
|
88
|
+
fetch(src, { method: "HEAD", mode: "cors" })
|
|
89
|
+
.then((response) => {
|
|
90
|
+
if (!response.ok) return;
|
|
91
|
+
const bytes = Number(response.headers.get("content-length"));
|
|
92
|
+
note({
|
|
93
|
+
type: response.headers.get("content-type") ?? undefined,
|
|
94
|
+
bytes: bytes > 0 ? bytes : undefined,
|
|
95
|
+
});
|
|
96
|
+
})
|
|
97
|
+
.catch(() => {});
|
|
98
|
+
}, [src, enabled]);
|
|
99
|
+
|
|
100
|
+
const info = src ? known.get(src) : undefined;
|
|
101
|
+
return src ? { src, ...info } : undefined;
|
|
102
|
+
}
|