@deepseek-ai/dsh-client-ui-primitives 0.2.2-alpha.3 → 0.2.2-rc.2

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 (412) hide show
  1. package/README.i18n.yaml +16 -16
  2. package/README.md +21 -14
  3. package/README.zh.md +21 -14
  4. package/lib/HoverCard.module.css +8 -3
  5. package/lib/JsonTree.module.css +7 -17
  6. package/lib/Menu.module.css +157 -37
  7. package/lib/Modal.module.css +16 -0
  8. package/lib/QuotePreview.module.css +37 -0
  9. package/lib/RiskConfirmation.module.css +11 -37
  10. package/lib/Toast.module.css +2 -3
  11. package/lib/Tooltip.module.css +2 -1
  12. package/lib/index.js +1447 -776
  13. package/lib/index.js.map +1 -0
  14. package/lib/markdown/CodeBlock.module.css +64 -0
  15. package/lib/markdown/MarkdownText.module.css +20 -1
  16. package/lib/types/AnimatedRows.d.ts +3 -0
  17. package/lib/types/AnimatedRows.d.ts.map +1 -0
  18. package/lib/types/AnimatedRows.js.map +1 -0
  19. package/lib/types/BrandWordmark.d.ts.map +1 -0
  20. package/lib/types/BrandWordmark.js.map +1 -0
  21. package/lib/types/Button.d.ts.map +1 -0
  22. package/lib/types/Button.js.map +1 -0
  23. package/lib/types/Checkbox.d.ts +3 -1
  24. package/lib/types/Checkbox.d.ts.map +1 -0
  25. package/lib/types/Checkbox.js.map +1 -0
  26. package/lib/types/CodeFileIcon.d.ts.map +1 -0
  27. package/lib/types/CodeFileIcon.js.map +1 -0
  28. package/lib/types/CodeToolbar.d.ts.map +1 -0
  29. package/lib/types/CodeToolbar.js.map +1 -0
  30. package/lib/types/CommandText.d.ts.map +1 -0
  31. package/lib/types/CommandText.js.map +1 -0
  32. package/lib/types/ConfigField.d.ts.map +1 -0
  33. package/lib/types/ConfigField.js.map +1 -0
  34. package/lib/types/ConnectionIndicator.d.ts.map +1 -0
  35. package/lib/types/ConnectionIndicator.js.map +1 -0
  36. package/lib/types/DetailDocument.d.ts.map +1 -0
  37. package/lib/types/DetailDocument.js.map +1 -0
  38. package/lib/types/DiffBlock.d.ts.map +1 -0
  39. package/lib/types/DiffBlock.js.map +1 -0
  40. package/lib/types/DisclosureRow.d.ts.map +1 -0
  41. package/lib/types/DisclosureRow.js.map +1 -0
  42. package/lib/types/DraggableImage.d.ts.map +1 -0
  43. package/lib/types/DraggableImage.js.map +1 -0
  44. package/lib/types/FileTypeIcon.d.ts.map +1 -0
  45. package/lib/types/FileTypeIcon.js.map +1 -0
  46. package/lib/types/FishLogo.d.ts.map +1 -0
  47. package/lib/types/FishLogo.js.map +1 -0
  48. package/lib/types/FoldToggle.d.ts.map +1 -0
  49. package/lib/types/FoldToggle.js.map +1 -0
  50. package/lib/types/HoverCard.d.ts.map +1 -0
  51. package/lib/types/HoverCard.js.map +1 -0
  52. package/lib/types/HoverCardGroup.d.ts.map +1 -0
  53. package/lib/types/HoverCardGroup.js.map +1 -0
  54. package/lib/types/ImageLightbox.d.ts.map +1 -0
  55. package/lib/types/ImageLightbox.js.map +1 -0
  56. package/lib/types/ImagePreview.d.ts.map +1 -0
  57. package/lib/types/ImagePreview.js.map +1 -0
  58. package/lib/types/InlineEditor.d.ts.map +1 -0
  59. package/lib/types/InlineEditor.js.map +1 -0
  60. package/lib/types/Input.d.ts.map +1 -0
  61. package/lib/types/Input.js.map +1 -0
  62. package/lib/types/JsonTree.d.ts.map +1 -0
  63. package/lib/types/JsonTree.js.map +1 -0
  64. package/lib/types/LinkIcon.d.ts.map +1 -0
  65. package/lib/types/LinkIcon.js.map +1 -0
  66. package/lib/types/Menu.d.ts +26 -14
  67. package/lib/types/Menu.d.ts.map +1 -0
  68. package/lib/types/Menu.js.map +1 -0
  69. package/lib/types/MenuGroup.d.ts.map +1 -0
  70. package/lib/types/MenuGroup.js.map +1 -0
  71. package/lib/types/MenuRow.d.ts +37 -0
  72. package/lib/types/MenuRow.d.ts.map +1 -0
  73. package/lib/types/MenuRow.js.map +1 -0
  74. package/lib/types/MenuSurface.d.ts.map +1 -0
  75. package/lib/types/MenuSurface.js.map +1 -0
  76. package/lib/types/Modal.d.ts +3 -1
  77. package/lib/types/Modal.d.ts.map +1 -0
  78. package/lib/types/Modal.js.map +1 -0
  79. package/lib/types/PathLabel.d.ts.map +1 -0
  80. package/lib/types/PathLabel.js.map +1 -0
  81. package/lib/types/PermissionIcon.d.ts.map +1 -0
  82. package/lib/types/PermissionIcon.js.map +1 -0
  83. package/lib/types/Pill.d.ts.map +1 -0
  84. package/lib/types/Pill.js.map +1 -0
  85. package/lib/types/QuotePreview.d.ts +17 -4
  86. package/lib/types/QuotePreview.d.ts.map +1 -0
  87. package/lib/types/QuotePreview.js.map +1 -0
  88. package/lib/types/ReadBlock.d.ts.map +1 -0
  89. package/lib/types/ReadBlock.js.map +1 -0
  90. package/lib/types/ReferenceIcon.d.ts.map +1 -0
  91. package/lib/types/ReferenceIcon.js.map +1 -0
  92. package/lib/types/RiskConfirmation.d.ts.map +1 -0
  93. package/lib/types/RiskConfirmation.js.map +1 -0
  94. package/lib/types/SearchBlock.d.ts.map +1 -0
  95. package/lib/types/SearchBlock.js.map +1 -0
  96. package/lib/types/SegmentedControl.d.ts.map +1 -0
  97. package/lib/types/SegmentedControl.js.map +1 -0
  98. package/lib/types/SegmentedTabs.d.ts.map +1 -0
  99. package/lib/types/SegmentedTabs.js.map +1 -0
  100. package/lib/types/ShortcutKeys.d.ts.map +1 -0
  101. package/lib/types/ShortcutKeys.js.map +1 -0
  102. package/lib/types/SiteGlyph.d.ts.map +1 -0
  103. package/lib/types/SiteGlyph.js.map +1 -0
  104. package/lib/types/StateDot.d.ts.map +1 -0
  105. package/lib/types/StateDot.js.map +1 -0
  106. package/lib/types/SubmenuPanel.d.ts +19 -0
  107. package/lib/types/SubmenuPanel.d.ts.map +1 -0
  108. package/lib/types/SubmenuPanel.js.map +1 -0
  109. package/lib/types/Switch.d.ts.map +1 -0
  110. package/lib/types/Switch.js.map +1 -0
  111. package/lib/types/Tag.d.ts.map +1 -0
  112. package/lib/types/Tag.js.map +1 -0
  113. package/lib/types/TerminalBlock.d.ts.map +1 -0
  114. package/lib/types/TerminalBlock.js.map +1 -0
  115. package/lib/types/TextShimmer.d.ts.map +1 -0
  116. package/lib/types/TextShimmer.js.map +1 -0
  117. package/lib/types/Toast.d.ts.map +1 -0
  118. package/lib/types/Toast.js.map +1 -0
  119. package/lib/types/Tooltip.d.ts.map +1 -0
  120. package/lib/types/Tooltip.js.map +1 -0
  121. package/lib/types/UserMessageBubble.d.ts.map +1 -0
  122. package/lib/types/UserMessageBubble.js.map +1 -0
  123. package/lib/types/WebBlock.d.ts.map +1 -0
  124. package/lib/types/WebBlock.js.map +1 -0
  125. package/lib/types/ZoomControls.d.ts.map +1 -0
  126. package/lib/types/ZoomControls.js.map +1 -0
  127. package/lib/types/ansi.d.ts.map +1 -0
  128. package/lib/types/ansi.js.map +1 -0
  129. package/lib/types/clipboard.d.ts.map +1 -0
  130. package/lib/types/clipboard.js.map +1 -0
  131. package/lib/types/code-file-icon-artwork.d.ts.map +1 -0
  132. package/lib/types/code-file-icon-artwork.js.map +1 -0
  133. package/lib/types/code-file-types.d.ts.map +1 -0
  134. package/lib/types/code-file-types.js.map +1 -0
  135. package/lib/types/code-highlighting.d.ts.map +1 -0
  136. package/lib/types/code-highlighting.js.map +1 -0
  137. package/lib/types/darwin-desktop.d.ts.map +1 -0
  138. package/lib/types/darwin-desktop.js.map +1 -0
  139. package/lib/types/document-zoom.d.ts.map +1 -0
  140. package/lib/types/document-zoom.js.map +1 -0
  141. package/lib/types/file-drag-actions.d.ts.map +1 -0
  142. package/lib/types/file-drag-actions.js.map +1 -0
  143. package/lib/types/file-size.d.ts.map +1 -0
  144. package/lib/types/file-size.js.map +1 -0
  145. package/lib/types/focus.d.ts.map +1 -0
  146. package/lib/types/focus.js.map +1 -0
  147. package/lib/types/guide-artwork.d.ts.map +1 -0
  148. package/lib/types/guide-artwork.js.map +1 -0
  149. package/lib/types/head-tail-cap.d.ts.map +1 -0
  150. package/lib/types/head-tail-cap.js.map +1 -0
  151. package/lib/types/icons/artwork.d.ts +12 -0
  152. package/lib/types/icons/artwork.d.ts.map +1 -0
  153. package/lib/types/icons/artwork.js.map +1 -0
  154. package/lib/types/icons/index.d.ts +12 -0
  155. package/lib/types/icons/index.d.ts.map +1 -0
  156. package/lib/types/icons/index.js.map +1 -0
  157. package/lib/types/icons/props.d.ts.map +1 -0
  158. package/lib/types/icons/props.js.map +1 -0
  159. package/lib/types/icons/shared-artwork.d.ts.map +1 -0
  160. package/lib/types/icons/shared-artwork.js.map +1 -0
  161. package/lib/types/image-drag.d.ts.map +1 -0
  162. package/lib/types/image-drag.js.map +1 -0
  163. package/lib/types/index.d.ts +5 -4
  164. package/lib/types/index.d.ts.map +1 -0
  165. package/lib/types/index.js.map +1 -0
  166. package/lib/types/input-modality.d.ts.map +1 -0
  167. package/lib/types/input-modality.js.map +1 -0
  168. package/lib/types/keyboard-composition.d.ts.map +1 -0
  169. package/lib/types/keyboard-composition.js.map +1 -0
  170. package/lib/types/markdown/CodeBlock.d.ts +8 -1
  171. package/lib/types/markdown/CodeBlock.d.ts.map +1 -0
  172. package/lib/types/markdown/CodeBlock.js.map +1 -0
  173. package/lib/types/markdown/FenceBlocks.d.ts +31 -0
  174. package/lib/types/markdown/FenceBlocks.d.ts.map +1 -0
  175. package/lib/types/markdown/FenceBlocks.js.map +1 -0
  176. package/lib/types/markdown/JsonBlock.d.ts.map +1 -0
  177. package/lib/types/markdown/JsonBlock.js.map +1 -0
  178. package/lib/types/markdown/MarkdownDelegate.d.ts +23 -1
  179. package/lib/types/markdown/MarkdownDelegate.d.ts.map +1 -0
  180. package/lib/types/markdown/MarkdownDelegate.js.map +1 -0
  181. package/lib/types/markdown/MarkdownText.d.ts +2 -0
  182. package/lib/types/markdown/MarkdownText.d.ts.map +1 -0
  183. package/lib/types/markdown/MarkdownText.js.map +1 -0
  184. package/lib/types/markdown/cjkFriendlyStrong.d.ts.map +1 -0
  185. package/lib/types/markdown/cjkFriendlyStrong.js.map +1 -0
  186. package/lib/types/markdown/copy-tex.d.ts +14 -0
  187. package/lib/types/markdown/copy-tex.d.ts.map +1 -0
  188. package/lib/types/markdown/copy-tex.js.map +1 -0
  189. package/lib/types/markdown/diff-fence.d.ts +58 -0
  190. package/lib/types/markdown/diff-fence.d.ts.map +1 -0
  191. package/lib/types/markdown/diff-fence.js.map +1 -0
  192. package/lib/types/markdown/file-link.d.ts.map +1 -0
  193. package/lib/types/markdown/file-link.js.map +1 -0
  194. package/lib/types/markdown/highlight.d.ts +18 -0
  195. package/lib/types/markdown/highlight.d.ts.map +1 -0
  196. package/lib/types/markdown/highlight.js.map +1 -0
  197. package/lib/types/markdown/incremental.d.ts.map +1 -0
  198. package/lib/types/markdown/incremental.js.map +1 -0
  199. package/lib/types/markdown/katex.d.ts.map +1 -0
  200. package/lib/types/markdown/katex.js.map +1 -0
  201. package/lib/types/markdown/local-image-syntax.d.ts.map +1 -0
  202. package/lib/types/markdown/local-image-syntax.js.map +1 -0
  203. package/lib/types/markdown/mathCompatibility.d.ts.map +1 -0
  204. package/lib/types/markdown/mathCompatibility.js.map +1 -0
  205. package/lib/types/markdown/parse.d.ts.map +1 -0
  206. package/lib/types/markdown/parse.js.map +1 -0
  207. package/lib/types/markdown/plain-text.d.ts.map +1 -0
  208. package/lib/types/markdown/plain-text.js.map +1 -0
  209. package/lib/types/markdown/quote-text.d.ts.map +1 -0
  210. package/lib/types/markdown/quote-text.js.map +1 -0
  211. package/lib/types/markdown/render.d.ts.map +1 -0
  212. package/lib/types/markdown/render.js.map +1 -0
  213. package/lib/types/markdown/useViewportHighlighting.d.ts.map +1 -0
  214. package/lib/types/markdown/useViewportHighlighting.js.map +1 -0
  215. package/lib/types/markdown/windows-path-destinations.d.ts.map +1 -0
  216. package/lib/types/markdown/windows-path-destinations.js.map +1 -0
  217. package/lib/types/menu-submenu-position.d.ts +15 -0
  218. package/lib/types/menu-submenu-position.d.ts.map +1 -0
  219. package/lib/types/menu-submenu-position.js.map +1 -0
  220. package/lib/types/overlay-top-margin.d.ts.map +1 -0
  221. package/lib/types/overlay-top-margin.js.map +1 -0
  222. package/lib/types/plugin-artwork.d.ts.map +1 -0
  223. package/lib/types/plugin-artwork.js.map +1 -0
  224. package/lib/types/pointer-grace.d.ts.map +1 -0
  225. package/lib/types/pointer-grace.js.map +1 -0
  226. package/lib/types/rank-by-name.d.ts.map +1 -0
  227. package/lib/types/rank-by-name.js.map +1 -0
  228. package/lib/types/relative-time.d.ts.map +1 -0
  229. package/lib/types/relative-time.js.map +1 -0
  230. package/lib/types/settings-form/SettingsForm.d.ts.map +1 -0
  231. package/lib/types/settings-form/SettingsForm.js.map +1 -0
  232. package/lib/types/settings-form/fields.d.ts.map +1 -0
  233. package/lib/types/settings-form/fields.js.map +1 -0
  234. package/lib/types/settings-form/form-model.d.ts.map +1 -0
  235. package/lib/types/settings-form/form-model.js.map +1 -0
  236. package/lib/types/tooltip-placement.d.ts.map +1 -0
  237. package/lib/types/tooltip-placement.js.map +1 -0
  238. package/lib/types/use-clipboard-copy.d.ts.map +1 -0
  239. package/lib/types/use-clipboard-copy.js.map +1 -0
  240. package/lib/types/useAnchoredMaxHeight.d.ts.map +1 -0
  241. package/lib/types/useAnchoredMaxHeight.js.map +1 -0
  242. package/lib/types/useDismissOnOutsidePointer.d.ts.map +1 -0
  243. package/lib/types/useDismissOnOutsidePointer.js.map +1 -0
  244. package/lib/types/useMenuSubmenu.d.ts +43 -0
  245. package/lib/types/useMenuSubmenu.d.ts.map +1 -0
  246. package/lib/types/useMenuSubmenu.js.map +1 -0
  247. package/lib/types/useModalLayer.d.ts.map +1 -0
  248. package/lib/types/useModalLayer.js.map +1 -0
  249. package/lib/types/useRowDragPreview.d.ts.map +1 -0
  250. package/lib/types/useRowDragPreview.js.map +1 -0
  251. package/lib/types/user-text.d.ts.map +1 -0
  252. package/lib/types/user-text.js.map +1 -0
  253. package/package.json +11 -8
  254. package/src/AnimatedRows.module.css +7 -0
  255. package/src/AnimatedRows.tsx +195 -0
  256. package/src/BrandWordmark.tsx +59 -0
  257. package/src/Button.module.css +84 -0
  258. package/src/Button.tsx +36 -0
  259. package/src/Checkbox.module.css +28 -0
  260. package/src/Checkbox.tsx +34 -0
  261. package/src/CodeCard.module.css +85 -0
  262. package/src/CodeFileIcon.tsx +28 -0
  263. package/src/CodeToolbar.tsx +62 -0
  264. package/src/CommandText.module.css +12 -0
  265. package/src/CommandText.tsx +46 -0
  266. package/src/ConfigField.module.css +12 -0
  267. package/src/ConfigField.tsx +53 -0
  268. package/src/ConnectionIndicator.module.css +120 -0
  269. package/src/ConnectionIndicator.tsx +103 -0
  270. package/src/DetailDocument.module.css +67 -0
  271. package/src/DetailDocument.tsx +120 -0
  272. package/src/DiffBlock.module.css +82 -0
  273. package/src/DiffBlock.tsx +221 -0
  274. package/src/DisclosureRow.module.css +115 -0
  275. package/src/DisclosureRow.tsx +124 -0
  276. package/src/DraggableImage.tsx +26 -0
  277. package/src/FileTypeIcon.module.css +50 -0
  278. package/src/FileTypeIcon.tsx +285 -0
  279. package/src/FishLogo.tsx +28 -0
  280. package/src/FoldToggle.tsx +33 -0
  281. package/src/HoverCard.module.css +108 -0
  282. package/src/HoverCard.tsx +356 -0
  283. package/src/HoverCardGroup.tsx +77 -0
  284. package/src/ImageLightbox.module.css +54 -0
  285. package/src/ImageLightbox.tsx +72 -0
  286. package/src/ImagePreview.module.css +24 -0
  287. package/src/ImagePreview.tsx +36 -0
  288. package/src/InlineEditor.module.css +23 -0
  289. package/src/InlineEditor.tsx +69 -0
  290. package/src/Input.module.css +38 -0
  291. package/src/Input.tsx +24 -0
  292. package/src/JsonTree.module.css +371 -0
  293. package/src/JsonTree.tsx +850 -0
  294. package/src/LinkIcon.tsx +118 -0
  295. package/src/Menu.module.css +443 -0
  296. package/src/Menu.tsx +553 -0
  297. package/src/MenuGroup.module.css +37 -0
  298. package/src/MenuGroup.tsx +103 -0
  299. package/src/MenuRow.tsx +89 -0
  300. package/src/MenuSurface.module.css +65 -0
  301. package/src/MenuSurface.tsx +54 -0
  302. package/src/Modal.module.css +147 -0
  303. package/src/Modal.tsx +96 -0
  304. package/src/PathLabel.module.css +28 -0
  305. package/src/PathLabel.tsx +41 -0
  306. package/src/PermissionIcon.tsx +119 -0
  307. package/src/Pill.module.css +30 -0
  308. package/src/Pill.tsx +36 -0
  309. package/src/QuotePreview.module.css +138 -0
  310. package/src/QuotePreview.tsx +133 -0
  311. package/src/ReadBlock.module.css +45 -0
  312. package/src/ReadBlock.tsx +151 -0
  313. package/src/ReferenceIcon.tsx +49 -0
  314. package/src/RiskConfirmation.module.css +47 -0
  315. package/src/RiskConfirmation.tsx +79 -0
  316. package/src/RowDragPreview.module.css +12 -0
  317. package/src/SearchBlock.module.css +114 -0
  318. package/src/SearchBlock.tsx +281 -0
  319. package/src/SegmentedControl.module.css +81 -0
  320. package/src/SegmentedControl.tsx +140 -0
  321. package/src/SegmentedTabs.module.css +77 -0
  322. package/src/SegmentedTabs.tsx +84 -0
  323. package/src/ShortcutKeys.module.css +33 -0
  324. package/src/ShortcutKeys.tsx +12 -0
  325. package/src/SiteGlyph.tsx +149 -0
  326. package/src/StateDot.module.css +100 -0
  327. package/src/StateDot.tsx +77 -0
  328. package/src/SubmenuPanel.tsx +51 -0
  329. package/src/Switch.module.css +49 -0
  330. package/src/Switch.tsx +42 -0
  331. package/src/Tag.module.css +57 -0
  332. package/src/Tag.tsx +42 -0
  333. package/src/TerminalBlock.module.css +207 -0
  334. package/src/TerminalBlock.tsx +294 -0
  335. package/src/TextShimmer.module.css +88 -0
  336. package/src/TextShimmer.tsx +58 -0
  337. package/src/Toast.module.css +102 -0
  338. package/src/Toast.tsx +101 -0
  339. package/src/Tooltip.module.css +68 -0
  340. package/src/Tooltip.tsx +273 -0
  341. package/src/UserMessageBubble.module.css +11 -0
  342. package/src/UserMessageBubble.tsx +16 -0
  343. package/src/WebBlock.module.css +153 -0
  344. package/src/WebBlock.tsx +196 -0
  345. package/src/ZoomControls.module.css +54 -0
  346. package/src/ZoomControls.tsx +92 -0
  347. package/src/ansi.ts +447 -0
  348. package/src/clipboard.ts +47 -0
  349. package/src/code-file-icon-artwork.manifest.json +71 -0
  350. package/src/code-file-icon-artwork.ts +60 -0
  351. package/src/code-file-types.ts +340 -0
  352. package/src/code-highlighting.ts +25 -0
  353. package/src/css-modules.d.ts +6 -0
  354. package/src/darwin-desktop.ts +11 -0
  355. package/src/document-zoom.ts +12 -0
  356. package/src/file-drag-actions.ts +29 -0
  357. package/src/file-size.ts +16 -0
  358. package/src/focus.ts +44 -0
  359. package/src/guide-artwork.tsx +32 -0
  360. package/src/head-tail-cap.ts +27 -0
  361. package/src/icons/artwork.ts +156 -0
  362. package/src/icons/index.tsx +634 -0
  363. package/src/icons/props.ts +8 -0
  364. package/src/icons/shared-artwork.tsx +71 -0
  365. package/src/image-drag.ts +84 -0
  366. package/src/index.ts +142 -0
  367. package/src/input-modality.ts +59 -0
  368. package/src/keyboard-composition.ts +36 -0
  369. package/src/markdown/CodeBlock.module.css +209 -0
  370. package/src/markdown/CodeBlock.tsx +268 -0
  371. package/src/markdown/FenceBlocks.tsx +51 -0
  372. package/src/markdown/JsonBlock.module.css +34 -0
  373. package/src/markdown/JsonBlock.tsx +36 -0
  374. package/src/markdown/MarkdownDelegate.tsx +102 -0
  375. package/src/markdown/MarkdownText.module.css +511 -0
  376. package/src/markdown/MarkdownText.tsx +218 -0
  377. package/src/markdown/cjkFriendlyStrong.ts +83 -0
  378. package/src/markdown/copy-tex.ts +53 -0
  379. package/src/markdown/diff-fence.ts +212 -0
  380. package/src/markdown/file-link.ts +45 -0
  381. package/src/markdown/highlight.ts +689 -0
  382. package/src/markdown/incremental.ts +360 -0
  383. package/src/markdown/katex.tsx +90 -0
  384. package/src/markdown/local-image-syntax.ts +29 -0
  385. package/src/markdown/mathCompatibility.ts +349 -0
  386. package/src/markdown/parse.ts +49 -0
  387. package/src/markdown/plain-text.ts +121 -0
  388. package/src/markdown/quote-text.ts +10 -0
  389. package/src/markdown/render.tsx +834 -0
  390. package/src/markdown/useViewportHighlighting.ts +72 -0
  391. package/src/markdown/windows-path-destinations.ts +34 -0
  392. package/src/menu-submenu-position.ts +20 -0
  393. package/src/overlay-top-margin.ts +18 -0
  394. package/src/plugin-artwork.tsx +110 -0
  395. package/src/pointer-grace.ts +47 -0
  396. package/src/rank-by-name.ts +93 -0
  397. package/src/relative-time.ts +36 -0
  398. package/src/settings-form/SettingsForm.module.css +53 -0
  399. package/src/settings-form/SettingsForm.tsx +74 -0
  400. package/src/settings-form/fields.module.css +153 -0
  401. package/src/settings-form/fields.tsx +151 -0
  402. package/src/settings-form/form-model.ts +393 -0
  403. package/src/tooltip-placement.ts +60 -0
  404. package/src/use-clipboard-copy.ts +44 -0
  405. package/src/useAnchoredMaxHeight.ts +45 -0
  406. package/src/useDismissOnOutsidePointer.ts +34 -0
  407. package/src/useMenuSubmenu.ts +182 -0
  408. package/src/useModalLayer.ts +108 -0
  409. package/src/useRowDragPreview.ts +76 -0
  410. package/src/user-text.module.css +46 -0
  411. package/src/user-text.tsx +67 -0
  412. package/lib/types/useAnchoredPosition.d.ts +0 -36
@@ -0,0 +1,850 @@
1
+ import clsx from 'clsx'
2
+ import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore } from 'react'
3
+ import type {
4
+ CSSProperties,
5
+ KeyboardEvent as ReactKeyboardEvent,
6
+ MouseEvent as ReactMouseEvent,
7
+ ReactNode,
8
+ } from 'react'
9
+ import { IconCheckOutlineRegular, IconCopyOutlineRegular, IconWrapLinesOutlineRegular } from './icons/index.tsx'
10
+ import { Menu } from './Menu.tsx'
11
+ import type { MenuEntry } from './Menu.tsx'
12
+ import css from './JsonTree.module.css'
13
+
14
+ const OBJECT_PREVIEW_LIMIT = 4
15
+ const ARRAY_PREVIEW_LIMIT = 5
16
+ const PREVIEW_DEPTH_LIMIT = 2
17
+
18
+ /**
19
+ * Display copy for the tree's copy affordance; the owner passes localized
20
+ * labels (this package is cordis-free, so copy arrives via props).
21
+ */
22
+ export interface JsonTreeLabels {
23
+ /** Menu item: copy the raw primitive value. */
24
+ copyValue: string
25
+ /** Menu item: copy the value as compact JSON (primitive rows). */
26
+ copyJson: string
27
+ /** Menu item: copy the property path. */
28
+ copyPath: string
29
+ /** Menu item: copy the value as pretty-printed JSON. */
30
+ copyPrettyJson: string
31
+ /** Menu item: copy the value as compact JSON (object rows). */
32
+ copyCompactJson: string
33
+ /** Copy-button state label after a successful copy. */
34
+ copied: string
35
+ /** Copy-button state label after a failed copy. */
36
+ copyFailed: string
37
+ /** Expander aria label while expanded. */
38
+ collapseNode: string
39
+ /** Expander aria label while collapsed. */
40
+ expandNode: string
41
+ /** Copy-button tooltip, given the current action label. */
42
+ copyButtonTitle: (action: string) => string
43
+ }
44
+
45
+ function valueCopyMenuItems(labels: JsonTreeLabels): readonly MenuEntry[] {
46
+ return [
47
+ { id: 'value', label: labels.copyValue },
48
+ { id: 'json', label: labels.copyJson },
49
+ { id: 'path', label: labels.copyPath },
50
+ ]
51
+ }
52
+
53
+ function objectCopyMenuItems(labels: JsonTreeLabels): readonly MenuEntry[] {
54
+ return [
55
+ { id: 'prettyJson', label: labels.copyPrettyJson },
56
+ { id: 'json', label: labels.copyCompactJson },
57
+ { id: 'path', label: labels.copyPath },
58
+ ]
59
+ }
60
+
61
+ type JsonPath = readonly (number | string)[]
62
+
63
+ interface RowTarget {
64
+ path: JsonPath
65
+ value: unknown
66
+ }
67
+
68
+ type CopyMode = 'json' | 'path' | 'prettyJson' | 'value'
69
+
70
+ interface CopySnapshot {
71
+ id: string
72
+ target: RowTarget
73
+ state: 'idle' | 'copied' | 'failed'
74
+ menuOpen: boolean
75
+ }
76
+
77
+ /** Notify only the old and new row actions; JSON values do not subscribe to hover state. */
78
+ function createCopyStore() {
79
+ let current: CopySnapshot | undefined
80
+ const listeners = new Map<string, Set<() => void>>()
81
+ return {
82
+ get: () => current,
83
+ set(next: CopySnapshot | undefined) {
84
+ const previous = current?.id
85
+ current = next
86
+ for (const id of new Set([previous, next?.id])) {
87
+ if (id === undefined) continue
88
+ for (const listener of listeners.get(id) ?? []) listener()
89
+ }
90
+ },
91
+ subscribe(id: string, listener: () => void) {
92
+ let row = listeners.get(id)
93
+ if (row === undefined) listeners.set(id, row = new Set())
94
+ row.add(listener)
95
+ return () => {
96
+ row.delete(listener)
97
+ if (row.size === 0) listeners.delete(id)
98
+ }
99
+ },
100
+ }
101
+ }
102
+
103
+ function JsonCopyAction({ store, target, persistent, labels, onCopy, onClose }: {
104
+ store: ReturnType<typeof createCopyStore>
105
+ target: RowTarget
106
+ persistent: boolean
107
+ labels: JsonTreeLabels
108
+ onCopy: (target: RowTarget, mode: CopyMode) => Promise<void>
109
+ onClose: () => void
110
+ }) {
111
+ const id = pathId(target.path)
112
+ const subscribe = useCallback((listener: () => void) => store.subscribe(id, listener), [id, store])
113
+ const getSnapshot = () => {
114
+ const current = store.get()
115
+ return current?.id === id ? current : undefined
116
+ }
117
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
118
+ const buttonRef = useRef<HTMLButtonElement>(null)
119
+ const state = snapshot?.state ?? 'idle'
120
+ const object = typeof target.value === 'object' && target.value !== null
121
+ const copyTitle = state === 'copied'
122
+ ? labels.copied
123
+ : state === 'failed'
124
+ ? labels.copyFailed
125
+ : object ? labels.copyPrettyJson : labels.copyValue
126
+ return (
127
+ <span className={css.copySlot}>
128
+ {(persistent || snapshot !== undefined) && (
129
+ <Menu
130
+ open={snapshot?.menuOpen === true}
131
+ compact
132
+ portal
133
+ align="end"
134
+ anchor={(
135
+ <button
136
+ ref={buttonRef}
137
+ type="button"
138
+ className={css.actionButton}
139
+ data-json-copy-button
140
+ data-state={state}
141
+ aria-label={copyTitle}
142
+ title={labels.copyButtonTitle(copyTitle)}
143
+ onClick={() => void onCopy(target, object ? 'prettyJson' : 'value')}
144
+ onContextMenu={(event) => {
145
+ event.preventDefault()
146
+ event.stopPropagation()
147
+ store.set({ id, target, state, menuOpen: true })
148
+ }}
149
+ >
150
+ {state === 'copied'
151
+ ? <IconCheckOutlineRegular size={12} />
152
+ : <IconCopyOutlineRegular size={12} />}
153
+ </button>
154
+ )}
155
+ items={object ? objectCopyMenuItems(labels) : valueCopyMenuItems(labels)}
156
+ onSelect={(mode) => {
157
+ void onCopy(target, mode as CopyMode)
158
+ }}
159
+ onClose={onClose}
160
+ anchorRef={buttonRef}
161
+ />
162
+ )}
163
+ </span>
164
+ )
165
+ }
166
+
167
+ function isExpandableValue(value: unknown): value is object | unknown[] {
168
+ return typeof value === 'object' && value !== null && !(value instanceof Date)
169
+ }
170
+
171
+ function entriesOf(value: object | unknown[]): readonly (readonly [string, unknown])[] {
172
+ if (Array.isArray(value)) {
173
+ return value.map((item, index) => [String(index), item] as const)
174
+ }
175
+ return Object.keys(value).map(key => [
176
+ key,
177
+ (value as Record<string, unknown>)[key],
178
+ ] as const)
179
+ }
180
+
181
+ function bracketOf(value: object | unknown[]): readonly [string, string] {
182
+ return Array.isArray(value) ? ['[', ']'] : ['{', '}']
183
+ }
184
+
185
+ function previewPrimitive(value: unknown): ReactNode {
186
+ if (value === null) return <span className={css.keywordValue}>null</span>
187
+ if (typeof value === 'string') {
188
+ return <span className={css.stringValue}>{JSON.stringify(value)}</span>
189
+ }
190
+ if (typeof value === 'number') {
191
+ return <span className={css.numberValue}>{String(value)}</span>
192
+ }
193
+ if (typeof value === 'boolean') {
194
+ return <span className={css.keywordValue}>{String(value)}</span>
195
+ }
196
+ if (typeof value === 'bigint') {
197
+ return <span className={css.otherValue}>{value.toString()}</span>
198
+ }
199
+ if (typeof value === 'undefined') {
200
+ return <span className={css.otherValue}>undefined</span>
201
+ }
202
+ if (typeof value === 'symbol') {
203
+ return <span className={css.otherValue}>{value.description ?? 'Symbol'}</span>
204
+ }
205
+ if (typeof value === 'function') {
206
+ return <span className={css.otherValue}>{value.name || 'Function'}</span>
207
+ }
208
+ return null
209
+ }
210
+
211
+ function previewValue(value: unknown, depth: number): ReactNode {
212
+ if (!isExpandableValue(value)) return previewPrimitive(value)
213
+
214
+ const array = Array.isArray(value)
215
+ const entries = entriesOf(value)
216
+ const limit = array ? ARRAY_PREVIEW_LIMIT : OBJECT_PREVIEW_LIMIT
217
+ const visible = entries.slice(0, limit)
218
+ const [open, close] = bracketOf(value)
219
+
220
+ return (
221
+ <>
222
+ <span className={css.punctuation}>{open}</span>
223
+ {depth >= PREVIEW_DEPTH_LIMIT
224
+ ? <span className={css.previewEllipsis}>…</span>
225
+ : visible.map(([key, item], index) => (
226
+ <span key={key}>
227
+ {index > 0 && <span className={css.punctuation}>, </span>}
228
+ {!array && (
229
+ <>
230
+ <span className={css.previewProperty}>{key}</span>
231
+ <span className={css.punctuation}>: </span>
232
+ </>
233
+ )}
234
+ {previewValue(item, depth + 1)}
235
+ </span>
236
+ ))}
237
+ {depth < PREVIEW_DEPTH_LIMIT && entries.length > limit && (
238
+ <span className={css.previewEllipsis}>, …</span>
239
+ )}
240
+ <span className={css.punctuation}>{close}</span>
241
+ </>
242
+ )
243
+ }
244
+
245
+ function primitiveValue(value: unknown): ReactNode {
246
+ if (value === null) return <span className={css.keywordValue}>null</span>
247
+ if (typeof value === 'string') {
248
+ return <span className={css.stringValue}>{JSON.stringify(value)}</span>
249
+ }
250
+ if (typeof value === 'boolean') {
251
+ return <span className={css.keywordValue}>{String(value)}</span>
252
+ }
253
+ if (typeof value === 'number') {
254
+ return <span className={css.numberValue}>{String(value)}</span>
255
+ }
256
+ if (typeof value === 'bigint') {
257
+ return <span className={css.numberValue}>{`${value.toString()}n`}</span>
258
+ }
259
+ if (value instanceof Date) {
260
+ return <span className={css.otherValue}>{value.toISOString()}</span>
261
+ }
262
+ if (typeof value === 'function') {
263
+ return <span className={css.otherValue}>function() {'{ }'}</span>
264
+ }
265
+ if (typeof value === 'undefined') {
266
+ return <span className={css.otherValue}>undefined</span>
267
+ }
268
+ return <span className={css.otherValue}>{(value as symbol).toString()}</span>
269
+ }
270
+
271
+ function fieldText(field: string): string {
272
+ return field === '' ? '""' : field
273
+ }
274
+
275
+ function pathId(path: JsonPath): string {
276
+ return path.map(part => (
277
+ typeof part === 'number' ? `n${String(part)}` : `s${String(part.length)}:${part}`
278
+ )).join('/')
279
+ }
280
+
281
+ function claimFocus(button: HTMLElement): void {
282
+ button.focus()
283
+ }
284
+
285
+ function moveFocus(button: HTMLElement, direction: -1 | 1): void {
286
+ const tree = button.closest<HTMLElement>('[role="tree"]')
287
+ /* v8 ignore next -- JsonTree attaches expander handlers only beneath its owning role=tree. */
288
+ if (tree === null) return
289
+ const expanders = Array.from(tree.querySelectorAll<HTMLElement>('[data-json-expander]'))
290
+ const current = expanders.indexOf(button)
291
+ /* v8 ignore next -- the current expander is a member of the queried non-empty set. */
292
+ if (current < 0 || expanders.length === 0) return
293
+ const next = (current + direction + expanders.length) % expanders.length
294
+ const nextExpander = expanders[next]
295
+ /* v8 ignore next -- modulo over the non-empty expander set always resolves a member. */
296
+ if (nextExpander !== undefined) claimFocus(nextExpander)
297
+ }
298
+
299
+ function NodeField({
300
+ field,
301
+ expandable,
302
+ onToggle,
303
+ }: {
304
+ field: string | undefined
305
+ expandable: boolean
306
+ onToggle: () => void
307
+ }) {
308
+ if (field === undefined) return null
309
+ return (
310
+ <span
311
+ className={clsx(css.label, expandable && css.clickableLabel)}
312
+ onClick={expandable ? onToggle : undefined}
313
+ >
314
+ {fieldText(field)}:
315
+ </span>
316
+ )
317
+ }
318
+
319
+ interface JsonTreeNodeProps {
320
+ collapsedStringLines: number
321
+ stringWrapping: JsonTreeProps['stringWrapping']
322
+ field?: string
323
+ initialExpanded: boolean
324
+ labels: JsonTreeLabels
325
+ lastElement: boolean
326
+ onClaimTabStop: (id: string) => void
327
+ onRowHover: (row: HTMLElement, target: RowTarget) => void
328
+ path: JsonPath
329
+ renderCopy: ((target: RowTarget, persistent?: boolean) => ReactNode) | undefined
330
+ tabStopId: string | null
331
+ value: unknown
332
+ }
333
+
334
+ function JsonString({
335
+ collapsedStringLines,
336
+ stringWrapping,
337
+ field,
338
+ labels,
339
+ lastElement,
340
+ renderCopy,
341
+ value,
342
+ }: {
343
+ collapsedStringLines: number
344
+ stringWrapping: JsonTreeProps['stringWrapping']
345
+ field: string | undefined
346
+ labels: JsonTreeLabels
347
+ lastElement: boolean
348
+ renderCopy: ((persistent?: boolean) => ReactNode) | undefined
349
+ value: string
350
+ }) {
351
+ const contentsId = useId()
352
+ const contentRef = useRef<HTMLSpanElement>(null)
353
+ const rawRef = useRef<HTMLPreElement>(null)
354
+ const [expanded, setExpanded] = useState(false)
355
+ const [wrapped, setWrapped] = useState(false)
356
+ const [truncated, setTruncated] = useState(false)
357
+
358
+ useLayoutEffect(() => {
359
+ if (expanded) return
360
+ const content = contentRef.current as HTMLSpanElement
361
+ const measure = () => {
362
+ const lineHeight = Number.parseFloat(getComputedStyle(content).lineHeight)
363
+ setTruncated(content.scrollHeight > lineHeight * collapsedStringLines)
364
+ }
365
+ measure()
366
+ if (typeof ResizeObserver === 'undefined') return
367
+ const observer = new ResizeObserver(measure)
368
+ observer.observe(content)
369
+ return () => { observer.disconnect() }
370
+ }, [collapsedStringLines, expanded, field, lastElement, value])
371
+
372
+ useLayoutEffect(() => {
373
+ if (!expanded) return
374
+ const raw = rawRef.current as HTMLPreElement
375
+ // Keep raw text within the window and clipping ancestors outside the tree.
376
+ // Capture scrolling because an ancestor can move the string without resizing it.
377
+ const clips: HTMLElement[] = []
378
+ const tree = raw.closest<HTMLElement>(`.${css.root}`) as HTMLElement
379
+ for (let parent = tree.parentElement; parent !== null; parent = parent.parentElement) {
380
+ if (/auto|scroll|hidden|clip/.test(getComputedStyle(parent).overflowY)) clips.push(parent)
381
+ }
382
+ const measure = () => {
383
+ let top = 0
384
+ let bottom = window.innerHeight
385
+ for (const clip of clips) {
386
+ const rect = clip.getBoundingClientRect()
387
+ const style = getComputedStyle(clip)
388
+ top = Math.max(top, rect.top + clip.clientTop)
389
+ bottom = Math.min(bottom, rect.top + clip.clientTop + clip.clientHeight
390
+ - Number.parseFloat(style.paddingBottom))
391
+ }
392
+ const available = bottom - Math.max(top, raw.getBoundingClientRect().top)
393
+ raw.style.maxHeight = `${Math.max(16, available - 4)}px`
394
+ }
395
+ measure()
396
+ const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(measure)
397
+ observer?.observe(raw)
398
+ for (const clip of clips) observer?.observe(clip)
399
+ window.addEventListener('resize', measure)
400
+ window.addEventListener('scroll', measure, true)
401
+ return () => {
402
+ observer?.disconnect()
403
+ window.removeEventListener('resize', measure)
404
+ window.removeEventListener('scroll', measure, true)
405
+ }
406
+ }, [expanded, value])
407
+
408
+ if (expanded) {
409
+ const fieldId = `${contentsId}-field`
410
+ return (
411
+ <div className={css.stringField} data-expanded>
412
+ {field !== undefined && <span id={fieldId} className={css.label}>{fieldText(field)}:</span>}
413
+ <pre
414
+ ref={rawRef}
415
+ id={contentsId}
416
+ className={css.stringRaw}
417
+ data-wrap={wrapped}
418
+ tabIndex={0}
419
+ aria-labelledby={field === undefined ? undefined : fieldId}
420
+ >
421
+ {value}
422
+ </pre>
423
+ {!lastElement && <span className={css.punctuation}>,</span>}
424
+ <div className={css.stringActions}>
425
+ {stringWrapping !== undefined && (
426
+ <button
427
+ type="button"
428
+ className={css.actionButton}
429
+ aria-label={stringWrapping.label}
430
+ title={stringWrapping.label}
431
+ aria-pressed={wrapped}
432
+ aria-controls={contentsId}
433
+ onClick={() => {
434
+ const next = !wrapped
435
+ setWrapped(next)
436
+ stringWrapping.setDefault(next)
437
+ }}
438
+ >
439
+ <IconWrapLinesOutlineRegular size={12} />
440
+ </button>
441
+ )}
442
+ <button
443
+ type="button"
444
+ className={css.actionButton}
445
+ aria-label={labels.collapseNode}
446
+ title={labels.collapseNode}
447
+ aria-expanded
448
+ aria-controls={contentsId}
449
+ onClick={() => { setExpanded(false) }}
450
+ >
451
+ <svg
452
+ width="12"
453
+ height="12"
454
+ viewBox="0 0 16 16"
455
+ fill="none"
456
+ stroke="currentColor"
457
+ aria-hidden="true"
458
+ >
459
+ <path d="M9.5 1.5V6.5H14.5M1.5 9.5H6.5V14.5" />
460
+ </svg>
461
+ </button>
462
+ {renderCopy?.(true)}
463
+ </div>
464
+ </div>
465
+ )
466
+ }
467
+
468
+ return (
469
+ <>
470
+ {renderCopy?.()}
471
+ <span className={css.stringField} data-expanded={expanded}>
472
+ <span ref={contentRef} id={contentsId} className={css.stringText}>
473
+ {truncated && (
474
+ <span className={css.stringToggleSlot}>
475
+ <button
476
+ type="button"
477
+ className={css.stringToggle}
478
+ aria-label={labels.expandNode}
479
+ aria-expanded={false}
480
+ aria-controls={contentsId}
481
+ onClick={() => {
482
+ setWrapped(stringWrapping?.getDefault() ?? false)
483
+ setExpanded(true)
484
+ }}
485
+ >
486
+ <span aria-hidden="true">…</span>{labels.expandNode}
487
+ </button>
488
+ </span>
489
+ )}
490
+ {field !== undefined && <span className={css.label}>{fieldText(field)}:</span>}
491
+ {primitiveValue(value)}
492
+ {!lastElement && <span className={css.punctuation}>,</span>}
493
+ </span>
494
+ </span>
495
+ </>
496
+ )
497
+ }
498
+
499
+ function JsonTreeNode({
500
+ collapsedStringLines,
501
+ stringWrapping,
502
+ field,
503
+ initialExpanded,
504
+ labels,
505
+ lastElement,
506
+ onClaimTabStop,
507
+ onRowHover,
508
+ path,
509
+ renderCopy,
510
+ tabStopId,
511
+ value,
512
+ }: JsonTreeNodeProps) {
513
+ const contentsId = useId()
514
+ const expanderRef = useRef<HTMLSpanElement>(null)
515
+ const [expanded, setExpanded] = useState(initialExpanded)
516
+ const nodeId = pathId(path)
517
+ const container = isExpandableValue(value)
518
+ const entries = container ? entriesOf(value) : []
519
+ const expandable = entries.length > 0
520
+
521
+ const toggle = () => {
522
+ setExpanded(current => !current)
523
+ claimFocus(expanderRef.current as HTMLSpanElement)
524
+ }
525
+
526
+ const onExpanderKeyDown = (event: ReactKeyboardEvent<HTMLSpanElement>) => {
527
+ if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
528
+ event.preventDefault()
529
+ setExpanded(event.key === 'ArrowRight')
530
+ return
531
+ }
532
+ if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
533
+ event.preventDefault()
534
+ moveFocus(event.currentTarget, event.key === 'ArrowUp' ? -1 : 1)
535
+ }
536
+ }
537
+
538
+ const row = (children: ReactNode, ariaExpanded?: boolean) => (
539
+ <div
540
+ className={css.row}
541
+ role="treeitem"
542
+ aria-expanded={ariaExpanded}
543
+ onMouseOver={(event) => {
544
+ event.stopPropagation()
545
+ onRowHover(event.currentTarget, { path, value })
546
+ }}
547
+ >
548
+ {typeof value !== 'string' && renderCopy?.({ path, value })}
549
+ {children}
550
+ </div>
551
+ )
552
+
553
+ if (typeof value === 'string') {
554
+ return row(
555
+ <JsonString
556
+ collapsedStringLines={collapsedStringLines}
557
+ stringWrapping={stringWrapping}
558
+ field={field}
559
+ value={value}
560
+ labels={labels}
561
+ lastElement={lastElement}
562
+ renderCopy={renderCopy === undefined ? undefined : persistent => renderCopy({ path, value }, persistent)}
563
+ />,
564
+ )
565
+ }
566
+
567
+ if (!container) {
568
+ return row((
569
+ <>
570
+ <NodeField field={field} expandable={false} onToggle={toggle} />
571
+ {primitiveValue(value)}
572
+ {!lastElement && <span className={css.punctuation}>,</span>}
573
+ </>
574
+ ))
575
+ }
576
+
577
+ const [open, close] = bracketOf(value)
578
+ if (!expandable) {
579
+ return row((
580
+ <>
581
+ <NodeField field={field} expandable={false} onToggle={toggle} />
582
+ <span className={css.punctuation}>{open}</span>
583
+ <span className={css.punctuation}>{close}</span>
584
+ {!lastElement && <span className={css.punctuation}>,</span>}
585
+ </>
586
+ ))
587
+ }
588
+
589
+ return row((
590
+ <>
591
+ <span
592
+ ref={expanderRef}
593
+ className={clsx(css.expander, expanded ? css.collapseIcon : css.expandIcon)}
594
+ data-json-expander
595
+ role="button"
596
+ aria-label={expanded ? labels.collapseNode : labels.expandNode}
597
+ aria-expanded={expanded}
598
+ aria-controls={expanded ? contentsId : undefined}
599
+ tabIndex={tabStopId === nodeId ? 0 : -1}
600
+ onFocus={() => { onClaimTabStop(nodeId) }}
601
+ onClick={toggle}
602
+ onKeyDown={onExpanderKeyDown}
603
+ />
604
+ <span className={css.summary}>
605
+ <NodeField field={field} expandable onToggle={toggle} />
606
+ <span className={css.preview}>{previewValue(value, 0)}</span>
607
+ {!lastElement && <span className={css.punctuation}>,</span>}
608
+ </span>
609
+ {expanded && (
610
+ <ul id={contentsId} role="group" className={css.children}>
611
+ {entries.map(([key, item], index) => (
612
+ <JsonTreeNode
613
+ key={key}
614
+ collapsedStringLines={collapsedStringLines}
615
+ stringWrapping={stringWrapping}
616
+ field={key}
617
+ value={item}
618
+ path={[...path, Array.isArray(value) ? index : key]}
619
+ labels={labels}
620
+ lastElement={index === entries.length - 1}
621
+ initialExpanded={false}
622
+ tabStopId={tabStopId}
623
+ onClaimTabStop={onClaimTabStop}
624
+ onRowHover={onRowHover}
625
+ renderCopy={renderCopy}
626
+ />
627
+ ))}
628
+ </ul>
629
+ )}
630
+ </>
631
+ ), expanded)
632
+ }
633
+
634
+ function formattedPath(path: JsonPath): string {
635
+ return path.reduce<string>((result, part) => {
636
+ if (typeof part === 'number') return `${result}[${String(part)}]`
637
+ return /^[A-Za-z_$][\w$]*$/.test(part)
638
+ ? `${result}.${part}`
639
+ : `${result}[${JSON.stringify(part)}]`
640
+ }, '$')
641
+ }
642
+
643
+ function copyText(target: RowTarget, mode: CopyMode): string {
644
+ if (mode === 'path') return formattedPath(target.path)
645
+ if (mode === 'prettyJson') return JSON.stringify(target.value, null, 2)
646
+ if (mode === 'json') return JSON.stringify(target.value)
647
+ if (typeof target.value === 'string') return target.value
648
+ if (typeof target.value === 'undefined') return 'undefined'
649
+ if (typeof target.value === 'bigint') return target.value.toString()
650
+ if (typeof target.value === 'symbol') return target.value.description ?? 'Symbol'
651
+ if (typeof target.value === 'function') return target.value.name || 'Function'
652
+ return JSON.stringify(target.value)
653
+ }
654
+
655
+ /** Props for the read-only, token-themed JSON tree. */
656
+ export interface JsonTreeProps {
657
+ /** Parsed JSON object or array. */
658
+ data: object | unknown[]
659
+ /** Accessible label for the tree. */
660
+ label: string
661
+ /** Optional positioning class owned by the caller. */
662
+ className?: string | undefined
663
+ /** Maximum visible lines per collapsed string; defaults to 3. */
664
+ collapsedStringLines?: number
665
+ /** Optional wrap toggle; each expansion reads the shared default without changing other open strings. */
666
+ stringWrapping?: {
667
+ /** Localized label for the wrapping toggle. */
668
+ label: string
669
+ /** Read the wrapping preference when a string is expanded. @returns Whether to wrap long lines. */
670
+ getDefault: () => boolean
671
+ /** Remember a user toggle for future expansions. @param wrapped - Whether to wrap long lines. */
672
+ setDefault: (wrapped: boolean) => void
673
+ } | undefined
674
+ /** Whether JSON rows expose copy actions. */
675
+ copyable?: boolean
676
+ /** Whether the top-level object or array is always expanded. */
677
+ expandTopLevel?: boolean
678
+ /** Localized display copy supplied by the owning render site. */
679
+ labels: JsonTreeLabels
680
+ }
681
+
682
+ /**
683
+ * Render parsed JSON as a compact, keyboard-accessible inspector tree.
684
+ * @param props - Parsed data, accessible label, and display options.
685
+ * @returns A read-only JSON tree with an optionally fixed-open top level.
686
+ */
687
+ export function JsonTree({
688
+ data,
689
+ label,
690
+ className,
691
+ collapsedStringLines = 3,
692
+ stringWrapping,
693
+ copyable = true,
694
+ expandTopLevel = true,
695
+ labels,
696
+ }: JsonTreeProps) {
697
+ const rootEntries = entriesOf(data)
698
+ const firstExpandableIndex = rootEntries.findIndex(([, value]) => (
699
+ isExpandableValue(value) && entriesOf(value).length > 0
700
+ ))
701
+ const firstExpandableEntry = rootEntries[firstExpandableIndex]
702
+ const initialTabStopId = expandTopLevel
703
+ ? firstExpandableEntry === undefined
704
+ ? null
705
+ : pathId([Array.isArray(data) ? firstExpandableIndex : firstExpandableEntry[0]])
706
+ : isExpandableValue(data) && rootEntries.length > 0 ? pathId([]) : null
707
+ const activeRowRef = useRef<HTMLElement>()
708
+ const resetTimer = useRef<ReturnType<typeof setTimeout>>()
709
+ const copySequence = useRef(0)
710
+ const [copyStore] = useState(createCopyStore)
711
+ const [tabStopId, setTabStopId] = useState<string | null>(initialTabStopId)
712
+
713
+ const setActiveRow = (row: HTMLElement | undefined) => {
714
+ activeRowRef.current?.removeAttribute('data-json-copy-active')
715
+ activeRowRef.current = row
716
+ row?.setAttribute('data-json-copy-active', '')
717
+ }
718
+
719
+ const clearCopyTarget = () => {
720
+ copySequence.current += 1
721
+ if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
722
+ setActiveRow(undefined)
723
+ copyStore.set(undefined)
724
+ }
725
+
726
+ useEffect(() => () => {
727
+ copySequence.current += 1
728
+ if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
729
+ activeRowRef.current?.removeAttribute('data-json-copy-active')
730
+ }, [])
731
+
732
+ useEffect(() => {
733
+ clearCopyTarget()
734
+ setTabStopId(initialTabStopId)
735
+ }, [data, expandTopLevel, initialTabStopId])
736
+
737
+ const handleRowHover = (row: HTMLElement, target: RowTarget) => {
738
+ if (!copyable || copyStore.get()?.menuOpen) return
739
+ if (activeRowRef.current === row) return
740
+ setActiveRow(row)
741
+ copyStore.set({ id: pathId(target.path), target, state: 'idle', menuOpen: false })
742
+ }
743
+
744
+ const handleRootMouseOver = (event: ReactMouseEvent<HTMLDivElement>) => {
745
+ if (!copyable || copyStore.get()?.menuOpen) return
746
+ /* v8 ignore next -- browser mouse events delivered through React target an Element. */
747
+ if (!(event.target instanceof Element)) return
748
+ if (event.target.closest('[data-json-copy-button]') === null) clearCopyTarget()
749
+ }
750
+
751
+ const copy = async (target: RowTarget, mode: CopyMode) => {
752
+ const sequence = ++copySequence.current
753
+ const snapshot: CopySnapshot = {
754
+ id: pathId(target.path), target, state: 'idle', menuOpen: false,
755
+ }
756
+ copyStore.set(snapshot)
757
+ let state: CopySnapshot['state']
758
+ try {
759
+ await navigator.clipboard.writeText(copyText(target, mode))
760
+ state = 'copied'
761
+ } catch {
762
+ state = 'failed'
763
+ }
764
+ const current = copyStore.get()
765
+ if (sequence !== copySequence.current || current?.target !== target) return
766
+ copyStore.set({ ...current, state })
767
+ if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
768
+ resetTimer.current = setTimeout(() => {
769
+ const current = copyStore.get()
770
+ if (current?.target === target) copyStore.set({ ...current, state: 'idle' })
771
+ }, 1_500)
772
+ }
773
+
774
+ const [rootOpen, rootClose] = bracketOf(data)
775
+ const renderCopy = copyable ? (target: RowTarget, persistent = false) => (
776
+ <JsonCopyAction store={copyStore} target={target} persistent={persistent} labels={labels}
777
+ onCopy={copy} onClose={clearCopyTarget} />
778
+ ) : undefined
779
+
780
+ return (
781
+ <div
782
+ className={clsx(css.root, className)}
783
+ style={{ '--json-tree-collapsed-lines': collapsedStringLines } as CSSProperties}
784
+ onMouseOver={handleRootMouseOver}
785
+ onMouseLeave={() => {
786
+ if (!copyStore.get()?.menuOpen) clearCopyTarget()
787
+ }}
788
+ >
789
+ {expandTopLevel
790
+ ? (
791
+ <div className={css.expandedTopLevel}>
792
+ <div
793
+ className={clsx(css.row, css.topLevelBracket)}
794
+ data-json-root-row
795
+ onMouseOver={(event) => {
796
+ event.stopPropagation()
797
+ handleRowHover(event.currentTarget, { path: [], value: data })
798
+ }}
799
+ >
800
+ {renderCopy?.({ path: [], value: data })}
801
+ <span className={css.punctuation}>{rootOpen}</span>
802
+ </div>
803
+ <div
804
+ aria-label={label}
805
+ className={clsx(css.container, css.expandedTopLevelContainer)}
806
+ role="tree"
807
+ >
808
+ {rootEntries.map(([key, value], index) => (
809
+ <JsonTreeNode
810
+ key={key}
811
+ collapsedStringLines={collapsedStringLines}
812
+ stringWrapping={stringWrapping}
813
+ field={key}
814
+ value={value}
815
+ path={[Array.isArray(data) ? index : key]}
816
+ labels={labels}
817
+ lastElement={index === rootEntries.length - 1}
818
+ initialExpanded={false}
819
+ tabStopId={tabStopId}
820
+ onClaimTabStop={setTabStopId}
821
+ onRowHover={handleRowHover}
822
+ renderCopy={renderCopy}
823
+ />
824
+ ))}
825
+ </div>
826
+ <div className={clsx(css.row, css.topLevelBracket)}>
827
+ <span className={css.punctuation}>{rootClose}</span>
828
+ </div>
829
+ </div>
830
+ )
831
+ : (
832
+ <div aria-label={label} className={css.container} role="tree">
833
+ <JsonTreeNode
834
+ collapsedStringLines={collapsedStringLines}
835
+ stringWrapping={stringWrapping}
836
+ value={data}
837
+ path={[]}
838
+ labels={labels}
839
+ lastElement
840
+ initialExpanded
841
+ tabStopId={tabStopId}
842
+ onClaimTabStop={setTabStopId}
843
+ onRowHover={handleRowHover}
844
+ renderCopy={renderCopy}
845
+ />
846
+ </div>
847
+ )}
848
+ </div>
849
+ )
850
+ }