@open-elements/ui 0.6.0 → 0.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-elements/ui",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Shared UI component library for Open Elements applications",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -0,0 +1,98 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { render, screen, cleanup, fireEvent } from "@testing-library/react";
3
+ import { TranslateButton } from "../translate-button.tsx";
4
+ import { LanguageProvider } from "../../i18n/language-context.tsx";
5
+ import { TooltipProvider } from "../tooltip.tsx";
6
+
7
+ afterEach(() => {
8
+ cleanup();
9
+ vi.restoreAllMocks();
10
+ });
11
+
12
+ const T = {
13
+ button: "Translate",
14
+ dialog: {
15
+ title: "Translation",
16
+ loading: "Translating…",
17
+ error: "Translation failed",
18
+ copy: "Copy",
19
+ copied: "Copied!",
20
+ close: "Close",
21
+ },
22
+ } as const;
23
+
24
+ const dummyTranslations = { de: {}, en: {} } as const;
25
+
26
+ function renderButton(props: {
27
+ readonly text: string | null | undefined;
28
+ readonly configured: boolean | null;
29
+ readonly size?: "sm" | "md";
30
+ }) {
31
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello" });
32
+ const utils = render(
33
+ <LanguageProvider translations={dummyTranslations} defaultLanguage="en">
34
+ <TooltipProvider>
35
+ <TranslateButton
36
+ text={props.text}
37
+ size={props.size}
38
+ configured={props.configured}
39
+ onTranslate={onTranslate}
40
+ translations={T}
41
+ />
42
+ </TooltipProvider>
43
+ </LanguageProvider>,
44
+ );
45
+ return { ...utils, onTranslate };
46
+ }
47
+
48
+ describe("TranslateButton", () => {
49
+ it("renders null when text is empty", () => {
50
+ renderButton({ text: "", configured: true });
51
+ expect(screen.queryByTestId("translate-button")).toBeNull();
52
+ });
53
+
54
+ it("renders null when text is whitespace", () => {
55
+ renderButton({ text: " ", configured: true });
56
+ expect(screen.queryByTestId("translate-button")).toBeNull();
57
+ });
58
+
59
+ it("renders null when text is null", () => {
60
+ renderButton({ text: null, configured: true });
61
+ expect(screen.queryByTestId("translate-button")).toBeNull();
62
+ });
63
+
64
+ it("renders null while configured is still null (probe in flight)", () => {
65
+ renderButton({ text: "something", configured: null });
66
+ expect(screen.queryByTestId("translate-button")).toBeNull();
67
+ });
68
+
69
+ it("renders null when configured is false", () => {
70
+ renderButton({ text: "something", configured: false });
71
+ expect(screen.queryByTestId("translate-button")).toBeNull();
72
+ });
73
+
74
+ it("renders the button when text is non-empty and configured is true", () => {
75
+ renderButton({ text: "something", configured: true });
76
+ const button = screen.getByTestId("translate-button");
77
+ expect(button).toBeInTheDocument();
78
+ expect(button).toHaveAttribute("aria-label", T.button);
79
+ });
80
+
81
+ it("uses size='sm' classes when size is 'sm'", () => {
82
+ renderButton({ text: "x", configured: true, size: "sm" });
83
+ const icon = screen.getByTestId("translate-button").querySelector("svg");
84
+ expect(icon).toHaveClass("h-3.5", "w-3.5");
85
+ });
86
+
87
+ it("uses size='md' (default) classes when size is omitted", () => {
88
+ renderButton({ text: "x", configured: true });
89
+ const icon = screen.getByTestId("translate-button").querySelector("svg");
90
+ expect(icon).toHaveClass("h-4", "w-4");
91
+ });
92
+
93
+ it("opens the dialog on click with sourceText equal to the button text", () => {
94
+ const { onTranslate } = renderButton({ text: "Hallo", configured: true });
95
+ fireEvent.click(screen.getByTestId("translate-button"));
96
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "en");
97
+ });
98
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { render, screen, cleanup, waitFor, fireEvent, act } from "@testing-library/react";
3
+ import { TranslateDialog, type TranslateResult } from "../translate-dialog.tsx";
4
+ import { LanguageProvider } from "../../i18n/language-context.tsx";
5
+
6
+ afterEach(() => {
7
+ cleanup();
8
+ vi.restoreAllMocks();
9
+ });
10
+
11
+ const T = {
12
+ title: "Translation",
13
+ loading: "Translating…",
14
+ error: "Translation failed",
15
+ copy: "Copy",
16
+ copied: "Copied!",
17
+ close: "Close",
18
+ } as const;
19
+
20
+ const dummyTranslations = { de: {}, en: {} } as const;
21
+
22
+ function renderDialog(props: {
23
+ readonly open: boolean;
24
+ readonly sourceText?: string;
25
+ readonly onTranslate?: (text: string, lang: string) => Promise<TranslateResult>;
26
+ readonly onOpenChange?: (open: boolean) => void;
27
+ readonly language?: "de" | "en";
28
+ }) {
29
+ const onTranslate = props.onTranslate ?? vi.fn().mockResolvedValue({ translatedText: "Hello" });
30
+ const onOpenChange = props.onOpenChange ?? vi.fn();
31
+ const utils = render(
32
+ <LanguageProvider translations={dummyTranslations} defaultLanguage={props.language ?? "en"}>
33
+ <TranslateDialog
34
+ open={props.open}
35
+ onOpenChange={onOpenChange}
36
+ sourceText={props.sourceText ?? "Hallo"}
37
+ onTranslate={onTranslate}
38
+ translations={T}
39
+ />
40
+ </LanguageProvider>,
41
+ );
42
+ return { ...utils, onTranslate, onOpenChange };
43
+ }
44
+
45
+ describe("TranslateDialog", () => {
46
+ it("calls onTranslate once with the current language and renders the result", async () => {
47
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello world" });
48
+ renderDialog({ open: true, sourceText: "Hallo Welt", onTranslate, language: "en" });
49
+ expect(onTranslate).toHaveBeenCalledTimes(1);
50
+ expect(onTranslate).toHaveBeenCalledWith("Hallo Welt", "en");
51
+ expect(screen.getByTestId("translate-dialog-loading")).toBeInTheDocument();
52
+ await waitFor(() => {
53
+ expect(screen.getByTestId("translate-dialog-result")).toHaveTextContent("Hello world");
54
+ });
55
+ });
56
+
57
+ it("uses 'de' as target when the language provider is set to de", () => {
58
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "x" });
59
+ renderDialog({ open: true, onTranslate, language: "de" });
60
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "de");
61
+ });
62
+
63
+ it("uses 'en' as target for any non-de language", () => {
64
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "x" });
65
+ renderDialog({ open: true, onTranslate, language: "en" });
66
+ expect(onTranslate).toHaveBeenCalledWith("Hallo", "en");
67
+ });
68
+
69
+ it("renders the error message when onTranslate rejects", async () => {
70
+ const onTranslate = vi.fn().mockRejectedValue(new Error("boom"));
71
+ renderDialog({ open: true, onTranslate });
72
+ await waitFor(() => {
73
+ expect(screen.getByTestId("translate-dialog-error")).toHaveTextContent(T.error);
74
+ });
75
+ expect(screen.getByTestId("translate-dialog-copy")).toBeDisabled();
76
+ });
77
+
78
+ it("does not call onTranslate when open is false", () => {
79
+ const onTranslate = vi.fn();
80
+ renderDialog({ open: false, onTranslate });
81
+ expect(onTranslate).not.toHaveBeenCalled();
82
+ expect(screen.queryByTestId("translate-dialog-loading")).toBeNull();
83
+ });
84
+
85
+ it("copies to clipboard and flips the label to 'copied'", async () => {
86
+ const writeText = vi.fn().mockResolvedValue(undefined);
87
+ Object.defineProperty(navigator, "clipboard", {
88
+ configurable: true,
89
+ value: { writeText },
90
+ });
91
+ const onTranslate = vi.fn().mockResolvedValue({ translatedText: "Hello" });
92
+ renderDialog({ open: true, onTranslate });
93
+
94
+ await waitFor(() => {
95
+ expect(screen.getByTestId("translate-dialog-result")).toBeInTheDocument();
96
+ });
97
+
98
+ const copyBtn = screen.getByTestId("translate-dialog-copy");
99
+ await act(async () => {
100
+ fireEvent.click(copyBtn);
101
+ });
102
+ expect(writeText).toHaveBeenCalledWith("Hello");
103
+ await waitFor(() => {
104
+ expect(copyBtn).toHaveTextContent(T.copied);
105
+ });
106
+ });
107
+ });
@@ -243,7 +243,7 @@ function ComboboxChip({
243
243
  );
244
244
  }
245
245
 
246
- function ComboboxChipsInput({ className, children, ...props }: ComboboxPrimitive.Input.Props) {
246
+ function ComboboxChipsInput({ className, ...props }: ComboboxPrimitive.Input.Props) {
247
247
  return (
248
248
  <ComboboxPrimitive.Input
249
249
  data-slot="combobox-chip-input"
@@ -0,0 +1,82 @@
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { Languages } from "lucide-react";
5
+ import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip.tsx";
6
+ import {
7
+ TranslateDialog,
8
+ type TranslateDialogTranslations,
9
+ type TranslateResult,
10
+ } from "./translate-dialog.tsx";
11
+
12
+ export interface TranslateButtonTranslations {
13
+ /** Tooltip / aria-label for the button. */
14
+ readonly button: string;
15
+ readonly dialog: TranslateDialogTranslations;
16
+ }
17
+
18
+ export interface TranslateButtonProps {
19
+ readonly text: string | null | undefined;
20
+ /** Visual size of the icon — "sm" (h-3.5 w-3.5), "md" (h-4 w-4, default). */
21
+ readonly size?: "sm" | "md";
22
+ /**
23
+ * Whether the backend translation feature is configured. `null` means the
24
+ * config probe is still in flight — the button hides until the answer is
25
+ * known to avoid a flash. The app owns this concern (it hits its own
26
+ * feature-toggle endpoint) and passes the resolved value as a prop.
27
+ */
28
+ readonly configured: boolean | null;
29
+ readonly onTranslate: (text: string, targetLanguage: string) => Promise<TranslateResult>;
30
+ readonly translations: TranslateButtonTranslations;
31
+ }
32
+
33
+ /**
34
+ * Translate icon button next to a piece of text. Returns `null` when the
35
+ * text is empty / whitespace / null, while the configuration probe is still
36
+ * in flight (`configured === null`), or when the backend feature is not
37
+ * configured. Clicking opens a {@link TranslateDialog} with the given text.
38
+ */
39
+ export function TranslateButton({
40
+ text,
41
+ size = "md",
42
+ configured,
43
+ onTranslate,
44
+ translations: T,
45
+ }: TranslateButtonProps) {
46
+ const [open, setOpen] = useState(false);
47
+
48
+ if (!text || text.trim().length === 0) {
49
+ return null;
50
+ }
51
+ if (configured !== true) {
52
+ return null;
53
+ }
54
+
55
+ const iconClass = size === "sm" ? "h-3.5 w-3.5" : "h-4 w-4";
56
+
57
+ return (
58
+ <>
59
+ <Tooltip>
60
+ <TooltipTrigger asChild>
61
+ <button
62
+ type="button"
63
+ onClick={() => setOpen(true)}
64
+ className="text-oe-gray-light hover:text-oe-dark shrink-0 ml-2"
65
+ aria-label={T.button}
66
+ data-testid="translate-button"
67
+ >
68
+ <Languages className={iconClass} />
69
+ </button>
70
+ </TooltipTrigger>
71
+ <TooltipContent>{T.button}</TooltipContent>
72
+ </Tooltip>
73
+ <TranslateDialog
74
+ open={open}
75
+ onOpenChange={setOpen}
76
+ sourceText={text}
77
+ onTranslate={onTranslate}
78
+ translations={T.dialog}
79
+ />
80
+ </>
81
+ );
82
+ }
@@ -0,0 +1,148 @@
1
+ "use client";
2
+
3
+ import { useEffect, useState } from "react";
4
+ import { Check, Copy, Loader2 } from "lucide-react";
5
+ import { Button } from "./button.tsx";
6
+ import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "./dialog.tsx";
7
+ import { useLanguage } from "../i18n/language-context.tsx";
8
+
9
+ export interface TranslateResult {
10
+ readonly translatedText: string;
11
+ }
12
+
13
+ export interface TranslateDialogTranslations {
14
+ readonly title: string;
15
+ readonly loading: string;
16
+ readonly error: string;
17
+ readonly copy: string;
18
+ readonly copied: string;
19
+ readonly close: string;
20
+ }
21
+
22
+ export interface TranslateDialogProps {
23
+ readonly open: boolean;
24
+ readonly onOpenChange: (open: boolean) => void;
25
+ readonly sourceText: string;
26
+ readonly onTranslate: (text: string, targetLanguage: string) => Promise<TranslateResult>;
27
+ readonly translations: TranslateDialogTranslations;
28
+ }
29
+
30
+ /**
31
+ * Prop-driven translate dialog. When `open` flips to true, calls
32
+ * `onTranslate(sourceText, targetLanguage)` where `targetLanguage` is `"de"`
33
+ * if the active `useLanguage()` language is `"de"`, else `"en"`. Renders a
34
+ * loading state during the in-flight request, an error message on rejection,
35
+ * the translated text on success, and a copy button that flashes a
36
+ * "copied" confirmation for ~2 seconds.
37
+ */
38
+ export function TranslateDialog({
39
+ open,
40
+ onOpenChange,
41
+ sourceText,
42
+ onTranslate,
43
+ translations: T,
44
+ }: TranslateDialogProps) {
45
+ const { language } = useLanguage();
46
+ const targetLanguage = language === "de" ? "de" : "en";
47
+
48
+ const [loading, setLoading] = useState(false);
49
+ const [translated, setTranslated] = useState<string | null>(null);
50
+ const [error, setError] = useState(false);
51
+ const [copied, setCopied] = useState(false);
52
+
53
+ useEffect(() => {
54
+ if (!open) {
55
+ return;
56
+ }
57
+ setLoading(true);
58
+ setTranslated(null);
59
+ setError(false);
60
+ setCopied(false);
61
+ let cancelled = false;
62
+ onTranslate(sourceText, targetLanguage)
63
+ .then((result) => {
64
+ if (!cancelled) {
65
+ setTranslated(result.translatedText);
66
+ }
67
+ })
68
+ .catch(() => {
69
+ if (!cancelled) {
70
+ setError(true);
71
+ }
72
+ })
73
+ .finally(() => {
74
+ if (!cancelled) {
75
+ setLoading(false);
76
+ }
77
+ });
78
+ return () => {
79
+ cancelled = true;
80
+ };
81
+ }, [open, sourceText, targetLanguage, onTranslate]);
82
+
83
+ const handleCopy = async () => {
84
+ if (!translated) return;
85
+ try {
86
+ await navigator.clipboard.writeText(translated);
87
+ setCopied(true);
88
+ setTimeout(() => setCopied(false), 2000);
89
+ } catch {
90
+ // Clipboard not available; ignore.
91
+ }
92
+ };
93
+
94
+ return (
95
+ <Dialog open={open} onOpenChange={onOpenChange}>
96
+ <DialogContent>
97
+ <DialogHeader>
98
+ <DialogTitle className="font-heading">{T.title}</DialogTitle>
99
+ </DialogHeader>
100
+ <div className="min-h-[80px] py-2">
101
+ {loading && (
102
+ <div
103
+ className="flex items-center gap-2 text-sm text-oe-gray-mid"
104
+ data-testid="translate-dialog-loading"
105
+ >
106
+ <Loader2 className="h-4 w-4 animate-spin" />
107
+ {T.loading}
108
+ </div>
109
+ )}
110
+ {!loading && error && (
111
+ <p className="text-sm text-oe-red" data-testid="translate-dialog-error">
112
+ {T.error}
113
+ </p>
114
+ )}
115
+ {!loading && !error && translated && (
116
+ <p
117
+ className="text-sm text-oe-black whitespace-pre-line break-words"
118
+ data-testid="translate-dialog-result"
119
+ >
120
+ {translated}
121
+ </p>
122
+ )}
123
+ </div>
124
+ <DialogFooter>
125
+ <Button
126
+ variant="outline"
127
+ onClick={handleCopy}
128
+ disabled={loading || error || !translated}
129
+ data-testid="translate-dialog-copy"
130
+ >
131
+ {copied ? (
132
+ <>
133
+ <Check className="mr-2 h-4 w-4" />
134
+ {T.copied}
135
+ </>
136
+ ) : (
137
+ <>
138
+ <Copy className="mr-2 h-4 w-4" />
139
+ {T.copy}
140
+ </>
141
+ )}
142
+ </Button>
143
+ <Button onClick={() => onOpenChange(false)}>{T.close}</Button>
144
+ </DialogFooter>
145
+ </DialogContent>
146
+ </Dialog>
147
+ );
148
+ }
package/src/index.ts CHANGED
@@ -119,6 +119,18 @@ export {
119
119
  } from "./components/table.tsx";
120
120
  export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider } from "./components/tooltip.tsx";
121
121
 
122
+ export { TranslateDialog } from "./components/translate-dialog.tsx";
123
+ export type {
124
+ TranslateDialogProps,
125
+ TranslateDialogTranslations,
126
+ TranslateResult,
127
+ } from "./components/translate-dialog.tsx";
128
+ export { TranslateButton } from "./components/translate-button.tsx";
129
+ export type {
130
+ TranslateButtonProps,
131
+ TranslateButtonTranslations,
132
+ } from "./components/translate-button.tsx";
133
+
122
134
  // App-level components
123
135
  export { DeleteConfirmDialog } from "./components/delete-confirm-dialog.tsx";
124
136
  export type { DeleteConfirmDialogProps } from "./components/delete-confirm-dialog.tsx";