@noya-app/noya-designsystem 0.1.83 → 0.1.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noya-app/noya-designsystem",
3
- "version": "0.1.83",
3
+ "version": "0.1.84",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -231,20 +231,27 @@ export const DialogProvider = function DialogProvider({
231
231
  const confirmButtonRef = useRef<HTMLButtonElement>(null);
232
232
  const dialogRef = useRef<IDialog>(null);
233
233
 
234
- // Handle auto-focus when dialog opens
234
+ // Handle auto-focus when dialog opens. The `resolve` function is created
235
+ // once per dialog instance and preserved across state updates (e.g. typing
236
+ // in the input replaces `contents` but keeps the same `resolve`), so keying
237
+ // the effect on it ensures focus is only set when a dialog opens, not on
238
+ // every keystroke.
239
+ const contentsType = contents?.type;
240
+ const contentsInstance = contents?.resolve;
241
+
235
242
  React.useEffect(() => {
236
- if (!contents) return;
243
+ if (!contentsType || !contentsInstance) return;
237
244
 
238
245
  // Use requestAnimationFrame to ensure the dialog is rendered
239
246
  requestAnimationFrame(() => {
240
- if (contents.type === "input") {
247
+ if (contentsType === "input") {
241
248
  inputRef.current?.focus();
242
249
  inputRef.current?.setSelectionRange(0, inputRef.current.value.length);
243
- } else if (contents.type === "confirmation") {
250
+ } else if (contentsType === "confirmation") {
244
251
  confirmButtonRef.current?.focus();
245
252
  }
246
253
  });
247
- }, [contents]);
254
+ }, [contentsType, contentsInstance]);
248
255
 
249
256
  const containsElement = useCallback((element: HTMLElement) => {
250
257
  if (!dialogRef.current) return false;
@@ -0,0 +1,142 @@
1
+ import { afterEach, describe, expect, mock, test } from "bun:test";
2
+ import React from "react";
3
+
4
+ // `@radix-ui/react-icons` has a nested React 18 copy in node_modules, which
5
+ // creates legacy React elements that React 19 refuses to render under
6
+ // `bun test` (app bundlers alias react, so this only affects tests). Mock the
7
+ // icon used by the Dialog close button so everything renders with one React.
8
+ const actualIcons = await import("@radix-ui/react-icons");
9
+
10
+ mock.module("@radix-ui/react-icons", () => ({
11
+ ...actualIcons,
12
+ Cross1Icon: (props: React.SVGProps<SVGSVGElement>) => (
13
+ <svg {...props} data-testid="cross-icon" />
14
+ ),
15
+ }));
16
+
17
+ const { cleanup, fireEvent, render, waitFor } = await import(
18
+ "@testing-library/react"
19
+ );
20
+ const { DialogProvider, useOpenInputDialog } = await import(
21
+ "../DialogContext"
22
+ );
23
+
24
+ const focusListeners: ((event: Event) => void)[] = [];
25
+
26
+ afterEach(() => {
27
+ cleanup();
28
+
29
+ for (const listener of focusListeners) {
30
+ document.removeEventListener("focus", listener, true);
31
+ }
32
+ focusListeners.length = 0;
33
+ });
34
+
35
+ function flushAnimationFrames() {
36
+ return new Promise<void>((resolve) => {
37
+ requestAnimationFrame(() => {
38
+ requestAnimationFrame(() => resolve());
39
+ });
40
+ });
41
+ }
42
+
43
+ function OpenDialogButton({
44
+ onResult,
45
+ }: {
46
+ onResult: (value: string | undefined) => void;
47
+ }) {
48
+ const openInputDialog = useOpenInputDialog();
49
+
50
+ return (
51
+ <button
52
+ onClick={async () => {
53
+ const result = await openInputDialog({
54
+ title: "Rename",
55
+ initialValue: "hello",
56
+ });
57
+ onResult(result);
58
+ }}
59
+ >
60
+ Open
61
+ </button>
62
+ );
63
+ }
64
+
65
+ async function openInputDialogForTest(
66
+ onResult: (value: string | undefined) => void = () => {}
67
+ ) {
68
+ const utils = render(
69
+ <DialogProvider>
70
+ <OpenDialogButton onResult={onResult} />
71
+ </DialogProvider>
72
+ );
73
+
74
+ // Count focus events on the dialog's input, attached before the dialog
75
+ // opens so the initial auto-focus is captured
76
+ let focusCount = 0;
77
+ const handleFocus = (event: Event) => {
78
+ if ((event.target as HTMLElement).tagName === "INPUT") {
79
+ focusCount++;
80
+ }
81
+ };
82
+ document.addEventListener("focus", handleFocus, true);
83
+ focusListeners.push(handleFocus);
84
+
85
+ fireEvent.click(utils.getByText("Open"));
86
+
87
+ const input = await waitFor(() => {
88
+ const element = document.querySelector<HTMLInputElement>(
89
+ 'input[type="text"], input:not([type])'
90
+ );
91
+ if (!element) throw new Error("Input not found");
92
+ return element;
93
+ });
94
+
95
+ await flushAnimationFrames();
96
+
97
+ return { ...utils, input, getFocusCount: () => focusCount };
98
+ }
99
+
100
+ describe("DialogProvider input dialog", () => {
101
+ test("focuses and selects the initial value when opened", async () => {
102
+ const { input, getFocusCount } = await openInputDialogForTest();
103
+
104
+ expect(getFocusCount()).toBeGreaterThanOrEqual(1);
105
+ expect(input.selectionStart).toEqual(0);
106
+ expect(input.selectionEnd).toEqual("hello".length);
107
+ });
108
+
109
+ test("does not re-focus or re-select on keystrokes", async () => {
110
+ const { input, getFocusCount } = await openInputDialogForTest();
111
+
112
+ const focusCountAfterOpen = getFocusCount();
113
+
114
+ fireEvent.change(input, { target: { value: "hello w" } });
115
+
116
+ // Simulate the caret position after typing (collapsed at the end)
117
+ input.setSelectionRange(input.value.length, input.value.length);
118
+
119
+ await flushAnimationFrames();
120
+
121
+ expect(input.value).toEqual("hello w");
122
+ // The focus side effect must not run again on a keystroke
123
+ expect(getFocusCount()).toEqual(focusCountAfterOpen);
124
+ // Selection must not be reset to cover the whole value
125
+ expect(input.selectionStart).toEqual(input.value.length);
126
+ expect(input.selectionEnd).toEqual(input.value.length);
127
+ });
128
+
129
+ test("submits the typed value on Enter", async () => {
130
+ let result: string | undefined;
131
+ const { input } = await openInputDialogForTest((value) => {
132
+ result = value;
133
+ });
134
+
135
+ fireEvent.change(input, { target: { value: "world" } });
136
+ fireEvent.keyDown(input, { key: "Enter" });
137
+
138
+ await waitFor(() => {
139
+ expect(result).toEqual("world");
140
+ });
141
+ });
142
+ });