@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,908 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { composeEventHandlers } from "../lib/compose-event-handlers";
|
|
4
|
+
import { useControllable } from "../lib/use-controllable";
|
|
5
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
6
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
7
|
+
import { AspectRatio } from "./aspect-ratio";
|
|
8
|
+
import { Dialog, DialogTitle, DialogTrigger } from "./dialog";
|
|
9
|
+
import { MediaEmpty } from "./media-empty";
|
|
10
|
+
import {
|
|
11
|
+
ItemActionsMenu,
|
|
12
|
+
MediaViewerActionsRegistry,
|
|
13
|
+
MediaViewerContext,
|
|
14
|
+
MediaViewerControlsContext,
|
|
15
|
+
TruncatedText,
|
|
16
|
+
defaultActionsLabel,
|
|
17
|
+
defaultTriggerLabel,
|
|
18
|
+
useMediaViewerContext,
|
|
19
|
+
type MediaViewerItem,
|
|
20
|
+
} from "../lib/media-viewer-internal";
|
|
21
|
+
import { MediaViewerContent } from "../lib/media-viewer-content";
|
|
22
|
+
|
|
23
|
+
export type { MediaViewerItem } from "../lib/media-viewer-internal";
|
|
24
|
+
export type { MediaViewerContentProps } from "../lib/media-viewer-content";
|
|
25
|
+
|
|
26
|
+
/* -------------------------------------------------------------------------------------------------
|
|
27
|
+
* MediaViewer
|
|
28
|
+
*
|
|
29
|
+
* One family for an image set: the previews that open it and the full view.
|
|
30
|
+
*
|
|
31
|
+
* <MediaViewer items={photos}>
|
|
32
|
+
* <MediaViewerTrigger dataHook="photo-tile" indicator />
|
|
33
|
+
* <MediaViewerContent dataHook="photo-viewer">
|
|
34
|
+
* <MediaViewerTitle>Riverside Plumbing</MediaViewerTitle>
|
|
35
|
+
* <MediaViewerActions>…</MediaViewerActions>
|
|
36
|
+
* </MediaViewerContent>
|
|
37
|
+
* </MediaViewer>
|
|
38
|
+
*
|
|
39
|
+
* The root owns the set and the state — which item is open, and whether the
|
|
40
|
+
* view is — so a single tile and a grid of tiles are the same markup: each
|
|
41
|
+
* trigger opens the view at its own `index`. Both states are uncontrolled by
|
|
42
|
+
* default and controllable (`open`/`onOpenChange`, `index`/`onIndexChange`).
|
|
43
|
+
*
|
|
44
|
+
* The view is a lightbox modelled on Telegram's: a Dialog that fills the
|
|
45
|
+
* window over a backdrop darker than a modal's (the panel is transparent and
|
|
46
|
+
* scopes the `dark` token set so its text reads on it, whatever the theme).
|
|
47
|
+
* The image takes the stage, with Previous and Next at its sides — shown on
|
|
48
|
+
* hover or keyboard focus; touch screens swipe. A bar along the bottom carries
|
|
49
|
+
* the title, the open image's description and date on the left, a strip of thumbnails in the middle
|
|
50
|
+
* (the current one at full opacity) and the consumer's `MediaViewerActions` on
|
|
51
|
+
* the right. There is no close button: Esc or a click on the image or the
|
|
52
|
+
* backdrop closes the view. The focus trap is Dialog's; focus returns to the
|
|
53
|
+
* trigger that opened the view, not to the last one mounted.
|
|
54
|
+
*
|
|
55
|
+
* A photo is fitted to the stage. A capture far taller than wide (a saved
|
|
56
|
+
* results page) is shown at the stage's width and scrolls — fitting it would
|
|
57
|
+
* shrink it past reading.
|
|
58
|
+
*
|
|
59
|
+
* "Which item is open" is text, not just a visual: "2 of 5" sits in a polite
|
|
60
|
+
* live region (visually hidden — the thumbnail strip shows it), and the
|
|
61
|
+
* current thumbnail carries `aria-current`.
|
|
62
|
+
*
|
|
63
|
+
* An item with `expired` set, or whose image fails to load, shows `MediaEmpty`
|
|
64
|
+
* in the same frame — the view keeps its size and its pager, so the reader can
|
|
65
|
+
* step past it.
|
|
66
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
67
|
+
|
|
68
|
+
/** Horizontal travel, in px, that browses a preview to the next image. */
|
|
69
|
+
const PREVIEW_SWIPE = 30;
|
|
70
|
+
|
|
71
|
+
/** Quiet time, in ms, that ends a trackpad gesture over a preview. */
|
|
72
|
+
const PREVIEW_WHEEL_QUIET = 250;
|
|
73
|
+
|
|
74
|
+
/** After a step, how many times its faintest inertia delta a delta must
|
|
75
|
+
rise to, once the inertia fades, to count as the next swipe. */
|
|
76
|
+
const PREVIEW_WHEEL_RISE = 2;
|
|
77
|
+
|
|
78
|
+
/** The most dots a trigger's indicator shows, however long the set. */
|
|
79
|
+
const INDICATOR_DOTS = 5;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Props for the MediaViewer root.
|
|
83
|
+
*/
|
|
84
|
+
export interface MediaViewerProps {
|
|
85
|
+
/**
|
|
86
|
+
* The set of images. Triggers preview them and the view steps through them.
|
|
87
|
+
*/
|
|
88
|
+
items: MediaViewerItem[];
|
|
89
|
+
/**
|
|
90
|
+
* Zero-based index of the open item (controlled).
|
|
91
|
+
*/
|
|
92
|
+
index?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Index of the open item when uncontrolled.
|
|
95
|
+
* @default 0
|
|
96
|
+
*/
|
|
97
|
+
defaultIndex?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Called with the new index when a trigger opens the view or the reader
|
|
100
|
+
* steps through the set.
|
|
101
|
+
*/
|
|
102
|
+
onIndexChange?: (index: number) => void;
|
|
103
|
+
/**
|
|
104
|
+
* Whether the view is open (controlled).
|
|
105
|
+
*/
|
|
106
|
+
open?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Whether the view is open when uncontrolled.
|
|
109
|
+
* @default false
|
|
110
|
+
*/
|
|
111
|
+
defaultOpen?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Called when the view opens or closes.
|
|
114
|
+
*/
|
|
115
|
+
onOpenChange?: (open: boolean) => void;
|
|
116
|
+
/**
|
|
117
|
+
* Actions on one item — DropdownMenuItems (Edit description, Replace,
|
|
118
|
+
* Remove) for the item and its index. The family puts them behind a More
|
|
119
|
+
* actions button in two places: the top-right corner of each preview
|
|
120
|
+
* (shown on hover, keyboard focus and while open; always on touch
|
|
121
|
+
* screens) and the view's bottom bar, after the built-in controls.
|
|
122
|
+
*/
|
|
123
|
+
actions?: (item: MediaViewerItem, index: number) => React.ReactNode;
|
|
124
|
+
/**
|
|
125
|
+
* Accessible label for the More actions button, per item — it names the
|
|
126
|
+
* item, as a grid shows one per preview.
|
|
127
|
+
* @default (item) => `More actions for ${item.alt}`
|
|
128
|
+
*/
|
|
129
|
+
formatActionsLabel?: (item: MediaViewerItem, index: number) => string;
|
|
130
|
+
/**
|
|
131
|
+
* Whether the previews open a full view. Off, a preview is only a picture
|
|
132
|
+
* — not a button, no hover zoom — that still carries its addons and the
|
|
133
|
+
* item's More actions: an editing grid, say. Leave `MediaViewerContent`
|
|
134
|
+
* out then; nothing of the view runs, and a bundler drops its code.
|
|
135
|
+
* @default true
|
|
136
|
+
*/
|
|
137
|
+
viewable?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* The family's parts: one or more `MediaViewerTrigger`s and the
|
|
140
|
+
* `MediaViewerContent`, anywhere inside — the root renders no DOM.
|
|
141
|
+
*/
|
|
142
|
+
children: React.ReactNode;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Root of the family: owns the image set, which item is open and whether the
|
|
147
|
+
* view is. Renders no DOM of its own.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```tsx
|
|
151
|
+
* <MediaViewer items={photos}>
|
|
152
|
+
* <MediaViewerTrigger dataHook="photo-tile" />
|
|
153
|
+
* <MediaViewerContent dataHook="photo-viewer">
|
|
154
|
+
* <MediaViewerTitle>Business photos</MediaViewerTitle>
|
|
155
|
+
* </MediaViewerContent>
|
|
156
|
+
* </MediaViewer>
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
function MediaViewer({
|
|
160
|
+
items,
|
|
161
|
+
index: indexProp,
|
|
162
|
+
defaultIndex = 0,
|
|
163
|
+
onIndexChange,
|
|
164
|
+
open: openProp,
|
|
165
|
+
defaultOpen = false,
|
|
166
|
+
onOpenChange,
|
|
167
|
+
actions,
|
|
168
|
+
formatActionsLabel,
|
|
169
|
+
viewable = true,
|
|
170
|
+
children,
|
|
171
|
+
}: MediaViewerProps) {
|
|
172
|
+
const [index, setIndex] = useControllable(
|
|
173
|
+
indexProp,
|
|
174
|
+
defaultIndex,
|
|
175
|
+
onIndexChange
|
|
176
|
+
);
|
|
177
|
+
const [open, setOpen] = useControllable(openProp, defaultOpen, onOpenChange);
|
|
178
|
+
const openerRef = React.useRef<HTMLButtonElement | null>(null);
|
|
179
|
+
const triggersRef = React.useRef(new Map<number, HTMLButtonElement>());
|
|
180
|
+
const pointerRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
181
|
+
React.useEffect(() => {
|
|
182
|
+
if (!open) return;
|
|
183
|
+
pointerRef.current = null;
|
|
184
|
+
const track = (event: PointerEvent) => {
|
|
185
|
+
pointerRef.current = { x: event.clientX, y: event.clientY };
|
|
186
|
+
};
|
|
187
|
+
window.addEventListener("pointermove", track, { passive: true });
|
|
188
|
+
window.addEventListener("pointerdown", track, { passive: true });
|
|
189
|
+
return () => {
|
|
190
|
+
window.removeEventListener("pointermove", track);
|
|
191
|
+
window.removeEventListener("pointerdown", track);
|
|
192
|
+
};
|
|
193
|
+
}, [open]);
|
|
194
|
+
const openedAtRef = React.useRef(0);
|
|
195
|
+
const [session, setSession] = React.useState({ open, count: open ? 1 : 0 });
|
|
196
|
+
if (session.open !== open) {
|
|
197
|
+
setSession({ open, count: open ? session.count + 1 : session.count });
|
|
198
|
+
}
|
|
199
|
+
const openCount = session.count;
|
|
200
|
+
const [revealedFor, setRevealedFor] = React.useState(0);
|
|
201
|
+
const revealed = revealedFor === openCount;
|
|
202
|
+
const reveal = React.useCallback(
|
|
203
|
+
() => setRevealedFor(openCount),
|
|
204
|
+
[openCount]
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
/* Removing the last item from an open view leaves nothing to show. */
|
|
208
|
+
const empty = items.length === 0;
|
|
209
|
+
React.useEffect(() => {
|
|
210
|
+
if (open && empty) setOpen(false);
|
|
211
|
+
}, [open, empty, setOpen]);
|
|
212
|
+
|
|
213
|
+
const context = React.useMemo(
|
|
214
|
+
() => ({
|
|
215
|
+
items,
|
|
216
|
+
index,
|
|
217
|
+
setIndex,
|
|
218
|
+
openerRef,
|
|
219
|
+
open,
|
|
220
|
+
setOpen,
|
|
221
|
+
openCount,
|
|
222
|
+
openedAtRef,
|
|
223
|
+
revealed,
|
|
224
|
+
reveal,
|
|
225
|
+
triggersRef,
|
|
226
|
+
pointerRef,
|
|
227
|
+
renderActions: actions ?? null,
|
|
228
|
+
actionsLabel: formatActionsLabel ?? defaultActionsLabel,
|
|
229
|
+
viewable,
|
|
230
|
+
}),
|
|
231
|
+
[
|
|
232
|
+
actions,
|
|
233
|
+
formatActionsLabel,
|
|
234
|
+
viewable,
|
|
235
|
+
items,
|
|
236
|
+
index,
|
|
237
|
+
setIndex,
|
|
238
|
+
open,
|
|
239
|
+
setOpen,
|
|
240
|
+
openCount,
|
|
241
|
+
revealed,
|
|
242
|
+
reveal,
|
|
243
|
+
]
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<MediaViewerContext.Provider value={context}>
|
|
248
|
+
{viewable ? (
|
|
249
|
+
<Dialog open={open} onOpenChange={setOpen}>
|
|
250
|
+
{children}
|
|
251
|
+
</Dialog>
|
|
252
|
+
) : (
|
|
253
|
+
children
|
|
254
|
+
)}
|
|
255
|
+
</MediaViewerContext.Provider>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
MediaViewer.displayName = "MediaViewer";
|
|
260
|
+
|
|
261
|
+
/* -------------------------------------------------------------------------------------------------
|
|
262
|
+
* MediaViewerTrigger
|
|
263
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Props for MediaViewerTrigger.
|
|
267
|
+
*/
|
|
268
|
+
export interface MediaViewerTriggerProps extends Omit<
|
|
269
|
+
React.ComponentPropsWithoutRef<"button">,
|
|
270
|
+
"type"
|
|
271
|
+
> {
|
|
272
|
+
/**
|
|
273
|
+
* Required data-hook attribute for automated testing. The parts derive
|
|
274
|
+
* theirs from it: `-tile`, `-frame`, `-empty`, `-indicator`, `-actions`.
|
|
275
|
+
* @example "photo-tile"
|
|
276
|
+
*/
|
|
277
|
+
dataHook: string;
|
|
278
|
+
/**
|
|
279
|
+
* The item this trigger previews and opens the view at.
|
|
280
|
+
* @default 0
|
|
281
|
+
*/
|
|
282
|
+
index?: number;
|
|
283
|
+
/**
|
|
284
|
+
* Width over height of the frame.
|
|
285
|
+
* @default 4 / 3
|
|
286
|
+
*/
|
|
287
|
+
ratio?: number;
|
|
288
|
+
/**
|
|
289
|
+
* Dots along the bottom of the preview, the shown item marked — for a
|
|
290
|
+
* single preview that stands for the whole set. Such a preview can also be
|
|
291
|
+
* browsed in place: a swipe (touch or trackpad) or the arrow keys show the
|
|
292
|
+
* next or previous image before it is opened, and after the view closes it
|
|
293
|
+
* shows the image the reader stopped on. Leave it off for a grid, where
|
|
294
|
+
* every item has its own trigger.
|
|
295
|
+
* @default false
|
|
296
|
+
*/
|
|
297
|
+
indicator?: boolean;
|
|
298
|
+
/**
|
|
299
|
+
* The line shown when the set has no item at `index`.
|
|
300
|
+
* @default "No photo available"
|
|
301
|
+
*/
|
|
302
|
+
emptyMessage?: string;
|
|
303
|
+
/**
|
|
304
|
+
* The line shown when the item has expired or its image cannot be loaded.
|
|
305
|
+
* @default "Image no longer available"
|
|
306
|
+
*/
|
|
307
|
+
unavailableMessage?: string;
|
|
308
|
+
/**
|
|
309
|
+
* The button's name — set on the button itself, so it holds whether the
|
|
310
|
+
* image loads, has expired or has no `alt`. An `aria-label` passed to the
|
|
311
|
+
* trigger wins over it.
|
|
312
|
+
* @default (item, index, total) => item.alt || `Image ${index + 1} of ${total}`
|
|
313
|
+
*/
|
|
314
|
+
formatLabel?: (item: MediaViewerItem, index: number, total: number) => string;
|
|
315
|
+
/**
|
|
316
|
+
* `MediaViewerAddon`s laid over the preview — a Badge with the position,
|
|
317
|
+
* a status. They sit beside the button, not inside it.
|
|
318
|
+
*/
|
|
319
|
+
children?: React.ReactNode;
|
|
320
|
+
/**
|
|
321
|
+
* Tracking element identifier for analytics.
|
|
322
|
+
* @default "media-viewer-trigger"
|
|
323
|
+
*/
|
|
324
|
+
trackingEl?: string;
|
|
325
|
+
/**
|
|
326
|
+
* Tracking label for analytics context.
|
|
327
|
+
*/
|
|
328
|
+
trackingLabel?: string;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/* Swiping a preview to browse the set: a horizontal touch drag of
|
|
332
|
+
PREVIEW_SWIPE px, or one horizontal trackpad swipe, steps once; the
|
|
333
|
+
click that ends a touch swipe is not a press. Horizontal wheel over the
|
|
334
|
+
preview never reaches the browser as back / forward. */
|
|
335
|
+
function usePreviewSwipe(
|
|
336
|
+
node: HTMLElement | null,
|
|
337
|
+
browse: (delta: -1 | 1) => void
|
|
338
|
+
) {
|
|
339
|
+
const browseRef = React.useRef(browse);
|
|
340
|
+
React.useLayoutEffect(() => {
|
|
341
|
+
browseRef.current = browse;
|
|
342
|
+
});
|
|
343
|
+
const startRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
344
|
+
const swipedRef = React.useRef(false);
|
|
345
|
+
|
|
346
|
+
const onPointerDown = (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
347
|
+
swipedRef.current = false;
|
|
348
|
+
startRef.current =
|
|
349
|
+
node && event.pointerType !== "mouse"
|
|
350
|
+
? { x: event.clientX, y: event.clientY }
|
|
351
|
+
: null;
|
|
352
|
+
};
|
|
353
|
+
const onPointerUp = (event: React.PointerEvent<HTMLButtonElement>) => {
|
|
354
|
+
const start = startRef.current;
|
|
355
|
+
startRef.current = null;
|
|
356
|
+
if (!start) return;
|
|
357
|
+
const dx = event.clientX - start.x;
|
|
358
|
+
const dy = event.clientY - start.y;
|
|
359
|
+
if (Math.abs(dx) < PREVIEW_SWIPE || Math.abs(dx) < Math.abs(dy)) return;
|
|
360
|
+
swipedRef.current = true;
|
|
361
|
+
browseRef.current(dx < 0 ? 1 : -1);
|
|
362
|
+
};
|
|
363
|
+
const consumeSwipe = () => {
|
|
364
|
+
const swiped = swipedRef.current;
|
|
365
|
+
swipedRef.current = false;
|
|
366
|
+
return swiped;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
React.useEffect(() => {
|
|
370
|
+
if (!node) return;
|
|
371
|
+
let travel = 0;
|
|
372
|
+
/* After a step: the inertia's direction, and the run of its deltas — the
|
|
373
|
+
last, the faintest, and whether they have started to fade. */
|
|
374
|
+
let stepped = false;
|
|
375
|
+
let direction = 0;
|
|
376
|
+
let previous = 0;
|
|
377
|
+
let faintest = Infinity;
|
|
378
|
+
let fading = false;
|
|
379
|
+
let quiet = 0;
|
|
380
|
+
const handleWheel = (event: WheelEvent) => {
|
|
381
|
+
if (event.ctrlKey || Math.abs(event.deltaY) >= Math.abs(event.deltaX))
|
|
382
|
+
return;
|
|
383
|
+
event.preventDefault();
|
|
384
|
+
window.clearTimeout(quiet);
|
|
385
|
+
quiet = window.setTimeout(() => {
|
|
386
|
+
travel = 0;
|
|
387
|
+
stepped = false;
|
|
388
|
+
}, PREVIEW_WHEEL_QUIET);
|
|
389
|
+
const delta = event.deltaX;
|
|
390
|
+
const size = Math.abs(delta);
|
|
391
|
+
if (stepped) {
|
|
392
|
+
/* One step per swipe: the rest of it — fingers still speeding up
|
|
393
|
+
just after the step, then inertia — is swallowed. Inertia only
|
|
394
|
+
fades; the next swipe turns the other way, or rises again after
|
|
395
|
+
the fade to twice its faintest delta. Either starts at once,
|
|
396
|
+
without waiting for the inertia to die. */
|
|
397
|
+
/* The faintest is counted from the fade on, not from a run-up that
|
|
398
|
+
was still rising. */
|
|
399
|
+
if (size < previous) {
|
|
400
|
+
if (!fading) faintest = size;
|
|
401
|
+
fading = true;
|
|
402
|
+
}
|
|
403
|
+
if (fading) faintest = Math.min(faintest, size);
|
|
404
|
+
previous = size;
|
|
405
|
+
const turned = Math.sign(delta) !== direction;
|
|
406
|
+
const rose = fading && size >= faintest * PREVIEW_WHEEL_RISE;
|
|
407
|
+
if (!turned && !rose) return;
|
|
408
|
+
stepped = false;
|
|
409
|
+
travel = 0;
|
|
410
|
+
}
|
|
411
|
+
travel += delta;
|
|
412
|
+
if (Math.abs(travel) < PREVIEW_SWIPE) return;
|
|
413
|
+
stepped = true;
|
|
414
|
+
direction = Math.sign(delta);
|
|
415
|
+
previous = size;
|
|
416
|
+
faintest = size;
|
|
417
|
+
fading = false;
|
|
418
|
+
travel = 0;
|
|
419
|
+
browseRef.current(delta > 0 ? 1 : -1);
|
|
420
|
+
};
|
|
421
|
+
node.addEventListener("wheel", handleWheel, { passive: false });
|
|
422
|
+
return () => {
|
|
423
|
+
node.removeEventListener("wheel", handleWheel);
|
|
424
|
+
window.clearTimeout(quiet);
|
|
425
|
+
};
|
|
426
|
+
}, [node]);
|
|
427
|
+
|
|
428
|
+
return { onPointerDown, onPointerUp, consumeSwipe };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* A pressable, fixed-ratio preview of one item that opens the view at it —
|
|
433
|
+
* hover and focus-visible states, optional dots for the set, addons over its
|
|
434
|
+
* corners, the item's More actions when the root has `actions`, and
|
|
435
|
+
* `MediaEmpty` when there is no item. The tile wraps the button: `className`
|
|
436
|
+
* sizes the tile, the other props go to the button.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```tsx
|
|
440
|
+
* <MediaViewerTrigger dataHook="photo-tile" indicator />
|
|
441
|
+
* ```
|
|
442
|
+
*/
|
|
443
|
+
const MediaViewerTrigger = React.forwardRef<
|
|
444
|
+
HTMLButtonElement,
|
|
445
|
+
MediaViewerTriggerProps
|
|
446
|
+
>(
|
|
447
|
+
(
|
|
448
|
+
{
|
|
449
|
+
className,
|
|
450
|
+
dataHook,
|
|
451
|
+
index = 0,
|
|
452
|
+
ratio = 4 / 3,
|
|
453
|
+
indicator = false,
|
|
454
|
+
emptyMessage,
|
|
455
|
+
unavailableMessage,
|
|
456
|
+
formatLabel = defaultTriggerLabel,
|
|
457
|
+
children,
|
|
458
|
+
trackingEl,
|
|
459
|
+
trackingLabel,
|
|
460
|
+
onClick,
|
|
461
|
+
onKeyDown,
|
|
462
|
+
onPointerDown,
|
|
463
|
+
onPointerUp,
|
|
464
|
+
...props
|
|
465
|
+
},
|
|
466
|
+
ref
|
|
467
|
+
) => {
|
|
468
|
+
const {
|
|
469
|
+
items,
|
|
470
|
+
index: openIndex,
|
|
471
|
+
setIndex,
|
|
472
|
+
openerRef,
|
|
473
|
+
open,
|
|
474
|
+
openedAtRef,
|
|
475
|
+
revealed,
|
|
476
|
+
triggersRef,
|
|
477
|
+
pointerRef,
|
|
478
|
+
renderActions,
|
|
479
|
+
actionsLabel,
|
|
480
|
+
viewable,
|
|
481
|
+
} = useMediaViewerContext("MediaViewerTrigger");
|
|
482
|
+
/* A preview that stands for the set (with the indicator) can be
|
|
483
|
+
browsed in place — swipe it, or press the arrow keys — to glance
|
|
484
|
+
through the set before opening it. While the view it opened is open,
|
|
485
|
+
it follows the view, and it stays on the image the view closed on, so
|
|
486
|
+
the close always zooms back into it. */
|
|
487
|
+
const browsable = viewable && indicator && items.length > 1;
|
|
488
|
+
const [browsed, setBrowsed] = React.useState(index);
|
|
489
|
+
const [openedHere, setOpenedHere] = React.useState(false);
|
|
490
|
+
/* The view closing under a resting pointer is not the pointer arriving:
|
|
491
|
+
the preview under it holds off its hover zoom until the pointer
|
|
492
|
+
leaves and comes back. */
|
|
493
|
+
const tileRef = React.useRef<HTMLDivElement>(null);
|
|
494
|
+
const [hoverHeld, setHoverHeld] = React.useState(false);
|
|
495
|
+
React.useLayoutEffect(() => {
|
|
496
|
+
if (open) return;
|
|
497
|
+
const pointer = pointerRef.current;
|
|
498
|
+
const tile = tileRef.current;
|
|
499
|
+
if (!pointer || !tile) return;
|
|
500
|
+
const box = tile.getBoundingClientRect();
|
|
501
|
+
if (
|
|
502
|
+
pointer.x >= box.left &&
|
|
503
|
+
pointer.x <= box.right &&
|
|
504
|
+
pointer.y >= box.top &&
|
|
505
|
+
pointer.y <= box.bottom
|
|
506
|
+
) {
|
|
507
|
+
setHoverHeld(true);
|
|
508
|
+
}
|
|
509
|
+
}, [open, pointerRef]);
|
|
510
|
+
const [wasOpen, setWasOpen] = React.useState(open);
|
|
511
|
+
if (open !== wasOpen) {
|
|
512
|
+
setWasOpen(open);
|
|
513
|
+
if (!open && openedHere) {
|
|
514
|
+
setOpenedHere(false);
|
|
515
|
+
if (browsable) setBrowsed(openIndex);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const shown = !browsable
|
|
519
|
+
? index
|
|
520
|
+
: openedHere && open
|
|
521
|
+
? openIndex
|
|
522
|
+
: Math.min(Math.max(browsed, 0), items.length - 1);
|
|
523
|
+
const browse = (delta: -1 | 1) => {
|
|
524
|
+
const next = shown + delta;
|
|
525
|
+
if (next >= 0 && next < items.length) setBrowsed(next);
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
/* While the view shows this item, the preview steps aside, as if the
|
|
529
|
+
image had lifted out of it: nothing of it shows through the backdrop,
|
|
530
|
+
and the close zooms back into its empty place. It keeps its box, so
|
|
531
|
+
the page does not shift. Only the open item's preview: once the reader
|
|
532
|
+
steps on, the preview they opened from is back, so closing on another
|
|
533
|
+
image never leaves a gap where the first one was. */
|
|
534
|
+
const lifted = open && revealed && openIndex === shown;
|
|
535
|
+
const item = items[shown];
|
|
536
|
+
const src = item?.thumbnailSrc ?? item?.src;
|
|
537
|
+
/* An expired item, or an image that will not load, shows the
|
|
538
|
+
unavailable message in the same frame — as the view does. */
|
|
539
|
+
const [failedSrc, setFailedSrc] = React.useState<string | null>(null);
|
|
540
|
+
const unavailable = Boolean(item && (item.expired || failedSrc === src));
|
|
541
|
+
const dotsFrom = Math.min(
|
|
542
|
+
Math.max(shown - Math.floor(INDICATOR_DOTS / 2), 0),
|
|
543
|
+
Math.max(items.length - INDICATOR_DOTS, 0)
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
/* Register with the root so the view can zoom from and back to this
|
|
547
|
+
preview — under the item it shows. */
|
|
548
|
+
const [node, setNode] = React.useState<HTMLButtonElement | null>(null);
|
|
549
|
+
const setRefs = React.useCallback(
|
|
550
|
+
(el: HTMLButtonElement | null) => {
|
|
551
|
+
setNode(el);
|
|
552
|
+
if (typeof ref === "function") ref(el);
|
|
553
|
+
else if (ref) ref.current = el;
|
|
554
|
+
},
|
|
555
|
+
[ref]
|
|
556
|
+
);
|
|
557
|
+
React.useLayoutEffect(() => {
|
|
558
|
+
if (!node) return;
|
|
559
|
+
const triggers = triggersRef.current;
|
|
560
|
+
triggers.set(shown, node);
|
|
561
|
+
return () => {
|
|
562
|
+
if (triggers.get(shown) === node) triggers.delete(shown);
|
|
563
|
+
};
|
|
564
|
+
}, [node, shown, triggersRef]);
|
|
565
|
+
|
|
566
|
+
const swipe = usePreviewSwipe(browsable ? node : null, browse);
|
|
567
|
+
|
|
568
|
+
const frame = (
|
|
569
|
+
<AspectRatio dataHook={`${dataHook}-frame`} ratio={ratio}>
|
|
570
|
+
{unavailable ? (
|
|
571
|
+
<MediaEmpty
|
|
572
|
+
dataHook={`${dataHook}-empty`}
|
|
573
|
+
message={unavailableMessage ?? "Image no longer available"}
|
|
574
|
+
/>
|
|
575
|
+
) : item && src ? (
|
|
576
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- onError is a load event, not an interaction
|
|
577
|
+
<img
|
|
578
|
+
alt={item.alt}
|
|
579
|
+
className={cn(
|
|
580
|
+
"duration-slow size-full object-cover object-top transition-transform ease-in-out motion-reduce:transition-none",
|
|
581
|
+
viewable &&
|
|
582
|
+
!hoverHeld &&
|
|
583
|
+
"group-hover/tile:scale-110 motion-reduce:group-hover/tile:scale-100"
|
|
584
|
+
)}
|
|
585
|
+
decoding="async"
|
|
586
|
+
draggable={false}
|
|
587
|
+
loading="lazy"
|
|
588
|
+
src={src}
|
|
589
|
+
onError={() => setFailedSrc(src)}
|
|
590
|
+
/>
|
|
591
|
+
) : (
|
|
592
|
+
<MediaEmpty dataHook={`${dataHook}-empty`} message={emptyMessage} />
|
|
593
|
+
)}
|
|
594
|
+
{indicator && items.length > 1 ? (
|
|
595
|
+
<div
|
|
596
|
+
aria-hidden
|
|
597
|
+
className="absolute inset-x-0 bottom-3 flex items-center justify-center gap-1.5"
|
|
598
|
+
data-hook={`${dataHook}-indicator`}
|
|
599
|
+
data-slot="media-viewer-trigger-indicator"
|
|
600
|
+
>
|
|
601
|
+
{/* At most INDICATOR_DOTS dots, a window around this item; a
|
|
602
|
+
smaller dot at an edge says the set goes on that way. */}
|
|
603
|
+
{items.slice(dotsFrom, dotsFrom + INDICATOR_DOTS).map((it, k) => {
|
|
604
|
+
const i = dotsFrom + k;
|
|
605
|
+
const more =
|
|
606
|
+
(k === 0 && dotsFrom > 0) ||
|
|
607
|
+
(k === INDICATOR_DOTS - 1 &&
|
|
608
|
+
dotsFrom + INDICATOR_DOTS < items.length);
|
|
609
|
+
return (
|
|
610
|
+
<span
|
|
611
|
+
key={it.src}
|
|
612
|
+
className={cn(
|
|
613
|
+
"rounded-full shadow-sm",
|
|
614
|
+
more ? "size-1" : "size-1.5",
|
|
615
|
+
i === shown ? "bg-background" : "bg-background/50"
|
|
616
|
+
)}
|
|
617
|
+
/>
|
|
618
|
+
);
|
|
619
|
+
})}
|
|
620
|
+
</div>
|
|
621
|
+
) : null}
|
|
622
|
+
</AspectRatio>
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
/* Without a view the preview is only a picture: no button, no hover
|
|
626
|
+
zoom — what it shows is the image's alt. */
|
|
627
|
+
const trigger = !viewable ? (
|
|
628
|
+
<div
|
|
629
|
+
className="border-border relative block w-full overflow-hidden rounded-lg border"
|
|
630
|
+
data-hook={dataHook}
|
|
631
|
+
data-slot="media-viewer-trigger"
|
|
632
|
+
>
|
|
633
|
+
{frame}
|
|
634
|
+
</div>
|
|
635
|
+
) : (
|
|
636
|
+
<DialogTrigger
|
|
637
|
+
ref={setRefs}
|
|
638
|
+
aria-label={item ? formatLabel(item, shown, items.length) : undefined}
|
|
639
|
+
className={cn(
|
|
640
|
+
"border-border relative block w-full overflow-hidden rounded-lg border text-left outline-none",
|
|
641
|
+
/* Hover is the tile's, not the button's: over an addon or the
|
|
642
|
+
actions the pointer is still on the preview, so the image does
|
|
643
|
+
not jump back as it moves onto them. */
|
|
644
|
+
"duration-fast focus-visible:shadow-default transition-shadow ease-out",
|
|
645
|
+
!hoverHeld && "group-hover/tile:shadow-md",
|
|
646
|
+
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
|
|
647
|
+
browsable && "touch-pan-y"
|
|
648
|
+
)}
|
|
649
|
+
data-slot="media-viewer-trigger"
|
|
650
|
+
data-tracking-el={trackingEl ?? "media-viewer-trigger"}
|
|
651
|
+
data-tracking-label={trackingLabel}
|
|
652
|
+
dataHook={dataHook}
|
|
653
|
+
disabled={!item}
|
|
654
|
+
type="button"
|
|
655
|
+
onClick={(event) => {
|
|
656
|
+
/* The click that ends a swipe browses; it does not open. */
|
|
657
|
+
if (swipe.consumeSwipe()) {
|
|
658
|
+
event.preventDefault();
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
onClick?.(event);
|
|
662
|
+
if (event.defaultPrevented) return;
|
|
663
|
+
openerRef.current = event.currentTarget;
|
|
664
|
+
openedAtRef.current = event.timeStamp;
|
|
665
|
+
setOpenedHere(true);
|
|
666
|
+
setIndex(shown);
|
|
667
|
+
}}
|
|
668
|
+
onKeyDown={(event) => {
|
|
669
|
+
onKeyDown?.(event);
|
|
670
|
+
if (event.defaultPrevented || !browsable) return;
|
|
671
|
+
if (event.key === "ArrowLeft") {
|
|
672
|
+
event.preventDefault();
|
|
673
|
+
browse(-1);
|
|
674
|
+
} else if (event.key === "ArrowRight") {
|
|
675
|
+
event.preventDefault();
|
|
676
|
+
browse(1);
|
|
677
|
+
}
|
|
678
|
+
}}
|
|
679
|
+
onPointerDown={(event) =>
|
|
680
|
+
composeEventHandlers(onPointerDown, swipe.onPointerDown)(event)
|
|
681
|
+
}
|
|
682
|
+
onPointerUp={(event) =>
|
|
683
|
+
composeEventHandlers(onPointerUp, swipe.onPointerUp)(event)
|
|
684
|
+
}
|
|
685
|
+
{...props}
|
|
686
|
+
>
|
|
687
|
+
{frame}
|
|
688
|
+
</DialogTrigger>
|
|
689
|
+
);
|
|
690
|
+
|
|
691
|
+
/* Addons and the actions are siblings of the button, not children:
|
|
692
|
+
interactive content cannot sit inside a button, and a badge over the
|
|
693
|
+
image stays out of its name. */
|
|
694
|
+
return (
|
|
695
|
+
<div
|
|
696
|
+
ref={tileRef}
|
|
697
|
+
className={cn(
|
|
698
|
+
"group/tile relative isolate",
|
|
699
|
+
lifted && "invisible",
|
|
700
|
+
className
|
|
701
|
+
)}
|
|
702
|
+
data-hook={`${dataHook}-tile`}
|
|
703
|
+
data-slot="media-viewer-trigger-tile"
|
|
704
|
+
onPointerLeave={() => setHoverHeld(false)}
|
|
705
|
+
>
|
|
706
|
+
{trigger}
|
|
707
|
+
{children}
|
|
708
|
+
{item && renderActions ? (
|
|
709
|
+
<div
|
|
710
|
+
className={cn(
|
|
711
|
+
"absolute top-3 right-3 z-10 flex",
|
|
712
|
+
"duration-fast opacity-0 transition-opacity ease-out",
|
|
713
|
+
"group-focus-within/tile:opacity-100 group-hover/tile:opacity-100 has-data-[state=open]:opacity-100 pointer-coarse:opacity-100"
|
|
714
|
+
)}
|
|
715
|
+
data-slot="media-viewer-trigger-actions"
|
|
716
|
+
>
|
|
717
|
+
<ItemActionsMenu
|
|
718
|
+
dataHook={`${dataHook}-actions`}
|
|
719
|
+
label={actionsLabel(item, shown)}
|
|
720
|
+
variant="secondary"
|
|
721
|
+
>
|
|
722
|
+
{renderActions(item, shown)}
|
|
723
|
+
</ItemActionsMenu>
|
|
724
|
+
</div>
|
|
725
|
+
) : null}
|
|
726
|
+
</div>
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
);
|
|
730
|
+
|
|
731
|
+
MediaViewerTrigger.displayName = "MediaViewerTrigger";
|
|
732
|
+
|
|
733
|
+
/* -------------------------------------------------------------------------------------------------
|
|
734
|
+
* MediaViewerAddon
|
|
735
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
736
|
+
|
|
737
|
+
const mediaViewerAddonVariants = cva(
|
|
738
|
+
/* Clicks on plain content fall through to the preview; a control inside
|
|
739
|
+
takes its own. */
|
|
740
|
+
"pointer-events-none absolute z-10 flex items-center gap-1 [&_:is(a,button,input,select,textarea,[tabindex])]:pointer-events-auto",
|
|
741
|
+
{
|
|
742
|
+
variants: {
|
|
743
|
+
align: {
|
|
744
|
+
"top-start": "top-3 left-3",
|
|
745
|
+
"top-end": "top-3 right-3",
|
|
746
|
+
"bottom-start": "bottom-3 left-3",
|
|
747
|
+
"bottom-end": "bottom-3 right-3",
|
|
748
|
+
},
|
|
749
|
+
},
|
|
750
|
+
defaultVariants: {
|
|
751
|
+
align: "top-start",
|
|
752
|
+
},
|
|
753
|
+
}
|
|
754
|
+
);
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Props for MediaViewerAddon.
|
|
758
|
+
*/
|
|
759
|
+
export interface MediaViewerAddonProps
|
|
760
|
+
extends
|
|
761
|
+
React.HTMLAttributes<HTMLDivElement>,
|
|
762
|
+
VariantProps<typeof mediaViewerAddonVariants> {
|
|
763
|
+
/**
|
|
764
|
+
* Data hook for automated testing.
|
|
765
|
+
*/
|
|
766
|
+
dataHook?: string;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Content laid over a corner of a `MediaViewerTrigger` — a Badge with the
|
|
771
|
+
* position, a status. Always shown. It sits beside the preview's button, so
|
|
772
|
+
* it is not part of its name, and a click on plain content still opens the
|
|
773
|
+
* view. The top-right corner holds the item's More actions when the root
|
|
774
|
+
* has `actions`.
|
|
775
|
+
*
|
|
776
|
+
* @example
|
|
777
|
+
* ```tsx
|
|
778
|
+
* <MediaViewerTrigger dataHook="photo-1" index={0}>
|
|
779
|
+
* <MediaViewerAddon align="top-start">
|
|
780
|
+
* <Badge dataHook="photo-1-position" variant="secondary">1</Badge>
|
|
781
|
+
* </MediaViewerAddon>
|
|
782
|
+
* </MediaViewerTrigger>
|
|
783
|
+
* ```
|
|
784
|
+
*/
|
|
785
|
+
const MediaViewerAddon = React.forwardRef<
|
|
786
|
+
HTMLDivElement,
|
|
787
|
+
MediaViewerAddonProps
|
|
788
|
+
>(({ className, align, dataHook, ...props }, ref) => (
|
|
789
|
+
<div
|
|
790
|
+
ref={ref}
|
|
791
|
+
className={cn(mediaViewerAddonVariants({ align }), className)}
|
|
792
|
+
data-hook={dataHook}
|
|
793
|
+
data-slot="media-viewer-addon"
|
|
794
|
+
{...props}
|
|
795
|
+
/>
|
|
796
|
+
));
|
|
797
|
+
|
|
798
|
+
MediaViewerAddon.displayName = "MediaViewerAddon";
|
|
799
|
+
|
|
800
|
+
/* -------------------------------------------------------------------------------------------------
|
|
801
|
+
* MediaViewerTitle
|
|
802
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Props for MediaViewerTitle.
|
|
806
|
+
*/
|
|
807
|
+
export interface MediaViewerTitleProps extends React.ComponentPropsWithoutRef<
|
|
808
|
+
typeof DialogTitle
|
|
809
|
+
> {
|
|
810
|
+
/**
|
|
811
|
+
* Data hook for automated testing.
|
|
812
|
+
*/
|
|
813
|
+
dataHook?: string;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* The view's title — names the dialog (`aria-labelledby`). Sits at the left of
|
|
818
|
+
* the bottom bar: who or what the images belong to.
|
|
819
|
+
*/
|
|
820
|
+
const MediaViewerTitle = React.forwardRef<
|
|
821
|
+
HTMLHeadingElement,
|
|
822
|
+
MediaViewerTitleProps
|
|
823
|
+
>(({ className, dataHook, children, ...props }, ref) => {
|
|
824
|
+
/* A long title is cut to one line; hover shows it in full. */
|
|
825
|
+
const forward = React.useCallback(
|
|
826
|
+
(node: HTMLHeadingElement | null) => {
|
|
827
|
+
if (typeof ref === "function") ref(node);
|
|
828
|
+
else if (ref) ref.current = node;
|
|
829
|
+
},
|
|
830
|
+
[ref]
|
|
831
|
+
);
|
|
832
|
+
return (
|
|
833
|
+
<TruncatedText<HTMLHeadingElement>
|
|
834
|
+
dataHook={dataHook ?? "media-viewer-title"}
|
|
835
|
+
forwardRef={forward}
|
|
836
|
+
full={children}
|
|
837
|
+
render={(measure) => (
|
|
838
|
+
<DialogTitle
|
|
839
|
+
ref={measure}
|
|
840
|
+
className={cn("text-label truncate [grid-area:title]", className)}
|
|
841
|
+
data-slot="media-viewer-title"
|
|
842
|
+
dataHook={dataHook}
|
|
843
|
+
{...props}
|
|
844
|
+
>
|
|
845
|
+
{children}
|
|
846
|
+
</DialogTitle>
|
|
847
|
+
)}
|
|
848
|
+
/>
|
|
849
|
+
);
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
MediaViewerTitle.displayName = "MediaViewerTitle";
|
|
853
|
+
|
|
854
|
+
/* -------------------------------------------------------------------------------------------------
|
|
855
|
+
* MediaViewerActions
|
|
856
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* Props for MediaViewerActions.
|
|
860
|
+
*/
|
|
861
|
+
export interface MediaViewerActionsProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
862
|
+
/**
|
|
863
|
+
* Data hook for automated testing.
|
|
864
|
+
*/
|
|
865
|
+
dataHook?: string;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/**
|
|
869
|
+
* Your own actions for the open image — share, say — at the right of the
|
|
870
|
+
* bottom bar, after the built-in Download, Zoom in and Zoom out, and before
|
|
871
|
+
* the item's More actions (the root's `actions`). Use ghost icon Buttons.
|
|
872
|
+
* Leave it out when the built-in controls are enough.
|
|
873
|
+
*/
|
|
874
|
+
const MediaViewerActions = React.forwardRef<
|
|
875
|
+
HTMLDivElement,
|
|
876
|
+
MediaViewerActionsProps
|
|
877
|
+
>(({ className, dataHook, children, ...props }, ref) => {
|
|
878
|
+
const { lead, more } = React.useContext(MediaViewerControlsContext);
|
|
879
|
+
const register = React.useContext(MediaViewerActionsRegistry);
|
|
880
|
+
React.useLayoutEffect(() => register?.(), [register]);
|
|
881
|
+
return (
|
|
882
|
+
<div
|
|
883
|
+
ref={ref}
|
|
884
|
+
className={cn(
|
|
885
|
+
"flex items-center justify-end gap-1 [grid-area:actions]",
|
|
886
|
+
className
|
|
887
|
+
)}
|
|
888
|
+
data-hook={dataHook}
|
|
889
|
+
data-slot="media-viewer-actions"
|
|
890
|
+
{...props}
|
|
891
|
+
>
|
|
892
|
+
{lead}
|
|
893
|
+
{children}
|
|
894
|
+
{more}
|
|
895
|
+
</div>
|
|
896
|
+
);
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
MediaViewerActions.displayName = "MediaViewerActions";
|
|
900
|
+
|
|
901
|
+
export {
|
|
902
|
+
MediaViewer,
|
|
903
|
+
MediaViewerActions,
|
|
904
|
+
MediaViewerAddon,
|
|
905
|
+
MediaViewerContent,
|
|
906
|
+
MediaViewerTitle,
|
|
907
|
+
MediaViewerTrigger,
|
|
908
|
+
};
|