motion-ui-skill 2.1.0 → 2.2.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 (1011) hide show
  1. package/README.md +449 -219
  2. package/SKILL.md +268 -22
  3. package/bin/index.mjs +4 -3
  4. package/bin/installer.mjs +11 -1
  5. package/catalog.json +5989 -239
  6. package/components/agents/ai-agent-activity.tsx +443 -0
  7. package/components/agents/ai-response.tsx +829 -0
  8. package/components/agents/ai-status.tsx +373 -0
  9. package/components/agents/chat-thread.module.css +242 -0
  10. package/components/agents/chat-thread.tsx +700 -0
  11. package/components/agents/citations.tsx +1 -1
  12. package/components/agents/comment-thread.module.css +195 -0
  13. package/components/agents/comment-thread.tsx +593 -0
  14. package/components/agents/json-viewer.module.css +92 -0
  15. package/components/agents/json-viewer.tsx +530 -0
  16. package/components/agents/matrix-orb.tsx +252 -0
  17. package/components/agents/text-stream.css +17 -0
  18. package/components/agents/text-stream.tsx +169 -0
  19. package/components/agents/thinking-orb.tsx +653 -0
  20. package/components/agents/web-search.tsx +932 -0
  21. package/components/analytics-dashboard/context.ts +47 -0
  22. package/components/analytics-dashboard/data.ts +351 -0
  23. package/components/analytics-dashboard/details.tsx +282 -0
  24. package/components/analytics-dashboard/icons.tsx +219 -0
  25. package/components/analytics-dashboard/number-animation.module.css +19 -0
  26. package/components/analytics-dashboard/number-animation.tsx +183 -0
  27. package/components/analytics-dashboard/pages.tsx +673 -0
  28. package/components/analytics-dashboard/ui.tsx +238 -0
  29. package/components/blocks/active-sessions.css +52 -0
  30. package/components/blocks/active-sessions.tsx +679 -0
  31. package/components/blocks/activity-feed.tsx +432 -0
  32. package/components/blocks/activity-heatmap.module.css +83 -0
  33. package/components/blocks/activity-heatmap.tsx +416 -0
  34. package/components/blocks/advanced-data-table.tsx +1078 -0
  35. package/components/blocks/analytics-dashboard.css +2822 -0
  36. package/components/blocks/analytics-dashboard.tsx +394 -0
  37. package/components/blocks/art-gallery.tsx +508 -0
  38. package/components/blocks/bar-chart.module.css +49 -0
  39. package/components/blocks/bar-chart.tsx +394 -0
  40. package/components/blocks/blog-grid-data.ts +127 -0
  41. package/components/blocks/blog-grid.module.css +93 -0
  42. package/components/blocks/blog-grid.tsx +263 -0
  43. package/components/blocks/brush-chart.module.css +69 -0
  44. package/components/blocks/brush-chart.tsx +459 -0
  45. package/components/blocks/changelog-feed.module.css +138 -0
  46. package/components/blocks/changelog-feed.tsx +401 -0
  47. package/components/blocks/chat.tsx +785 -0
  48. package/components/blocks/comparison-table-data.ts +63 -0
  49. package/components/blocks/comparison-table.module.css +92 -0
  50. package/components/blocks/comparison-table.tsx +209 -0
  51. package/components/blocks/cta-section-data.ts +42 -0
  52. package/components/blocks/cta-section.module.css +95 -0
  53. package/components/blocks/cta-section.tsx +261 -0
  54. package/components/blocks/dashboard-shell.css +110 -0
  55. package/components/blocks/dashboard-shell.tsx +1067 -0
  56. package/components/blocks/empty-states-art.ts +331 -0
  57. package/components/blocks/empty-states.module.css +52 -0
  58. package/components/blocks/empty-states.tsx +326 -0
  59. package/components/blocks/family-drawer.tsx +695 -0
  60. package/components/blocks/faq.tsx +214 -0
  61. package/components/blocks/footer.tsx +117 -0
  62. package/components/blocks/form.tsx +594 -0
  63. package/components/blocks/hero-cadence.module.css +46 -0
  64. package/components/blocks/hero-cadence.tsx +68 -0
  65. package/components/blocks/hero-content.module.css +49 -0
  66. package/components/blocks/hero-content.tsx +93 -0
  67. package/components/blocks/hero-lumen-data.ts +97 -0
  68. package/components/blocks/hero-lumen.module.css +127 -0
  69. package/components/blocks/hero-lumen.tsx +204 -0
  70. package/components/blocks/hero-mesh.module.css +5 -0
  71. package/components/blocks/hero-mesh.tsx +200 -0
  72. package/components/blocks/hero-motion.ts +31 -0
  73. package/components/blocks/hero-relay.module.css +102 -0
  74. package/components/blocks/hero-relay.tsx +252 -0
  75. package/components/blocks/hero-section.module.css +31 -0
  76. package/components/blocks/hero-section.tsx +83 -0
  77. package/components/blocks/hook-sidebar.tsx +233 -0
  78. package/components/blocks/line-chart.module.css +60 -0
  79. package/components/blocks/line-chart.tsx +434 -0
  80. package/components/blocks/login-centered.module.css +94 -0
  81. package/components/blocks/login-centered.tsx +444 -0
  82. package/components/blocks/logo-marquee.module.css +162 -0
  83. package/components/blocks/logo-marquee.tsx +106 -0
  84. package/components/blocks/newsletter-signup-data.ts +95 -0
  85. package/components/blocks/newsletter-signup.module.css +175 -0
  86. package/components/blocks/newsletter-signup.tsx +369 -0
  87. package/components/blocks/notification-center.module.css +79 -0
  88. package/components/blocks/notification-center.tsx +244 -0
  89. package/components/blocks/page-header.module.css +161 -0
  90. package/components/blocks/page-header.tsx +495 -0
  91. package/components/blocks/plan-comparison.module.css +87 -0
  92. package/components/blocks/plan-comparison.tsx +120 -0
  93. package/components/blocks/pricing.tsx +421 -0
  94. package/components/blocks/product-tour.tsx +621 -0
  95. package/components/blocks/proximity-sidebar.tsx +397 -0
  96. package/components/blocks/rail-toc.tsx +468 -0
  97. package/components/blocks/scroll-progress.tsx +375 -0
  98. package/components/blocks/sidebar-stackbits.tsx +362 -0
  99. package/components/blocks/sign-in.module.css +73 -0
  100. package/components/blocks/sign-in.tsx +337 -0
  101. package/components/blocks/site-header.module.css +115 -0
  102. package/components/blocks/site-header.tsx +548 -0
  103. package/components/blocks/slope-chart.module.css +51 -0
  104. package/components/blocks/slope-chart.tsx +256 -0
  105. package/components/blocks/sortable-data-table.module.css +118 -0
  106. package/components/blocks/sortable-data-table.tsx +312 -0
  107. package/components/blocks/stats-band-data.ts +67 -0
  108. package/components/blocks/stats-band-world.ts +20 -0
  109. package/components/blocks/stats-band.module.css +150 -0
  110. package/components/blocks/stats-band.tsx +236 -0
  111. package/components/blocks/status-bars.css +52 -0
  112. package/components/blocks/status-bars.tsx +406 -0
  113. package/components/blocks/step-player.tsx +344 -0
  114. package/components/blocks/task-list.tsx +374 -0
  115. package/components/blocks/waffle-chart.module.css +52 -0
  116. package/components/blocks/waffle-chart.tsx +303 -0
  117. package/components/buttons/ash-burst-button.tsx +1 -1
  118. package/components/buttons/minecraft-button.tsx +1 -1
  119. package/components/craft/Button.tsx +137 -0
  120. package/components/craft/Form.tsx +594 -0
  121. package/components/craft/airport-matrix-clock.tsx +389 -0
  122. package/components/craft/animated-counter.tsx +384 -0
  123. package/components/craft/batch-gesture-tray.tsx +273 -0
  124. package/components/craft/car-smoke-page-transition.tsx +1147 -0
  125. package/components/craft/circular-orbit.tsx +270 -0
  126. package/components/craft/click-spark.tsx +205 -0
  127. package/components/craft/code-snippet-deck.tsx +275 -0
  128. package/components/craft/command.tsx +186 -0
  129. package/components/craft/cursor-follower.tsx +156 -0
  130. package/components/craft/delete-button.tsx +285 -0
  131. package/components/craft/density-lens.tsx +115 -0
  132. package/components/craft/dependency-trace.tsx +219 -0
  133. package/components/craft/depth-corridor.tsx +179 -0
  134. package/components/craft/discover-button.css +83 -0
  135. package/components/craft/discover-button.tsx +46 -0
  136. package/components/craft/drag-to-confirm.tsx +387 -0
  137. package/components/craft/draggable-marquee.css +5 -0
  138. package/components/craft/draggable-marquee.tsx +352 -0
  139. package/components/craft/duration-picker.tsx +291 -0
  140. package/components/craft/emoji-reaction.tsx +610 -0
  141. package/components/craft/empty.tsx +108 -0
  142. package/components/craft/evil-eye.tsx +350 -0
  143. package/components/craft/expandable-data-row.tsx +419 -0
  144. package/components/craft/field.tsx +250 -0
  145. package/components/craft/file-input.tsx +553 -0
  146. package/components/craft/flip-text.tsx +128 -0
  147. package/components/craft/fluid-orb.tsx +199 -0
  148. package/components/craft/focus-mode.tsx +262 -0
  149. package/components/craft/folder-component.tsx +474 -0
  150. package/components/craft/framing.ts +53 -0
  151. package/components/craft/github-activity.tsx +682 -0
  152. package/components/craft/glitch-text.tsx +414 -0
  153. package/components/craft/glyph-matrix.tsx +411 -0
  154. package/components/craft/golden-gate-404/framing.ts +53 -0
  155. package/components/craft/golden-gate-404/golden-gate-404.css +271 -0
  156. package/components/craft/golden-gate-404/golden-gate-404.mdx +55 -0
  157. package/components/craft/golden-gate-404/golden-gate-scene.tsx +73 -0
  158. package/components/craft/golden-gate-404/golden-gate.css +157 -0
  159. package/components/craft/golden-gate-404/live.ts +592 -0
  160. package/components/craft/golden-gate-404/model.json +176 -0
  161. package/components/craft/golden-gate-404/projection.ts +41 -0
  162. package/components/craft/golden-gate-404.css +271 -0
  163. package/components/craft/golden-gate-404.tsx +67 -0
  164. package/components/craft/golden-gate-scene.tsx +73 -0
  165. package/components/craft/golden-gate.css +157 -0
  166. package/components/craft/gooey-nav.tsx +286 -0
  167. package/components/craft/gravity-letters.tsx +687 -0
  168. package/components/craft/gravity-particle-burst.tsx +150 -0
  169. package/components/craft/grid-reveal.tsx +683 -0
  170. package/components/craft/hover-img.css +156 -0
  171. package/components/craft/hover-img.tsx +160 -0
  172. package/components/craft/input-otp.tsx +79 -0
  173. package/components/craft/interactive-area-chart.tsx +482 -0
  174. package/components/craft/interactive-timeline.tsx +315 -0
  175. package/components/craft/intro-loader.tsx +250 -0
  176. package/components/craft/ios-search-bar.tsx +130 -0
  177. package/components/craft/liquid-metal.tsx +212 -0
  178. package/components/craft/liquid-toggle.tsx +308 -0
  179. package/components/craft/live.ts +592 -0
  180. package/components/craft/loaders-gooey-blobs.tsx +61 -0
  181. package/components/craft/lock-input.tsx +169 -0
  182. package/components/craft/login.tsx +276 -0
  183. package/components/craft/meteors.tsx +139 -0
  184. package/components/craft/metric-hud.tsx +371 -0
  185. package/components/craft/model.json +176 -0
  186. package/components/craft/morphing-blob.tsx +289 -0
  187. package/components/craft/morphing-dialog.tsx +118 -0
  188. package/components/craft/morphing-icon.tsx +74 -0
  189. package/components/craft/morphing-shape-loader.tsx +241 -0
  190. package/components/craft/nimbu-mirchi.tsx +360 -0
  191. package/components/craft/notification-bell.tsx +357 -0
  192. package/components/craft/orbital-loading-ring.tsx +104 -0
  193. package/components/craft/origin-dropdown.tsx +196 -0
  194. package/components/craft/particle-delete.tsx +266 -0
  195. package/components/craft/payment-receipt-printer.tsx +600 -0
  196. package/components/craft/payment-status.tsx +359 -0
  197. package/components/craft/projection.ts +41 -0
  198. package/components/craft/recovery-ledger.tsx +215 -0
  199. package/components/craft/rocket-party-popper.tsx +308 -0
  200. package/components/craft/scroll-progress-nav.tsx +256 -0
  201. package/components/craft/scrollvelocitytext.tsx +285 -0
  202. package/components/craft/selection-basket.tsx +147 -0
  203. package/components/craft/sheet.tsx +324 -0
  204. package/components/craft/shooting-stars.tsx +710 -0
  205. package/components/craft/sidebar.tsx +448 -0
  206. package/components/craft/sign-up.tsx +396 -0
  207. package/components/craft/skeumorphic-music-card.tsx +177 -0
  208. package/components/craft/slide-pagination.tsx +170 -0
  209. package/components/craft/slot-text.module.css +19 -0
  210. package/components/craft/slot-text.tsx +183 -0
  211. package/components/craft/smart-comparison.tsx +375 -0
  212. package/components/craft/smooth-scroll.tsx +63 -0
  213. package/components/craft/speed-warp.tsx +179 -0
  214. package/components/craft/split-showcase.tsx +268 -0
  215. package/components/craft/spotlight-search.tsx +337 -0
  216. package/components/craft/stack-unfold-panel.tsx +137 -0
  217. package/components/craft/sticky-pages.tsx +210 -0
  218. package/components/craft/text-scramble-decoder.tsx +139 -0
  219. package/components/craft/toggle.tsx +49 -0
  220. package/components/craft/torque-dial.tsx +293 -0
  221. package/components/craft/v-prism-settings.ts +31 -0
  222. package/components/craft/v-prism.tsx +797 -0
  223. package/components/craft/voice-note.tsx +730 -0
  224. package/components/docks/mac-dock.tsx +1 -1
  225. package/components/motion/accordion.module.css +15 -0
  226. package/components/motion/accordion.tsx +59 -0
  227. package/components/motion/action-button.module.css +20 -0
  228. package/components/motion/action-button.tsx +120 -0
  229. package/components/motion/action-sheet.tsx +1 -1
  230. package/components/motion/alert.module.css +15 -0
  231. package/components/motion/alert.tsx +109 -0
  232. package/components/motion/animated-counter.tsx +2 -0
  233. package/components/motion/animated-sidebar.tsx +1 -1
  234. package/components/motion/animated-toast-stack.tsx +1 -1
  235. package/components/motion/attachment-upload.tsx +1 -1
  236. package/components/motion/avatar.module.css +60 -0
  237. package/components/motion/avatar.tsx +21 -0
  238. package/components/motion/badge.module.css +52 -0
  239. package/components/motion/badge.tsx +82 -0
  240. package/components/motion/banner-stacking.tsx +3 -3
  241. package/components/motion/billing-toggle.module.css +139 -0
  242. package/components/motion/billing-toggle.tsx +205 -0
  243. package/components/motion/book-call-button.tsx +332 -0
  244. package/components/motion/bottom-sheet.tsx +1 -1
  245. package/components/motion/branching-submenu.tsx +223 -0
  246. package/components/motion/button/index.tsx +1 -0
  247. package/components/motion/button-group.module.css +87 -0
  248. package/components/motion/button-group.tsx +346 -0
  249. package/components/motion/calendar.module.css +51 -0
  250. package/components/motion/calendar.tsx +313 -0
  251. package/components/motion/card-stack.module.css +63 -0
  252. package/components/motion/card-stack.tsx +372 -0
  253. package/components/motion/card.module.css +51 -0
  254. package/components/motion/card.tsx +201 -0
  255. package/components/motion/carousel.module.css +42 -0
  256. package/components/motion/carousel.tsx +347 -0
  257. package/components/motion/center-morph-modal.tsx +1 -1
  258. package/components/motion/chart.tsx +359 -0
  259. package/components/motion/chip-group.module.css +38 -0
  260. package/components/motion/chip-group.tsx +256 -0
  261. package/components/motion/combobox/content.tsx +1 -1
  262. package/components/motion/command-menu.tsx +295 -0
  263. package/components/motion/command-palette.tsx +1 -1
  264. package/components/motion/confirm-morph.module.css +146 -0
  265. package/components/motion/confirm-morph.tsx +445 -0
  266. package/components/motion/context-menu.tsx +1 -1
  267. package/components/motion/cookie-consent.module.css +68 -0
  268. package/components/motion/cookie-consent.tsx +364 -0
  269. package/components/motion/countdown.module.css +49 -0
  270. package/components/motion/countdown.tsx +218 -0
  271. package/components/motion/date-picker.module.css +34 -0
  272. package/components/motion/date-picker.tsx +131 -0
  273. package/components/motion/dialog.module.css +36 -0
  274. package/components/motion/dialog.tsx +75 -0
  275. package/components/motion/dock.tsx +1 -1
  276. package/components/motion/dropdown-menu-morph.tsx +4 -4
  277. package/components/motion/dropdown-menu.module.css +38 -0
  278. package/components/motion/dropdown-menu.tsx +76 -0
  279. package/components/motion/elastic-slider.module.css +40 -0
  280. package/components/motion/elastic-slider.tsx +296 -0
  281. package/components/motion/empty-state.module.css +13 -0
  282. package/components/motion/empty-state.tsx +88 -0
  283. package/components/motion/expandable-card.module.css +22 -0
  284. package/components/motion/expandable-card.tsx +129 -0
  285. package/components/motion/expandable-text.module.css +19 -0
  286. package/components/motion/expandable-text.tsx +168 -0
  287. package/components/motion/expanding-button-group.module.css +44 -0
  288. package/components/motion/expanding-button-group.tsx +367 -0
  289. package/components/motion/expanding-search.module.css +51 -0
  290. package/components/motion/expanding-search.tsx +336 -0
  291. package/components/motion/file-dropzone.module.css +115 -0
  292. package/components/motion/file-dropzone.tsx +442 -0
  293. package/components/motion/filter-toolbar.module.css +70 -0
  294. package/components/motion/filter-toolbar.tsx +461 -0
  295. package/components/motion/floating-button-group.module.css +74 -0
  296. package/components/motion/floating-button-group.tsx +307 -0
  297. package/components/motion/gauge.module.css +22 -0
  298. package/components/motion/gauge.tsx +106 -0
  299. package/components/motion/hamburger-menu.tsx +103 -0
  300. package/components/motion/hold-to-confirm.module.css +20 -0
  301. package/components/motion/hold-to-confirm.tsx +209 -0
  302. package/components/motion/icon-morph.module.css +5 -0
  303. package/components/motion/icon-morph.tsx +239 -0
  304. package/components/motion/image-compare.module.css +25 -0
  305. package/components/motion/image-compare.tsx +278 -0
  306. package/components/motion/in-view-title.module.css +13 -0
  307. package/components/motion/in-view-title.tsx +74 -0
  308. package/components/motion/inline-edit.module.css +73 -0
  309. package/components/motion/inline-edit.tsx +360 -0
  310. package/components/motion/input-group.tsx +172 -0
  311. package/components/motion/item.tsx +197 -0
  312. package/components/motion/label.tsx +26 -0
  313. package/components/motion/liquid-ripple-button.tsx +193 -0
  314. package/components/motion/mac-os-folder-cards.tsx +268 -0
  315. package/components/motion/magnetic-button.tsx +96 -0
  316. package/components/motion/mention-input.module.css +134 -0
  317. package/components/motion/mention-input.tsx +522 -0
  318. package/components/motion/menubar.tsx +282 -0
  319. package/components/motion/money-card.tsx +1134 -0
  320. package/components/motion/money-input.module.css +77 -0
  321. package/components/motion/money-input.tsx +563 -0
  322. package/components/motion/morph-select.module.css +86 -0
  323. package/components/motion/morph-select.tsx +463 -0
  324. package/components/motion/morphing-button.tsx +141 -0
  325. package/components/motion/morphing-mega-menu.tsx +452 -0
  326. package/components/motion/morphing-search.tsx +1 -1
  327. package/components/motion/morphing-tabs.tsx +1 -1
  328. package/components/motion/multi-select/content.tsx +1 -1
  329. package/components/motion/navigation-menu.tsx +172 -0
  330. package/components/motion/neon-edge-button.tsx +64 -0
  331. package/components/motion/onboarding-checklist.module.css +71 -0
  332. package/components/motion/onboarding-checklist.tsx +349 -0
  333. package/components/motion/organic-shimmer.tsx +6 -6
  334. package/components/motion/otp-input.tsx +7 -4
  335. package/components/motion/password-field.module.css +27 -0
  336. package/components/motion/password-field.tsx +65 -0
  337. package/components/motion/password-strength.module.css +57 -0
  338. package/components/motion/password-strength.tsx +327 -0
  339. package/components/motion/peek-card.tsx +239 -0
  340. package/components/motion/popover-morph.tsx +1 -1
  341. package/components/motion/popover.tsx +1 -1
  342. package/components/motion/preview-rail.tsx +1 -1
  343. package/components/motion/profile-card.tsx +326 -0
  344. package/components/motion/progress.module.css +1 -0
  345. package/components/motion/progress.tsx +60 -0
  346. package/components/motion/project-folder.tsx +1 -1
  347. package/components/motion/radio-cards.module.css +84 -0
  348. package/components/motion/radio-cards.tsx +153 -0
  349. package/components/motion/radio-group.module.css +30 -0
  350. package/components/motion/radio-group.tsx +52 -0
  351. package/components/motion/rainbow-button.tsx +186 -0
  352. package/components/motion/raised-button.tsx +108 -0
  353. package/components/motion/range-histogram-slider.module.css +27 -0
  354. package/components/motion/range-histogram-slider.tsx +284 -0
  355. package/components/motion/reactions.module.css +49 -0
  356. package/components/motion/reactions.tsx +288 -0
  357. package/components/motion/resizable-panels.module.css +40 -0
  358. package/components/motion/resizable-panels.tsx +376 -0
  359. package/components/motion/resizable.tsx +58 -0
  360. package/components/motion/rich-text-editor.module.css +201 -0
  361. package/components/motion/rich-text-editor.tsx +1566 -0
  362. package/components/motion/ridgeline.module.css +58 -0
  363. package/components/motion/ridgeline.tsx +355 -0
  364. package/components/motion/scroll-area.module.css +79 -0
  365. package/components/motion/scroll-area.tsx +197 -0
  366. package/components/motion/scrub-input.module.css +135 -0
  367. package/components/motion/scrub-input.tsx +468 -0
  368. package/components/motion/search-field.module.css +17 -0
  369. package/components/motion/search-field.tsx +19 -0
  370. package/components/motion/segmented-control.tsx +2 -0
  371. package/components/motion/separator.tsx +30 -0
  372. package/components/motion/shortcut-recorder.module.css +85 -0
  373. package/components/motion/shortcut-recorder.tsx +513 -0
  374. package/components/motion/signature-pad.module.css +84 -0
  375. package/components/motion/signature-pad.tsx +546 -0
  376. package/components/motion/skeleton.module.css +11 -0
  377. package/components/motion/skeleton.tsx +70 -0
  378. package/components/motion/slider.module.css +45 -0
  379. package/components/motion/slider.tsx +405 -0
  380. package/components/motion/sonner.tsx +42 -0
  381. package/components/motion/sparkline.module.css +26 -0
  382. package/components/motion/sparkline.tsx +260 -0
  383. package/components/motion/spinner.tsx +20 -0
  384. package/components/motion/split-button.module.css +54 -0
  385. package/components/motion/split-button.tsx +114 -0
  386. package/components/motion/story-card.tsx +632 -0
  387. package/components/motion/streamgraph.module.css +55 -0
  388. package/components/motion/streamgraph.tsx +398 -0
  389. package/components/motion/swipe-actions.module.css +64 -0
  390. package/components/motion/swipe-actions.tsx +360 -0
  391. package/components/motion/table/row-handle.tsx +1 -1
  392. package/components/motion/table/table-header.tsx +1 -1
  393. package/components/motion/table/table-menu.tsx +1 -1
  394. package/components/motion/tag-input.module.css +34 -0
  395. package/components/motion/tag-input.tsx +150 -0
  396. package/components/motion/textarea.module.css +22 -0
  397. package/components/motion/textarea.tsx +64 -0
  398. package/components/motion/theme-switch-eclipse.tsx +82 -0
  399. package/components/motion/theme-switch-rise.tsx +82 -0
  400. package/components/motion/theme-switch-split.tsx +82 -0
  401. package/components/motion/theme-switch.module.css +31 -0
  402. package/components/motion/theme-switch.tsx +82 -0
  403. package/components/motion/time-picker.module.css +23 -0
  404. package/components/motion/time-picker.tsx +106 -0
  405. package/components/motion/toast.module.css +100 -0
  406. package/components/motion/toast.tsx +155 -0
  407. package/components/motion/tooltip.tsx +1 -1
  408. package/components/motion/treemap.module.css +59 -0
  409. package/components/motion/treemap.tsx +333 -0
  410. package/components/motion/undo-toast.tsx +252 -0
  411. package/components/motion/usage-meter.module.css +61 -0
  412. package/components/motion/usage-meter.tsx +340 -0
  413. package/components/otp/otp-boxed-input.tsx +18 -18
  414. package/components/previews/agents/ai-agent-activity.preview.tsx +14 -0
  415. package/components/previews/agents/ai-response.preview.tsx +14 -0
  416. package/components/previews/agents/ai-status.preview.tsx +14 -0
  417. package/components/previews/agents/chat-thread.preview.tsx +18 -0
  418. package/components/previews/agents/comment-thread.preview.tsx +17 -0
  419. package/components/previews/agents/json-viewer.preview.tsx +14 -0
  420. package/components/previews/agents/matrix-orb.preview.tsx +14 -0
  421. package/components/previews/agents/text-stream.preview.tsx +19 -0
  422. package/components/previews/agents/thinking-orb.preview.tsx +14 -0
  423. package/components/previews/agents/web-search.preview.tsx +17 -0
  424. package/components/previews/blocks/active-sessions.preview.tsx +14 -0
  425. package/components/previews/blocks/activity-feed.preview.tsx +14 -0
  426. package/components/previews/blocks/activity-heatmap.preview.tsx +18 -0
  427. package/components/previews/blocks/advanced-data-table.preview.tsx +17 -0
  428. package/components/previews/blocks/analytics-dashboard.preview.tsx +14 -0
  429. package/components/previews/blocks/art-gallery.preview.tsx +14 -0
  430. package/components/previews/blocks/bar-chart.preview.tsx +18 -0
  431. package/components/previews/blocks/blog-grid.preview.tsx +14 -0
  432. package/components/previews/blocks/brush-chart.preview.tsx +17 -0
  433. package/components/previews/blocks/changelog-feed.preview.tsx +14 -0
  434. package/components/previews/blocks/chat.preview.tsx +18 -0
  435. package/components/previews/blocks/comparison-table.preview.tsx +14 -0
  436. package/components/previews/blocks/cta-section.preview.tsx +14 -0
  437. package/components/previews/blocks/dashboard-shell.preview.tsx +18 -0
  438. package/components/previews/blocks/empty-states.preview.tsx +14 -0
  439. package/components/previews/blocks/expandable-tabs.preview.tsx +1 -1
  440. package/components/previews/blocks/family-drawer.preview.tsx +14 -0
  441. package/components/previews/blocks/faq.preview.tsx +14 -0
  442. package/components/previews/blocks/footer.preview.tsx +14 -0
  443. package/components/previews/blocks/form.preview.tsx +14 -0
  444. package/components/previews/blocks/frame.preview.tsx +18 -0
  445. package/components/previews/blocks/gantt.preview.tsx +18 -0
  446. package/components/previews/blocks/hero-section.preview.tsx +14 -0
  447. package/components/previews/blocks/hook-sidebar.preview.tsx +14 -0
  448. package/components/previews/blocks/kanban.preview.tsx +68 -0
  449. package/components/previews/blocks/line-chart.preview.tsx +18 -0
  450. package/components/previews/blocks/login-centered.preview.tsx +14 -0
  451. package/components/previews/blocks/logo-marquee.preview.tsx +14 -0
  452. package/components/previews/blocks/newsletter-signup.preview.tsx +14 -0
  453. package/components/previews/blocks/notification-center.preview.tsx +14 -0
  454. package/components/previews/blocks/otp-input.preview.tsx +2 -2
  455. package/components/previews/blocks/page-header.preview.tsx +14 -0
  456. package/components/previews/blocks/plan-comparison.preview.tsx +14 -0
  457. package/components/previews/blocks/pricing.preview.tsx +14 -0
  458. package/components/previews/blocks/product-tour.preview.tsx +14 -0
  459. package/components/previews/blocks/proximity-sidebar.preview.tsx +14 -0
  460. package/components/previews/blocks/rail-toc.preview.tsx +14 -0
  461. package/components/previews/blocks/scroll-progress.preview.tsx +14 -0
  462. package/components/previews/blocks/sidebar-stackbits.preview.tsx +14 -0
  463. package/components/previews/blocks/sign-in.preview.tsx +14 -0
  464. package/components/previews/blocks/site-header.preview.tsx +14 -0
  465. package/components/previews/blocks/slope-chart.preview.tsx +19 -0
  466. package/components/previews/blocks/sortable-data-table.preview.tsx +18 -0
  467. package/components/previews/blocks/stats-band.preview.tsx +14 -0
  468. package/components/previews/blocks/status-bars.preview.tsx +17 -0
  469. package/components/previews/blocks/step-player.preview.tsx +14 -0
  470. package/components/previews/blocks/task-list.preview.tsx +14 -0
  471. package/components/previews/blocks/waffle-chart.preview.tsx +17 -0
  472. package/components/previews/craft/airport-matrix-clock.preview.tsx +14 -0
  473. package/components/previews/craft/animated-counter.preview.tsx +14 -0
  474. package/components/previews/craft/apple-notification.preview.tsx +11 -0
  475. package/components/previews/craft/batch-gesture-tray.preview.tsx +19 -0
  476. package/components/previews/craft/booking-slot-calendar.preview.tsx +11 -0
  477. package/components/previews/craft/cafe-menu-board.preview.tsx +11 -0
  478. package/components/previews/craft/calendar-reminder-notification.preview.tsx +11 -0
  479. package/components/previews/craft/car-smoke-page-transition.preview.tsx +14 -0
  480. package/components/previews/craft/chat-bubble-notification.preview.tsx +11 -0
  481. package/components/previews/craft/cinema-ticket.preview.tsx +11 -0
  482. package/components/previews/craft/circle-cut-frame.preview.tsx +11 -0
  483. package/components/previews/craft/circular-orbit.preview.tsx +14 -0
  484. package/components/previews/craft/click-spark.preview.tsx +14 -0
  485. package/components/previews/craft/code-snippet-deck.preview.tsx +14 -0
  486. package/components/previews/craft/command.preview.tsx +14 -0
  487. package/components/previews/craft/contact-profile.preview.tsx +11 -0
  488. package/components/previews/craft/credit-card-glass.preview.tsx +11 -0
  489. package/components/previews/craft/cursor-follower.preview.tsx +14 -0
  490. package/components/previews/craft/customers-table.preview.tsx +11 -0
  491. package/components/previews/craft/daily-motivation.preview.tsx +11 -0
  492. package/components/previews/craft/date-range-picker-card.preview.tsx +11 -0
  493. package/components/previews/craft/delete-button.preview.tsx +14 -0
  494. package/components/previews/craft/delivery-notification.preview.tsx +11 -0
  495. package/components/previews/craft/denim-product-editorial.preview.tsx +11 -0
  496. package/components/previews/craft/density-lens.preview.tsx +14 -0
  497. package/components/previews/craft/dependency-trace.preview.tsx +17 -0
  498. package/components/previews/craft/deploy-notification.preview.tsx +11 -0
  499. package/components/previews/craft/depth-corridor.preview.tsx +14 -0
  500. package/components/previews/craft/discord-chat.preview.tsx +11 -0
  501. package/components/previews/craft/discover-button.preview.tsx +14 -0
  502. package/components/previews/craft/doubt-button.preview.tsx +11 -0
  503. package/components/previews/craft/drag-to-confirm.preview.tsx +14 -0
  504. package/components/previews/craft/draggable-marquee.preview.tsx +17 -0
  505. package/components/previews/craft/drop-cap-editorial.preview.tsx +11 -0
  506. package/components/previews/craft/duration-picker.preview.tsx +14 -0
  507. package/components/previews/craft/editorial-staff-profile.preview.tsx +11 -0
  508. package/components/previews/craft/electric-scooter.preview.tsx +11 -0
  509. package/components/previews/craft/email-notification.preview.tsx +11 -0
  510. package/components/previews/craft/emoji-reaction.preview.tsx +14 -0
  511. package/components/previews/craft/empty.preview.tsx +14 -0
  512. package/components/previews/craft/event-ticket.preview.tsx +11 -0
  513. package/components/previews/craft/evil-eye.preview.tsx +14 -0
  514. package/components/previews/craft/expandable-data-row.preview.tsx +14 -0
  515. package/components/previews/craft/field.preview.tsx +14 -0
  516. package/components/previews/craft/file-input.preview.tsx +14 -0
  517. package/components/previews/craft/film-strip.preview.tsx +11 -0
  518. package/components/previews/craft/flight-boarding.preview.tsx +11 -0
  519. package/components/previews/craft/flip-text.preview.tsx +14 -0
  520. package/components/previews/craft/fluid-orb.preview.tsx +14 -0
  521. package/components/previews/craft/focus-mode.preview.tsx +14 -0
  522. package/components/previews/craft/folder-component.preview.tsx +14 -0
  523. package/components/previews/craft/folder-tab-card.preview.tsx +11 -0
  524. package/components/previews/craft/gallery-grid.preview.tsx +11 -0
  525. package/components/previews/craft/gallery-wall.preview.tsx +11 -0
  526. package/components/previews/craft/github-activity.preview.tsx +14 -0
  527. package/components/previews/craft/github-contribution.preview.tsx +11 -0
  528. package/components/previews/craft/github-repo.preview.tsx +11 -0
  529. package/components/previews/craft/glass-overlay-image.preview.tsx +11 -0
  530. package/components/previews/craft/glitch-text.preview.tsx +14 -0
  531. package/components/previews/craft/glyph-matrix.preview.tsx +14 -0
  532. package/components/previews/craft/golden-gate-404.preview.tsx +14 -0
  533. package/components/previews/craft/gooey-nav.preview.tsx +14 -0
  534. package/components/previews/craft/gravity-letters.preview.tsx +14 -0
  535. package/components/previews/craft/gravity-particle-burst.preview.tsx +14 -0
  536. package/components/previews/craft/grid-reveal.preview.tsx +14 -0
  537. package/components/previews/craft/hard-shadow-polaroid-frame.preview.tsx +11 -0
  538. package/components/previews/craft/hover-img.preview.tsx +14 -0
  539. package/components/previews/craft/incoming-call-notification.preview.tsx +11 -0
  540. package/components/previews/craft/ink-stamp-document.preview.tsx +11 -0
  541. package/components/previews/craft/input-group-field.preview.tsx +11 -0
  542. package/components/previews/craft/input-otp.preview.tsx +19 -0
  543. package/components/previews/craft/interactive-area-chart.preview.tsx +14 -0
  544. package/components/previews/craft/interactive-timeline.preview.tsx +14 -0
  545. package/components/previews/craft/intro-loader.preview.tsx +14 -0
  546. package/components/previews/craft/ios-search-bar.preview.tsx +15 -0
  547. package/components/previews/craft/ipad.preview.tsx +11 -0
  548. package/components/previews/craft/journal-writing.preview.tsx +11 -0
  549. package/components/previews/craft/keyboard-shortcuts.preview.tsx +11 -0
  550. package/components/previews/craft/linked-in-profile.preview.tsx +11 -0
  551. package/components/previews/craft/liquid-metal.preview.tsx +14 -0
  552. package/components/previews/craft/liquid-toggle.preview.tsx +14 -0
  553. package/components/previews/craft/loaders-gooey-blobs.preview.tsx +14 -0
  554. package/components/previews/craft/lock-input.preview.tsx +14 -0
  555. package/components/previews/craft/login.preview.tsx +14 -0
  556. package/components/previews/craft/mac-dock.preview.tsx +11 -0
  557. package/components/previews/craft/magazine-cover.preview.tsx +11 -0
  558. package/components/previews/craft/meteors.preview.tsx +14 -0
  559. package/components/previews/craft/metric-hud.preview.tsx +14 -0
  560. package/components/previews/craft/minimal-agenda.preview.tsx +11 -0
  561. package/components/previews/craft/month-picker-calendar.preview.tsx +11 -0
  562. package/components/previews/craft/morphing-blob.preview.tsx +14 -0
  563. package/components/previews/craft/morphing-dialog.preview.tsx +18 -0
  564. package/components/previews/craft/morphing-icon.preview.tsx +15 -0
  565. package/components/previews/craft/morphing-shape-loader.preview.tsx +14 -0
  566. package/components/previews/craft/museum-placard.preview.tsx +11 -0
  567. package/components/previews/craft/music-player.preview.tsx +11 -0
  568. package/components/previews/craft/music-playlist.preview.tsx +11 -0
  569. package/components/previews/craft/nimbu-mirchi.preview.tsx +14 -0
  570. package/components/previews/craft/notification-bell.preview.tsx +14 -0
  571. package/components/previews/craft/now-playing-bar.preview.tsx +11 -0
  572. package/components/previews/craft/orbital-loading-ring.preview.tsx +14 -0
  573. package/components/previews/craft/orders-table.preview.tsx +11 -0
  574. package/components/previews/craft/origin-dropdown.preview.tsx +14 -0
  575. package/components/previews/craft/otp-boxed-input.preview.tsx +11 -0
  576. package/components/previews/craft/otp-underline-input.preview.tsx +11 -0
  577. package/components/previews/craft/particle-delete.preview.tsx +14 -0
  578. package/components/previews/craft/payment-notification.preview.tsx +11 -0
  579. package/components/previews/craft/payment-receipt-printer.preview.tsx +14 -0
  580. package/components/previews/craft/payment-status.preview.tsx +14 -0
  581. package/components/previews/craft/phone.preview.tsx +11 -0
  582. package/components/previews/craft/photo-album.preview.tsx +11 -0
  583. package/components/previews/craft/progress-ring-card.preview.tsx +11 -0
  584. package/components/previews/craft/recent-transactions-table.preview.tsx +11 -0
  585. package/components/previews/craft/recovery-ledger.preview.tsx +14 -0
  586. package/components/previews/craft/resource-links-panel.preview.tsx +11 -0
  587. package/components/previews/craft/rocket-party-popper.preview.tsx +14 -0
  588. package/components/previews/craft/scroll-progress-nav.preview.tsx +14 -0
  589. package/components/previews/craft/scrollvelocitytext.preview.tsx +14 -0
  590. package/components/previews/craft/selection-basket.preview.tsx +14 -0
  591. package/components/previews/craft/sheet.preview.tsx +15 -0
  592. package/components/previews/craft/shooting-stars.preview.tsx +14 -0
  593. package/components/previews/craft/sidebar.preview.tsx +19 -0
  594. package/components/previews/craft/sign-up.preview.tsx +14 -0
  595. package/components/previews/craft/skeumorphic-music-card.preview.tsx +18 -0
  596. package/components/previews/craft/slide-pagination.preview.tsx +14 -0
  597. package/components/previews/craft/slide-to-confirm-button.preview.tsx +11 -0
  598. package/components/previews/craft/slide-to-detonate.preview.tsx +11 -0
  599. package/components/previews/craft/slot-text.preview.tsx +14 -0
  600. package/components/previews/craft/slow-living-polaroid.preview.tsx +11 -0
  601. package/components/previews/craft/smart-comparison.preview.tsx +17 -0
  602. package/components/previews/craft/smooth-scroll.preview.tsx +14 -0
  603. package/components/previews/craft/speed-warp.preview.tsx +14 -0
  604. package/components/previews/craft/split-showcase.preview.tsx +14 -0
  605. package/components/previews/craft/spotlight-search.preview.tsx +14 -0
  606. package/components/previews/craft/stack-unfold-panel.preview.tsx +14 -0
  607. package/components/previews/craft/stacked-folder-card.preview.tsx +11 -0
  608. package/components/previews/craft/step-count.preview.tsx +11 -0
  609. package/components/previews/craft/sticky-pages.preview.tsx +14 -0
  610. package/components/previews/craft/system-alert.preview.tsx +11 -0
  611. package/components/previews/craft/team-members-table.preview.tsx +11 -0
  612. package/components/previews/craft/testimonial.preview.tsx +11 -0
  613. package/components/previews/craft/text-scramble-decoder.preview.tsx +14 -0
  614. package/components/previews/craft/threads-post.preview.tsx +11 -0
  615. package/components/previews/craft/toggle-pricing-cards.preview.tsx +11 -0
  616. package/components/previews/craft/toggle.preview.tsx +14 -0
  617. package/components/previews/craft/torque-dial.preview.tsx +15 -0
  618. package/components/previews/craft/travel-postcard.preview.tsx +11 -0
  619. package/components/previews/craft/twitter-profile.preview.tsx +11 -0
  620. package/components/previews/craft/user-profile.preview.tsx +11 -0
  621. package/components/previews/craft/users-select-table.preview.tsx +11 -0
  622. package/components/previews/craft/v-prism.preview.tsx +14 -0
  623. package/components/previews/craft/voice-note.preview.tsx +14 -0
  624. package/components/previews/craft/wallet-pass.preview.tsx +11 -0
  625. package/components/previews/craft/weather-snapshot.preview.tsx +11 -0
  626. package/components/previews/craft/week-strip-calendar.preview.tsx +11 -0
  627. package/components/previews/motion/accordion.preview.tsx +14 -0
  628. package/components/previews/motion/action-button.preview.tsx +14 -0
  629. package/components/previews/motion/alert.preview.tsx +14 -0
  630. package/components/previews/motion/avatar.preview.tsx +14 -0
  631. package/components/previews/motion/badge.preview.tsx +14 -0
  632. package/components/previews/motion/banner-stacking.preview.tsx +21 -0
  633. package/components/previews/motion/billing-toggle.preview.tsx +15 -0
  634. package/components/previews/motion/book-call-button.preview.tsx +14 -0
  635. package/components/previews/motion/branching-submenu.preview.tsx +14 -0
  636. package/components/previews/motion/button-group.preview.tsx +19 -0
  637. package/components/previews/motion/calendar.preview.tsx +14 -0
  638. package/components/previews/motion/card-stack.preview.tsx +19 -0
  639. package/components/previews/motion/card.preview.tsx +14 -0
  640. package/components/previews/motion/carousel.preview.tsx +17 -0
  641. package/components/previews/motion/cascader.preview.tsx +54 -0
  642. package/components/previews/motion/chart.preview.tsx +20 -0
  643. package/components/previews/motion/chip-group.preview.tsx +20 -0
  644. package/components/previews/motion/combobox.preview.tsx +1 -1
  645. package/components/previews/motion/command-menu.preview.tsx +16 -0
  646. package/components/previews/motion/confirm-morph.preview.tsx +14 -0
  647. package/components/previews/motion/cookie-consent.preview.tsx +14 -0
  648. package/components/previews/motion/countdown.preview.tsx +15 -0
  649. package/components/previews/motion/date-picker.preview.tsx +14 -0
  650. package/components/previews/motion/delete-with-smoky-dissolve.preview.tsx +14 -0
  651. package/components/previews/motion/dialog.preview.tsx +24 -0
  652. package/components/previews/motion/drag-drop-with-physics.preview.tsx +15 -0
  653. package/components/previews/motion/dropdown-menu-morph.preview.tsx +11 -0
  654. package/components/previews/motion/dropdown-menu.preview.tsx +19 -0
  655. package/components/previews/motion/elastic-slider.preview.tsx +14 -0
  656. package/components/previews/motion/empty-state.preview.tsx +14 -0
  657. package/components/previews/motion/expandable-card.preview.tsx +14 -0
  658. package/components/previews/motion/expandable-text.preview.tsx +14 -0
  659. package/components/previews/motion/expanding-button-group.preview.tsx +18 -0
  660. package/components/previews/motion/expanding-search.preview.tsx +17 -0
  661. package/components/previews/motion/file-dropzone.preview.tsx +14 -0
  662. package/components/previews/motion/filter-toolbar.preview.tsx +15 -0
  663. package/components/previews/motion/floating-button-group.preview.tsx +14 -0
  664. package/components/previews/motion/gauge.preview.tsx +14 -0
  665. package/components/previews/motion/hamburger-menu.preview.tsx +15 -0
  666. package/components/previews/motion/hold-to-confirm.preview.tsx +16 -0
  667. package/components/previews/motion/icon-morph.preview.tsx +14 -0
  668. package/components/previews/motion/image-compare.preview.tsx +17 -0
  669. package/components/previews/motion/in-view-title.preview.tsx +14 -0
  670. package/components/previews/motion/inline-edit.preview.tsx +15 -0
  671. package/components/previews/motion/input-group.preview.tsx +14 -0
  672. package/components/previews/motion/item.preview.tsx +14 -0
  673. package/components/previews/motion/label.preview.tsx +14 -0
  674. package/components/previews/motion/liquid-ripple-button.preview.tsx +14 -0
  675. package/components/previews/motion/mac-os-folder-cards.preview.tsx +14 -0
  676. package/components/previews/motion/magnetic-button.preview.tsx +14 -0
  677. package/components/previews/motion/mention-input.preview.tsx +14 -0
  678. package/components/previews/motion/menubar.preview.tsx +14 -0
  679. package/components/previews/motion/money-card.preview.tsx +14 -0
  680. package/components/previews/motion/money-input.preview.tsx +14 -0
  681. package/components/previews/motion/morph-select.preview.tsx +14 -0
  682. package/components/previews/motion/morphing-button.preview.tsx +14 -0
  683. package/components/previews/motion/morphing-mega-menu.preview.tsx +14 -0
  684. package/components/previews/motion/navigation-menu.preview.tsx +14 -0
  685. package/components/previews/motion/neon-edge-button.preview.tsx +14 -0
  686. package/components/previews/motion/onboarding-checklist.preview.tsx +17 -0
  687. package/components/previews/motion/organic-shimmer.preview.tsx +18 -0
  688. package/components/previews/motion/page-transition.preview.tsx +35 -0
  689. package/components/previews/motion/pagination.preview.tsx +20 -0
  690. package/components/previews/motion/password-field.preview.tsx +14 -0
  691. package/components/previews/motion/password-strength.preview.tsx +14 -0
  692. package/components/previews/motion/peek-card.preview.tsx +14 -0
  693. package/components/previews/motion/popover.preview.tsx +1 -1
  694. package/components/previews/motion/profile-card.preview.tsx +14 -0
  695. package/components/previews/motion/progress.preview.tsx +14 -0
  696. package/components/previews/motion/radio-cards.preview.tsx +14 -0
  697. package/components/previews/motion/radio-group.preview.tsx +20 -0
  698. package/components/previews/motion/rainbow-button.preview.tsx +14 -0
  699. package/components/previews/motion/raised-button.preview.tsx +14 -0
  700. package/components/previews/motion/range-histogram-slider.preview.tsx +14 -0
  701. package/components/previews/motion/reactions.preview.tsx +15 -0
  702. package/components/previews/motion/resizable-panels.preview.tsx +15 -0
  703. package/components/previews/motion/resizable.preview.tsx +19 -0
  704. package/components/previews/motion/rich-text-editor.preview.tsx +14 -0
  705. package/components/previews/motion/ridgeline.preview.tsx +17 -0
  706. package/components/previews/motion/scroll-area.preview.tsx +14 -0
  707. package/components/previews/motion/scrub-input.preview.tsx +14 -0
  708. package/components/previews/motion/search-field.preview.tsx +15 -0
  709. package/components/previews/motion/segmented-control.preview.tsx +23 -0
  710. package/components/previews/motion/select.preview.tsx +1 -1
  711. package/components/previews/motion/separator.preview.tsx +14 -0
  712. package/components/previews/motion/shortcut-recorder.preview.tsx +14 -0
  713. package/components/previews/motion/signature-pad.preview.tsx +14 -0
  714. package/components/previews/motion/skeleton.preview.tsx +14 -0
  715. package/components/previews/motion/slider.preview.tsx +14 -0
  716. package/components/previews/motion/sonner.preview.tsx +14 -0
  717. package/components/previews/motion/sparkline.preview.tsx +14 -0
  718. package/components/previews/motion/spinner.preview.tsx +14 -0
  719. package/components/previews/motion/split-button.preview.tsx +14 -0
  720. package/components/previews/motion/stepper.preview.tsx +39 -0
  721. package/components/previews/motion/story-card.preview.tsx +14 -0
  722. package/components/previews/motion/streamgraph.preview.tsx +18 -0
  723. package/components/previews/motion/swipe-actions.preview.tsx +18 -0
  724. package/components/previews/motion/tag-input.preview.tsx +14 -0
  725. package/components/previews/motion/text-animation.preview.tsx +2 -2
  726. package/components/previews/motion/text-reveal.preview.tsx +14 -0
  727. package/components/previews/motion/text-shimmer.preview.tsx +14 -0
  728. package/components/previews/motion/textarea.preview.tsx +14 -0
  729. package/components/previews/motion/theme-switch-eclipse.preview.tsx +15 -0
  730. package/components/previews/motion/theme-switch-rise.preview.tsx +15 -0
  731. package/components/previews/motion/theme-switch-split.preview.tsx +15 -0
  732. package/components/previews/motion/theme-switch.preview.tsx +15 -0
  733. package/components/previews/motion/tilt-card.preview.tsx +2 -2
  734. package/components/previews/motion/time-picker.preview.tsx +14 -0
  735. package/components/previews/motion/toast.preview.tsx +14 -0
  736. package/components/previews/motion/treemap.preview.tsx +17 -0
  737. package/components/previews/motion/undo-toast.preview.tsx +14 -0
  738. package/components/previews/motion/usage-meter.preview.tsx +19 -0
  739. package/components/profile/contact-profile-card.tsx +8 -8
  740. package/components/ui/button.tsx +59 -0
  741. package/components/ui/dialog.tsx +149 -0
  742. package/components/ui/input.tsx +20 -0
  743. package/components/ui/label.tsx +1 -0
  744. package/components/ui/loaders-gooey-blobs.tsx +2 -0
  745. package/components/ui/raised-button.tsx +2 -0
  746. package/components/ui/separator.tsx +1 -0
  747. package/components/ui/textarea.tsx +17 -0
  748. package/lib/arc-provider.tsx +101 -0
  749. package/lib/colorUtils.ts +34 -0
  750. package/lib/effects/shared/webgl-surface.jsx +47 -0
  751. package/lib/foundation.css +192 -0
  752. package/lib/media.ts +90 -0
  753. package/lib/motion-tokens.ts +5 -0
  754. package/lib/particle-delete.ts +500 -0
  755. package/lib/portal.tsx +23 -0
  756. package/lib/spring-tokens.ts +25 -0
  757. package/lib/theme/useTheme.tsx +157 -0
  758. package/lib/transition-tokens.ts +75 -0
  759. package/lib/use-copy-feedback.ts +40 -0
  760. package/lib/use-today.ts +8 -0
  761. package/lib/utils.ts +10 -0
  762. package/package.json +32 -4
  763. package/references/agents/ai-agent-activity.md +45 -0
  764. package/references/agents/ai-response.md +45 -0
  765. package/references/agents/ai-status.md +45 -0
  766. package/references/agents/chat-thread.md +45 -0
  767. package/references/agents/comment-thread.md +45 -0
  768. package/references/agents/json-viewer.md +45 -0
  769. package/references/agents/matrix-orb.md +43 -0
  770. package/references/agents/text-stream.md +44 -0
  771. package/references/agents/thinking-orb.md +43 -0
  772. package/references/agents/web-search.md +44 -0
  773. package/references/blocks/active-sessions.md +45 -0
  774. package/references/blocks/activity-feed.md +45 -0
  775. package/references/blocks/activity-heatmap.md +44 -0
  776. package/references/blocks/advanced-data-table.md +45 -0
  777. package/references/blocks/analytics-dashboard.md +43 -0
  778. package/references/blocks/art-gallery.md +44 -0
  779. package/references/blocks/bar-chart.md +44 -0
  780. package/references/blocks/blog-grid.md +45 -0
  781. package/references/blocks/brush-chart.md +44 -0
  782. package/references/blocks/changelog-feed.md +47 -0
  783. package/references/blocks/chat.md +45 -0
  784. package/references/blocks/comparison-table.md +45 -0
  785. package/references/blocks/cta-section.md +45 -0
  786. package/references/blocks/dashboard-shell.md +50 -0
  787. package/references/blocks/empty-states.md +44 -0
  788. package/references/blocks/family-drawer.md +46 -0
  789. package/references/blocks/faq.md +44 -0
  790. package/references/blocks/footer.md +43 -0
  791. package/references/blocks/form.md +45 -0
  792. package/references/blocks/hero-section.md +43 -0
  793. package/references/blocks/hook-sidebar.md +45 -0
  794. package/references/blocks/line-chart.md +44 -0
  795. package/references/blocks/login-centered.md +45 -0
  796. package/references/blocks/logo-marquee.md +44 -0
  797. package/references/blocks/newsletter-signup.md +46 -0
  798. package/references/blocks/notification-center.md +46 -0
  799. package/references/blocks/page-header.md +47 -0
  800. package/references/blocks/plan-comparison.md +45 -0
  801. package/references/blocks/pricing.md +45 -0
  802. package/references/blocks/product-tour.md +46 -0
  803. package/references/blocks/proximity-sidebar.md +44 -0
  804. package/references/blocks/rail-toc.md +44 -0
  805. package/references/blocks/scroll-progress.md +44 -0
  806. package/references/blocks/sidebar-stackbits.md +45 -0
  807. package/references/blocks/sign-in.md +45 -0
  808. package/references/blocks/site-header.md +46 -0
  809. package/references/blocks/slope-chart.md +44 -0
  810. package/references/blocks/sortable-data-table.md +45 -0
  811. package/references/blocks/stats-band.md +44 -0
  812. package/references/blocks/status-bars.md +45 -0
  813. package/references/blocks/step-player.md +45 -0
  814. package/references/blocks/task-list.md +44 -0
  815. package/references/blocks/waffle-chart.md +44 -0
  816. package/references/component-preview-contract.md +42 -0
  817. package/references/craft/airport-matrix-clock.md +44 -0
  818. package/references/craft/animated-counter.md +44 -0
  819. package/references/craft/batch-gesture-tray.md +45 -0
  820. package/references/craft/car-smoke-page-transition.md +44 -0
  821. package/references/craft/circular-orbit.md +44 -0
  822. package/references/craft/click-spark.md +45 -0
  823. package/references/craft/code-snippet-deck.md +45 -0
  824. package/references/craft/command.md +45 -0
  825. package/references/craft/cursor-follower.md +44 -0
  826. package/references/craft/delete-button.md +44 -0
  827. package/references/craft/density-lens.md +44 -0
  828. package/references/craft/dependency-trace.md +44 -0
  829. package/references/craft/depth-corridor.md +45 -0
  830. package/references/craft/discover-button.md +44 -0
  831. package/references/craft/drag-to-confirm.md +45 -0
  832. package/references/craft/draggable-marquee.md +45 -0
  833. package/references/craft/duration-picker.md +48 -0
  834. package/references/craft/emoji-reaction.md +47 -0
  835. package/references/craft/empty.md +44 -0
  836. package/references/craft/evil-eye.md +44 -0
  837. package/references/craft/expandable-data-row.md +45 -0
  838. package/references/craft/field.md +44 -0
  839. package/references/craft/file-input.md +46 -0
  840. package/references/craft/flip-text.md +43 -0
  841. package/references/craft/fluid-orb.md +43 -0
  842. package/references/craft/focus-mode.md +45 -0
  843. package/references/craft/folder-component.md +44 -0
  844. package/references/craft/github-activity.md +45 -0
  845. package/references/craft/glitch-text.md +44 -0
  846. package/references/craft/glyph-matrix.md +43 -0
  847. package/references/craft/golden-gate-404.md +45 -0
  848. package/references/craft/gooey-nav.md +45 -0
  849. package/references/craft/gravity-letters.md +43 -0
  850. package/references/craft/gravity-particle-burst.md +44 -0
  851. package/references/craft/grid-reveal.md +44 -0
  852. package/references/craft/hover-img.md +44 -0
  853. package/references/craft/input-otp.md +45 -0
  854. package/references/craft/interactive-area-chart.md +44 -0
  855. package/references/craft/interactive-timeline.md +45 -0
  856. package/references/craft/intro-loader.md +45 -0
  857. package/references/craft/ios-search-bar.md +45 -0
  858. package/references/craft/liquid-metal.md +45 -0
  859. package/references/craft/liquid-toggle.md +44 -0
  860. package/references/craft/loaders-gooey-blobs.md +44 -0
  861. package/references/craft/lock-input.md +44 -0
  862. package/references/craft/login.md +45 -0
  863. package/references/craft/meteors.md +43 -0
  864. package/references/craft/metric-hud.md +45 -0
  865. package/references/craft/morphing-blob.md +44 -0
  866. package/references/craft/morphing-dialog.md +45 -0
  867. package/references/craft/morphing-icon.md +44 -0
  868. package/references/craft/morphing-shape-loader.md +44 -0
  869. package/references/craft/nimbu-mirchi.md +44 -0
  870. package/references/craft/notification-bell.md +45 -0
  871. package/references/craft/orbital-loading-ring.md +43 -0
  872. package/references/craft/origin-dropdown.md +45 -0
  873. package/references/craft/particle-delete.md +44 -0
  874. package/references/craft/payment-receipt-printer.md +45 -0
  875. package/references/craft/payment-status.md +45 -0
  876. package/references/craft/recovery-ledger.md +45 -0
  877. package/references/craft/rocket-party-popper.md +45 -0
  878. package/references/craft/scroll-progress-nav.md +45 -0
  879. package/references/craft/scrollvelocitytext.md +43 -0
  880. package/references/craft/selection-basket.md +45 -0
  881. package/references/craft/sheet.md +46 -0
  882. package/references/craft/shooting-stars.md +44 -0
  883. package/references/craft/sidebar.md +45 -0
  884. package/references/craft/sign-up.md +45 -0
  885. package/references/craft/skeumorphic-music-card.md +45 -0
  886. package/references/craft/slide-pagination.md +45 -0
  887. package/references/craft/slot-text.md +44 -0
  888. package/references/craft/smart-comparison.md +45 -0
  889. package/references/craft/smooth-scroll.md +44 -0
  890. package/references/craft/speed-warp.md +43 -0
  891. package/references/craft/split-showcase.md +44 -0
  892. package/references/craft/spotlight-search.md +45 -0
  893. package/references/craft/stack-unfold-panel.md +45 -0
  894. package/references/craft/sticky-pages.md +43 -0
  895. package/references/craft/text-scramble-decoder.md +44 -0
  896. package/references/craft/toggle.md +45 -0
  897. package/references/craft/torque-dial.md +44 -0
  898. package/references/craft/v-prism.md +46 -0
  899. package/references/craft/voice-note.md +44 -0
  900. package/references/guides/better-accessibility.md +104 -0
  901. package/references/guides/better-colors.md +88 -0
  902. package/references/guides/better-interface.md +187 -0
  903. package/references/guides/better-layout.md +79 -0
  904. package/references/guides/better-typography.md +136 -0
  905. package/references/guides/better-ui.md +113 -0
  906. package/references/guides/better-writing.md +115 -0
  907. package/references/guides/dark-neutral-design-system.md +776 -0
  908. package/references/guides/fluid-interaction-conventions.md +30 -0
  909. package/references/guides/interface-review.md +142 -0
  910. package/references/guides/spring-motion-system.md +232 -0
  911. package/references/motion/accordion.md +46 -0
  912. package/references/motion/action-button.md +45 -0
  913. package/references/motion/alert.md +45 -0
  914. package/references/motion/avatar.md +44 -0
  915. package/references/motion/badge.md +44 -0
  916. package/references/motion/billing-toggle.md +44 -0
  917. package/references/motion/book-call-button.md +44 -0
  918. package/references/motion/branching-submenu.md +45 -0
  919. package/references/motion/button-group.md +46 -0
  920. package/references/motion/calendar.md +45 -0
  921. package/references/motion/card-stack.md +45 -0
  922. package/references/motion/card.md +46 -0
  923. package/references/motion/carousel.md +45 -0
  924. package/references/motion/chart.md +52 -0
  925. package/references/motion/chip-group.md +44 -0
  926. package/references/motion/command-menu.md +45 -0
  927. package/references/motion/confirm-morph.md +45 -0
  928. package/references/motion/cookie-consent.md +45 -0
  929. package/references/motion/countdown.md +44 -0
  930. package/references/motion/date-picker.md +45 -0
  931. package/references/motion/dialog.md +46 -0
  932. package/references/motion/dropdown-menu.md +46 -0
  933. package/references/motion/elastic-slider.md +44 -0
  934. package/references/motion/empty-state.md +45 -0
  935. package/references/motion/expandable-card.md +45 -0
  936. package/references/motion/expandable-text.md +45 -0
  937. package/references/motion/expanding-button-group.md +44 -0
  938. package/references/motion/expanding-search.md +46 -0
  939. package/references/motion/file-dropzone.md +45 -0
  940. package/references/motion/filter-toolbar.md +45 -0
  941. package/references/motion/floating-button-group.md +45 -0
  942. package/references/motion/gauge.md +44 -0
  943. package/references/motion/hamburger-menu.md +44 -0
  944. package/references/motion/hold-to-confirm.md +45 -0
  945. package/references/motion/icon-morph.md +44 -0
  946. package/references/motion/image-compare.md +45 -0
  947. package/references/motion/in-view-title.md +44 -0
  948. package/references/motion/inline-edit.md +45 -0
  949. package/references/motion/input-group.md +44 -0
  950. package/references/motion/item.md +45 -0
  951. package/references/motion/label.md +44 -0
  952. package/references/motion/liquid-ripple-button.md +45 -0
  953. package/references/motion/mac-os-folder-cards.md +45 -0
  954. package/references/motion/magnetic-button.md +44 -0
  955. package/references/motion/mention-input.md +45 -0
  956. package/references/motion/menubar.md +45 -0
  957. package/references/motion/money-card.md +46 -0
  958. package/references/motion/money-input.md +45 -0
  959. package/references/motion/morph-select.md +45 -0
  960. package/references/motion/morphing-button.md +45 -0
  961. package/references/motion/morphing-mega-menu.md +44 -0
  962. package/references/motion/navigation-menu.md +46 -0
  963. package/references/motion/neon-edge-button.md +44 -0
  964. package/references/motion/onboarding-checklist.md +45 -0
  965. package/references/motion/password-field.md +44 -0
  966. package/references/motion/password-strength.md +44 -0
  967. package/references/motion/peek-card.md +45 -0
  968. package/references/motion/profile-card.md +44 -0
  969. package/references/motion/progress.md +45 -0
  970. package/references/motion/radio-cards.md +44 -0
  971. package/references/motion/radio-group.md +44 -0
  972. package/references/motion/rainbow-button.md +43 -0
  973. package/references/motion/raised-button.md +45 -0
  974. package/references/motion/range-histogram-slider.md +44 -0
  975. package/references/motion/reactions.md +46 -0
  976. package/references/motion/resizable-panels.md +45 -0
  977. package/references/motion/resizable.md +45 -0
  978. package/references/motion/rich-text-editor.md +45 -0
  979. package/references/motion/ridgeline.md +44 -0
  980. package/references/motion/scroll-area.md +43 -0
  981. package/references/motion/scrub-input.md +45 -0
  982. package/references/motion/search-field.md +45 -0
  983. package/references/motion/separator.md +44 -0
  984. package/references/motion/shortcut-recorder.md +45 -0
  985. package/references/motion/signature-pad.md +45 -0
  986. package/references/motion/skeleton.md +44 -0
  987. package/references/motion/slider.md +44 -0
  988. package/references/motion/sonner.md +46 -0
  989. package/references/motion/sparkline.md +44 -0
  990. package/references/motion/spinner.md +44 -0
  991. package/references/motion/split-button.md +46 -0
  992. package/references/motion/story-card.md +44 -0
  993. package/references/motion/streamgraph.md +44 -0
  994. package/references/motion/swipe-actions.md +46 -0
  995. package/references/motion/tag-input.md +45 -0
  996. package/references/motion/text-reveal.md +43 -0
  997. package/references/motion/text-shimmer.md +44 -0
  998. package/references/motion/textarea.md +44 -0
  999. package/references/motion/theme-switch-eclipse.md +45 -0
  1000. package/references/motion/theme-switch-rise.md +45 -0
  1001. package/references/motion/theme-switch-split.md +45 -0
  1002. package/references/motion/theme-switch.md +45 -0
  1003. package/references/motion/time-picker.md +45 -0
  1004. package/references/motion/toast.md +45 -0
  1005. package/references/motion/treemap.md +44 -0
  1006. package/references/motion/undo-toast.md +45 -0
  1007. package/references/motion/usage-meter.md +45 -0
  1008. package/scripts/build_docs.py +209 -205
  1009. package/scripts/verify_skill.py +4 -4
  1010. package/test/test_installer.mjs +31 -1
  1011. package/test/test_preview_contracts.mjs +124 -0
@@ -0,0 +1,1566 @@
1
+ "use client";
2
+
3
+ import { forwardRef, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
4
+ import type { ClipboardEvent as ReactClipboardEvent, CSSProperties, FormEvent, KeyboardEvent as ReactKeyboardEvent, ReactNode } from "react";
5
+ import { AnimatePresence, animate, motion, useIsPresent, useMotionValue, useReducedMotion, useTransform } from "motion/react";
6
+ import type { Transition, Variants } from "motion/react";
7
+ import { ArrowLeft, Bold, Check, Code, Heading1, Heading2, Heading3, Italic, Link, List, ListOrdered, Minus, Pilcrow, Quote, SquareCode, Strikethrough, Unlink } from "lucide-react";
8
+ import { motionTokens } from "@/lib/motion-tokens";
9
+ import styles from "./rich-text-editor.module.css";
10
+
11
+ /* ------------------------------------------------------------------------------------------------
12
+ * Markdown and HTML. Small, dependency free converters for the subset the editor writes.
13
+ * ---------------------------------------------------------------------------------------------- */
14
+
15
+ export type BlockType = "p" | "h1" | "h2" | "h3" | "ul" | "ol" | "blockquote" | "pre" | "hr";
16
+
17
+ const ZWSP = /\u200b/g;
18
+ const escapeHtml = (value: string) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
19
+ const safeUrl = (url: string) => /^(https?:|mailto:|tel:|\/|#)/i.test(url.trim());
20
+
21
+ /** Adds a scheme to bare domains so `example.com/docs` becomes a working link. */
22
+ export function normalizeUrl(input: string) {
23
+ const url = input.trim();
24
+ if (!url) return "";
25
+ if (/^[a-z][a-z\d+.-]*:/i.test(url) || url.startsWith("/") || url.startsWith("#")) return url;
26
+ if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(url)) return `mailto:${url}`;
27
+ return `https://${url}`;
28
+ }
29
+
30
+ function inlineMarkdown(text: string) {
31
+ const codes: string[] = [];
32
+ let out = escapeHtml(text).replace(/`([^`]+)`/g, (_, code: string) => `\u0000${codes.push(code) - 1}\u0000`);
33
+ out = out.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, label: string, url: string) => safeUrl(url) ? `<a href="${url}">${label}</a>` : label);
34
+ out = out.replace(/(\*\*|__)(?=\S)([\s\S]*?\S)\1/g, "<strong>$2</strong>");
35
+ out = out.replace(/~~(?=\S)([\s\S]*?\S)~~/g, "<s>$1</s>");
36
+ out = out.replace(/(^|[^\w*])\*(?=\S)([^*]*?\S)\*(?!\*)/g, "$1<em>$2</em>");
37
+ out = out.replace(/(^|[^\w_])_(?=\S)([^_]*?\S)_(?![\w_])/g, "$1<em>$2</em>");
38
+ return out.replace(/\u0000(\d+)\u0000/g, (_, index: string) => `<code>${codes[Number(index)]}</code>`);
39
+ }
40
+
41
+ function listMarkdown(lines: string[]) {
42
+ const items = lines.map(line => {
43
+ const match = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/.exec(line)!;
44
+ return { indent: match[1]!.replace(/\t/g, " ").length, tag: /\d/.test(match[2]!) ? "ol" : "ul", text: match[3]! };
45
+ });
46
+ let html = "";
47
+ const stack: { indent: number; tag: string }[] = [];
48
+ for (const item of items) {
49
+ while (stack.length > 1 && item.indent < stack[stack.length - 1]!.indent) html += `</li></${stack.pop()!.tag}>`;
50
+ const top = stack[stack.length - 1];
51
+ if (!top || item.indent > top.indent) { html += `<${item.tag}><li>`; stack.push({ indent: item.indent, tag: item.tag }); }
52
+ else if (top.tag !== item.tag) { html += `</li></${top.tag}><${item.tag}><li>`; top.tag = item.tag; }
53
+ else html += "</li><li>";
54
+ html += inlineMarkdown(item.text);
55
+ }
56
+ while (stack.length) html += `</li></${stack.pop()!.tag}>`;
57
+ return html;
58
+ }
59
+
60
+ /** Converts Markdown to the editor's HTML: headings (three levels), lists, quotes, fenced code, rules, and inline marks. */
61
+ export function markdownToHtml(markdown: string) {
62
+ const lines = markdown.replace(/\r\n?/g, "\n").split("\n");
63
+ const out: string[] = [];
64
+ const paragraph: string[] = [];
65
+ const flush = () => { if (paragraph.length) { out.push(`<p>${inlineMarkdown(paragraph.join(" "))}</p>`); paragraph.length = 0; } };
66
+ const isItem = (line: string) => /^\s*([-*+]|\d+[.)])\s+/.test(line);
67
+ for (let index = 0; index < lines.length;) {
68
+ const line = lines[index]!;
69
+ if (/^\s*```/.test(line)) {
70
+ flush();
71
+ const code: string[] = [];
72
+ index++;
73
+ while (index < lines.length && !/^\s*```/.test(lines[index]!)) code.push(lines[index++]!);
74
+ index++;
75
+ out.push(`<pre>${escapeHtml(code.join("\n")) || "<br>"}</pre>`);
76
+ continue;
77
+ }
78
+ if (!line.trim()) { flush(); index++; continue; }
79
+ const heading = /^(#{1,6})\s+(.*)$/.exec(line);
80
+ if (heading) { flush(); const level = Math.min(3, heading[1]!.length); out.push(`<h${level}>${inlineMarkdown(heading[2]!)}</h${level}>`); index++; continue; }
81
+ if (/^ {0,3}([-*_])( *\1){2,} *$/.test(line)) { flush(); out.push("<hr>"); index++; continue; }
82
+ if (/^\s*>/.test(line)) {
83
+ flush();
84
+ const quote: string[] = [];
85
+ while (index < lines.length && /^\s*>/.test(lines[index]!)) quote.push(lines[index++]!.replace(/^\s*>\s?/, ""));
86
+ out.push(`<blockquote>${quote.map(inlineMarkdown).join("<br>")}</blockquote>`);
87
+ continue;
88
+ }
89
+ if (isItem(line)) {
90
+ flush();
91
+ const items: string[] = [];
92
+ while (index < lines.length && isItem(lines[index]!)) items.push(lines[index++]!);
93
+ out.push(listMarkdown(items));
94
+ continue;
95
+ }
96
+ paragraph.push(line.trim());
97
+ index++;
98
+ }
99
+ flush();
100
+ return out.join("") || "<p><br></p>";
101
+ }
102
+
103
+ function wrapMark(inner: string, mark: string) {
104
+ const match = /^(\s*)([\s\S]*?)(\s*)$/.exec(inner)!;
105
+ return match[2] ? `${match[1]}${mark}${match[2]}${mark}${match[3]}` : inner;
106
+ }
107
+
108
+ function inlineToMarkdown(node: Node): string {
109
+ let out = "";
110
+ node.childNodes.forEach(child => {
111
+ if (child.nodeType === Node.TEXT_NODE) { out += (child as Text).data.replace(ZWSP, ""); return; }
112
+ if (child.nodeType !== Node.ELEMENT_NODE) return;
113
+ const element = child as HTMLElement;
114
+ switch (element.nodeName) {
115
+ case "BR": out += "\n"; break;
116
+ case "STRONG": case "B": out += wrapMark(inlineToMarkdown(element), "**"); break;
117
+ case "EM": case "I": out += wrapMark(inlineToMarkdown(element), "_"); break;
118
+ case "S": case "STRIKE": case "DEL": out += wrapMark(inlineToMarkdown(element), "~~"); break;
119
+ case "CODE": out += `\`${(element.textContent ?? "").replace(ZWSP, "")}\``; break;
120
+ case "A": out += `[${inlineToMarkdown(element)}](${element.getAttribute("href") ?? ""})`; break;
121
+ case "UL": case "OL": break;
122
+ default: out += inlineToMarkdown(element);
123
+ }
124
+ });
125
+ return out;
126
+ }
127
+
128
+ function listToMarkdown(list: Element, indent: string): string {
129
+ let number = 1;
130
+ return Array.from(list.children).filter(item => item.nodeName === "LI").map(item => {
131
+ const marker = list.nodeName === "OL" ? `${number++}. ` : "- ";
132
+ const nested = Array.from(item.children).filter(child => child.nodeName === "UL" || child.nodeName === "OL").map(child => listToMarkdown(child, indent + " ".repeat(marker.length)));
133
+ return [`${indent}${marker}${inlineToMarkdown(item).trim()}`, ...nested].join("\n");
134
+ }).join("\n");
135
+ }
136
+
137
+ function blockToMarkdown(node: Node): string {
138
+ if (node.nodeType === Node.TEXT_NODE) return (node as Text).data.replace(ZWSP, "").trim();
139
+ if (node.nodeType !== Node.ELEMENT_NODE) return "";
140
+ const element = node as HTMLElement;
141
+ switch (element.nodeName) {
142
+ case "H1": return `# ${inlineToMarkdown(element).trim()}`;
143
+ case "H2": return `## ${inlineToMarkdown(element).trim()}`;
144
+ case "H3": return `### ${inlineToMarkdown(element).trim()}`;
145
+ case "HR": return "---";
146
+ case "PRE": return `\`\`\`\n${(element.textContent ?? "").replace(ZWSP, "").replace(/\n$/, "")}\n\`\`\``;
147
+ case "UL": case "OL": return listToMarkdown(element, "");
148
+ case "BLOCKQUOTE": {
149
+ const blocks = Array.from(element.children).some(child => BLOCK_TAGS.has(child.nodeName));
150
+ const body = blocks ? Array.from(element.childNodes).map(blockToMarkdown).filter(Boolean).join("\n\n") : inlineToMarkdown(element).trim();
151
+ return body.split("\n").map(line => line ? `> ${line}` : ">").join("\n");
152
+ }
153
+ default: return inlineToMarkdown(element).trim();
154
+ }
155
+ }
156
+
157
+ /** Converts editor HTML (or an element) to Markdown. */
158
+ export function htmlToMarkdown(input: string | HTMLElement) {
159
+ const root = typeof input === "string" ? parseHtml(input) : input;
160
+ return Array.from(root.childNodes).map(blockToMarkdown).filter(Boolean).join("\n\n");
161
+ }
162
+
163
+ function parseHtml(html: string) {
164
+ const template = document.createElement("template");
165
+ template.innerHTML = html;
166
+ const root = document.createElement("div");
167
+ root.appendChild(template.content);
168
+ return root;
169
+ }
170
+
171
+ const insertAfter = (reference: Node, ...nodes: Node[]) => { let at = reference; nodes.forEach(node => { at.parentNode!.insertBefore(node, at.nextSibling); at = node; }); };
172
+ const insertBefore = (reference: Node, ...nodes: Node[]) => nodes.forEach(node => reference.parentNode!.insertBefore(node, reference));
173
+ const replaceNode = (old: Node, ...nodes: Node[]) => { insertBefore(old, ...nodes); old.parentNode!.removeChild(old); };
174
+ const appendNodes = (parent: Node, ...nodes: Node[]) => nodes.forEach(node => parent.appendChild(node));
175
+
176
+ const INLINE_MAP: Record<string, string> = { B: "strong", STRONG: "strong", I: "em", EM: "em", S: "s", STRIKE: "s", DEL: "s", CODE: "code", A: "a", BR: "br" };
177
+ const BLOCK_MAP: Record<string, string> = { P: "p", H1: "h1", H2: "h2", H3: "h3", H4: "h3", H5: "h3", H6: "h3", UL: "ul", OL: "ol", LI: "li", BLOCKQUOTE: "blockquote", PRE: "pre", HR: "hr" };
178
+ const DROP = new Set(["SCRIPT", "STYLE", "META", "LINK", "TITLE", "TEMPLATE", "IFRAME", "OBJECT", "EMBED", "SVG", "CANVAS", "IMG", "VIDEO", "AUDIO", "INPUT", "BUTTON", "SELECT", "TEXTAREA", "HEAD"]);
179
+ const BLOCK_TAGS = new Set(["P", "H1", "H2", "H3", "LI", "PRE", "BLOCKQUOTE", "DIV", "UL", "OL", "HR"]);
180
+
181
+ /** Keeps only the tags the editor understands and drops every attribute except safe link targets. Bold and italic spans from other editors become marks. */
182
+ export function sanitizeHtml(html: string) {
183
+ const source = new DOMParser().parseFromString(html, "text/html").body;
184
+ const out = document.createElement("div");
185
+ const convert = (from: Node, into: Node) => {
186
+ from.childNodes.forEach(child => {
187
+ if (child.nodeType === Node.TEXT_NODE) { into.appendChild(document.createTextNode((child as Text).data)); return; }
188
+ if (child.nodeType !== Node.ELEMENT_NODE) return;
189
+ const element = child as HTMLElement;
190
+ if (DROP.has(element.nodeName)) return;
191
+ const style = element.getAttribute("style") ?? "";
192
+ let tag = INLINE_MAP[element.nodeName] ?? BLOCK_MAP[element.nodeName];
193
+ if (element.nodeName === "B" && /font-weight:\s*(normal|[1-4]00)/.test(style)) tag = "";
194
+ if (element.nodeName === "SPAN") tag = /font-weight:\s*(bold|[6-9]00)/.test(style) ? "strong" : /font-style:\s*italic/.test(style) ? "em" : /line-through/.test(style) ? "s" : "";
195
+ if (!tag) { convert(element, into); return; }
196
+ const next = document.createElement(tag);
197
+ if (tag === "a") {
198
+ const href = element.getAttribute("href") ?? "";
199
+ if (!safeUrl(href)) { convert(element, into); return; }
200
+ next.setAttribute("href", href);
201
+ }
202
+ if (tag === "pre") next.textContent = element.textContent ?? "";
203
+ else if (tag !== "br" && tag !== "hr") convert(element, next);
204
+ into.appendChild(next);
205
+ });
206
+ };
207
+ convert(source, out);
208
+ // Inline runs at the top level become paragraphs; whitespace between blocks disappears.
209
+ const fixed = document.createElement("div");
210
+ let run: HTMLElement | null = null;
211
+ Array.from(out.childNodes).forEach(node => {
212
+ const block = node.nodeType === Node.ELEMENT_NODE && BLOCK_TAGS.has(node.nodeName);
213
+ if (block) { run = null; fixed.appendChild(node); return; }
214
+ if (node.nodeType === Node.TEXT_NODE && !(node as Text).data.trim() && !run) return;
215
+ if (!run) { run = document.createElement("p"); fixed.appendChild(run); }
216
+ run.appendChild(node);
217
+ });
218
+ fixed.querySelectorAll("li > p").forEach(paragraph => replaceNode(paragraph, ...Array.from(paragraph.childNodes)));
219
+ // The editor keeps typed whitespace, so formatting whitespace between blocks and list items has to go.
220
+ [fixed, ...Array.from(fixed.querySelectorAll("ul, ol, blockquote, li"))].forEach(parent => Array.from(parent.childNodes).forEach(node => {
221
+ if (isText(node) && !node.data.trim() && (parent === fixed || parent.nodeName === "UL" || parent.nodeName === "OL" || (node.previousSibling && BLOCK_TAGS.has(node.previousSibling.nodeName)) || (node.nextSibling && BLOCK_TAGS.has(node.nextSibling.nodeName)))) node.remove();
222
+ }));
223
+ return fixed.innerHTML;
224
+ }
225
+
226
+ /* ------------------------------------------------------------------------------------------------
227
+ * DOM helpers. Positions are stored as character offsets where every block start and line break
228
+ * counts as one character, so the caret survives any rewrite that keeps the text.
229
+ * ---------------------------------------------------------------------------------------------- */
230
+
231
+ const COUNTED = new Set(["P", "H1", "H2", "H3", "LI", "PRE", "BLOCKQUOTE", "DIV"]);
232
+ const isElement = (node: Node | null): node is HTMLElement => !!node && node.nodeType === Node.ELEMENT_NODE;
233
+ const isText = (node: Node | null): node is Text => !!node && node.nodeType === Node.TEXT_NODE;
234
+
235
+ function pointToOffset(root: Node, container: Node, offset: number) {
236
+ let count = 0, found = -1;
237
+ const visit = (node: Node) => {
238
+ if (found >= 0) return;
239
+ if (isText(node)) { if (node === container) found = count + offset; else count += node.data.length; return; }
240
+ if (!isElement(node)) return;
241
+ if (node !== root && COUNTED.has(node.nodeName)) count += 1;
242
+ if (node.nodeName === "BR" || node.nodeName === "HR") count += 1;
243
+ const children = node.childNodes;
244
+ for (let index = 0; index < children.length; index++) {
245
+ if (node === container && index === offset) { found = count; return; }
246
+ visit(children[index]!);
247
+ if (found >= 0) return;
248
+ }
249
+ if (node === container) found = count;
250
+ };
251
+ visit(root);
252
+ return found < 0 ? count : found;
253
+ }
254
+
255
+ function offsetToPoint(root: Node, target: number): [Node, number] {
256
+ let count = 0;
257
+ let result: [Node, number] | null = null;
258
+ const visit = (node: Node) => {
259
+ if (result) return;
260
+ if (isText(node)) {
261
+ if (target <= count + node.data.length) { result = [node, Math.max(0, target - count)]; return; }
262
+ count += node.data.length;
263
+ return;
264
+ }
265
+ if (!isElement(node)) return;
266
+ if (node !== root && COUNTED.has(node.nodeName)) count += 1;
267
+ if (node.nodeName === "BR" || node.nodeName === "HR") {
268
+ if (count === target && node.parentNode) { result = [node.parentNode, Array.prototype.indexOf.call(node.parentNode.childNodes, node)]; return; }
269
+ count += 1;
270
+ return;
271
+ }
272
+ node.childNodes.forEach(visit);
273
+ if (!result && count === target && node !== root && COUNTED.has(node.nodeName)) result = [node, node.childNodes.length];
274
+ };
275
+ visit(root);
276
+ if (result) return result;
277
+ const last = root.lastChild;
278
+ return last && isElement(last) && last.nodeName !== "HR" ? [last, last.childNodes.length] : [root, root.childNodes.length];
279
+ }
280
+
281
+ type Offsets = { start: number; end: number };
282
+
283
+ function readOffsets(root: HTMLElement): Offsets | null {
284
+ const selection = window.getSelection();
285
+ if (!selection?.rangeCount) return null;
286
+ const range = selection.getRangeAt(0);
287
+ if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return null;
288
+ return { start: pointToOffset(root, range.startContainer, range.startOffset), end: pointToOffset(root, range.endContainer, range.endOffset) };
289
+ }
290
+
291
+ function writeOffsets(root: HTMLElement, offsets: Offsets | null) {
292
+ const selection = window.getSelection();
293
+ if (!selection) return;
294
+ const range = document.createRange();
295
+ if (!offsets) { range.selectNodeContents(root.lastElementChild ?? root); range.collapse(false); }
296
+ else {
297
+ const [startNode, startOffset] = offsetToPoint(root, offsets.start);
298
+ const [endNode, endOffset] = offsets.end === offsets.start ? [startNode, startOffset] : offsetToPoint(root, offsets.end);
299
+ range.setStart(startNode, startOffset);
300
+ range.setEnd(endNode, endOffset);
301
+ }
302
+ selection.removeAllRanges();
303
+ selection.addRange(range);
304
+ }
305
+
306
+ /** The visible band of the nearest scrolling or clipping ancestor, so floating layers flip before they get cut off. */
307
+ function clipBand(element: HTMLElement) {
308
+ let top = 0, bottom = window.innerHeight;
309
+ for (let node = element.parentElement; node; node = node.parentElement) {
310
+ const style = getComputedStyle(node);
311
+ if (style.overflowY !== "visible" || style.overflowX !== "visible") {
312
+ const rect = node.getBoundingClientRect();
313
+ top = Math.max(top, rect.top);
314
+ bottom = Math.min(bottom, rect.bottom);
315
+ }
316
+ }
317
+ return { top, bottom };
318
+ }
319
+
320
+ function placeCaret(node: Node, offset: number) {
321
+ const selection = window.getSelection();
322
+ if (!selection) return;
323
+ const range = document.createRange();
324
+ range.setStart(node, offset);
325
+ range.collapse(true);
326
+ selection.removeAllRanges();
327
+ selection.addRange(range);
328
+ }
329
+
330
+ function placeAtStart(element: HTMLElement) {
331
+ const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
332
+ const first = walker.nextNode();
333
+ if (first) placeCaret(first, 0); else placeCaret(element, 0);
334
+ }
335
+
336
+ /** The nearest block around a node: a paragraph, heading, list item, quote, or code block. */
337
+ function blockAt(root: HTMLElement, node: Node | null): HTMLElement | null {
338
+ let current: Node | null = node;
339
+ while (current && current !== root) {
340
+ if (isElement(current) && COUNTED.has(current.nodeName)) return current;
341
+ current = current.parentNode;
342
+ }
343
+ return null;
344
+ }
345
+
346
+ const visibleText = (element: Node) => (element.textContent ?? "").replace(ZWSP, "");
347
+
348
+ function ensureFilled(element: HTMLElement) {
349
+ if (element.nodeName === "HR") return;
350
+ if (!visibleText(element) && !element.querySelector("br, hr, ul, ol")) element.appendChild(document.createElement("br"));
351
+ }
352
+
353
+ function textBefore(block: HTMLElement, range: Range) {
354
+ const before = document.createRange();
355
+ before.setStart(block, 0);
356
+ before.setEnd(range.startContainer, range.startOffset);
357
+ return before.toString().replace(ZWSP, "").replace(/\u00a0/g, " ");
358
+ }
359
+
360
+ function atBlockStart(block: HTMLElement, range: Range) {
361
+ if (!range.collapsed) return false;
362
+ const before = document.createRange();
363
+ before.setStart(block, 0);
364
+ before.setEnd(range.startContainer, range.startOffset);
365
+ return !before.toString().replace(ZWSP, "") && !before.cloneContents().querySelector("br, hr");
366
+ }
367
+
368
+ function moveChildren(from: Node, to: Node) {
369
+ while (from.firstChild) to.appendChild(from.firstChild);
370
+ }
371
+
372
+ /** Plain text of a block, with line breaks for `<br>`, for moving content into a code block. */
373
+ function plainText(element: HTMLElement) {
374
+ let out = "";
375
+ const walk = (node: Node) => node.childNodes.forEach(child => {
376
+ if (isText(child)) out += child.data.replace(ZWSP, "");
377
+ else if (child.nodeName === "BR") out += "\n";
378
+ else walk(child);
379
+ });
380
+ walk(element);
381
+ return out.replace(/\n$/, "");
382
+ }
383
+
384
+ function linesToNodes(text: string) {
385
+ const nodes: Node[] = [];
386
+ text.replace(/\n$/, "").split("\n").forEach((line, index) => {
387
+ if (index) nodes.push(document.createElement("br"));
388
+ if (line) nodes.push(document.createTextNode(line));
389
+ });
390
+ return nodes;
391
+ }
392
+
393
+ function mergeAdjacentLists(list: HTMLElement) {
394
+ const next = list.nextElementSibling;
395
+ if (next && next.nodeName === list.nodeName) { moveChildren(next, list); next.remove(); }
396
+ const previous = list.previousElementSibling;
397
+ if (previous && previous.nodeName === list.nodeName) { moveChildren(list, previous); list.remove(); return previous as HTMLElement; }
398
+ return list;
399
+ }
400
+
401
+ /** Moves a list item one level out. A top level item leaves the list and becomes a paragraph between its halves. */
402
+ function outdent(item: HTMLElement): HTMLElement {
403
+ const list = item.parentElement!;
404
+ const parentItem = list.parentElement;
405
+ const following: Node[] = [];
406
+ for (let node = item.nextSibling; node; node = node.nextSibling) following.push(node);
407
+ if (parentItem?.nodeName === "LI") {
408
+ if (following.length) {
409
+ const sub = document.createElement(list.nodeName.toLowerCase());
410
+ following.forEach(node => sub.appendChild(node));
411
+ item.appendChild(sub);
412
+ }
413
+ insertAfter(parentItem, item);
414
+ if (!list.children.length) list.remove();
415
+ return item;
416
+ }
417
+ const paragraph = document.createElement("p");
418
+ const nested: Node[] = [];
419
+ Array.from(item.childNodes).forEach(child => { if (child.nodeName === "UL" || child.nodeName === "OL") nested.push(child); else paragraph.appendChild(child); });
420
+ ensureFilled(paragraph);
421
+ const tail = following.length || nested.length ? document.createElement(list.nodeName.toLowerCase()) : null;
422
+ if (tail) {
423
+ nested.forEach(sub => sub.childNodes.length && Array.from(sub.childNodes).forEach(node => tail.appendChild(node)));
424
+ following.forEach(node => tail.appendChild(node));
425
+ }
426
+ insertAfter(list, paragraph);
427
+ if (tail) insertAfter(paragraph, tail);
428
+ item.remove();
429
+ if (!list.children.length) list.remove();
430
+ return paragraph;
431
+ }
432
+
433
+ function indent(item: HTMLElement) {
434
+ const previous = item.previousElementSibling as HTMLElement | null;
435
+ if (!previous || previous.nodeName !== "LI") return false;
436
+ const listTag = item.parentElement!.nodeName;
437
+ let sub = previous.lastElementChild as HTMLElement | null;
438
+ if (!sub || (sub.nodeName !== "UL" && sub.nodeName !== "OL")) { sub = document.createElement(listTag.toLowerCase()); previous.appendChild(sub); }
439
+ sub.appendChild(item);
440
+ return true;
441
+ }
442
+
443
+ /** Turns one block into another type, keeping its content. Choosing the type a block already has turns it back into a paragraph. */
444
+ function convertBlock(block: HTMLElement, type: Exclude<BlockType, "hr">): HTMLElement {
445
+ if (block.nodeName === "LI") {
446
+ const list = block.parentElement!;
447
+ if (type === "ul" || type === "ol") {
448
+ if (list.nodeName.toLowerCase() === type) return outdent(block);
449
+ const swapped = document.createElement(type);
450
+ moveChildren(list, swapped);
451
+ replaceNode(list, swapped);
452
+ return block;
453
+ }
454
+ const paragraph = outdent(block);
455
+ return paragraph.nodeName === "P" && type !== "p" ? convertBlock(paragraph, type) : paragraph;
456
+ }
457
+ if (type === "ul" || type === "ol") {
458
+ const item = document.createElement("li");
459
+ if (block.nodeName === "PRE") appendNodes(item, ...linesToNodes(block.textContent ?? "")); else moveChildren(block, item);
460
+ ensureFilled(item);
461
+ const list = document.createElement(type);
462
+ list.appendChild(item);
463
+ replaceNode(block, list);
464
+ mergeAdjacentLists(list);
465
+ return item;
466
+ }
467
+ const tag = block.nodeName.toLowerCase() === type ? "p" : type;
468
+ const next = document.createElement(tag);
469
+ if (tag === "pre") next.textContent = plainText(block);
470
+ else if (block.nodeName === "PRE") appendNodes(next, ...linesToNodes(block.textContent ?? ""));
471
+ else moveChildren(block, next);
472
+ ensureFilled(next);
473
+ replaceNode(block, next);
474
+ return next;
475
+ }
476
+
477
+ /** Makes sure a paragraph follows a block the caret cannot type after, such as a rule or a code block at the end. */
478
+ function paragraphAfter(block: Element) {
479
+ const next = block.nextElementSibling;
480
+ if (next && next.nodeName === "P") return next as HTMLElement;
481
+ const paragraph = document.createElement("p");
482
+ paragraph.appendChild(document.createElement("br"));
483
+ insertAfter(block, paragraph);
484
+ return paragraph;
485
+ }
486
+
487
+ /** Repairs what browsers leave behind: styled spans from merges, stray inline content at the top level, and an empty root. */
488
+ function tidy(root: HTMLElement) {
489
+ const loose = Array.from(root.childNodes).some(node => (isText(node) && node.data.trim()) || (isElement(node) && !BLOCK_TAGS.has(node.nodeName)));
490
+ const dirty = loose || !root.firstChild || root.querySelector("span, font, [style], div, b, i, strike");
491
+ if (!dirty) return;
492
+ const offsets = readOffsets(root);
493
+ root.querySelectorAll("[style]").forEach(element => {
494
+ const style = element.getAttribute("style") ?? "";
495
+ if (element.nodeName === "SPAN" && /font-weight:\s*(bold|[6-9]00)/.test(style)) replaceNode(element, Object.assign(document.createElement("strong"), { innerHTML: element.innerHTML }));
496
+ else element.removeAttribute("style");
497
+ });
498
+ const rename = (selector: string, tag: string) => root.querySelectorAll(selector).forEach(element => {
499
+ const next = document.createElement(tag);
500
+ moveChildren(element, next);
501
+ replaceNode(element, next);
502
+ });
503
+ rename("b", "strong");
504
+ rename("i", "em");
505
+ rename("strike", "s");
506
+ rename(":scope > div", "p");
507
+ root.querySelectorAll("span, font, div").forEach(element => replaceNode(element, ...Array.from(element.childNodes)));
508
+ let run: HTMLElement | null = null;
509
+ Array.from(root.childNodes).forEach(node => {
510
+ if (isElement(node) && BLOCK_TAGS.has(node.nodeName)) { run = null; return; }
511
+ if (isText(node) && !node.data.trim() && !run) { node.remove(); return; }
512
+ if (!run) { run = document.createElement("p"); insertBefore(node, run); }
513
+ run.appendChild(node);
514
+ });
515
+ if (!root.firstChild) root.innerHTML = "<p><br></p>";
516
+ if (root.contains(document.activeElement) || document.activeElement === root) writeOffsets(root, offsets);
517
+ }
518
+
519
+ /** A clean copy for output: canonical tags, no attributes but links, no invisible helpers, no trailing empty lines. */
520
+ function cleanClone(root: HTMLElement) {
521
+ const clone = root.cloneNode(true) as HTMLElement;
522
+ clone.querySelectorAll("*").forEach(element => {
523
+ Array.from(element.attributes).forEach(attribute => { if (!(element.nodeName === "A" && attribute.name === "href")) element.removeAttribute(attribute.name); });
524
+ });
525
+ const walker = document.createTreeWalker(clone, NodeFilter.SHOW_TEXT);
526
+ const empty: Text[] = [];
527
+ for (let node = walker.nextNode() as Text | null; node; node = walker.nextNode() as Text | null) {
528
+ node.data = node.data.replace(ZWSP, "");
529
+ if (!node.data) empty.push(node);
530
+ }
531
+ empty.forEach(node => node.remove());
532
+ clone.querySelectorAll("p, h1, h2, h3, li, blockquote").forEach(block => {
533
+ const last = block.lastChild;
534
+ if (last?.nodeName === "BR" && visibleText(block)) last.remove();
535
+ });
536
+ clone.querySelectorAll("pre").forEach(pre => { pre.textContent = (pre.textContent ?? "").replace(/\n$/, ""); });
537
+ while (clone.lastElementChild && clone.lastElementChild.nodeName !== "HR" && !visibleText(clone.lastElementChild) && clone.children.length > 1) clone.lastElementChild.remove();
538
+ return clone;
539
+ }
540
+
541
+ function plainOutput(clone: HTMLElement) {
542
+ return Array.from(clone.querySelectorAll("p, h1, h2, h3, li, blockquote, pre")).filter(block => !block.querySelector("p, li")).map(block => (block.textContent ?? "").trim()).filter(Boolean).join("\n");
543
+ }
544
+
545
+ /* ------------------------------------------------------------------------------------------------
546
+ * Component
547
+ * ---------------------------------------------------------------------------------------------- */
548
+
549
+ export interface RichTextValue {
550
+ html: string;
551
+ markdown: string;
552
+ text: string;
553
+ empty: boolean;
554
+ }
555
+
556
+ export interface RichTextEditorHandle {
557
+ focus: () => void;
558
+ getHTML: () => string;
559
+ getMarkdown: () => string;
560
+ getText: () => string;
561
+ setHTML: (html: string) => void;
562
+ setMarkdown: (markdown: string) => void;
563
+ clear: () => void;
564
+ undo: () => void;
565
+ redo: () => void;
566
+ element: HTMLDivElement | null;
567
+ }
568
+
569
+ export interface RichTextEditorProps {
570
+ /** Controlled HTML. The editor only rewrites its content when this differs from what it last emitted. */
571
+ value?: string;
572
+ defaultValue?: string;
573
+ /** Initial content as Markdown, used when no HTML value is given. */
574
+ defaultMarkdown?: string;
575
+ onChange?: (value: RichTextValue) => void;
576
+ /** Reports whether undo and redo are available, for your own toolbar. */
577
+ onHistoryChange?: (state: { canUndo: boolean; canRedo: boolean }) => void;
578
+ placeholder?: string;
579
+ /** The hint on an empty line while typing. */
580
+ blockHint?: string;
581
+ readOnly?: boolean;
582
+ autoFocus?: boolean;
583
+ "aria-label"?: string;
584
+ className?: string;
585
+ style?: CSSProperties;
586
+ }
587
+
588
+ type Formats = { bold: boolean; italic: boolean; strike: boolean; code: boolean; link: string | null; block: string };
589
+ type SlashItem = { type: BlockType; label: string; hint: string; keywords: string; icon: ReactNode };
590
+ type Slash = { node: Text; offset: number; query: string; x: number; y: number; above: boolean; max: number };
591
+ type Snapshot = { html: string; selection: Offsets | null; kind: "type" | "command"; at: number };
592
+
593
+ const icon = { size: 16, strokeWidth: 1.75, "aria-hidden": true } as const;
594
+ const SLASH_ITEMS: SlashItem[] = [
595
+ { type: "p", label: "Text", hint: "", keywords: "paragraph plain body", icon: <Pilcrow {...icon} /> },
596
+ { type: "h1", label: "Heading 1", hint: "#", keywords: "title h1 large", icon: <Heading1 {...icon} /> },
597
+ { type: "h2", label: "Heading 2", hint: "##", keywords: "subtitle h2 section", icon: <Heading2 {...icon} /> },
598
+ { type: "h3", label: "Heading 3", hint: "###", keywords: "h3 small", icon: <Heading3 {...icon} /> },
599
+ { type: "ul", label: "Bulleted list", hint: "-", keywords: "bullet unordered ul points", icon: <List {...icon} /> },
600
+ { type: "ol", label: "Numbered list", hint: "1.", keywords: "ordered ol steps numbers", icon: <ListOrdered {...icon} /> },
601
+ { type: "blockquote", label: "Quote", hint: ">", keywords: "blockquote citation callout", icon: <Quote {...icon} /> },
602
+ { type: "pre", label: "Code block", hint: "```", keywords: "pre snippet monospace", icon: <SquareCode {...icon} /> },
603
+ { type: "hr", label: "Divider", hint: "---", keywords: "rule hr line separator", icon: <Minus {...icon} /> },
604
+ ];
605
+
606
+ const { spring, duration, ease, blur } = motionTokens;
607
+ const enter = [...ease.enter] as [number, number, number, number];
608
+ const standard = [...ease.standard] as [number, number, number, number];
609
+ const physical = (visualDuration: number, bounce: number): Transition => {
610
+ const root = 2 * Math.PI / (visualDuration * 1.2);
611
+ return { type: "spring", stiffness: root * root, damping: 2 * (1 - bounce) * root, mass: 1 };
612
+ };
613
+ const MORPH = physical(spring.morph.visualDuration, .12), GLIDE = physical(.3, 0), GROW = physical(spring.smooth.visualDuration, 0);
614
+ const TRAVEL = 18;
615
+
616
+ const faceVariants: Variants = {
617
+ hidden: (direction: number) => ({ opacity: 0, x: direction * TRAVEL, filter: `blur(${blur.subtle}px)` }),
618
+ shown: { opacity: 1, x: 0, filter: "blur(0px)", transition: { x: MORPH, opacity: { duration: .2, ease: enter, delay: .03 }, filter: { duration: .2, ease: enter, delay: .03 } } },
619
+ gone: (direction: number) => ({ opacity: 0, x: direction * -TRAVEL, filter: `blur(${blur.subtle}px)`, transition: { x: MORPH, opacity: { duration: .12, ease: standard }, filter: { duration: .12, ease: standard } } }),
620
+ };
621
+ const fadeVariants: Variants = { hidden: { opacity: 0 }, shown: { opacity: 1, transition: { duration: .14 } }, gone: { opacity: 0, transition: { duration: .1 } } };
622
+
623
+ /** A toolbar face reports its natural size while current, so the shared surface can spring to it. */
624
+ function Face({ direction, reduced, onSize, children }: { direction: number; reduced: boolean; onSize: (width: number, height: number) => void; children: ReactNode }) {
625
+ const ref = useRef<HTMLDivElement>(null);
626
+ const present = useIsPresent();
627
+ useLayoutEffect(() => {
628
+ const node = ref.current;
629
+ if (!node || !present) return;
630
+ const report = () => onSize(node.offsetWidth, node.offsetHeight);
631
+ report();
632
+ if (typeof ResizeObserver === "undefined") return;
633
+ const observer = new ResizeObserver(report);
634
+ observer.observe(node);
635
+ return () => observer.disconnect();
636
+ }, [onSize, present]);
637
+ return <motion.div ref={ref} className={styles.face} custom={direction} variants={reduced ? fadeVariants : faceVariants} initial="hidden" animate="shown" exit="gone" inert={!present || undefined}>{children}</motion.div>;
638
+ }
639
+
640
+ function ToolButton({ label, pressed, shortcut, onPress, children }: { label: string; pressed: boolean; shortcut?: string; onPress: () => void; children: ReactNode }) {
641
+ return <button type="button" className={styles.tool} aria-label={label} aria-pressed={pressed} title={shortcut ? `${label} (${shortcut})` : label} onMouseDown={event => event.preventDefault()} onClick={onPress}>{children}</button>;
642
+ }
643
+
644
+ /**
645
+ * A light rich text editor on contentEditable. Markdown shortcuts as you type, a floating selection toolbar that morphs into a link field,
646
+ * a slash menu for blocks, its own undo history, and HTML plus Markdown output. No dependencies beyond Motion and icons.
647
+ */
648
+ export const RichTextEditor = forwardRef<RichTextEditorHandle, RichTextEditorProps>(function RichTextEditor({
649
+ value, defaultValue, defaultMarkdown, onChange, onHistoryChange, placeholder = "Start writing", blockHint = "Type / for blocks",
650
+ readOnly = false, autoFocus = false, "aria-label": ariaLabel = "Editor", className, style,
651
+ }, ref) {
652
+ const reduced = !!useReducedMotion();
653
+ const uid = useId();
654
+ const rootRef = useRef<HTMLDivElement>(null);
655
+ const editorRef = useRef<HTMLDivElement>(null);
656
+ const busy = useRef(false);
657
+ const dragging = useRef(false);
658
+ const lastInput = useRef("");
659
+ const lastEmitted = useRef<string | null>(null);
660
+ const history = useRef<{ stack: Snapshot[]; index: number }>({ stack: [], index: -1 });
661
+ const callbacks = useRef({ onChange, onHistoryChange });
662
+ useEffect(() => { callbacks.current = { onChange, onHistoryChange }; }, [onChange, onHistoryChange]);
663
+
664
+ const [empty, setEmpty] = useState(true);
665
+ const [formats, setFormats] = useState<Formats>({ bold: false, italic: false, strike: false, code: false, link: null, block: "p" });
666
+ const [bar, setBar] = useState<{ x: number; y: number; below: boolean } | null>(null);
667
+ const [face, setFace] = useState<"format" | "link">("format");
668
+ const [linkDraft, setLinkDraft] = useState("");
669
+ const [slash, setSlash] = useState<Slash | null>(null);
670
+ const [slashActive, setSlashActive] = useState(0);
671
+ const [announce, setAnnounce] = useState("");
672
+ const faceRef = useRef(face);
673
+ const slashRef = useRef(slash);
674
+ const savedRange = useRef<Range | null>(null);
675
+ const linkInputRef = useRef<HTMLInputElement>(null);
676
+ const hinted = useRef<HTMLElement | null>(null);
677
+ useEffect(() => { faceRef.current = face; }, [face]);
678
+ useEffect(() => { slashRef.current = slash; }, [slash]);
679
+
680
+ /* Output and history. */
681
+ const emit = useCallback(() => {
682
+ const editor = editorRef.current;
683
+ if (!editor) return;
684
+ const clone = cleanClone(editor);
685
+ const html = clone.innerHTML;
686
+ const text = plainOutput(clone);
687
+ const isEmpty = !text && !clone.querySelector("hr");
688
+ lastEmitted.current = html;
689
+ setEmpty(isEmpty);
690
+ callbacks.current.onChange?.({ html, markdown: htmlToMarkdown(clone), text, empty: isEmpty });
691
+ }, []);
692
+
693
+ const reportHistory = useCallback(() => {
694
+ const { stack, index } = history.current;
695
+ callbacks.current.onHistoryChange?.({ canUndo: index > 0, canRedo: index < stack.length - 1 });
696
+ }, []);
697
+
698
+ const record = useCallback((kind: Snapshot["kind"], boundary = false) => {
699
+ const editor = editorRef.current;
700
+ if (!editor) return;
701
+ const snap: Snapshot = { html: editor.innerHTML.replace(/ data-hint=""/g, ""), selection: readOffsets(editor), kind, at: Date.now() };
702
+ const state = history.current;
703
+ const top = state.stack[state.index];
704
+ if (top && top.html === snap.html) { top.selection = snap.selection; return; }
705
+ if (kind === "type" && top?.kind === "type" && state.index > 0 && snap.at - top.at < 1000 && !boundary) {
706
+ state.stack[state.index] = { ...snap, at: top.at };
707
+ } else {
708
+ state.stack = state.stack.slice(0, state.index + 1);
709
+ state.stack.push(snap);
710
+ if (state.stack.length > 300) state.stack.shift();
711
+ state.index = state.stack.length - 1;
712
+ }
713
+ reportHistory();
714
+ }, [reportHistory]);
715
+
716
+ const restore = useCallback((snap: Snapshot) => {
717
+ const editor = editorRef.current;
718
+ if (!editor) return;
719
+ editor.innerHTML = snap.html;
720
+ hinted.current = null;
721
+ editor.focus({ preventScroll: true });
722
+ writeOffsets(editor, snap.selection);
723
+ emit();
724
+ reportHistory();
725
+ }, [emit, reportHistory]);
726
+
727
+ const undo = useCallback(() => {
728
+ const state = history.current;
729
+ if (state.index <= 0) return;
730
+ state.index -= 1;
731
+ setSlash(null);
732
+ restore(state.stack[state.index]!);
733
+ setAnnounce("Undone");
734
+ }, [restore]);
735
+ const redo = useCallback(() => {
736
+ const state = history.current;
737
+ if (state.index >= state.stack.length - 1) return;
738
+ state.index += 1;
739
+ setSlash(null);
740
+ restore(state.stack[state.index]!);
741
+ setAnnounce("Redone");
742
+ }, [restore]);
743
+
744
+ /* Selection driven chrome: block hint, active formats, the slash query, and the toolbar. */
745
+ const sync = useCallback(() => {
746
+ const editor = editorRef.current, root = rootRef.current;
747
+ if (!editor || !root || busy.current) return;
748
+ const selection = window.getSelection();
749
+ const inside = !!selection?.rangeCount && editor.contains(selection.anchorNode);
750
+ const range = inside ? selection!.getRangeAt(0) : null;
751
+
752
+ const block = range ? blockAt(editor, range.startContainer) : null;
753
+ const hint = range?.collapsed && block && !visibleText(block) && block.nodeName !== "PRE" ? block : null;
754
+ if (hinted.current !== hint) {
755
+ hinted.current?.removeAttribute("data-hint");
756
+ hint?.setAttribute("data-hint", "");
757
+ hinted.current = hint;
758
+ }
759
+
760
+ if (!range) {
761
+ if (faceRef.current !== "link") setBar(null);
762
+ setSlash(null);
763
+ return;
764
+ }
765
+
766
+ const start = isElement(range.startContainer) ? range.startContainer : range.startContainer.parentElement;
767
+ const within = (selector: string) => { const found = start?.closest(selector); return !!found && editor.contains(found); };
768
+ const anchor = start?.closest("a");
769
+ const listItem = block?.nodeName === "LI" ? block.parentElement?.nodeName.toLowerCase() ?? "ul" : null;
770
+ const next: Formats = {
771
+ bold: within("strong, b"), italic: within("em, i"), strike: within("s, strike, del"),
772
+ code: within("code") && !within("pre"),
773
+ link: anchor && editor.contains(anchor) ? anchor.getAttribute("href") : null,
774
+ block: listItem ?? block?.nodeName.toLowerCase() ?? "p",
775
+ };
776
+ setFormats(current => (Object.keys(next) as (keyof Formats)[]).every(key => current[key] === next[key]) ? current : next);
777
+
778
+ const open = slashRef.current;
779
+ if (open) {
780
+ const valid = range.collapsed && range.startContainer === open.node && range.startOffset > open.offset && open.node.data[open.offset] === "/";
781
+ const query = valid ? open.node.data.slice(open.offset + 1, range.startOffset) : "";
782
+ if (!valid || query.length > 24 || /^\s/.test(query) || /\s\s/.test(query)) setSlash(null);
783
+ else if (query !== open.query) setSlash({ ...open, query });
784
+ }
785
+
786
+ const inCode = !!start?.closest("pre");
787
+ if (range.collapsed || dragging.current || inCode || slashRef.current || readOnly) {
788
+ if (faceRef.current !== "link") setBar(null);
789
+ return;
790
+ }
791
+ const rects = range.getClientRects();
792
+ const rect = rects.length ? rects[0]! : range.getBoundingClientRect();
793
+ const whole = range.getBoundingClientRect();
794
+ const box = root.getBoundingClientRect();
795
+ const below = rect.top - clipBand(root).top < 60;
796
+ setBar({ x: whole.left + whole.width / 2 - box.left, y: below ? whole.bottom - box.top + 8 : rect.top - box.top - 8, below });
797
+ }, [readOnly]);
798
+
799
+ const run = useCallback((change: () => void) => {
800
+ const editor = editorRef.current;
801
+ if (!editor) return;
802
+ busy.current = true;
803
+ try { change(); tidy(editor); } finally { busy.current = false; }
804
+ record("command");
805
+ emit();
806
+ sync();
807
+ }, [emit, record, sync]);
808
+
809
+ /* Content in and out. */
810
+ const load = useCallback((html: string) => {
811
+ const editor = editorRef.current;
812
+ if (!editor) return;
813
+ editor.innerHTML = sanitizeHtml(html) || "<p><br></p>";
814
+ editor.querySelectorAll("p, h1, h2, h3, li, blockquote, pre").forEach(block => ensureFilled(block as HTMLElement));
815
+ hinted.current = null;
816
+ }, []);
817
+
818
+ const initial = useRef({ value, defaultValue, defaultMarkdown, autoFocus });
819
+ useLayoutEffect(() => {
820
+ const editor = editorRef.current;
821
+ if (!editor) return;
822
+ const start = initial.current;
823
+ load(start.value ?? start.defaultValue ?? (start.defaultMarkdown !== undefined ? markdownToHtml(start.defaultMarkdown) : "<p><br></p>"));
824
+ history.current = { stack: [{ html: editor.innerHTML, selection: null, kind: "command", at: 0 }], index: 0 };
825
+ emit();
826
+ reportHistory();
827
+ if (start.autoFocus) { editor.focus(); writeOffsets(editor, null); }
828
+ }, [emit, load, reportHistory]);
829
+
830
+ useEffect(() => {
831
+ if (value === undefined || value === lastEmitted.current) return;
832
+ load(value);
833
+ record("command");
834
+ emit();
835
+ }, [emit, load, record, value]);
836
+
837
+ /* Markdown shortcuts, applied right after the character that completes them. */
838
+ const shortcut = useCallback((data: string | null) => {
839
+ const editor = editorRef.current;
840
+ const selection = window.getSelection();
841
+ if (!editor || !data || !selection?.rangeCount) return false;
842
+ const range = selection.getRangeAt(0);
843
+ if (!range.collapsed) return false;
844
+ const block = blockAt(editor, range.startContainer);
845
+ if (!block || block.nodeName === "PRE") return false;
846
+ const before = textBefore(block, range);
847
+ const plainBlock = block.nodeName === "P" || block.nodeName === "DIV";
848
+
849
+ if (data === " " && plainBlock && block.parentElement === editor) {
850
+ const rule: [RegExp, Exclude<BlockType, "hr">][] = [[/^# $/, "h1"], [/^## $/, "h2"], [/^### $/, "h3"], [/^[-*+] $/, "ul"], [/^\d+[.)] $/, "ol"], [/^> $/, "blockquote"]];
851
+ const match = rule.find(([pattern]) => pattern.test(before));
852
+ if (match) {
853
+ const prefix = document.createRange();
854
+ prefix.setStart(block, 0);
855
+ prefix.setEnd(range.startContainer, range.startOffset);
856
+ prefix.deleteContents();
857
+ const next = convertBlock(block, match[1]);
858
+ placeAtStart(next);
859
+ return true;
860
+ }
861
+ }
862
+ if (data === "-" && plainBlock && block.parentElement === editor && before === "---" && visibleText(block) === "---") {
863
+ const rule = document.createElement("hr");
864
+ replaceNode(block, rule);
865
+ placeAtStart(paragraphAfter(rule));
866
+ return true;
867
+ }
868
+ if (data === "`" && plainBlock && block.parentElement === editor && before === "```" && visibleText(block) === "```") {
869
+ block.textContent = "";
870
+ const next = convertBlock(block, "pre");
871
+ placeAtStart(next);
872
+ return true;
873
+ }
874
+ const node = range.startContainer;
875
+ if (!isText(node) || node.parentElement?.closest("code")) return false;
876
+ const text = node.data.slice(0, range.startOffset);
877
+ const inline: [RegExp, string][] = [
878
+ [/(\*\*|__)([^*_\s](?:[^*_]*[^*_\s])?)\1$/, "strong"],
879
+ [/~~([^~\s](?:[^~]*[^~\s])?)~~$/, "s"],
880
+ [/`([^`]+)`$/, "code"],
881
+ [/(?:^|[^*\w])(\*([^*\s](?:[^*]*[^*\s])?)\*)$/, "em"],
882
+ [/(?:^|[^_\w])(_([^_\s](?:[^_]*[^_\s])?)_)$/, "em"],
883
+ ];
884
+ for (const [pattern, tag] of inline) {
885
+ const match = pattern.exec(text);
886
+ if (!match) continue;
887
+ const whole = tag === "em" ? match[1]! : match[0];
888
+ const inner = tag === "em" ? match[2]! : tag === "code" ? match[1]! : tag === "s" ? match[1]! : match[2]!;
889
+ const from = range.startOffset - whole.length;
890
+ node.splitText(range.startOffset);
891
+ const middle = node.splitText(from);
892
+ const element = document.createElement(tag);
893
+ element.textContent = inner;
894
+ replaceNode(middle, element);
895
+ // A zero width space after the mark keeps the next keystroke outside it; output strips it.
896
+ const exit = document.createTextNode("\u200b");
897
+ insertAfter(element, exit);
898
+ placeCaret(exit, 1);
899
+ return true;
900
+ }
901
+ return false;
902
+ }, []);
903
+
904
+ const openSlashIfTyped = useCallback((data: string | null) => {
905
+ const editor = editorRef.current, root = rootRef.current;
906
+ const selection = window.getSelection();
907
+ if (data !== "/" || !editor || !root || !selection?.rangeCount) return;
908
+ const range = selection.getRangeAt(0);
909
+ const node = range.startContainer;
910
+ if (!range.collapsed || !isText(node) || node.parentElement?.closest("pre, code")) return;
911
+ const offset = range.startOffset - 1;
912
+ if (node.data[offset] !== "/" || (offset > 0 && !/[\s\u00a0\u200b]/.test(node.data[offset - 1]!))) return;
913
+ const mark = document.createRange();
914
+ mark.setStart(node, offset);
915
+ mark.setEnd(node, offset + 1);
916
+ const rect = mark.getBoundingClientRect(), box = root.getBoundingClientRect();
917
+ const band = clipBand(root);
918
+ const above = band.bottom - rect.bottom < 330 && rect.top - band.top > band.bottom - rect.bottom;
919
+ const room = above ? rect.top - band.top - 12 : band.bottom - rect.bottom - 12;
920
+ setSlash({ node, offset, query: "", x: Math.max(0, Math.min(rect.left - box.left - 8, box.width - 244)), y: above ? rect.top - box.top - 6 : rect.bottom - box.top + 6, above, max: Math.max(150, room) });
921
+ setSlashActive(0);
922
+ setBar(null);
923
+ }, []);
924
+
925
+ const slashResults = useMemo(() => {
926
+ const query = slash?.query.trim().toLowerCase() ?? "";
927
+ if (!query) return SLASH_ITEMS;
928
+ return SLASH_ITEMS.filter(item => `${item.label} ${item.keywords}`.toLowerCase().split(/\s+/).some(word => word.startsWith(query)) || item.label.toLowerCase().includes(query));
929
+ }, [slash?.query]);
930
+ const slashIndex = Math.min(slashActive, Math.max(0, slashResults.length - 1));
931
+ const slashOpen = !!slash && slashResults.length > 0;
932
+
933
+ const applyBlock = useCallback((type: BlockType, target?: HTMLElement | null) => {
934
+ const editor = editorRef.current;
935
+ const selection = window.getSelection();
936
+ if (!editor || !selection?.rangeCount) return;
937
+ const range = selection.getRangeAt(0);
938
+ const offsets = readOffsets(editor);
939
+ const block = target ?? blockAt(editor, range.startContainer);
940
+ if (!block) return;
941
+ if (type === "hr") {
942
+ const top = block.nodeName === "LI" ? block.closest("ul, ol")! : block;
943
+ const rule = document.createElement("hr");
944
+ if (!visibleText(block) && block.nodeName !== "LI") replaceNode(block, rule); else insertAfter(top, rule);
945
+ placeAtStart(paragraphAfter(rule));
946
+ return;
947
+ }
948
+ // Several selected blocks turn together; if every one already has the type, they all turn back into paragraphs.
949
+ const blocks = range.collapsed ? [block] : Array.from(editor.querySelectorAll<HTMLElement>("p, h1, h2, h3, li, blockquote, pre")).filter(candidate => range.intersectsNode(candidate) && !candidate.querySelector("p, li"));
950
+ const current = (candidate: HTMLElement) => candidate.nodeName === "LI" ? candidate.parentElement!.nodeName.toLowerCase() : candidate.nodeName.toLowerCase();
951
+ const allSame = blocks.every(candidate => current(candidate) === type);
952
+ blocks.forEach(candidate => {
953
+ if (allSame) convertBlock(candidate, type);
954
+ else if (current(candidate) !== type) convertBlock(candidate, type);
955
+ });
956
+ writeOffsets(editor, offsets);
957
+ if (type === "pre") {
958
+ const pre = blockAt(editor, window.getSelection()?.anchorNode ?? null);
959
+ if (pre && !pre.nextElementSibling) paragraphAfter(pre);
960
+ }
961
+ }, []);
962
+
963
+ const chooseSlash = (item: SlashItem | undefined) => {
964
+ const open = slashRef.current;
965
+ const editor = editorRef.current;
966
+ if (!item || !open || !editor) return;
967
+ const selection = window.getSelection();
968
+ const caret = selection?.rangeCount && selection.getRangeAt(0).startContainer === open.node ? selection.getRangeAt(0).startOffset : open.offset + 1 + open.query.length;
969
+ setSlash(null);
970
+ run(() => {
971
+ open.node.deleteData(open.offset, Math.max(1, caret - open.offset));
972
+ const block = blockAt(editor, open.node);
973
+ placeCaret(open.node, open.offset);
974
+ if (block) { ensureFilled(block); applyBlock(item.type, block); }
975
+ });
976
+ setAnnounce(`${item.label} added`);
977
+ };
978
+
979
+ /* Inline marks and links. */
980
+ const toggleMark = (mark: "bold" | "italic" | "strike" | "code") => run(() => {
981
+ const editor = editorRef.current!;
982
+ const selection = window.getSelection();
983
+ if (!selection?.rangeCount) return;
984
+ const range = selection.getRangeAt(0);
985
+ if (mark !== "code") { document.execCommand("styleWithCSS", false, "false"); document.execCommand(mark === "strike" ? "strikeThrough" : mark); return; }
986
+ const startElement = isElement(range.startContainer) ? range.startContainer : range.startContainer.parentElement;
987
+ const code = startElement?.closest("code");
988
+ if (code && editor.contains(code) && !code.closest("pre")) {
989
+ const text = document.createTextNode(code.textContent ?? "");
990
+ replaceNode(code, text);
991
+ const next = document.createRange();
992
+ next.selectNodeContents(text);
993
+ selection.removeAllRanges();
994
+ selection.addRange(next);
995
+ return;
996
+ }
997
+ if (range.collapsed || blockAt(editor, range.startContainer) !== blockAt(editor, range.endContainer)) return;
998
+ const element = document.createElement("code");
999
+ element.textContent = range.toString();
1000
+ range.deleteContents();
1001
+ range.insertNode(element);
1002
+ const next = document.createRange();
1003
+ next.selectNodeContents(element);
1004
+ selection.removeAllRanges();
1005
+ selection.addRange(next);
1006
+ });
1007
+
1008
+ const openLink = () => {
1009
+ const editor = editorRef.current;
1010
+ const selection = window.getSelection();
1011
+ if (!editor || !selection?.rangeCount) return;
1012
+ let range = selection.getRangeAt(0);
1013
+ const startElement = isElement(range.startContainer) ? range.startContainer : range.startContainer.parentElement;
1014
+ const existing = startElement?.closest("a");
1015
+ if (existing && editor.contains(existing)) {
1016
+ range = document.createRange();
1017
+ range.selectNodeContents(existing);
1018
+ selection.removeAllRanges();
1019
+ selection.addRange(range);
1020
+ }
1021
+ if (range.collapsed) return;
1022
+ savedRange.current = range.cloneRange();
1023
+ setLinkDraft(existing?.getAttribute("href") ?? "");
1024
+ setFace("link");
1025
+ faceRef.current = "link";
1026
+ sync();
1027
+ requestAnimationFrame(() => linkInputRef.current?.focus({ preventScroll: true }));
1028
+ };
1029
+
1030
+ const closeLink = (focusEditor = true) => {
1031
+ setFace("format");
1032
+ faceRef.current = "format";
1033
+ const editor = editorRef.current, range = savedRange.current;
1034
+ savedRange.current = null;
1035
+ if (focusEditor && editor && range) {
1036
+ editor.focus({ preventScroll: true });
1037
+ const selection = window.getSelection();
1038
+ selection?.removeAllRanges();
1039
+ selection?.addRange(range);
1040
+ }
1041
+ sync();
1042
+ };
1043
+
1044
+ const applyLink = (remove = false) => {
1045
+ const range = savedRange.current;
1046
+ const editor = editorRef.current;
1047
+ const url = normalizeUrl(linkDraft);
1048
+ if (!range || !editor) { closeLink(); return; }
1049
+ editor.focus({ preventScroll: true });
1050
+ const selection = window.getSelection()!;
1051
+ selection.removeAllRanges();
1052
+ selection.addRange(range);
1053
+ run(() => {
1054
+ const startElement = isElement(range.startContainer) ? range.startContainer : range.startContainer.parentElement;
1055
+ const existing = startElement?.closest("a");
1056
+ if (remove || !url) {
1057
+ if (existing && editor.contains(existing)) replaceNode(existing, ...Array.from(existing.childNodes));
1058
+ else document.execCommand("unlink");
1059
+ return;
1060
+ }
1061
+ if (existing && editor.contains(existing) && range.toString() === existing.textContent) { existing.setAttribute("href", url); return; }
1062
+ if (blockAt(editor, range.startContainer) !== blockAt(editor, range.endContainer)) { document.execCommand("createLink", false, url); return; }
1063
+ const contents = range.extractContents();
1064
+ contents.querySelectorAll("a").forEach(anchor => replaceNode(anchor, ...Array.from(anchor.childNodes)));
1065
+ const anchor = document.createElement("a");
1066
+ anchor.setAttribute("href", url);
1067
+ anchor.appendChild(contents);
1068
+ range.insertNode(anchor);
1069
+ const after = document.createRange();
1070
+ after.setStartAfter(anchor);
1071
+ after.collapse(true);
1072
+ selection.removeAllRanges();
1073
+ selection.addRange(after);
1074
+ });
1075
+ savedRange.current = null;
1076
+ setFace("format");
1077
+ faceRef.current = "format";
1078
+ setBar(null);
1079
+ setAnnounce(remove || !url ? "Link removed" : "Link added");
1080
+ };
1081
+
1082
+ /* Keyboard. Enter, Backspace at a block edge, and history go through beforeinput so mobile keyboards behave the same. */
1083
+ const enterKey = useCallback((soft: boolean) => {
1084
+ const editor = editorRef.current!;
1085
+ const selection = window.getSelection();
1086
+ if (!selection?.rangeCount) return;
1087
+ let range = selection.getRangeAt(0);
1088
+ if (!range.collapsed) { range.deleteContents(); range = selection.getRangeAt(0); }
1089
+ const block = blockAt(editor, range.startContainer);
1090
+ if (!block) return;
1091
+
1092
+ if (block.nodeName === "PRE") {
1093
+ const full = block.textContent ?? "";
1094
+ const caret = textBefore(block, range).length;
1095
+ const head = full.slice(0, caret), tail = full.slice(caret);
1096
+ if (!tail.replace(/\n$/, "") && head.endsWith("\n") && !soft) {
1097
+ block.textContent = head.slice(0, -1) || "";
1098
+ ensureFilled(block);
1099
+ const paragraph = document.createElement("p");
1100
+ paragraph.appendChild(document.createElement("br"));
1101
+ insertAfter(block, paragraph);
1102
+ placeAtStart(paragraph);
1103
+ return;
1104
+ }
1105
+ block.textContent = head + "\n" + (tail || "\n");
1106
+ placeCaret(block.firstChild!, caret + 1);
1107
+ return;
1108
+ }
1109
+
1110
+ if (soft) {
1111
+ const br = document.createElement("br");
1112
+ range.insertNode(br);
1113
+ if (!br.nextSibling || (isText(br.nextSibling) && !br.nextSibling.data && !br.nextSibling.nextSibling)) insertAfter(br, document.createElement("br"));
1114
+ const after = document.createRange();
1115
+ after.setStartAfter(br);
1116
+ after.collapse(true);
1117
+ selection.removeAllRanges();
1118
+ selection.addRange(after);
1119
+ return;
1120
+ }
1121
+
1122
+ if (block.nodeName === "LI" && !visibleText(block) && !block.querySelector("ul, ol")) { placeAtStart(outdent(block)); return; }
1123
+ if (block.nodeName === "BLOCKQUOTE" && !visibleText(block)) { placeAtStart(convertBlock(block, "p")); return; }
1124
+
1125
+ const tail = document.createRange();
1126
+ tail.setStart(range.startContainer, range.startOffset);
1127
+ tail.setEnd(block, block.childNodes.length);
1128
+ const atEnd = !tail.toString().replace(ZWSP, "") && !tail.cloneContents().querySelector("ul, ol");
1129
+ const heading = /^H[1-3]$/.test(block.nodeName);
1130
+ const fragment = tail.extractContents();
1131
+ const next = document.createElement(heading && atEnd ? "p" : block.nodeName.toLowerCase());
1132
+ if (!(heading && atEnd)) next.appendChild(fragment);
1133
+ // Formatting open at the caret carries into the new line only when there is text to carry.
1134
+ if (heading && atEnd) next.appendChild(document.createElement("br"));
1135
+ insertAfter(block, next);
1136
+ block.querySelectorAll(":scope > br:last-child").forEach(br => { if (visibleText(block)) br.remove(); });
1137
+ ensureFilled(block);
1138
+ ensureFilled(next);
1139
+ if (!visibleText(next)) next.querySelectorAll("strong, em, s, code, a").forEach(element => { if (!visibleText(element)) element.remove(); });
1140
+ ensureFilled(next);
1141
+ placeAtStart(next);
1142
+ }, []);
1143
+
1144
+ const backspaceAtStart = useCallback(() => {
1145
+ const editor = editorRef.current!;
1146
+ const selection = window.getSelection();
1147
+ if (!selection?.rangeCount) return false;
1148
+ const range = selection.getRangeAt(0);
1149
+ const block = blockAt(editor, range.startContainer);
1150
+ if (!block || !atBlockStart(block, range)) return false;
1151
+ if (/^(H[1-3]|BLOCKQUOTE|PRE)$/.test(block.nodeName)) { placeAtStart(convertBlock(block, "p")); return true; }
1152
+ if (block.nodeName === "LI") { placeAtStart(outdent(block)); return true; }
1153
+ const previous = block.previousElementSibling;
1154
+ if (previous?.nodeName === "HR") { previous.remove(); return true; }
1155
+ if (!previous && !visibleText(block) && block.nextElementSibling) { const next = block.nextElementSibling as HTMLElement; block.remove(); placeAtStart(next); return true; }
1156
+ return false;
1157
+ }, []);
1158
+
1159
+ useEffect(() => {
1160
+ const editor = editorRef.current;
1161
+ if (!editor) return;
1162
+ const onBeforeInput = (event: InputEvent) => {
1163
+ if (readOnly) return;
1164
+ switch (event.inputType) {
1165
+ case "historyUndo": event.preventDefault(); undo(); return;
1166
+ case "historyRedo": event.preventDefault(); redo(); return;
1167
+ case "insertParagraph":
1168
+ case "insertLineBreak":
1169
+ event.preventDefault();
1170
+ if (slashRef.current) return;
1171
+ run(() => enterKey(event.inputType === "insertLineBreak"));
1172
+ return;
1173
+ case "deleteContentBackward": {
1174
+ const selection = window.getSelection();
1175
+ if (!selection?.rangeCount || !selection.getRangeAt(0).collapsed) return;
1176
+ busy.current = true;
1177
+ let handled = false;
1178
+ try { handled = backspaceAtStart(); } finally { busy.current = false; }
1179
+ if (handled) { event.preventDefault(); tidy(editor); record("command"); emit(); sync(); }
1180
+ return;
1181
+ }
1182
+ case "insertText": {
1183
+ // Typing into an emptied editor lands in a paragraph, never loose at the root.
1184
+ const selection = window.getSelection();
1185
+ if (!selection?.rangeCount || blockAt(editor, selection.anchorNode) || editor.querySelector("p, h1, h2, h3, li, pre, blockquote")) return;
1186
+ event.preventDefault();
1187
+ const paragraph = document.createElement("p");
1188
+ const text = document.createTextNode(event.data ?? "");
1189
+ paragraph.appendChild(text);
1190
+ editor.replaceChildren(paragraph);
1191
+ placeCaret(text, text.length);
1192
+ record("type");
1193
+ emit();
1194
+ sync();
1195
+ return;
1196
+ }
1197
+ case "insertFromDrop": event.preventDefault(); return;
1198
+ case "formatUnderline": event.preventDefault(); return;
1199
+ }
1200
+ };
1201
+ editor.addEventListener("beforeinput", onBeforeInput);
1202
+ return () => editor.removeEventListener("beforeinput", onBeforeInput);
1203
+ }, [backspaceAtStart, emit, enterKey, readOnly, record, redo, run, sync, undo]);
1204
+
1205
+ const onInput = (event: FormEvent<HTMLDivElement>) => {
1206
+ if (busy.current) return;
1207
+ const editor = editorRef.current!;
1208
+ const native = event.nativeEvent as InputEvent;
1209
+ if (native.isComposing) return;
1210
+ busy.current = true;
1211
+ let converted = false;
1212
+ try {
1213
+ tidy(editor);
1214
+ if (native.inputType === "insertText") converted = shortcut(native.data);
1215
+ } finally { busy.current = false; }
1216
+ if (converted) {
1217
+ tidy(editor);
1218
+ record("command");
1219
+ setAnnounce("Formatted");
1220
+ } else record("type", native.data === " " || native.inputType !== lastInput.current);
1221
+ lastInput.current = native.inputType;
1222
+ emit();
1223
+ if (!converted && native.inputType === "insertText") openSlashIfTyped(native.data);
1224
+ sync();
1225
+ };
1226
+
1227
+ const focusToolbar = () => rootRef.current?.querySelector<HTMLElement>(`[data-toolbar="${uid}"] button`)?.focus();
1228
+
1229
+ const onKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
1230
+ if (readOnly) return;
1231
+ const mod = event.metaKey || event.ctrlKey;
1232
+ const key = event.key.toLowerCase();
1233
+ if (slashOpen) {
1234
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
1235
+ event.preventDefault();
1236
+ setSlashActive((slashIndex + (event.key === "ArrowDown" ? 1 : -1) + slashResults.length) % slashResults.length);
1237
+ return;
1238
+ }
1239
+ if (event.key === "Enter" || event.key === "Tab") { event.preventDefault(); chooseSlash(slashResults[slashIndex]); return; }
1240
+ if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); setSlash(null); return; }
1241
+ }
1242
+ if (mod && key === "z") { event.preventDefault(); if (event.shiftKey) redo(); else undo(); return; }
1243
+ if (mod && key === "y") { event.preventDefault(); redo(); return; }
1244
+ if (mod && !event.shiftKey && key === "b") { event.preventDefault(); toggleMark("bold"); return; }
1245
+ if (mod && !event.shiftKey && key === "i") { event.preventDefault(); toggleMark("italic"); return; }
1246
+ if (mod && event.shiftKey && key === "x") { event.preventDefault(); toggleMark("strike"); return; }
1247
+ if (mod && key === "e") { event.preventDefault(); toggleMark("code"); return; }
1248
+ if (mod && key === "k") { event.preventDefault(); openLink(); return; }
1249
+ if (mod && key === "u") { event.preventDefault(); return; }
1250
+ if (event.altKey && event.key === "F10" && bar) { event.preventDefault(); focusToolbar(); return; }
1251
+ if (event.key === "Tab") {
1252
+ const editor = editorRef.current!;
1253
+ const selection = window.getSelection();
1254
+ const block = selection?.rangeCount ? blockAt(editor, selection.anchorNode) : null;
1255
+ if (block?.nodeName === "LI") {
1256
+ event.preventDefault();
1257
+ run(() => {
1258
+ const offsets = readOffsets(editor);
1259
+ if (event.shiftKey) outdent(block); else indent(block);
1260
+ writeOffsets(editor, offsets);
1261
+ });
1262
+ } else if (block?.nodeName === "PRE" && !event.shiftKey) {
1263
+ event.preventDefault();
1264
+ run(() => {
1265
+ const range = selection!.getRangeAt(0);
1266
+ range.deleteContents();
1267
+ const spaces = document.createTextNode(" ");
1268
+ range.insertNode(spaces);
1269
+ placeCaret(spaces, 2);
1270
+ block.normalize();
1271
+ });
1272
+ }
1273
+ }
1274
+ };
1275
+
1276
+ const onPaste = (event: ReactClipboardEvent<HTMLDivElement>) => {
1277
+ if (readOnly) return;
1278
+ event.preventDefault();
1279
+ const editor = editorRef.current!;
1280
+ const html = event.clipboardData.getData("text/html");
1281
+ const text = event.clipboardData.getData("text/plain");
1282
+ run(() => {
1283
+ const selection = window.getSelection();
1284
+ if (!selection?.rangeCount) return;
1285
+ const range = selection.getRangeAt(0);
1286
+ range.deleteContents();
1287
+ const block = blockAt(editor, range.startContainer);
1288
+ const markdownish = /(^|\n)\s*(#{1,3}\s|[-*+]\s|\d+[.)]\s|>|```)|\*\*|`[^`]+`|\[[^\]]+\]\(/.test(text);
1289
+ const source = html ? sanitizeHtml(html) : markdownish || text.includes("\n\n") ? markdownToHtml(text) : "";
1290
+ const fragment = source ? parseHtml(source) : null;
1291
+ const single = fragment && fragment.children.length === 1 && fragment.firstElementChild!.nodeName === "P";
1292
+ // Plain text, code blocks, and list items take the text as it is; everything else keeps its structure.
1293
+ if (!fragment || !block || block.nodeName === "PRE" || (block.nodeName === "LI" && !single)) {
1294
+ const plain = block?.nodeName === "PRE" ? text : text.replace(/\s*\n\s*/g, " ");
1295
+ const node = document.createTextNode(plain);
1296
+ range.insertNode(node);
1297
+ placeCaret(node, node.length);
1298
+ return;
1299
+ }
1300
+ if (single) {
1301
+ const nodes = Array.from(fragment.firstElementChild!.childNodes);
1302
+ const last = nodes[nodes.length - 1];
1303
+ const holder = document.createDocumentFragment();
1304
+ nodes.forEach(node => holder.appendChild(node));
1305
+ range.insertNode(holder);
1306
+ if (last) { const after = document.createRange(); after.setStartAfter(last); after.collapse(true); selection.removeAllRanges(); selection.addRange(after); }
1307
+ return;
1308
+ }
1309
+ const tail = document.createRange();
1310
+ tail.setStart(range.startContainer, range.startOffset);
1311
+ tail.setEnd(block, block.childNodes.length);
1312
+ const rest = document.createElement(block.nodeName === "LI" ? "p" : block.nodeName.toLowerCase());
1313
+ rest.appendChild(tail.extractContents());
1314
+ const top = block.nodeName === "LI" ? block.closest("ul, ol")! : block;
1315
+ const blocks = Array.from(fragment.childNodes);
1316
+ let anchor: Element = top;
1317
+ blocks.forEach(node => { insertAfter(anchor, node); if (isElement(node)) anchor = node; });
1318
+ if (visibleText(rest) || rest.querySelector("br")) { insertAfter(anchor, rest); ensureFilled(rest); }
1319
+ if (!visibleText(block) && block.nodeName === "P") block.remove();
1320
+ const end = document.createRange();
1321
+ end.selectNodeContents(anchor);
1322
+ end.collapse(false);
1323
+ selection.removeAllRanges();
1324
+ selection.addRange(end);
1325
+ });
1326
+ };
1327
+
1328
+ /* Selection events. */
1329
+ useEffect(() => {
1330
+ let frame = 0;
1331
+ const onChange = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(sync); };
1332
+ const onUp = () => { if (!dragging.current) return; dragging.current = false; onChange(); };
1333
+ document.addEventListener("selectionchange", onChange);
1334
+ document.addEventListener("pointerup", onUp);
1335
+ return () => { cancelAnimationFrame(frame); document.removeEventListener("selectionchange", onChange); document.removeEventListener("pointerup", onUp); };
1336
+ }, [sync]);
1337
+
1338
+ useEffect(() => {
1339
+ if (face !== "link") return;
1340
+ const down = (event: PointerEvent) => { if (!rootRef.current?.querySelector(`[data-toolbar="${uid}"]`)?.contains(event.target as Node)) { savedRange.current = null; setFace("format"); faceRef.current = "format"; setBar(null); } };
1341
+ document.addEventListener("pointerdown", down);
1342
+ return () => document.removeEventListener("pointerdown", down);
1343
+ }, [face, uid]);
1344
+
1345
+ useImperativeHandle(ref, () => ({
1346
+ focus: () => editorRef.current?.focus(),
1347
+ getHTML: () => editorRef.current ? cleanClone(editorRef.current).innerHTML : "",
1348
+ getMarkdown: () => editorRef.current ? htmlToMarkdown(cleanClone(editorRef.current)) : "",
1349
+ getText: () => editorRef.current ? plainOutput(cleanClone(editorRef.current)) : "",
1350
+ setHTML: html => { load(html); record("command"); emit(); },
1351
+ setMarkdown: markdown => { load(markdownToHtml(markdown)); record("command"); emit(); },
1352
+ clear: () => { load("<p><br></p>"); record("command"); emit(); },
1353
+ undo,
1354
+ redo,
1355
+ get element() { return editorRef.current; },
1356
+ }), [emit, load, record, redo, undo]);
1357
+
1358
+ /* Toolbar geometry: one surface whose size springs between faces and whose position glides with the selection. */
1359
+ const tx = useMotionValue(0), ty = useMotionValue(0), tw = useMotionValue(0), th = useMotionValue(0);
1360
+ const shown = useRef(false);
1361
+ /* Until a face has reported its size the surface has nothing to show, so it stays hidden rather than flashing a 1px dot. */
1362
+ const toolbarVisibility = useTransform(tw, width => width > 0 ? "visible" : "hidden");
1363
+ const targetSize = useRef({ width: 0, height: 0 });
1364
+ const onFaceSize = useCallback((width: number, height: number) => {
1365
+ targetSize.current = { width, height };
1366
+ if (!shown.current || reduced || tw.get() === 0) { tw.jump(width); th.jump(height); return; }
1367
+ animate(tw, width, MORPH);
1368
+ animate(th, height, MORPH);
1369
+ }, [reduced, th, tw]);
1370
+ const toolbarOpen = !!bar && !readOnly;
1371
+ // A fresh toolbar is placed, not glided, and stays "fresh" until it has painted once, so a selection that settles in the same
1372
+ // frame cannot send it sliding in from its last spot or from the root's corner. Motion resets a remounted element's bound
1373
+ // values to what they held at render (Strict Mode does this on every open in development), so the placement is applied
1374
+ // again once mounting is done. Its size is left alone on close so the exit keeps its shape instead of collapsing.
1375
+ const placedFrame = useRef(0);
1376
+ const placement = useRef<{ x: number; y: number } | null>(null);
1377
+ const place = useCallback(() => {
1378
+ const at = placement.current;
1379
+ if (!at) return;
1380
+ const { width, height } = targetSize.current;
1381
+ if (tx.get() !== at.x) tx.jump(at.x);
1382
+ if (ty.get() !== at.y) ty.jump(at.y);
1383
+ if (width && tw.get() !== width) { tw.jump(width); th.jump(height); }
1384
+ }, [th, tw, tx, ty]);
1385
+ useLayoutEffect(() => {
1386
+ if (!bar) { shown.current = false; placement.current = null; cancelAnimationFrame(placedFrame.current); return; }
1387
+ const root = rootRef.current;
1388
+ const half = (targetSize.current.width || 280) / 2;
1389
+ const x = root ? Math.max(half + 4, Math.min(bar.x, root.offsetWidth - half - 4)) : bar.x;
1390
+ if (!shown.current || reduced) {
1391
+ placement.current = { x, y: bar.y };
1392
+ place();
1393
+ cancelAnimationFrame(placedFrame.current);
1394
+ placedFrame.current = requestAnimationFrame(() => { place(); shown.current = true; placement.current = null; });
1395
+ return;
1396
+ }
1397
+ animate(tx, x, GLIDE);
1398
+ animate(ty, bar.y, GLIDE);
1399
+ }, [bar, place, reduced, tx, ty]);
1400
+ useEffect(() => { if (bar) place(); }, [bar, place]);
1401
+ useEffect(() => () => cancelAnimationFrame(placedFrame.current), []);
1402
+ const direction = face === "link" ? 1 : -1;
1403
+
1404
+ /* Slash menu geometry: the highlight glides between rows and the panel springs to its rows. */
1405
+ const listRef = useRef<HTMLUListElement>(null);
1406
+ const hy = useMotionValue(0), hh = useMotionValue(0), panelHeight = useMotionValue<number | "auto">("auto");
1407
+ const panelBody = useRef<HTMLDivElement>(null);
1408
+ const slashPanel = useRef<HTMLDivElement>(null);
1409
+ const slashMax = slash?.max ?? 320;
1410
+ const panelSized = useRef(false);
1411
+ const highlightPlaced = useRef(false);
1412
+ useLayoutEffect(() => {
1413
+ const row = listRef.current?.querySelector<HTMLElement>(`[data-index="${slashIndex}"]`);
1414
+ if (!row) return;
1415
+ if (reduced || !highlightPlaced.current || hh.get() === 0) { hy.jump(row.offsetTop); hh.jump(row.offsetHeight); highlightPlaced.current = true; return; }
1416
+ animate(hy, row.offsetTop, GLIDE);
1417
+ animate(hh, row.offsetHeight, GLIDE);
1418
+ // A short panel scrolls to keep the active row in view.
1419
+ const panel = slashPanel.current;
1420
+ if (panel) {
1421
+ const top = row.offsetTop + 6, bottom = top + row.offsetHeight;
1422
+ if (top - 6 < panel.scrollTop) panel.scrollTop = top - 6;
1423
+ else if (bottom + 6 > panel.scrollTop + panel.clientHeight) panel.scrollTop = bottom + 6 - panel.clientHeight;
1424
+ }
1425
+ }, [hh, hy, reduced, slashIndex, slashResults, slashOpen]);
1426
+ useLayoutEffect(() => {
1427
+ // The highlight keeps its row while the menu fades out; the next open places it fresh.
1428
+ if (!slashOpen) { panelSized.current = false; highlightPlaced.current = false; return; }
1429
+ const body = panelBody.current;
1430
+ if (!body) return;
1431
+ const fit = () => {
1432
+ const target = Math.min(body.offsetHeight, slashMax);
1433
+ if (!panelSized.current || reduced) { panelHeight.jump(target); panelSized.current = true; return; }
1434
+ animate(panelHeight, target, GROW);
1435
+ };
1436
+ fit();
1437
+ if (typeof ResizeObserver === "undefined") return;
1438
+ const observer = new ResizeObserver(fit);
1439
+ observer.observe(body);
1440
+ return () => observer.disconnect();
1441
+ }, [hh, panelHeight, reduced, slashMax, slashOpen]);
1442
+
1443
+ const onToolbarKey = (event: ReactKeyboardEvent<HTMLDivElement>) => {
1444
+ if (face !== "format") return;
1445
+ const buttons = Array.from(event.currentTarget.querySelectorAll<HTMLButtonElement>("button"));
1446
+ const at = buttons.indexOf(document.activeElement as HTMLButtonElement);
1447
+ if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
1448
+ event.preventDefault();
1449
+ buttons[(at + (event.key === "ArrowRight" ? 1 : -1) + buttons.length) % buttons.length]?.focus();
1450
+ } else if (event.key === "Escape") {
1451
+ event.preventDefault();
1452
+ editorRef.current?.focus();
1453
+ sync();
1454
+ }
1455
+ };
1456
+
1457
+ const optionId = (index: number) => `${uid}-slash-${index}`;
1458
+ const cssVars = { "--placeholder": JSON.stringify(placeholder), "--hint": JSON.stringify(blockHint) } as CSSProperties;
1459
+
1460
+ return <div ref={rootRef} className={[styles.root, className].filter(Boolean).join(" ")} style={style}>
1461
+ <div
1462
+ ref={editorRef}
1463
+ className={styles.editor}
1464
+ style={cssVars}
1465
+ contentEditable={!readOnly}
1466
+ suppressContentEditableWarning
1467
+ role="textbox"
1468
+ aria-multiline="true"
1469
+ aria-label={ariaLabel}
1470
+ aria-readonly={readOnly || undefined}
1471
+ aria-controls={slashOpen ? `${uid}-slash` : undefined}
1472
+ aria-activedescendant={slashOpen ? optionId(slashIndex) : undefined}
1473
+ data-empty={empty || undefined}
1474
+ spellCheck
1475
+ onInput={onInput}
1476
+ onKeyDown={onKeyDown}
1477
+ onPaste={onPaste}
1478
+ onDrop={event => event.preventDefault()}
1479
+ onPointerDown={() => { dragging.current = true; }}
1480
+ onCompositionEnd={() => { record("type", true); emit(); }}
1481
+ />
1482
+ <span className={styles.srOnly} aria-live="polite">{announce}</span>
1483
+
1484
+ <AnimatePresence>
1485
+ {toolbarOpen && <motion.div
1486
+ key="toolbar"
1487
+ data-toolbar={uid}
1488
+ role="toolbar"
1489
+ aria-label="Formatting"
1490
+ className={styles.toolbar}
1491
+ data-below={bar.below || undefined}
1492
+ style={{ left: tx, top: ty, width: tw, height: th, visibility: toolbarVisibility, transformOrigin: bar.below ? "50% 0" : "50% 100%" }}
1493
+ initial={reduced ? { opacity: 0 } : { opacity: 0, scale: .94, y: bar.below ? -4 : 4 }}
1494
+ animate={{ opacity: 1, scale: 1, y: 0 }}
1495
+ exit={reduced ? { opacity: 0 } : { opacity: 0, scale: .96, transition: { duration: duration.exit * .8, ease: standard } }}
1496
+ transition={reduced ? { duration: duration.fast } : { ...spring.snappy, opacity: { duration: duration.fast, ease: enter } }}
1497
+ onKeyDown={onToolbarKey}
1498
+ >
1499
+ <AnimatePresence initial={false} custom={direction}>
1500
+ {face === "format" ? <Face key="format" direction={direction} reduced={reduced} onSize={onFaceSize}>
1501
+ <div className={styles.row}>
1502
+ <ToolButton label="Bold" pressed={formats.bold} shortcut="⌘B" onPress={() => toggleMark("bold")}><Bold {...icon} /></ToolButton>
1503
+ <ToolButton label="Italic" pressed={formats.italic} shortcut="⌘I" onPress={() => toggleMark("italic")}><Italic {...icon} /></ToolButton>
1504
+ <ToolButton label="Strikethrough" pressed={formats.strike} shortcut="⌘⇧X" onPress={() => toggleMark("strike")}><Strikethrough {...icon} /></ToolButton>
1505
+ <ToolButton label="Inline code" pressed={formats.code} shortcut="⌘E" onPress={() => toggleMark("code")}><Code {...icon} /></ToolButton>
1506
+ <ToolButton label={formats.link ? "Edit link" : "Link"} pressed={!!formats.link} shortcut="⌘K" onPress={openLink}><Link {...icon} /></ToolButton>
1507
+ <span className={styles.blockTools}>
1508
+ <span className={styles.divider} aria-hidden="true" />
1509
+ <ToolButton label="Heading 1" pressed={formats.block === "h1"} onPress={() => run(() => applyBlock("h1"))}><Heading1 {...icon} /></ToolButton>
1510
+ <ToolButton label="Heading 2" pressed={formats.block === "h2"} onPress={() => run(() => applyBlock("h2"))}><Heading2 {...icon} /></ToolButton>
1511
+ <ToolButton label="Quote" pressed={formats.block === "blockquote"} onPress={() => run(() => applyBlock("blockquote"))}><Quote {...icon} /></ToolButton>
1512
+ </span>
1513
+ </div>
1514
+ </Face> : <Face key="link" direction={direction} reduced={reduced} onSize={onFaceSize}>
1515
+ <form className={styles.linkRow} onSubmit={event => { event.preventDefault(); applyLink(); }}>
1516
+ <button type="button" className={styles.tool} aria-label="Back to formatting" onClick={() => closeLink()}><ArrowLeft {...icon} /></button>
1517
+ <input
1518
+ ref={linkInputRef}
1519
+ className={styles.linkInput}
1520
+ value={linkDraft}
1521
+ onChange={event => setLinkDraft(event.target.value)}
1522
+ onKeyDown={event => { if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); closeLink(); } }}
1523
+ placeholder="Paste or type a link"
1524
+ aria-label="Link address"
1525
+ inputMode="url"
1526
+ autoComplete="off"
1527
+ spellCheck={false}
1528
+ />
1529
+ {formats.link && <button type="button" className={styles.tool} aria-label="Remove link" onClick={() => applyLink(true)}><Unlink {...icon} /></button>}
1530
+ <button type="submit" className={styles.apply} aria-label="Apply link" data-ready={!!linkDraft.trim() || undefined}><Check size={16} strokeWidth={2} aria-hidden="true" /></button>
1531
+ </form>
1532
+ </Face>}
1533
+ </AnimatePresence>
1534
+ </motion.div>}
1535
+ </AnimatePresence>
1536
+
1537
+ <AnimatePresence>
1538
+ {slashOpen && slash && <motion.div
1539
+ key={`${slash.offset}-${slash.y}`}
1540
+ ref={slashPanel}
1541
+ className={styles.slash}
1542
+ data-above={slash.above || undefined}
1543
+ style={{ left: slash.x, top: slash.y, height: panelHeight, maxHeight: slashMax, transformOrigin: slash.above ? "16px 100%" : "16px 0" }}
1544
+ initial={reduced ? { opacity: 0 } : { opacity: 0, scale: .96, y: slash.above ? 4 : -4 }}
1545
+ animate={{ opacity: 1, scale: 1, y: 0 }}
1546
+ exit={reduced ? { opacity: 0 } : { opacity: 0, scale: .97, transition: { duration: duration.exit * .8, ease: standard } }}
1547
+ transition={reduced ? { duration: duration.fast } : { ...spring.snappy, opacity: { duration: duration.fast, ease: enter } }}
1548
+ onMouseDown={event => event.preventDefault()}
1549
+ >
1550
+ <div ref={panelBody} className={styles.slashBody}>
1551
+ <ul ref={listRef} id={`${uid}-slash`} role="listbox" aria-label="Blocks" className={styles.slashList}>
1552
+ <motion.li aria-hidden="true" className={styles.slashHighlight} style={{ y: hy, height: hh }} />
1553
+ {slashResults.map((item, index) => <li key={item.type} id={optionId(index)} role="option" aria-selected={index === slashIndex} data-index={index} className={styles.slashOption}
1554
+ onPointerMove={() => { if (index !== slashIndex) setSlashActive(index); }} onClick={() => chooseSlash(item)}>
1555
+ <span className={styles.slashIcon}>{item.icon}</span>
1556
+ <span className={styles.slashLabel}>{item.label}</span>
1557
+ {item.hint && <kbd className={styles.slashHint}>{item.hint}</kbd>}
1558
+ </li>)}
1559
+ </ul>
1560
+ </div>
1561
+ </motion.div>}
1562
+ </AnimatePresence>
1563
+ </div>;
1564
+ });
1565
+
1566
+ export default RichTextEditor;