@brightlocal/ui-components 3.2.0 → 3.3.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.
Files changed (428) hide show
  1. package/AI_USAGE.md +10 -0
  2. package/README.md +3 -1
  3. package/component-meta.json +1271 -428
  4. package/deprecations.json +44 -0
  5. package/dist/_virtual/_rolldown/runtime.cjs +1 -0
  6. package/dist/accordion.cjs +1 -1
  7. package/dist/accordion.js +73 -102
  8. package/dist/alert-dialog.cjs +1 -1
  9. package/dist/alert-dialog.d.ts +6 -4
  10. package/dist/alert-dialog.d.ts.map +1 -1
  11. package/dist/alert-dialog.js +85 -133
  12. package/dist/alert.cjs +1 -1
  13. package/dist/alert.d.ts +4 -4
  14. package/dist/alert.d.ts.map +1 -1
  15. package/dist/alert.js +106 -168
  16. package/dist/aspect-ratio.cjs +1 -1
  17. package/dist/aspect-ratio.js +13 -16
  18. package/dist/avatar.cjs +1 -1
  19. package/dist/avatar.js +30 -45
  20. package/dist/badge.cjs +1 -1
  21. package/dist/badge.d.ts +23 -1
  22. package/dist/badge.d.ts.map +1 -1
  23. package/dist/badge.js +90 -91
  24. package/dist/banner.cjs +1 -1
  25. package/dist/banner.js +102 -148
  26. package/dist/blocks/empty-state.cjs +1 -1
  27. package/dist/blocks/empty-state.d.ts +20 -3
  28. package/dist/blocks/empty-state.d.ts.map +1 -1
  29. package/dist/blocks/empty-state.js +73 -140
  30. package/dist/blocks/input-list.cjs +2 -2
  31. package/dist/blocks/input-list.d.ts +2 -2
  32. package/dist/blocks/input-list.d.ts.map +1 -1
  33. package/dist/blocks/input-list.js +136 -176
  34. package/dist/blocks/progress-state.cjs +1 -1
  35. package/dist/blocks/progress-state.js +223 -347
  36. package/dist/blocks/sidebar.cjs +1 -1
  37. package/dist/blocks/sidebar.d.ts +11 -6
  38. package/dist/blocks/sidebar.d.ts.map +1 -1
  39. package/dist/blocks/sidebar.js +763 -1092
  40. package/dist/brand-illustration-motif.cjs +1 -1
  41. package/dist/brand-illustration-motif.js +40 -61
  42. package/dist/breadcrumb.cjs +1 -1
  43. package/dist/breadcrumb.js +62 -102
  44. package/dist/button-group.cjs +1 -1
  45. package/dist/button-group.js +23 -31
  46. package/dist/button.cjs +1 -1
  47. package/dist/button.d.ts +4 -2
  48. package/dist/button.d.ts.map +1 -1
  49. package/dist/button.js +149 -189
  50. package/dist/calendar.cjs +1 -1
  51. package/dist/calendar.d.ts +87 -3
  52. package/dist/calendar.d.ts.map +1 -1
  53. package/dist/calendar.js +116 -173
  54. package/dist/callout.cjs +1 -1
  55. package/dist/callout.js +73 -96
  56. package/dist/card.cjs +1 -1
  57. package/dist/card.d.ts +10 -4
  58. package/dist/card.d.ts.map +1 -1
  59. package/dist/card.js +233 -316
  60. package/dist/carousel.cjs +1 -1
  61. package/dist/carousel.d.ts +3 -3
  62. package/dist/carousel.d.ts.map +1 -1
  63. package/dist/carousel.js +174 -268
  64. package/dist/centred-layout.cjs +1 -1
  65. package/dist/centred-layout.js +27 -48
  66. package/dist/chart-empty.cjs +1 -1
  67. package/dist/chart-empty.js +18 -27
  68. package/dist/chart.cjs +7 -7
  69. package/dist/chart.d.ts +23 -13
  70. package/dist/chart.d.ts.map +1 -1
  71. package/dist/chart.js +445 -651
  72. package/dist/checkbox.cjs +1 -1
  73. package/dist/checkbox.js +36 -69
  74. package/dist/chip.cjs +1 -1
  75. package/dist/chip.js +116 -173
  76. package/dist/collapsible.cjs +1 -1
  77. package/dist/collapsible.d.ts.map +1 -1
  78. package/dist/collapsible.js +35 -41
  79. package/dist/combobox.cjs +3 -3
  80. package/dist/combobox.d.ts +14 -4
  81. package/dist/combobox.d.ts.map +1 -1
  82. package/dist/combobox.js +439 -638
  83. package/dist/command.cjs +1 -1
  84. package/dist/command.d.ts.map +1 -1
  85. package/dist/command.js +273 -420
  86. package/dist/context-menu.cjs +1 -1
  87. package/dist/context-menu.d.ts +5 -5
  88. package/dist/context-menu.d.ts.map +1 -1
  89. package/dist/context-menu.js +121 -191
  90. package/dist/data-table.cjs +1 -1
  91. package/dist/data-table.d.ts +280 -129
  92. package/dist/data-table.d.ts.map +1 -1
  93. package/dist/data-table.js +687 -948
  94. package/dist/date-picker.cjs +1 -1
  95. package/dist/date-picker.d.ts +15 -4
  96. package/dist/date-picker.d.ts.map +1 -1
  97. package/dist/date-picker.js +156 -231
  98. package/dist/delta.cjs +1 -0
  99. package/dist/delta.d.ts +79 -0
  100. package/dist/delta.d.ts.map +1 -0
  101. package/dist/delta.js +62 -0
  102. package/dist/description.cjs +1 -1
  103. package/dist/description.js +11 -20
  104. package/dist/dialog.cjs +1 -1
  105. package/dist/dialog.d.ts +1 -1
  106. package/dist/dialog.d.ts.map +1 -1
  107. package/dist/dialog.js +76 -116
  108. package/dist/drawer.cjs +1 -1
  109. package/dist/drawer.d.ts +11 -11
  110. package/dist/drawer.d.ts.map +1 -1
  111. package/dist/drawer.js +76 -134
  112. package/dist/dropdown-menu.cjs +1 -1
  113. package/dist/dropdown-menu.d.ts +6 -6
  114. package/dist/dropdown-menu.d.ts.map +1 -1
  115. package/dist/dropdown-menu.js +179 -300
  116. package/dist/field.cjs +1 -1
  117. package/dist/field.d.ts +9 -9
  118. package/dist/field.d.ts.map +1 -1
  119. package/dist/field.js +166 -286
  120. package/dist/global-layout.cjs +1 -1
  121. package/dist/global-layout.js +203 -342
  122. package/dist/grid.cjs +1 -1
  123. package/dist/grid.js +16 -29
  124. package/dist/header.cjs +1 -1
  125. package/dist/header.js +21 -38
  126. package/dist/height-transition.cjs +1 -1
  127. package/dist/height-transition.js +38 -55
  128. package/dist/hooks/use-breakpoint.cjs +1 -1
  129. package/dist/hooks/use-breakpoint.js +9 -9
  130. package/dist/hooks/use-debounce.cjs +1 -1
  131. package/dist/hooks/use-debounce.js +12 -11
  132. package/dist/hooks/use-delayed-pending.cjs +1 -1
  133. package/dist/hooks/use-delayed-pending.js +8 -9
  134. package/dist/hooks/use-filter-search-params.cjs +1 -0
  135. package/dist/hooks/use-filter-search-params.d.ts +93 -0
  136. package/dist/hooks/use-filter-search-params.d.ts.map +1 -0
  137. package/dist/hooks/use-filter-search-params.js +122 -0
  138. package/dist/hooks/use-mobile.cjs +1 -1
  139. package/dist/hooks/use-mobile.js +6 -6
  140. package/dist/hooks/use-overlay-pointer-lock.cjs +1 -1
  141. package/dist/hooks/use-overlay-pointer-lock.js +8 -9
  142. package/dist/hooks/use-prefers-reduced-motion.cjs +1 -1
  143. package/dist/hooks/use-prefers-reduced-motion.js +5 -8
  144. package/dist/hover-card.cjs +1 -1
  145. package/dist/hover-card.d.ts +1 -1
  146. package/dist/hover-card.d.ts.map +1 -1
  147. package/dist/hover-card.js +30 -62
  148. package/dist/index.cjs +1 -1
  149. package/dist/index.d.ts +6 -0
  150. package/dist/index.d.ts.map +1 -1
  151. package/dist/index.js +630 -608
  152. package/dist/info-tooltip.cjs +1 -1
  153. package/dist/info-tooltip.js +22 -36
  154. package/dist/input-chip.cjs +1 -1
  155. package/dist/input-chip.d.ts +32 -3
  156. package/dist/input-chip.d.ts.map +1 -1
  157. package/dist/input-chip.js +181 -230
  158. package/dist/input-group.cjs +1 -1
  159. package/dist/input-group.js +174 -221
  160. package/dist/input-otp.cjs +1 -1
  161. package/dist/input-otp.d.ts +3 -3
  162. package/dist/input-otp.d.ts.map +1 -1
  163. package/dist/input-otp.js +102 -146
  164. package/dist/input-password.cjs +1 -1
  165. package/dist/input-password.d.ts +12 -16
  166. package/dist/input-password.d.ts.map +1 -1
  167. package/dist/input-password.js +139 -171
  168. package/dist/input.cjs +1 -1
  169. package/dist/input.d.ts.map +1 -1
  170. package/dist/input.js +51 -73
  171. package/dist/label.cjs +1 -1
  172. package/dist/label.js +20 -38
  173. package/dist/lib/block-illustration.cjs +1 -1
  174. package/dist/lib/block-illustration.d.ts +1 -1
  175. package/dist/lib/block-illustration.d.ts.map +1 -1
  176. package/dist/lib/block-illustration.js +16 -18
  177. package/dist/lib/compose-event-handlers.cjs +1 -0
  178. package/dist/lib/compose-event-handlers.d.ts +9 -0
  179. package/dist/lib/compose-event-handlers.d.ts.map +1 -0
  180. package/dist/lib/compose-event-handlers.js +8 -0
  181. package/dist/lib/download-file.cjs +1 -0
  182. package/dist/lib/download-file.d.ts +14 -0
  183. package/dist/lib/download-file.d.ts.map +1 -0
  184. package/dist/lib/download-file.js +40 -0
  185. package/dist/lib/empty-canvas.cjs +1 -1
  186. package/dist/lib/empty-canvas.js +28 -56
  187. package/dist/lib/empty-chip.cjs +1 -1
  188. package/dist/lib/empty-chip.d.ts +14 -3
  189. package/dist/lib/empty-chip.d.ts.map +1 -1
  190. package/dist/lib/empty-chip.js +15 -11
  191. package/dist/lib/field-aria.cjs +1 -0
  192. package/dist/lib/field-aria.d.ts +34 -0
  193. package/dist/lib/field-aria.d.ts.map +1 -0
  194. package/dist/lib/field-aria.js +52 -0
  195. package/dist/lib/image-file-info.cjs +1 -0
  196. package/dist/lib/image-file-info.d.ts +25 -0
  197. package/dist/lib/image-file-info.d.ts.map +1 -0
  198. package/dist/lib/image-file-info.js +67 -0
  199. package/dist/lib/media-viewer-content.cjs +1 -0
  200. package/dist/lib/media-viewer-content.d.ts +102 -0
  201. package/dist/lib/media-viewer-content.d.ts.map +1 -0
  202. package/dist/lib/media-viewer-content.js +650 -0
  203. package/dist/lib/media-viewer-internal.cjs +1 -0
  204. package/dist/lib/media-viewer-internal.d.ts +98 -0
  205. package/dist/lib/media-viewer-internal.d.ts.map +1 -0
  206. package/dist/lib/media-viewer-internal.js +61 -0
  207. package/dist/lib/media-viewer-motion.cjs +1 -0
  208. package/dist/lib/media-viewer-motion.d.ts +40 -0
  209. package/dist/lib/media-viewer-motion.d.ts.map +1 -0
  210. package/dist/lib/media-viewer-motion.js +37 -0
  211. package/dist/lib/motion.cjs +1 -1
  212. package/dist/lib/motion.d.ts +8 -0
  213. package/dist/lib/motion.d.ts.map +1 -1
  214. package/dist/lib/motion.js +11 -10
  215. package/dist/lib/optical-center.cjs +1 -1
  216. package/dist/lib/optical-center.d.ts +1 -1
  217. package/dist/lib/optical-center.d.ts.map +1 -1
  218. package/dist/lib/optical-center.js +12 -5
  219. package/dist/lib/spring-settle.cjs +1 -1
  220. package/dist/lib/spring-settle.js +1 -1
  221. package/dist/lib/table-internal.cjs +1 -0
  222. package/dist/lib/table-internal.d.ts +62 -0
  223. package/dist/lib/table-internal.d.ts.map +1 -0
  224. package/dist/lib/table-internal.js +18 -0
  225. package/dist/lib/use-controllable.cjs +1 -0
  226. package/dist/lib/use-controllable.d.ts +12 -0
  227. package/dist/lib/use-controllable.d.ts.map +1 -0
  228. package/dist/lib/use-controllable.js +15 -0
  229. package/dist/lib/use-dev-warning.cjs +1 -1
  230. package/dist/lib/utils.cjs +1 -1
  231. package/dist/lib/utils.js +26 -33
  232. package/dist/link.cjs +1 -1
  233. package/dist/link.d.ts +1 -1
  234. package/dist/link.js +78 -124
  235. package/dist/list.cjs +1 -1
  236. package/dist/list.d.ts +8 -0
  237. package/dist/list.d.ts.map +1 -1
  238. package/dist/list.js +94 -156
  239. package/dist/loading.cjs +1 -1
  240. package/dist/loading.js +124 -151
  241. package/dist/logo-mark.cjs +1 -0
  242. package/dist/logo-mark.d.ts +79 -0
  243. package/dist/logo-mark.d.ts.map +1 -0
  244. package/dist/logo-mark.js +219 -0
  245. package/dist/logo.cjs +1 -1
  246. package/dist/logo.d.ts +1 -1
  247. package/dist/logo.d.ts.map +1 -1
  248. package/dist/logo.js +51 -87
  249. package/dist/map.cjs +1 -1
  250. package/dist/map.d.ts.map +1 -1
  251. package/dist/map.js +390 -586
  252. package/dist/media-empty.cjs +1 -1
  253. package/dist/media-empty.js +27 -42
  254. package/dist/media-viewer.cjs +1 -0
  255. package/dist/media-viewer.d.ts +224 -0
  256. package/dist/media-viewer.d.ts.map +1 -0
  257. package/dist/media-viewer.js +295 -0
  258. package/dist/menubar.cjs +1 -1
  259. package/dist/menubar.d.ts +14 -14
  260. package/dist/menubar.d.ts.map +1 -1
  261. package/dist/menubar.js +130 -267
  262. package/dist/multi-select-filter.cjs +1 -1
  263. package/dist/multi-select-filter.d.ts +9 -1
  264. package/dist/multi-select-filter.d.ts.map +1 -1
  265. package/dist/multi-select-filter.js +357 -446
  266. package/dist/navigation-menu.cjs +1 -1
  267. package/dist/navigation-menu.js +108 -172
  268. package/dist/pagination.cjs +1 -1
  269. package/dist/pagination.d.ts +36 -12
  270. package/dist/pagination.d.ts.map +1 -1
  271. package/dist/pagination.js +208 -281
  272. package/dist/popover.cjs +1 -1
  273. package/dist/popover.d.ts +1 -1
  274. package/dist/popover.d.ts.map +1 -1
  275. package/dist/popover.js +34 -45
  276. package/dist/progress.cjs +1 -1
  277. package/dist/progress.d.ts +17 -2
  278. package/dist/progress.d.ts.map +1 -1
  279. package/dist/progress.js +116 -162
  280. package/dist/radio-group.cjs +1 -1
  281. package/dist/radio-group.js +90 -146
  282. package/dist/rating.cjs +1 -1
  283. package/dist/rating.d.ts +80 -13
  284. package/dist/rating.d.ts.map +1 -1
  285. package/dist/rating.js +70 -65
  286. package/dist/relative-date.cjs +1 -1
  287. package/dist/relative-date.d.ts.map +1 -1
  288. package/dist/relative-date.js +73 -89
  289. package/dist/resizable.cjs +1 -1
  290. package/dist/resizable.d.ts +39 -23
  291. package/dist/resizable.d.ts.map +1 -1
  292. package/dist/resizable.js +42 -44
  293. package/dist/result-count.cjs +1 -0
  294. package/dist/result-count.d.ts +110 -0
  295. package/dist/result-count.d.ts.map +1 -0
  296. package/dist/result-count.js +49 -0
  297. package/dist/route-progress.cjs +1 -1
  298. package/dist/route-progress.js +58 -102
  299. package/dist/scroll-area.cjs +1 -1
  300. package/dist/scroll-area.d.ts +9 -1
  301. package/dist/scroll-area.d.ts.map +1 -1
  302. package/dist/scroll-area.js +44 -79
  303. package/dist/select.cjs +2 -2
  304. package/dist/select.d.ts +10 -10
  305. package/dist/select.d.ts.map +1 -1
  306. package/dist/select.js +118 -238
  307. package/dist/separator.cjs +1 -1
  308. package/dist/separator.js +90 -78
  309. package/dist/sheet.cjs +1 -1
  310. package/dist/sheet.d.ts +8 -5
  311. package/dist/sheet.d.ts.map +1 -1
  312. package/dist/sheet.js +250 -317
  313. package/dist/single-select-filter.cjs +1 -1
  314. package/dist/single-select-filter.d.ts +32 -0
  315. package/dist/single-select-filter.d.ts.map +1 -1
  316. package/dist/single-select-filter.js +201 -203
  317. package/dist/skeleton.cjs +1 -1
  318. package/dist/skeleton.js +24 -38
  319. package/dist/slider.cjs +1 -1
  320. package/dist/slider.js +51 -81
  321. package/dist/sonner.cjs +1 -1
  322. package/dist/sonner.d.ts +1 -1
  323. package/dist/sonner.d.ts.map +1 -1
  324. package/dist/sonner.js +101 -125
  325. package/dist/spinner.cjs +1 -1
  326. package/dist/spinner.js +22 -31
  327. package/dist/split-layout.cjs +1 -1
  328. package/dist/split-layout.js +52 -101
  329. package/dist/status.cjs +1 -0
  330. package/dist/status.d.ts +51 -0
  331. package/dist/status.d.ts.map +1 -0
  332. package/dist/status.js +60 -0
  333. package/dist/stepper.cjs +1 -1
  334. package/dist/stepper.js +184 -316
  335. package/dist/switch.cjs +1 -1
  336. package/dist/switch.js +17 -32
  337. package/dist/table.cjs +1 -1
  338. package/dist/table.d.ts +103 -30
  339. package/dist/table.d.ts.map +1 -1
  340. package/dist/table.js +170 -209
  341. package/dist/tabs.cjs +1 -1
  342. package/dist/tabs.d.ts.map +1 -1
  343. package/dist/tabs.js +102 -147
  344. package/dist/text-reveal-motion.cjs +1 -1
  345. package/dist/text-reveal-motion.js +307 -363
  346. package/dist/textarea.cjs +1 -1
  347. package/dist/textarea.d.ts.map +1 -1
  348. package/dist/textarea.js +22 -35
  349. package/dist/toggle-group.cjs +1 -1
  350. package/dist/toggle-group.js +43 -62
  351. package/dist/toggle.cjs +1 -1
  352. package/dist/toggle.js +34 -46
  353. package/dist/tooltip.cjs +1 -1
  354. package/dist/tooltip.d.ts +2 -2
  355. package/dist/tooltip.d.ts.map +1 -1
  356. package/dist/tooltip.js +157 -249
  357. package/dist/typography-heading.cjs +1 -1
  358. package/dist/typography-heading.js +25 -61
  359. package/dist/typography-text.cjs +1 -1
  360. package/dist/typography-text.js +27 -57
  361. package/dist/typography.cjs +1 -1
  362. package/dist/typography.js +132 -340
  363. package/package.json +46 -46
  364. package/src/blocks/empty-state.tsx +34 -18
  365. package/src/blocks/sidebar.tsx +86 -31
  366. package/src/components/accordion.tsx +1 -1
  367. package/src/components/alert-dialog.tsx +4 -2
  368. package/src/components/badge.tsx +127 -15
  369. package/src/components/breadcrumb.tsx +1 -1
  370. package/src/components/button.tsx +8 -6
  371. package/src/components/calendar.tsx +155 -9
  372. package/src/components/card.tsx +63 -10
  373. package/src/components/carousel.tsx +4 -1
  374. package/src/components/chart.tsx +151 -17
  375. package/src/components/checkbox.tsx +1 -1
  376. package/src/components/collapsible.tsx +4 -1
  377. package/src/components/combobox.tsx +40 -7
  378. package/src/components/command.tsx +9 -10
  379. package/src/components/context-menu.tsx +4 -4
  380. package/src/components/data-table.tsx +619 -237
  381. package/src/components/date-picker.tsx +31 -4
  382. package/src/components/delta.tsx +217 -0
  383. package/src/components/drawer.tsx +5 -5
  384. package/src/components/dropdown-menu.tsx +4 -4
  385. package/src/components/field.tsx +36 -15
  386. package/src/components/global-layout.tsx +1 -1
  387. package/src/components/input-chip.tsx +51 -7
  388. package/src/components/input-group.tsx +1 -1
  389. package/src/components/input-password.tsx +15 -19
  390. package/src/components/input.tsx +10 -1
  391. package/src/components/list.tsx +14 -2
  392. package/src/components/logo-mark.tsx +345 -0
  393. package/src/components/map.tsx +18 -20
  394. package/src/components/media-viewer.tsx +908 -0
  395. package/src/components/multi-select-filter.tsx +255 -121
  396. package/src/components/navigation-menu.tsx +2 -2
  397. package/src/components/pagination.tsx +86 -26
  398. package/src/components/progress.tsx +91 -23
  399. package/src/components/rating.tsx +200 -74
  400. package/src/components/relative-date.tsx +19 -11
  401. package/src/components/resizable.tsx +86 -33
  402. package/src/components/result-count.tsx +253 -0
  403. package/src/components/scroll-area.tsx +31 -5
  404. package/src/components/select.tsx +2 -2
  405. package/src/components/sheet.tsx +14 -4
  406. package/src/components/single-select-filter.tsx +136 -41
  407. package/src/components/slider.tsx +2 -2
  408. package/src/components/status.tsx +162 -0
  409. package/src/components/switch.tsx +1 -1
  410. package/src/components/table.tsx +278 -106
  411. package/src/components/tabs.tsx +1 -0
  412. package/src/components/textarea.tsx +20 -2
  413. package/src/components/toggle.tsx +2 -2
  414. package/src/components/tooltip.tsx +11 -20
  415. package/src/hooks/use-filter-search-params.ts +302 -0
  416. package/src/index.ts +6 -0
  417. package/src/lib/compose-event-handlers.ts +14 -0
  418. package/src/lib/download-file.ts +61 -0
  419. package/src/lib/empty-chip.tsx +52 -0
  420. package/src/lib/field-aria.tsx +97 -0
  421. package/src/lib/image-file-info.ts +102 -0
  422. package/src/lib/media-viewer-content.tsx +1587 -0
  423. package/src/lib/media-viewer-internal.tsx +191 -0
  424. package/src/lib/media-viewer-motion.ts +120 -0
  425. package/src/lib/motion.ts +9 -0
  426. package/src/lib/table-internal.ts +74 -0
  427. package/src/lib/use-controllable.ts +43 -0
  428. 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,6 +27,7 @@ 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";
31
32
  export * from "./components/dialog";
32
33
  export * from "./components/drawer";
@@ -49,6 +50,7 @@ export * from "./components/loading";
49
50
  export * from "./components/logo";
50
51
  export * from "./components/map";
51
52
  export * from "./components/media-empty";
53
+ export * from "./components/media-viewer";
52
54
  export * from "./components/menubar";
53
55
  export * from "./components/multi-select-filter";
54
56
  export * from "./components/navigation-menu";
@@ -60,6 +62,7 @@ export * from "./components/radio-group";
60
62
  export * from "./components/rating";
61
63
  export * from "./components/relative-date";
62
64
  export * from "./components/resizable";
65
+ export * from "./components/result-count";
63
66
  export * from "./components/route-progress";
64
67
  export * from "./components/scroll-area";
65
68
  export * from "./components/select";
@@ -69,8 +72,10 @@ export * from "./components/single-select-filter";
69
72
  export * from "./components/skeleton";
70
73
  export * from "./components/slider";
71
74
  export * from "./components/sonner";
75
+ export * from "./components/logo-mark";
72
76
  export * from "./components/spinner";
73
77
  export * from "./components/split-layout";
78
+ export * from "./components/status";
74
79
  export * from "./components/stepper";
75
80
  export * from "./components/switch";
76
81
  export * from "./components/table";
@@ -93,6 +98,7 @@ export * from "./blocks/sidebar";
93
98
  // Hooks
94
99
  export * from "./hooks/use-debounce";
95
100
  export * from "./hooks/use-delayed-pending";
101
+ export * from "./hooks/use-filter-search-params";
96
102
  export * from "./hooks/use-breakpoint";
97
103
  export * from "./hooks/use-mobile";
98
104
  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
+ }