@zyro-sec/ui 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/AGENTS.md +29 -0
  2. package/bunfig.toml +2 -0
  3. package/package.json +21 -40
  4. package/perf/motion.bench.ts +14 -0
  5. package/perf/motion.ts +16 -0
  6. package/script/build-oc2-v2-overrides.ts +38 -0
  7. package/script/colors.txt +288 -0
  8. package/script/generate-spinner.ts +121 -0
  9. package/script/pack.ts +5 -0
  10. package/script/tailwind.ts +24 -0
  11. package/src/actions/button/button.stories.tsx +158 -0
  12. package/src/actions/icon-button/icon-button.stories.tsx +122 -0
  13. package/src/actions/split-button/split-button.stories.tsx +59 -0
  14. package/src/assets/favicon/apple-touch-icon-v3.png +0 -0
  15. package/src/assets/favicon/apple-touch-icon.png +0 -0
  16. package/src/assets/favicon/favicon-96x96-v3.png +0 -0
  17. package/src/assets/favicon/favicon-96x96.png +0 -0
  18. package/src/assets/favicon/favicon-v3.ico +0 -0
  19. package/src/assets/favicon/favicon-v3.svg +7 -0
  20. package/src/assets/favicon/favicon.ico +0 -0
  21. package/src/assets/favicon/favicon.svg +7 -0
  22. package/src/assets/favicon/site.webmanifest +24 -0
  23. package/src/assets/favicon/web-app-manifest-192x192.png +0 -0
  24. package/src/assets/favicon/web-app-manifest-512x512.png +0 -0
  25. package/src/assets/icons/provider/302ai.svg +7 -0
  26. package/src/assets/icons/provider/abacus.svg +14 -0
  27. package/src/assets/icons/provider/aihubmix.svg +3 -0
  28. package/src/assets/icons/provider/alibaba-cn.svg +3 -0
  29. package/src/assets/icons/provider/alibaba-coding-plan-cn.svg +3 -0
  30. package/src/assets/icons/provider/alibaba-coding-plan.svg +3 -0
  31. package/src/assets/icons/provider/alibaba.svg +3 -0
  32. package/src/assets/icons/provider/amazon-bedrock.svg +3 -0
  33. package/src/assets/icons/provider/anthropic.svg +3 -0
  34. package/src/assets/icons/provider/azure-cognitive-services.svg +24 -0
  35. package/src/assets/icons/provider/azure.svg +3 -0
  36. package/src/assets/icons/provider/bailing.svg +3 -0
  37. package/src/assets/icons/provider/baseten.svg +3 -0
  38. package/src/assets/icons/provider/berget.svg +3 -0
  39. package/src/assets/icons/provider/cerebras.svg +4 -0
  40. package/src/assets/icons/provider/chutes.svg +24 -0
  41. package/src/assets/icons/provider/clarifai.svg +24 -0
  42. package/src/assets/icons/provider/claude-agent.svg +3 -0
  43. package/src/assets/icons/provider/cloudferro-sherlock.svg +5 -0
  44. package/src/assets/icons/provider/cloudflare-ai-gateway.svg +4 -0
  45. package/src/assets/icons/provider/cloudflare-workers-ai.svg +4 -0
  46. package/src/assets/icons/provider/cohere.svg +5 -0
  47. package/src/assets/icons/provider/cortecs.svg +24 -0
  48. package/src/assets/icons/provider/deepinfra.svg +4 -0
  49. package/src/assets/icons/provider/deepseek.svg +3 -0
  50. package/src/assets/icons/provider/digitalocean.svg +6 -0
  51. package/src/assets/icons/provider/dinference.svg +1 -0
  52. package/src/assets/icons/provider/drun.svg +8 -0
  53. package/src/assets/icons/provider/evroc.svg +3 -0
  54. package/src/assets/icons/provider/fastrouter.svg +3 -0
  55. package/src/assets/icons/provider/fireworks-ai.svg +3 -0
  56. package/src/assets/icons/provider/firmware.svg +18 -0
  57. package/src/assets/icons/provider/friendli.svg +3 -0
  58. package/src/assets/icons/provider/github-copilot.svg +3 -0
  59. package/src/assets/icons/provider/github-models.svg +3 -0
  60. package/src/assets/icons/provider/gitlab.svg +3 -0
  61. package/src/assets/icons/provider/google-vertex-anthropic.svg +24 -0
  62. package/src/assets/icons/provider/google-vertex.svg +10 -0
  63. package/src/assets/icons/provider/google.svg +3 -0
  64. package/src/assets/icons/provider/groq.svg +3 -0
  65. package/src/assets/icons/provider/helicone.svg +6 -0
  66. package/src/assets/icons/provider/huggingface.svg +3 -0
  67. package/src/assets/icons/provider/iflowcn.svg +3 -0
  68. package/src/assets/icons/provider/inception.svg +4 -0
  69. package/src/assets/icons/provider/inference.svg +3 -0
  70. package/src/assets/icons/provider/io-net.svg +5 -0
  71. package/src/assets/icons/provider/jiekou.svg +4 -0
  72. package/src/assets/icons/provider/kilo.svg +4 -0
  73. package/src/assets/icons/provider/kimi-for-coding.svg +3 -0
  74. package/src/assets/icons/provider/kuae-cloud-coding-plan.svg +3 -0
  75. package/src/assets/icons/provider/llama.svg +3 -0
  76. package/src/assets/icons/provider/lmstudio.svg +24 -0
  77. package/src/assets/icons/provider/lucidquery.svg +3 -0
  78. package/src/assets/icons/provider/meganova.svg +3 -0
  79. package/src/assets/icons/provider/meituan.svg +6 -0
  80. package/src/assets/icons/provider/meta.svg +3 -0
  81. package/src/assets/icons/provider/minimax-cn-coding-plan.svg +24 -0
  82. package/src/assets/icons/provider/minimax-cn.svg +3 -0
  83. package/src/assets/icons/provider/minimax-coding-plan.svg +24 -0
  84. package/src/assets/icons/provider/minimax.svg +3 -0
  85. package/src/assets/icons/provider/mistral.svg +3 -0
  86. package/src/assets/icons/provider/moark.svg +3 -0
  87. package/src/assets/icons/provider/modelscope.svg +5 -0
  88. package/src/assets/icons/provider/moonshotai-cn.svg +3 -0
  89. package/src/assets/icons/provider/moonshotai.svg +3 -0
  90. package/src/assets/icons/provider/morph.svg +24 -0
  91. package/src/assets/icons/provider/nano-gpt.svg +3 -0
  92. package/src/assets/icons/provider/nebius.svg +4 -0
  93. package/src/assets/icons/provider/nova.svg +3 -0
  94. package/src/assets/icons/provider/novita-ai.svg +10 -0
  95. package/src/assets/icons/provider/nvidia.svg +3 -0
  96. package/src/assets/icons/provider/ollama-cloud.svg +7 -0
  97. package/src/assets/icons/provider/openai.svg +3 -0
  98. package/src/assets/icons/provider/opencode-go.svg +3 -0
  99. package/src/assets/icons/provider/opencode.svg +3 -0
  100. package/src/assets/icons/provider/openrouter.svg +3 -0
  101. package/src/assets/icons/provider/ovhcloud.svg +3 -0
  102. package/src/assets/icons/provider/perplexity-agent.svg +3 -0
  103. package/src/assets/icons/provider/perplexity.svg +3 -0
  104. package/src/assets/icons/provider/poe.svg +7 -0
  105. package/src/assets/icons/provider/privatemode-ai.svg +5 -0
  106. package/src/assets/icons/provider/qihang-ai.svg +9 -0
  107. package/src/assets/icons/provider/qiniu-ai.svg +7 -0
  108. package/src/assets/icons/provider/reflection.svg +3 -0
  109. package/src/assets/icons/provider/requesty.svg +24 -0
  110. package/src/assets/icons/provider/sap-ai-core.svg +24 -0
  111. package/src/assets/icons/provider/scaleway.svg +5 -0
  112. package/src/assets/icons/provider/siliconflow-cn.svg +3 -0
  113. package/src/assets/icons/provider/siliconflow.svg +3 -0
  114. package/src/assets/icons/provider/stackit.svg +4 -0
  115. package/src/assets/icons/provider/stepfun.svg +16 -0
  116. package/src/assets/icons/provider/submodel.svg +3 -0
  117. package/src/assets/icons/provider/synthetic.svg +24 -0
  118. package/src/assets/icons/provider/tencent-coding-plan.svg +5 -0
  119. package/src/assets/icons/provider/tencent.svg +3 -0
  120. package/src/assets/icons/provider/thinkingmachines.svg +3 -0
  121. package/src/assets/icons/provider/togetherai.svg +4 -0
  122. package/src/assets/icons/provider/upstage.svg +24 -0
  123. package/src/assets/icons/provider/v0.svg +3 -0
  124. package/src/assets/icons/provider/venice.svg +4 -0
  125. package/src/assets/icons/provider/vercel.svg +3 -0
  126. package/src/assets/icons/provider/vivgrid.svg +4 -0
  127. package/src/assets/icons/provider/vultr.svg +6 -0
  128. package/src/assets/icons/provider/wandb.svg +24 -0
  129. package/src/assets/icons/provider/xai.svg +3 -0
  130. package/src/assets/icons/provider/xiaomi.svg +3 -0
  131. package/src/assets/icons/provider/zai-coding-plan.svg +3 -0
  132. package/src/assets/icons/provider/zai.svg +3 -0
  133. package/src/assets/icons/provider/zenmux.svg +4 -0
  134. package/src/assets/icons/provider/zhipuai-coding-plan.svg +3 -0
  135. package/src/assets/icons/provider/zhipuai.svg +3 -0
  136. package/src/assets/images/social-share-black.png +0 -0
  137. package/src/assets/images/social-share-zen.png +0 -0
  138. package/src/assets/images/social-share.png +0 -0
  139. package/src/components/animated-number-machine.test.ts +140 -0
  140. package/src/components/animated-number.stories.tsx +35 -0
  141. package/src/components/app-icon.stories.tsx +73 -0
  142. package/src/components/card.stories.tsx +101 -0
  143. package/src/components/collapsible.stories.tsx +76 -0
  144. package/src/components/context-menu.stories.tsx +150 -0
  145. package/src/components/diagram-preview.stories.tsx +62 -0
  146. package/src/components/dock-surface.stories.tsx +26 -0
  147. package/src/components/document-surface.stories.tsx +42 -0
  148. package/src/components/error-details.stories.tsx +72 -0
  149. package/src/components/favicon.stories.tsx +49 -0
  150. package/src/components/file-icon-machine.test.ts +52 -0
  151. package/src/components/file-icon.stories.tsx +93 -0
  152. package/src/components/file-icons/sprite.svg +11707 -0
  153. package/src/components/font.stories.tsx +48 -0
  154. package/src/components/hover-card.stories.tsx +79 -0
  155. package/src/components/image-flight-machine.test.ts +184 -0
  156. package/src/components/image-flight.test.ts +173 -0
  157. package/src/components/image-preview.stories.tsx +392 -0
  158. package/src/components/image-zoom-machine.test.ts +154 -0
  159. package/src/components/image-zoom.test.ts +96 -0
  160. package/src/components/list.stories.tsx +221 -0
  161. package/src/components/logo.stories.tsx +62 -0
  162. package/src/components/menu-scroll.stories.tsx +73 -0
  163. package/src/components/popover-machine.test.ts +150 -0
  164. package/src/components/popover.stories.tsx +84 -0
  165. package/src/components/preview-controls.stories.tsx +40 -0
  166. package/src/components/progress.stories.tsx +69 -0
  167. package/src/components/provider-icon.stories.tsx +75 -0
  168. package/src/components/resize-handle-machine.test.ts +164 -0
  169. package/src/components/resize-handle.stories.tsx +81 -0
  170. package/src/components/rolling-number.stories.tsx +23 -0
  171. package/src/components/scroll-view-machine.test.ts +141 -0
  172. package/src/components/scroll-view.stories.tsx +68 -0
  173. package/src/components/scroll-view.test.ts +114 -0
  174. package/src/components/spinner-radar.test.ts +61 -0
  175. package/src/components/spinner.stories.tsx +40 -0
  176. package/src/components/sticky-accordion-header.stories.tsx +64 -0
  177. package/src/components/text-field-machine.test.ts +99 -0
  178. package/src/components/text-field.stories.tsx +132 -0
  179. package/src/components/text-reveal-machine.test.ts +157 -0
  180. package/src/components/text-reveal.stories.tsx +363 -0
  181. package/src/components/text-strikethrough.stories.tsx +296 -0
  182. package/src/components/thinking-heading.stories.tsx +900 -0
  183. package/src/components/truncated-text.stories.tsx +37 -0
  184. package/src/components/typewriter-machine.test.ts +177 -0
  185. package/src/components/typewriter.stories.tsx +51 -0
  186. package/src/context/dialog-machine.test.ts +295 -0
  187. package/src/context/i18n.test.ts +89 -0
  188. package/src/context/marked-base.test.ts +75 -0
  189. package/src/context/marked-parser.test.ts +167 -0
  190. package/src/context/marked-regression.test.ts +12 -0
  191. package/src/data-display/accordion/accordion.stories.tsx +76 -0
  192. package/src/data-display/avatar/avatar.stories.tsx +48 -0
  193. package/src/data-display/badge/badge.stories.tsx +75 -0
  194. package/src/data-display/diff-changes/diff-changes.stories.tsx +63 -0
  195. package/src/data-display/keybind/keybind.stories.tsx +77 -0
  196. package/src/data-display/line-comment/line-comment.stories.tsx +288 -0
  197. package/src/data-display/line-comment/machine.test.ts +385 -0
  198. package/src/data-display/project-avatar/animation-machine.test.ts +134 -0
  199. package/src/data-display/project-avatar/generated.test.ts +50 -0
  200. package/src/data-display/project-avatar/procedural.test.ts +16 -0
  201. package/src/data-display/project-avatar/project-avatar.stories.tsx +102 -0
  202. package/src/data-display/project-avatar/project-avatar.test.ts +21 -0
  203. package/src/data-display/project-avatar/project-icon-styles.stories.tsx +772 -0
  204. package/src/feedback/activity-ring/activity-ring.stories.tsx +20 -0
  205. package/src/feedback/loader/loader.stories.tsx +44 -0
  206. package/src/feedback/progress-circle/progress-circle.stories.tsx +56 -0
  207. package/src/feedback/toast/description.test.ts +20 -0
  208. package/src/feedback/toast/machine.test.ts +106 -0
  209. package/src/feedback/toast/toast.stories.tsx +186 -0
  210. package/src/forms/checkbox/checkbox.stories.tsx +104 -0
  211. package/src/forms/field/field.stories.tsx +129 -0
  212. package/src/forms/inline-input/inline-input.stories.tsx +37 -0
  213. package/src/forms/radio/radio.stories.tsx +145 -0
  214. package/src/forms/redirect-paste/redirect-paste.stories.tsx +101 -0
  215. package/src/forms/search-list/navigation.test.ts +40 -0
  216. package/src/forms/search-list/search-list.stories.tsx +108 -0
  217. package/src/forms/select/machine.test.ts +105 -0
  218. package/src/forms/select/select.stories.tsx +208 -0
  219. package/src/forms/switch/switch.stories.tsx +97 -0
  220. package/src/forms/text-input/text-input.stories.tsx +220 -0
  221. package/src/forms/textarea/textarea.stories.tsx +137 -0
  222. package/src/forms/typeahead/machine.test.ts +143 -0
  223. package/src/forms/typeahead/typeahead.stories.tsx +113 -0
  224. package/src/hooks/auto-scroll-machine.test.ts +212 -0
  225. package/src/hooks/list-machine.test.ts +263 -0
  226. package/src/hooks/spin-phase.test.ts +32 -0
  227. package/src/hooks/use-filtered-list.test.ts +41 -0
  228. package/src/icons/icon/icon.stories.tsx +131 -0
  229. package/src/layout/divider/divider.stories.tsx +38 -0
  230. package/src/motion/README.md +113 -0
  231. package/src/motion/core.test.ts +328 -0
  232. package/src/motion-tuning-features.test.ts +129 -0
  233. package/src/motion-tuning.test.ts +68 -0
  234. package/src/navigation/menu/machine.test.ts +85 -0
  235. package/src/navigation/menu/menu.stories.tsx +328 -0
  236. package/src/navigation/menu/submenu-machine.test.ts +71 -0
  237. package/src/navigation/segmented-control/segmented-control.stories.tsx +115 -0
  238. package/src/navigation/segmented-control/segmented-control.test.ts +123 -0
  239. package/src/navigation/tab-state-indicator.stories.tsx +24 -0
  240. package/src/navigation/tabs/tabs.stories.tsx +193 -0
  241. package/src/overlays/dialog/dialog.stories.tsx +365 -0
  242. package/src/overlays/mcp-sign-in/machine.test.ts +215 -0
  243. package/src/overlays/tooltip/machine.test.ts +214 -0
  244. package/src/overlays/tooltip/tooltip.stories.tsx +155 -0
  245. package/src/styles/blue-tokens.stories.tsx +215 -0
  246. package/src/theme/desktop-theme.schema.json +168 -0
  247. package/src/theme/machine.test.ts +498 -0
  248. package/src/theme/v2/resolve.test.ts +120 -0
  249. package/src/typography/slash/slash.stories.tsx +26 -0
  250. package/src/typography/text-shimmer/machine.test.ts +117 -0
  251. package/src/typography/text-shimmer/text-shimmer.stories.tsx +64 -0
  252. package/src/typography/wordmark/wordmark.stories.tsx +19 -0
  253. package/test/solid-jsx.ts +20 -0
  254. package/tsconfig.build.json +14 -0
  255. package/tsconfig.json +23 -0
  256. package/vite.config.ts +61 -0
@@ -0,0 +1,37 @@
1
+ import { TruncatedText } from "./truncated-text"
2
+
3
+ const args = { text: "Workspace tools for shared development environments", width: 216, rtl: false }
4
+
5
+ function Preview(props: typeof args) {
6
+ return (
7
+ <div class="grid w-96 max-w-full gap-2 text-sm">
8
+ <div class="font-medium">Extension name</div>
9
+ <p class="text-xs text-v2-text-text-muted">
10
+ Hover or focus a clipped name to read it. On touch screens the name wraps.
11
+ </p>
12
+ <div
13
+ dir={props.rtl ? "rtl" : undefined}
14
+ style={{ width: `${props.width}px` }}
15
+ class="flex max-w-full rounded-md border border-v2-border-border-base p-2"
16
+ >
17
+ <TruncatedText text={props.text} />
18
+ </div>
19
+ </div>
20
+ )
21
+ }
22
+
23
+ export default {
24
+ title: "UI/TruncatedText",
25
+ id: "ui-truncated-text",
26
+ component: Preview,
27
+ args,
28
+ argTypes: { width: { control: { type: "range", min: 96, max: 384, step: 8 } } },
29
+ }
30
+
31
+ export const Truncated = {}
32
+
33
+ export const Fits = { args: { text: "Workspace tools" } }
34
+
35
+ export const Unbroken = { args: { text: "workspace-development-environment-configuration-tools", width: 136 } }
36
+
37
+ export const MixedDirection = { args: { rtl: true, text: "أدوات المشروع — Workspace tools v2", width: 160 } }
@@ -0,0 +1,177 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import {
3
+ DISPOSED_TYPEWRITER_EVENT,
4
+ DISPOSED_TYPEWRITER_STATE,
5
+ INITIAL_EMPTY_TYPEWRITER_STATE,
6
+ selectTypewriterCursor,
7
+ selectTypewriterDisplayed,
8
+ transitionTypewriter,
9
+ typingStepDelayMs,
10
+ type TypewriterEvent,
11
+ type TypewriterState,
12
+ type TypewriterTransition,
13
+ } from "./typewriter-machine"
14
+
15
+ const run = (state: TypewriterState, ...events: TypewriterEvent[]) =>
16
+ events.reduce<TypewriterTransition>((result, event) => transitionTypewriter(result.state, event), {
17
+ state,
18
+ effect: { type: "none" },
19
+ })
20
+
21
+ const sync = (text: string | undefined, reducedMotion = false): TypewriterEvent => ({
22
+ type: "sync",
23
+ text,
24
+ reducedMotion,
25
+ })
26
+
27
+ const step = (attempt = 1): TypewriterEvent => ({ type: "typing.step", attempt })
28
+
29
+ const lingerEnded = (attempt = 1): TypewriterEvent => ({ type: "linger.ended", attempt })
30
+
31
+ const view = (state: TypewriterState) => [selectTypewriterDisplayed(state), selectTypewriterCursor(state)]
32
+
33
+ describe("empty text", () => {
34
+ test.each([undefined, ""])("mounting with %p renders nothing and arms nothing", (text) => {
35
+ // Before the machine, an empty mount left a blinking cursor on screen forever.
36
+ const next = transitionTypewriter(INITIAL_EMPTY_TYPEWRITER_STATE, sync(text))
37
+
38
+ expect(next.state).toBe(INITIAL_EMPTY_TYPEWRITER_STATE)
39
+ expect(next.effect).toEqual({ type: "none" })
40
+ expect(view(next.state)).toEqual(["", "none"])
41
+ })
42
+
43
+ test("clearing the text mid-typing clears the partial text and the cursor", () => {
44
+ // Before the machine, "Hel" and a solid cursor stayed on screen forever.
45
+ const typing = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hello"), step(), step(), step())
46
+ expect(view(typing.state)).toEqual(["Hel", "solid"])
47
+
48
+ const cleared = transitionTypewriter(typing.state, sync(""))
49
+ expect(cleared.state).toEqual({ status: "empty", attempts: 1 })
50
+ expect(cleared.effect).toEqual({ type: "clear" })
51
+ expect(view(cleared.state)).toEqual(["", "none"])
52
+ expect(transitionTypewriter(cleared.state, step()).state).toBe(cleared.state)
53
+ })
54
+
55
+ test("clearing the text while the cursor lingers hides it at once", () => {
56
+ const lingering = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"), step(), step())
57
+ expect(lingering.state.status).toBe("lingering")
58
+
59
+ const cleared = transitionTypewriter(lingering.state, sync(undefined))
60
+ expect(cleared.effect).toEqual({ type: "clear" })
61
+ expect(view(cleared.state)).toEqual(["", "none"])
62
+ expect(transitionTypewriter(cleared.state, lingerEnded()).state).toBe(cleared.state)
63
+ })
64
+
65
+ test("clearing a finished text needs no timer to be cleared", () => {
66
+ const done = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"), step(), step(), lingerEnded())
67
+ const cleared = transitionTypewriter(done.state, sync(""))
68
+
69
+ expect(cleared.state).toEqual({ status: "empty", attempts: 1 })
70
+ expect(cleared.effect).toEqual({ type: "none" })
71
+ })
72
+ })
73
+
74
+ describe("typing", () => {
75
+ test("types each character behind a solid cursor, lingers with a blinking one, then hides it", () => {
76
+ const started = transitionTypewriter(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"))
77
+ expect(started.effect).toEqual({ type: "schedule", kind: "initial", attempt: 1 })
78
+ expect(view(started.state)).toEqual(["", "solid"])
79
+
80
+ const first = transitionTypewriter(started.state, step())
81
+ expect(first.effect).toEqual({ type: "schedule", kind: "step", attempt: 1 })
82
+ expect(view(first.state)).toEqual(["H", "solid"])
83
+
84
+ const typed = transitionTypewriter(first.state, step())
85
+ expect(typed.effect).toEqual({ type: "schedule", kind: "linger", attempt: 1 })
86
+ expect(view(typed.state)).toEqual(["Hi", "blinking"])
87
+
88
+ const done = transitionTypewriter(typed.state, lingerEnded())
89
+ expect(done.effect).toEqual({ type: "none" })
90
+ expect(view(done.state)).toEqual(["Hi", "none"])
91
+ })
92
+
93
+ test("the same text again changes nothing", () => {
94
+ const typing = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"), step())
95
+ expect(transitionTypewriter(typing.state, sync("Hi")).state).toBe(typing.state)
96
+
97
+ const done = run(typing.state, step(), lingerEnded())
98
+ expect(transitionTypewriter(done.state, sync("Hi")).state).toBe(done.state)
99
+ })
100
+
101
+ test("a new text restarts with a new attempt and ignores the old text's timers", () => {
102
+ const replaced = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"), step(), sync("Bye"))
103
+
104
+ expect(replaced.effect).toEqual({ type: "schedule", kind: "initial", attempt: 2 })
105
+ expect(view(replaced.state)).toEqual(["", "solid"])
106
+ expect(transitionTypewriter(replaced.state, step(1)).state).toBe(replaced.state)
107
+ expect(view(transitionTypewriter(replaced.state, step(2)).state)).toEqual(["B", "solid"])
108
+ })
109
+
110
+ test("each timeout only applies to its own phase", () => {
111
+ const typing = run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hi"))
112
+ expect(transitionTypewriter(typing.state, lingerEnded()).state).toBe(typing.state)
113
+
114
+ const lingering = run(typing.state, step(), step())
115
+ expect(transitionTypewriter(lingering.state, step()).state).toBe(lingering.state)
116
+ expect(transitionTypewriter(lingering.state, lingerEnded(2)).state).toBe(lingering.state)
117
+ })
118
+
119
+ test("never splits a surrogate pair", () => {
120
+ // The old code sliced UTF-16 units, so "A🚀B" showed a lone surrogate for one step.
121
+ const displayed = [0, 1, 2, 3].map((count) =>
122
+ selectTypewriterDisplayed(
123
+ run(INITIAL_EMPTY_TYPEWRITER_STATE, sync("A🚀B"), ...Array.from({ length: count }, () => step())).state,
124
+ ),
125
+ )
126
+
127
+ expect(displayed).toEqual(["", "A", "A🚀", "A🚀B"])
128
+ })
129
+
130
+ test("step delays stay in their bands", () => {
131
+ expect(typingStepDelayMs(0, 0)).toBe(150)
132
+ expect(typingStepDelayMs(0.1, 1)).toBe(140)
133
+ expect(typingStepDelayMs(0.5, 0.5)).toBe(55)
134
+ })
135
+ })
136
+
137
+ describe("reduced motion", () => {
138
+ test("shows the whole text at once without a cursor", () => {
139
+ const next = transitionTypewriter(INITIAL_EMPTY_TYPEWRITER_STATE, sync("Hello", true))
140
+
141
+ expect(next.state).toEqual({ status: "done", attempts: 0, text: "Hello" })
142
+ expect(next.effect).toEqual({ type: "none" })
143
+ expect(view(next.state)).toEqual(["Hello", "none"])
144
+ })
145
+
146
+ test.each([
147
+ ["typing", [sync("Hi"), step()]],
148
+ ["lingering", [sync("Hi"), step(), step()]],
149
+ ] as const)("turning it on while %s finishes the text and clears the timer", (_, events) => {
150
+ const next = transitionTypewriter(run(INITIAL_EMPTY_TYPEWRITER_STATE, ...events).state, sync("Hi", true))
151
+
152
+ expect(next.effect).toEqual({ type: "clear" })
153
+ expect(view(next.state)).toEqual(["Hi", "none"])
154
+ })
155
+ })
156
+
157
+ describe("disposal", () => {
158
+ test.each([
159
+ ["typing", [sync("Hi")], "clear"],
160
+ ["lingering", [sync("Hi"), step(), step()], "clear"],
161
+ ["empty", [], "none"],
162
+ ["done", [sync("Hi", true)], "none"],
163
+ ] as const)("disposing while %s ends every later event", (_, events, effect) => {
164
+ const disposed = transitionTypewriter(
165
+ run(INITIAL_EMPTY_TYPEWRITER_STATE, ...events).state,
166
+ DISPOSED_TYPEWRITER_EVENT,
167
+ )
168
+
169
+ expect(disposed.state).toBe(DISPOSED_TYPEWRITER_STATE)
170
+ expect(disposed.effect).toEqual({ type: effect })
171
+ ;[sync("Bye"), step(), lingerEnded(), DISPOSED_TYPEWRITER_EVENT].forEach((event) => {
172
+ const next = transitionTypewriter(disposed.state, event)
173
+ expect(next.state).toBe(disposed.state)
174
+ expect(next.effect).toEqual({ type: "none" })
175
+ })
176
+ })
177
+ })
@@ -0,0 +1,51 @@
1
+ import { Typewriter } from "./typewriter"
2
+
3
+ const docs = `### Overview
4
+ Animated typewriter text effect for short inline messages.
5
+
6
+ Use for short status lines; avoid long paragraphs.
7
+
8
+ ### API
9
+ - Optional: \`text\` string; if absent, nothing is rendered.
10
+ - Optional: \`as\` to change the rendered element.
11
+
12
+ ### Variants and states
13
+ - Single animation style with cursor blink.
14
+
15
+ ### Behavior
16
+ - Types one character at a time with randomized delays.
17
+
18
+ ### Accessibility
19
+ - TODO: confirm if cursor should be aria-hidden in all contexts.
20
+
21
+ ### Theming/tokens
22
+ - Uses \`blinking-cursor\` class for cursor styling.
23
+
24
+ `
25
+
26
+ export default {
27
+ title: "UI/Typewriter",
28
+ id: "components-typewriter",
29
+ component: Typewriter,
30
+ args: { text: "Typewriter text", as: "p" },
31
+ argTypes: { as: { control: "select", options: ["p", "span"] } },
32
+ tags: ["autodocs"],
33
+ parameters: {
34
+ docs: {
35
+ description: {
36
+ component: docs,
37
+ },
38
+ },
39
+ },
40
+ }
41
+
42
+ export const Basic = {}
43
+
44
+ export const Inline = {
45
+ args: {
46
+ text: "Inline typewriter",
47
+ as: "span",
48
+ },
49
+ }
50
+
51
+ export const Empty = { args: { text: "" } }
@@ -0,0 +1,295 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import {
3
+ createDialogStackState,
4
+ selectLayerClosing,
5
+ selectLayerIndex,
6
+ selectLayerModal,
7
+ selectTopDialog,
8
+ transitionDialogStack,
9
+ type DialogStackEffect,
10
+ type DialogStackEvent,
11
+ type DialogStackState,
12
+ } from "./dialog-machine"
13
+
14
+ type Event = DialogStackEvent<string, string>
15
+
16
+ // Focus is where it was when the open was asked for; the request names the content the driver would mount.
17
+ const open = (mode: "show" | "push", id: string, aborted = false): Event => ({
18
+ type: "open",
19
+ mode,
20
+ id,
21
+ aborted,
22
+ focus: `before ${id}`,
23
+ request: id,
24
+ })
25
+
26
+ const push = (id: string) => open("push", id)
27
+
28
+ const show = (id: string) => open("show", id)
29
+
30
+ function run(state: DialogStackState<string>, ...events: Event[]) {
31
+ return events.reduce<{ state: DialogStackState<string>; effects: readonly DialogStackEffect<string, string>[] }>(
32
+ (result, event) => transitionDialogStack(result.state, event),
33
+ { state, effects: [] },
34
+ )
35
+ }
36
+
37
+ function closed(attempt: number, delayMs: number, focus?: string): DialogStackEffect<string, string>[] {
38
+ return [
39
+ { type: "timer.start", attempt, delayMs },
40
+ { type: "notify.close", attempt },
41
+ ...(focus ? [{ type: "focus.return" as const, attempt, focus }] : []),
42
+ ]
43
+ }
44
+
45
+ const statuses = (state: DialogStackState<string>) =>
46
+ state.status === "active" ? state.entries.map((entry) => `${entry.id}:${entry.status}`) : []
47
+
48
+ describe("closing a lower dialog by id", () => {
49
+ // A at the bottom plays the image preview's 960 ms flight out; B on top must close at once, however it is closed.
50
+ const below = run(
51
+ createDialogStackState(),
52
+ push("a"),
53
+ { type: "layer.exit-duration", attempt: 1, exitMs: 960 },
54
+ push("b"),
55
+ { type: "close", id: "a" },
56
+ )
57
+
58
+ test("starts only that dialog's exit and leaves focus with the top dialog", () => {
59
+ expect(below.effects).toEqual(closed(1, 960))
60
+ expect(statuses(below.state)).toEqual(["a:closing", "b:open"])
61
+ expect(selectTopDialog(below.state)?.id).toBe("b")
62
+ })
63
+
64
+ test.each<[string, Event]>([
65
+ ["Escape", { type: "dismiss" }],
66
+ ["a backdrop click", { type: "backdrop.click", attempt: 2 }],
67
+ ["its close button", { type: "layer.dismiss", attempt: 2 }],
68
+ ["an un-keyed close", { type: "close" }],
69
+ ["a dismissal by id", { type: "dismiss", id: "b" }],
70
+ ])("does not block %s on the top dialog", (_, event) => {
71
+ const next = transitionDialogStack(below.state, event)
72
+
73
+ expect(next.effects).toEqual(closed(2, 100, "before b"))
74
+ expect(statuses(next.state)).toEqual(["a:closing", "b:closing"])
75
+ })
76
+ })
77
+
78
+ describe("while the top dialog plays its exit animation", () => {
79
+ const exiting = run(createDialogStackState(), push("a"), push("b"), { type: "dismiss" })
80
+
81
+ test("the dialog below is the top dialog and owns the focus trap", () => {
82
+ expect(selectTopDialog(exiting.state)?.id).toBe("a")
83
+ expect(selectLayerModal(exiting.state, 1, false)).toBe(true)
84
+ expect(selectLayerModal(exiting.state, 2, false)).toBe(false)
85
+ expect(selectLayerModal(exiting.state, 1, true)).toBe(false)
86
+ })
87
+
88
+ test.each<[string, Event]>([
89
+ ["its close button", { type: "layer.dismiss", attempt: 2 }],
90
+ ["its backdrop", { type: "backdrop.click", attempt: 2 }],
91
+ ["a dismissal by its id", { type: "dismiss", id: "b" }],
92
+ ["a second un-keyed close", { type: "close" }],
93
+ ["a second close from inside it", { type: "close", layer: 2 }],
94
+ ])("%s does nothing more", (_, event) => {
95
+ const next = transitionDialogStack(exiting.state, event)
96
+
97
+ expect(next.state).toBe(exiting.state)
98
+ expect(next.effects).toEqual([])
99
+ })
100
+
101
+ test.each<[string, Event]>([
102
+ ["Escape", { type: "dismiss" }],
103
+ ["a dismissal by its id", { type: "dismiss", id: "a" }],
104
+ ["its close button", { type: "layer.dismiss", attempt: 1 }],
105
+ ["its backdrop", { type: "backdrop.click", attempt: 1 }],
106
+ ["a close by its id", { type: "close", id: "a" }],
107
+ ["its own Cancel (a close from inside it)", { type: "close", layer: 1 }],
108
+ ])("%s closes the dialog below at once", (_, event) => {
109
+ const next = transitionDialogStack(exiting.state, event)
110
+
111
+ expect(next.effects).toEqual(closed(1, 100, "before a"))
112
+ expect(statuses(next.state)).toEqual(["a:closing", "b:closing"])
113
+ })
114
+
115
+ test("an un-keyed close from outside every dialog waits for the exit, then closes the dialog below", () => {
116
+ const next = run(exiting.state, { type: "exit.completed", attempt: 2 }, { type: "close" })
117
+
118
+ expect(next.effects).toEqual(closed(1, 100, "before a"))
119
+ })
120
+ })
121
+
122
+ describe("a close without an id from inside a dialog", () => {
123
+ const stacked = run(createDialogStackState(), push("a"), push("b"))
124
+
125
+ test("closes that dialog, not the one on top, and leaves focus with the top dialog", () => {
126
+ const next = transitionDialogStack(stacked.state, { type: "close", layer: 1 })
127
+
128
+ expect(next.effects).toEqual(closed(1, 100))
129
+ expect(statuses(next.state)).toEqual(["a:closing", "b:open"])
130
+ })
131
+
132
+ test("closes only itself on a double click, never the dialog below", () => {
133
+ const first = transitionDialogStack(stacked.state, { type: "close", layer: 2 })
134
+ const second = transitionDialogStack(first.state, { type: "close", layer: 2 })
135
+
136
+ expect(first.effects).toEqual(closed(2, 100, "before b"))
137
+ expect(second.state).toBe(first.state)
138
+ expect(second.effects).toEqual([])
139
+ })
140
+
141
+ test("from a dialog a show replaced, closes the replacement, along a chain of replacements", () => {
142
+ const replaced = run(stacked.state, show("c"))
143
+
144
+ expect(transitionDialogStack(replaced.state, { type: "close", layer: 1 }).effects).toEqual(closed(3, 100, "before a"))
145
+
146
+ const again = run(replaced.state, show("d"))
147
+
148
+ expect(transitionDialogStack(again.state, { type: "close", layer: 1 }).effects).toEqual(closed(4, 100, "before a"))
149
+ expect(transitionDialogStack(again.state, { type: "close", layer: 3 }).effects).toEqual(closed(4, 100, "before a"))
150
+ })
151
+
152
+ test.each<[string, Event[]]>([
153
+ // Escape closed it, its exit ended, and another dialog opened before its async work called close().
154
+ ["closed and gone", [push("a"), { type: "dismiss" }, { type: "exit.completed", attempt: 1 }, push("b")]],
155
+ ["closed, then torn down by a show", [push("a"), push("x"), { type: "close", id: "a" }, show("b")]],
156
+ ])("from a dialog already %s, closes nothing", (_, events) => {
157
+ const before = run(createDialogStackState(), ...events)
158
+ const next = transitionDialogStack(before.state, { type: "close", layer: 1 })
159
+
160
+ expect(next.state).toBe(before.state)
161
+ expect(next.effects).toEqual([])
162
+ })
163
+ })
164
+
165
+ test("a layer reads closing as soon as its close starts, before its exit animation ends", () => {
166
+ const opened = run(createDialogStackState(), push("a"))
167
+ const closing = run(opened.state, { type: "dismiss" })
168
+
169
+ expect(selectLayerClosing(opened.state, 1)).toBe(false)
170
+ expect(selectLayerClosing(closing.state, 1)).toBe(true)
171
+ // Escape stops being captured, though the dialog stays mounted for its exit animation.
172
+ expect(selectTopDialog(closing.state)).toBeUndefined()
173
+ expect(statuses(closing.state)).toEqual(["a:closing"])
174
+
175
+ const gone = run(closing.state, { type: "exit.completed", attempt: 1 })
176
+
177
+ expect(gone.effects).toEqual([{ type: "dispose", attempt: 1, notify: false }])
178
+ expect(statuses(gone.state)).toEqual([])
179
+ })
180
+
181
+ test("an id reused while its earlier dialog exits names only the new dialog", () => {
182
+ const reused = run(createDialogStackState(), push("x"), { type: "close", id: "x" }, push("x"))
183
+
184
+ expect(selectTopDialog(reused.state)?.attempt).toBe(2)
185
+ expect(selectLayerClosing(reused.state, 1)).toBe(true)
186
+ expect(selectLayerClosing(reused.state, 2)).toBe(false)
187
+
188
+ const exited = run(reused.state, { type: "exit.completed", attempt: 1 })
189
+
190
+ expect(exited.effects).toEqual([{ type: "dispose", attempt: 1, notify: false }])
191
+ expect(statuses(exited.state)).toEqual(["x:open"])
192
+ expect(selectLayerIndex(exited.state, 2)).toBe(0)
193
+ expect(run(exited.state, { type: "close", id: "x" }).effects).toEqual(closed(2, 100, "before x"))
194
+ })
195
+
196
+ describe("a dialog that opts out of backdrop dismissal", () => {
197
+ const kept = run(createDialogStackState(), push("a"), { type: "layer.backdrop-dismiss", attempt: 1, enabled: false })
198
+
199
+ test("stays open on a backdrop click", () => {
200
+ const next = transitionDialogStack(kept.state, { type: "backdrop.click", attempt: 1 })
201
+
202
+ expect(next.state).toBe(kept.state)
203
+ expect(next.effects).toEqual([])
204
+ })
205
+
206
+ test.each<[string, Event]>([
207
+ ["Escape", { type: "dismiss" }],
208
+ ["its close button", { type: "layer.dismiss", attempt: 1 }],
209
+ ["an un-keyed close", { type: "close" }],
210
+ ])("still closes on %s", (_, event) => {
211
+ expect(transitionDialogStack(kept.state, event).effects).toEqual(closed(1, 100, "before a"))
212
+ })
213
+
214
+ test("an unchanged setting writes nothing", () => {
215
+ const next = transitionDialogStack(kept.state, { type: "layer.backdrop-dismiss", attempt: 1, enabled: false })
216
+
217
+ expect(next.state).toBe(kept.state)
218
+ })
219
+ })
220
+
221
+ test("a replacing dialog returns focus to the origin and never notifies a closing dialog again", () => {
222
+ const before = run(createDialogStackState(), push("a"), push("b"), { type: "close", id: "b" })
223
+ const next = transitionDialogStack(before.state, show("c"))
224
+
225
+ expect(next.effects).toEqual([
226
+ { type: "dispose", attempt: 1, notify: true },
227
+ { type: "timer.stop", attempt: 2 },
228
+ { type: "dispose", attempt: 2, notify: false },
229
+ { type: "mount", attempt: 3, id: "c", request: "c" },
230
+ ])
231
+ expect(statuses(next.state)).toEqual(["c:open"])
232
+ expect(run(next.state, { type: "dismiss" }).effects).toEqual(closed(3, 100, "before a"))
233
+ })
234
+
235
+ test("an open whose signal aborted neither mounts nor replaces, and its caller hears onClose", () => {
236
+ const before = run(createDialogStackState(), push("a"))
237
+ const next = transitionDialogStack(before.state, open("show", "b", true))
238
+
239
+ expect(next.state).toBe(before.state)
240
+ expect(next.effects).toEqual([{ type: "discard", request: "b", notify: true }])
241
+ })
242
+
243
+ describe("exit timers and disposal", () => {
244
+ test.each<[string, Event[]]>([
245
+ ["an open dialog", [push("a")]],
246
+ ["a dialog already gone", [push("a"), { type: "dismiss" }, { type: "exit.completed", attempt: 1 }]],
247
+ ["a replaced dialog", [push("a"), { type: "dismiss" }, show("b")]],
248
+ ])("ignore a late exit for %s", (_, events) => {
249
+ const before = run(createDialogStackState(), ...events)
250
+ const next = transitionDialogStack(before.state, { type: "exit.completed", attempt: 1 })
251
+
252
+ expect(next.state).toBe(before.state)
253
+ expect(next.effects).toEqual([])
254
+ })
255
+
256
+ test("a longer exit asked for after the close starts does not change the running timer", () => {
257
+ const before = run(createDialogStackState(), push("a"), { type: "dismiss" })
258
+ const next = transitionDialogStack(before.state, { type: "layer.exit-duration", attempt: 1, exitMs: 960 })
259
+
260
+ expect(next.state).toBe(before.state)
261
+ })
262
+
263
+ test("the provider going away stops exit timers, disposes every dialog silently and ignores what follows", () => {
264
+ const before = run(createDialogStackState(), push("a"), push("b"), { type: "dismiss" })
265
+ const gone = transitionDialogStack(before.state, { type: "disposed" })
266
+
267
+ expect(gone.effects).toEqual([
268
+ { type: "dispose", attempt: 1, notify: false },
269
+ { type: "timer.stop", attempt: 2 },
270
+ { type: "dispose", attempt: 2, notify: false },
271
+ ])
272
+ expect(selectTopDialog(gone.state)).toBeUndefined()
273
+ expect(selectLayerClosing(gone.state, 1)).toBe(true)
274
+
275
+ const late = [
276
+ { type: "dismiss" },
277
+ { type: "close" },
278
+ { type: "exit.completed", attempt: 2 },
279
+ { type: "disposed" },
280
+ ] satisfies Event[]
281
+
282
+ late.forEach((event) => {
283
+ const next = transitionDialogStack(gone.state, event)
284
+
285
+ expect(next.state).toBe(gone.state)
286
+ expect(next.effects).toEqual([])
287
+ })
288
+ expect(transitionDialogStack(gone.state, push("c")).effects).toEqual([
289
+ { type: "discard", request: "c", notify: false },
290
+ ])
291
+ expect(transitionDialogStack(gone.state, open("push", "c", true)).effects).toEqual([
292
+ { type: "discard", request: "c", notify: true },
293
+ ])
294
+ })
295
+ })
@@ -0,0 +1,89 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import { createUiI18n, localizedListSeparator, pluralCategory, resolveTemplate, type UiI18nSource } from "./i18n"
3
+
4
+ const i18n = (locale: string, translated: string) => {
5
+ const source: UiI18nSource = {
6
+ locale: () => locale,
7
+ t: () => translated,
8
+ plural: () => "",
9
+ }
10
+
11
+ return createUiI18n(source)
12
+ }
13
+
14
+ describe("pluralCategory", () => {
15
+ test.each([
16
+ ["en", 0, "other"],
17
+ ["en", 1, "one"],
18
+ ["fr", 0, "one"],
19
+ ["fr", 1_000_000, "many"],
20
+ ["ru", 1, "one"],
21
+ ["ru", 2, "few"],
22
+ ["ru", 5, "many"],
23
+ ["ru", 21, "one"],
24
+ ["ar", 0, "zero"],
25
+ ["ar", 1, "one"],
26
+ ["ar", 2, "two"],
27
+ ["ar", 3, "few"],
28
+ ["ar", 11, "many"],
29
+ ["ar", 100, "other"],
30
+ ["ja", 1, "other"],
31
+ ] as const)("selects %s for %d as %s", (locale, count, expected) => {
32
+ expect(pluralCategory(locale, count)).toBe(expected)
33
+ })
34
+ })
35
+
36
+ describe("dynamic source copy", () => {
37
+ test("keeps runtime copy for English locale tags", () => {
38
+ expect(
39
+ i18n("en-US", "Dictionary copy").tDynamic("dialog.usageExceeded.freeTier.title", "Runtime {{name}}", {
40
+ name: "copy",
41
+ }),
42
+ ).toBe("Runtime copy")
43
+ })
44
+
45
+ test("uses dictionary copy for non-English locales", () => {
46
+ expect(i18n("fr", "Texte traduit").tDynamic("dialog.usageExceeded.freeTier.title", "Runtime copy")).toBe(
47
+ "Texte traduit",
48
+ )
49
+ })
50
+ })
51
+
52
+ describe("localizedListSeparator", () => {
53
+ test("uses locale list punctuation and conjunctions", () => {
54
+ expect(localizedListSeparator("en", 1, 3)).toBe(", ")
55
+ expect(localizedListSeparator("en", 2, 3)).toBe(", and ")
56
+ expect(localizedListSeparator("de", 1, 2)).toBe(" und ")
57
+ })
58
+
59
+ test("formats a complete localized list", () => {
60
+ expect(i18n("en", "").list(["Read", "Search", "List"])).toBe("Read, Search, List")
61
+ expect(i18n("en-US", "").list(["Read", "Search"])).toBe("Read, Search")
62
+ expect(i18n("en", "").listSeparator(2, 3)).toBe(",")
63
+ expect(i18n("de", "").list(["Lesen", "Suchen"])).toBe("Lesen und Suchen")
64
+ })
65
+ })
66
+
67
+ describe("resolveTemplate", () => {
68
+ test("fills params with optional inner spacing and repeats", () => {
69
+ expect(resolveTemplate("Hello {{name}}, {{ name }} and {{ other }}", { name: "Ada", other: 2 })).toBe(
70
+ "Hello Ada, Ada and 2",
71
+ )
72
+ })
73
+
74
+ test("stringifies numbers and booleans, including zero and false", () => {
75
+ expect(resolveTemplate("{{count}} {{done}}", { count: 0, done: false })).toBe("0 false")
76
+ })
77
+
78
+ test("returns the text untouched without params", () => {
79
+ expect(resolveTemplate("Hello {{name}}")).toBe("Hello {{name}}")
80
+ })
81
+
82
+ test("does not treat $ in a value as a replacement pattern", () => {
83
+ expect(resolveTemplate("file {{name}}", { name: "a$&b$1" })).toBe("file a$&b$1")
84
+ })
85
+
86
+ test("blanks a placeholder whose param is missing", () => {
87
+ expect(resolveTemplate("a {{missing}} b", { other: "x" })).toBe("a b")
88
+ })
89
+ })