@open-elements/ui 0.5.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.
Files changed (60) hide show
  1. package/dist/components/action-icon-button.d.ts +11 -0
  2. package/dist/components/action-icon-button.d.ts.map +1 -0
  3. package/dist/components/action-icon-button.js +14 -0
  4. package/dist/components/action-icon-button.js.map +1 -0
  5. package/dist/components/combobox.d.ts +1 -1
  6. package/dist/components/combobox.d.ts.map +1 -1
  7. package/dist/components/combobox.js +1 -1
  8. package/dist/components/combobox.js.map +1 -1
  9. package/dist/components/copy-to-clipboard-button.d.ts +7 -0
  10. package/dist/components/copy-to-clipboard-button.d.ts.map +1 -0
  11. package/dist/components/copy-to-clipboard-button.js +16 -0
  12. package/dist/components/copy-to-clipboard-button.js.map +1 -0
  13. package/dist/components/external-link-button.d.ts +7 -0
  14. package/dist/components/external-link-button.d.ts.map +1 -0
  15. package/dist/components/external-link-button.js +10 -0
  16. package/dist/components/external-link-button.js.map +1 -0
  17. package/dist/components/mailto-button.d.ts +7 -0
  18. package/dist/components/mailto-button.d.ts.map +1 -0
  19. package/dist/components/mailto-button.js +10 -0
  20. package/dist/components/mailto-button.js.map +1 -0
  21. package/dist/components/table-pagination.d.ts +21 -0
  22. package/dist/components/table-pagination.d.ts.map +1 -0
  23. package/dist/components/table-pagination.js +15 -0
  24. package/dist/components/table-pagination.js.map +1 -0
  25. package/dist/components/tooltip-icon-button.d.ts +24 -0
  26. package/dist/components/tooltip-icon-button.d.ts.map +1 -0
  27. package/dist/components/tooltip-icon-button.js +28 -0
  28. package/dist/components/tooltip-icon-button.js.map +1 -0
  29. package/dist/components/translate-button.d.ts +28 -0
  30. package/dist/components/translate-button.d.ts.map +1 -0
  31. package/dist/components/translate-button.js +24 -0
  32. package/dist/components/translate-button.js.map +1 -0
  33. package/dist/components/translate-dialog.d.ts +28 -0
  34. package/dist/components/translate-dialog.d.ts.map +1 -0
  35. package/dist/components/translate-dialog.js +66 -0
  36. package/dist/components/translate-dialog.js.map +1 -0
  37. package/dist/index.d.ts +12 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +10 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/tsconfig.build.tsbuildinfo +1 -1
  42. package/package.json +1 -1
  43. package/src/components/__tests__/action-icon-button.test.tsx +62 -0
  44. package/src/components/__tests__/copy-to-clipboard-button.test.tsx +48 -0
  45. package/src/components/__tests__/external-link-button.test.tsx +28 -0
  46. package/src/components/__tests__/mailto-button.test.tsx +30 -0
  47. package/src/components/__tests__/table-pagination.test.tsx +115 -0
  48. package/src/components/__tests__/tooltip-icon-button.test.tsx +94 -0
  49. package/src/components/__tests__/translate-button.test.tsx +98 -0
  50. package/src/components/__tests__/translate-dialog.test.tsx +107 -0
  51. package/src/components/action-icon-button.tsx +40 -0
  52. package/src/components/combobox.tsx +1 -1
  53. package/src/components/copy-to-clipboard-button.tsx +28 -0
  54. package/src/components/external-link-button.tsx +22 -0
  55. package/src/components/mailto-button.tsx +22 -0
  56. package/src/components/table-pagination.tsx +88 -0
  57. package/src/components/tooltip-icon-button.tsx +83 -0
  58. package/src/components/translate-button.tsx +82 -0
  59. package/src/components/translate-dialog.tsx +148 -0
  60. package/src/index.ts +24 -0
@@ -0,0 +1,22 @@
1
+ "use client";
2
+
3
+ import { ExternalLink } from "lucide-react";
4
+ import { ActionIconButton } from "./action-icon-button.tsx";
5
+
6
+ type ExternalLinkButtonProps = {
7
+ readonly href: string;
8
+ readonly title?: string;
9
+ };
10
+
11
+ export function ExternalLinkButton({ href, title }: ExternalLinkButtonProps) {
12
+ return (
13
+ <ActionIconButton
14
+ onClick={() => {
15
+ window.open(href, "_blank", "noopener,noreferrer");
16
+ }}
17
+ title={title}
18
+ >
19
+ <ExternalLink />
20
+ </ActionIconButton>
21
+ );
22
+ }
@@ -0,0 +1,22 @@
1
+ "use client";
2
+
3
+ import { Mail } from "lucide-react";
4
+ import { ActionIconButton } from "./action-icon-button.tsx";
5
+
6
+ type MailtoButtonProps = {
7
+ readonly email: string;
8
+ readonly title?: string;
9
+ };
10
+
11
+ export function MailtoButton({ email, title }: MailtoButtonProps) {
12
+ return (
13
+ <ActionIconButton
14
+ onClick={() => {
15
+ window.location.href = `mailto:${email}`;
16
+ }}
17
+ title={title}
18
+ >
19
+ <Mail />
20
+ </ActionIconButton>
21
+ );
22
+ }
@@ -0,0 +1,88 @@
1
+ "use client";
2
+
3
+ import { Button } from "./button.tsx";
4
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.tsx";
5
+
6
+ export type PaginationTranslations = {
7
+ readonly perPage: string;
8
+ readonly previous: string;
9
+ readonly next: string;
10
+ readonly totalOne: string;
11
+ readonly totalOther: string;
12
+ };
13
+
14
+ type TablePaginationProps = {
15
+ readonly page: number;
16
+ readonly pageSize: number;
17
+ readonly totalElements: number;
18
+ readonly totalPages: number;
19
+ readonly pageSizeOptions: readonly number[];
20
+ readonly storageKey: string;
21
+ readonly translations: PaginationTranslations;
22
+ readonly onPageChange: (page: number) => void;
23
+ readonly onPageSizeChange: (size: number) => void;
24
+ };
25
+
26
+ export function TablePagination({
27
+ page,
28
+ pageSize,
29
+ totalElements,
30
+ totalPages,
31
+ pageSizeOptions,
32
+ storageKey,
33
+ translations,
34
+ onPageChange,
35
+ onPageSizeChange,
36
+ }: TablePaginationProps) {
37
+ const totalTemplate = totalElements === 1 ? translations.totalOne : translations.totalOther;
38
+ const totalLabel = totalTemplate.replace("{count}", String(totalElements));
39
+
40
+ return (
41
+ <div className="mt-4 flex items-center justify-between">
42
+ <div className="flex items-center gap-3">
43
+ <Select
44
+ value={String(pageSize)}
45
+ onValueChange={(v) => {
46
+ const n = Number(v);
47
+ onPageSizeChange(n);
48
+ localStorage.setItem(storageKey, v);
49
+ onPageChange(0);
50
+ }}
51
+ >
52
+ <SelectTrigger className="w-[80px] h-8 text-sm" aria-label={translations.perPage}>
53
+ <SelectValue />
54
+ </SelectTrigger>
55
+ <SelectContent>
56
+ {pageSizeOptions.map((s) => (
57
+ <SelectItem key={s} value={String(s)}>
58
+ {s}
59
+ </SelectItem>
60
+ ))}
61
+ </SelectContent>
62
+ </Select>
63
+ <span className="text-sm text-muted-foreground">{translations.perPage}</span>
64
+ <span className="text-sm text-muted-foreground">· {totalLabel}</span>
65
+ </div>
66
+ {totalPages > 1 && (
67
+ <div className="flex gap-2">
68
+ <Button
69
+ variant="outline"
70
+ size="sm"
71
+ disabled={page === 0}
72
+ onClick={() => onPageChange(page - 1)}
73
+ >
74
+ {translations.previous}
75
+ </Button>
76
+ <Button
77
+ variant="outline"
78
+ size="sm"
79
+ disabled={page >= totalPages - 1}
80
+ onClick={() => onPageChange(page + 1)}
81
+ >
82
+ {translations.next}
83
+ </Button>
84
+ </div>
85
+ )}
86
+ </div>
87
+ );
88
+ }
@@ -0,0 +1,83 @@
1
+ "use client";
2
+
3
+ import type { MouseEvent, ReactNode } from "react";
4
+ import { cn } from "../lib/utils.ts";
5
+ import { Button } from "./button.tsx";
6
+ import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip.tsx";
7
+
8
+ type Tone = "default" | "destructive";
9
+
10
+ type CommonProps = {
11
+ readonly tooltip: string;
12
+ readonly tone?: Tone;
13
+ };
14
+
15
+ type AsButtonProps = CommonProps & {
16
+ readonly icon: ReactNode;
17
+ readonly onClick: (event: MouseEvent<HTMLButtonElement>) => void;
18
+ readonly disabled?: boolean;
19
+ readonly asChild?: never;
20
+ readonly children?: never;
21
+ };
22
+
23
+ type AsChildProps = CommonProps & {
24
+ readonly asChild: true;
25
+ readonly children: ReactNode;
26
+ readonly icon?: never;
27
+ readonly onClick?: never;
28
+ readonly disabled?: never;
29
+ };
30
+
31
+ export type TooltipIconButtonProps = AsButtonProps | AsChildProps;
32
+
33
+ const TONE_CLASS: Record<Tone, string> = {
34
+ default: "[&_svg]:text-primary",
35
+ destructive: "[&_svg]:text-destructive",
36
+ };
37
+
38
+ export function TooltipIconButton(props: TooltipIconButtonProps) {
39
+ const { tooltip, tone = "default" } = props;
40
+ const className = cn("[&_svg]:h-4 [&_svg]:w-4", TONE_CLASS[tone]);
41
+
42
+ let trigger: ReactNode;
43
+ if ("asChild" in props && props.asChild) {
44
+ trigger = (
45
+ <Button
46
+ asChild
47
+ variant="ghost"
48
+ size="icon"
49
+ className={className}
50
+ aria-label={tooltip}
51
+ onClick={(event) => event.stopPropagation()}
52
+ >
53
+ {props.children}
54
+ </Button>
55
+ );
56
+ } else {
57
+ const button = (
58
+ <Button
59
+ variant="ghost"
60
+ size="icon"
61
+ className={className}
62
+ aria-label={tooltip}
63
+ disabled={props.disabled}
64
+ onClick={(event) => {
65
+ event.stopPropagation();
66
+ props.onClick(event);
67
+ }}
68
+ >
69
+ {props.icon}
70
+ </Button>
71
+ );
72
+ // Disabled buttons swallow pointer events, so the Tooltip stops working
73
+ // unless the trigger is wrapped in a span.
74
+ trigger = props.disabled ? <span>{button}</span> : button;
75
+ }
76
+
77
+ return (
78
+ <Tooltip>
79
+ <TooltipTrigger asChild>{trigger}</TooltipTrigger>
80
+ <TooltipContent>{tooltip}</TooltipContent>
81
+ </Tooltip>
82
+ );
83
+ }
@@ -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";
@@ -155,6 +167,18 @@ export { MarkdownView } from "./components/markdown-view.tsx";
155
167
  export { UserAvatar } from "./components/user-avatar.tsx";
156
168
  export { UserMultiSelect } from "./components/user-multi-select.tsx";
157
169
 
170
+ // Action buttons
171
+ export { ActionIconButton } from "./components/action-icon-button.tsx";
172
+ export { CopyToClipboardButton } from "./components/copy-to-clipboard-button.tsx";
173
+ export { ExternalLinkButton } from "./components/external-link-button.tsx";
174
+ export { MailtoButton } from "./components/mailto-button.tsx";
175
+ export { TooltipIconButton } from "./components/tooltip-icon-button.tsx";
176
+ export type { TooltipIconButtonProps } from "./components/tooltip-icon-button.tsx";
177
+
178
+ // Table pagination
179
+ export { TablePagination } from "./components/table-pagination.tsx";
180
+ export type { PaginationTranslations } from "./components/table-pagination.tsx";
181
+
158
182
  // Types
159
183
  export type { TagDto } from "./types/index.ts";
160
184
  export type {