@open-elements/ui 0.10.0 → 0.11.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 (104) hide show
  1. package/README.md +21 -59
  2. package/dist/components/action-icon-button.d.ts +1 -1
  3. package/dist/components/action-icon-button.d.ts.map +1 -1
  4. package/dist/components/alert-dialog.d.ts +12 -12
  5. package/dist/components/alert-dialog.d.ts.map +1 -1
  6. package/dist/components/badge.d.ts +1 -1
  7. package/dist/components/badge.d.ts.map +1 -1
  8. package/dist/components/button.d.ts +1 -1
  9. package/dist/components/button.d.ts.map +1 -1
  10. package/dist/components/calendar.d.ts +2 -2
  11. package/dist/components/calendar.d.ts.map +1 -1
  12. package/dist/components/calendar.js +1 -1
  13. package/dist/components/calendar.js.map +1 -1
  14. package/dist/components/capability-status.d.ts +1 -1
  15. package/dist/components/capability-status.d.ts.map +1 -1
  16. package/dist/components/card.d.ts +7 -7
  17. package/dist/components/card.d.ts.map +1 -1
  18. package/dist/components/combobox.d.ts +14 -14
  19. package/dist/components/combobox.d.ts.map +1 -1
  20. package/dist/components/copy-to-clipboard-button.d.ts +1 -1
  21. package/dist/components/copy-to-clipboard-button.d.ts.map +1 -1
  22. package/dist/components/delete-confirm-dialog.d.ts +1 -1
  23. package/dist/components/delete-confirm-dialog.d.ts.map +1 -1
  24. package/dist/components/detail-field.d.ts +1 -1
  25. package/dist/components/detail-field.d.ts.map +1 -1
  26. package/dist/components/dialog.d.ts +10 -10
  27. package/dist/components/dialog.d.ts.map +1 -1
  28. package/dist/components/external-link-button.d.ts +1 -1
  29. package/dist/components/external-link-button.d.ts.map +1 -1
  30. package/dist/components/health-status.d.ts +1 -1
  31. package/dist/components/health-status.d.ts.map +1 -1
  32. package/dist/components/input-group.d.ts +6 -6
  33. package/dist/components/input-group.d.ts.map +1 -1
  34. package/dist/components/input.d.ts +1 -1
  35. package/dist/components/input.d.ts.map +1 -1
  36. package/dist/components/label.d.ts +1 -1
  37. package/dist/components/label.d.ts.map +1 -1
  38. package/dist/components/language-switch.d.ts +1 -1
  39. package/dist/components/language-switch.d.ts.map +1 -1
  40. package/dist/components/mailto-button.d.ts +1 -1
  41. package/dist/components/mailto-button.d.ts.map +1 -1
  42. package/dist/components/markdown-editor.d.ts +1 -1
  43. package/dist/components/markdown-editor.d.ts.map +1 -1
  44. package/dist/components/markdown-view.d.ts +1 -1
  45. package/dist/components/markdown-view.d.ts.map +1 -1
  46. package/dist/components/popover.d.ts +7 -7
  47. package/dist/components/popover.d.ts.map +1 -1
  48. package/dist/components/select.d.ts +10 -10
  49. package/dist/components/select.d.ts.map +1 -1
  50. package/dist/components/separator.d.ts +1 -1
  51. package/dist/components/separator.d.ts.map +1 -1
  52. package/dist/components/sheet.d.ts +8 -8
  53. package/dist/components/sheet.d.ts.map +1 -1
  54. package/dist/components/sidebar.d.ts +5 -5
  55. package/dist/components/sidebar.d.ts.map +1 -1
  56. package/dist/components/skeleton.d.ts +1 -1
  57. package/dist/components/skeleton.d.ts.map +1 -1
  58. package/dist/components/table-pagination.d.ts +1 -1
  59. package/dist/components/table-pagination.d.ts.map +1 -1
  60. package/dist/components/table-pagination.js +2 -1
  61. package/dist/components/table-pagination.js.map +1 -1
  62. package/dist/components/table.d.ts +8 -8
  63. package/dist/components/table.d.ts.map +1 -1
  64. package/dist/components/tag-chips.d.ts +1 -1
  65. package/dist/components/tag-chips.d.ts.map +1 -1
  66. package/dist/components/tag-form.d.ts +1 -1
  67. package/dist/components/tag-form.d.ts.map +1 -1
  68. package/dist/components/tag-multi-select.d.ts +1 -1
  69. package/dist/components/tag-multi-select.d.ts.map +1 -1
  70. package/dist/components/textarea.d.ts +1 -1
  71. package/dist/components/textarea.d.ts.map +1 -1
  72. package/dist/components/tooltip-icon-button.d.ts +1 -1
  73. package/dist/components/tooltip-icon-button.d.ts.map +1 -1
  74. package/dist/components/translate-button.d.ts +1 -1
  75. package/dist/components/translate-button.d.ts.map +1 -1
  76. package/dist/components/translate-dialog.d.ts +1 -1
  77. package/dist/components/translate-dialog.d.ts.map +1 -1
  78. package/dist/components/user-avatar.d.ts +1 -1
  79. package/dist/components/user-avatar.d.ts.map +1 -1
  80. package/dist/components/user-multi-select.d.ts +1 -1
  81. package/dist/components/user-multi-select.d.ts.map +1 -1
  82. package/dist/i18n/language-context.d.ts +1 -1
  83. package/dist/i18n/language-context.d.ts.map +1 -1
  84. package/dist/i18n/language-context.js +6 -20
  85. package/dist/i18n/language-context.js.map +1 -1
  86. package/dist/index.d.ts +1 -0
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +1 -0
  89. package/dist/index.js.map +1 -1
  90. package/dist/lib/safe-storage.d.ts +9 -0
  91. package/dist/lib/safe-storage.d.ts.map +1 -0
  92. package/dist/lib/safe-storage.js +48 -0
  93. package/dist/lib/safe-storage.js.map +1 -0
  94. package/package.json +34 -34
  95. package/src/components/__tests__/capability-status.test.tsx +4 -3
  96. package/src/components/__tests__/table-pagination.test.tsx +54 -0
  97. package/src/components/__tests__/tag-multi-select.test.tsx +26 -13
  98. package/src/components/calendar.tsx +1 -1
  99. package/src/components/table-pagination.tsx +2 -1
  100. package/src/i18n/language-context.tsx +6 -17
  101. package/src/index.ts +1 -0
  102. package/src/lib/__tests__/safe-storage.test.ts +62 -0
  103. package/src/lib/safe-storage.ts +43 -0
  104. package/dist/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-storage.d.ts","sourceRoot":"","sources":["../../src/lib/safe-storage.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,WAAW;IACtB,0FAA0F;wBACjF,MAAM,KAAG,MAAM,GAAG,IAAI;IAQ/B,gEAAgE;wBACvD,MAAM,SAAS,MAAM,KAAG,IAAI;IAQrC,iEAAiE;2BACrD,MAAM,KAAG,IAAI;CAOjB,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Access to `window.localStorage` that never throws.
3
+ *
4
+ * Storage can be missing (server rendering, `dom.storage.enabled = false`) or throw on access
5
+ * (site data blocked in hardened browsers such as LibreWolf, quota exceeded). Persisted values in
6
+ * this library are only conveniences, so every failure falls back to "nothing stored".
7
+ */
8
+ function storage() {
9
+ try {
10
+ return typeof window === "undefined" ? null : window.localStorage;
11
+ }
12
+ catch (_a) {
13
+ return null;
14
+ }
15
+ }
16
+ export const safeStorage = {
17
+ /** Returns the stored value, or `null` if nothing is stored or storage is unavailable. */
18
+ get(key) {
19
+ var _a, _b;
20
+ try {
21
+ return (_b = (_a = storage()) === null || _a === void 0 ? void 0 : _a.getItem(key)) !== null && _b !== void 0 ? _b : null;
22
+ }
23
+ catch (_c) {
24
+ return null;
25
+ }
26
+ },
27
+ /** Stores the value; does nothing if storage is unavailable. */
28
+ set(key, value) {
29
+ var _a;
30
+ try {
31
+ (_a = storage()) === null || _a === void 0 ? void 0 : _a.setItem(key, value);
32
+ }
33
+ catch (_b) {
34
+ // Storage unavailable or full: the value is simply not persisted.
35
+ }
36
+ },
37
+ /** Removes the value; does nothing if storage is unavailable. */
38
+ remove(key) {
39
+ var _a;
40
+ try {
41
+ (_a = storage()) === null || _a === void 0 ? void 0 : _a.removeItem(key);
42
+ }
43
+ catch (_b) {
44
+ // Storage unavailable: nothing to remove.
45
+ }
46
+ },
47
+ };
48
+ //# sourceMappingURL=safe-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-storage.js","sourceRoot":"","sources":["../../src/lib/safe-storage.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,SAAS,OAAO;IACd,IAAI,CAAC;QACH,OAAO,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;IACpE,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,0FAA0F;IAC1F,GAAG,CAAC,GAAW;;QACb,IAAI,CAAC;YACH,OAAO,MAAA,MAAA,OAAO,EAAE,0CAAE,OAAO,CAAC,GAAG,CAAC,mCAAI,IAAI,CAAC;QACzC,CAAC;QAAC,WAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,gEAAgE;IAChE,GAAG,CAAC,GAAW,EAAE,KAAa;;QAC5B,IAAI,CAAC;YACH,MAAA,OAAO,EAAE,0CAAE,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACjC,CAAC;QAAC,WAAM,CAAC;YACP,kEAAkE;QACpE,CAAC;IACH,CAAC;IAED,iEAAiE;IACjE,MAAM,CAAC,GAAW;;QAChB,IAAI,CAAC;YACH,MAAA,OAAO,EAAE,0CAAE,UAAU,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC;QAAC,WAAM,CAAC;YACP,0CAA0C;QAC5C,CAAC;IACH,CAAC;CACO,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-elements/ui",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Shared UI component library for Open Elements applications",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -29,56 +29,56 @@
29
29
  "**/*.css"
30
30
  ],
31
31
  "dependencies": {
32
- "@tiptap/core": "^3.22.0",
33
- "@tiptap/extension-list": "^3.22.0",
34
- "@tiptap/extension-placeholder": "^3.22.0",
35
- "@tiptap/react": "^3.22.0",
36
- "@tiptap/starter-kit": "^3.22.0",
37
- "class-variance-authority": "^0.7.0",
38
- "clsx": "^2.1.0",
39
- "date-fns": "^4.1.0",
40
- "react-day-picker": "^9.14.0",
41
- "tailwind-merge": "^3.0.0",
32
+ "@tiptap/core": "^3.31.3",
33
+ "@tiptap/extension-list": "^3.31.3",
34
+ "@tiptap/extension-placeholder": "^3.31.3",
35
+ "@tiptap/react": "^3.31.3",
36
+ "@tiptap/starter-kit": "^3.31.3",
37
+ "class-variance-authority": "^0.7.1",
38
+ "clsx": "^2.1.1",
39
+ "date-fns": "^4.4.0",
40
+ "react-day-picker": "^10.0.1",
41
+ "tailwind-merge": "^3.6.0",
42
42
  "tiptap-markdown": "^0.9.0"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@base-ui/react": "^1.3.0",
46
- "lucide-react": "^0.500.0",
46
+ "lucide-react": "^0.500.0 || ^1.0.0",
47
47
  "radix-ui": "^1.4.0",
48
48
  "react": "^19.0.0",
49
49
  "react-dom": "^19.0.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@base-ui/react": "^1.3.0",
52
+ "@base-ui/react": "^1.8.0",
53
53
  "@cyclonedx/cyclonedx-library": "^10.2.0",
54
- "@eslint/js": "^9.0.0",
55
- "@storybook/addon-a11y": "^10.5.9",
56
- "@storybook/addon-docs": "^10.5.9",
57
- "@storybook/react-vite": "^10.5.9",
54
+ "@eslint/js": "^10.0.1",
55
+ "@storybook/addon-a11y": "^10.6.0",
56
+ "@storybook/addon-docs": "^10.6.0",
57
+ "@storybook/react-vite": "^10.6.0",
58
58
  "@tailwindcss/typography": "^0.5.20",
59
59
  "@tailwindcss/vite": "^4.3.3",
60
- "@testing-library/jest-dom": "^6.0.0",
61
- "@testing-library/react": "^16.0.0",
62
- "@types/react": "^19.0.0",
63
- "@types/react-dom": "^19.0.0",
64
- "@vitejs/plugin-react": "^4.0.0",
60
+ "@testing-library/jest-dom": "^7.0.1",
61
+ "@testing-library/react": "^16.3.3",
62
+ "@types/react": "^19.2.18",
63
+ "@types/react-dom": "^19.2.7",
64
+ "@vitejs/plugin-react": "^6.1.1",
65
65
  "ajv": "^8.20.0",
66
66
  "ajv-formats": "^3.0.1",
67
67
  "ajv-formats-draft2019": "^1.6.1",
68
- "eslint": "^9.0.0",
69
- "jsdom": "^26.0.0",
70
- "lucide-react": "^0.500.0",
71
- "prettier": "^3.0.0",
72
- "radix-ui": "^1.4.0",
73
- "react": "^19.0.0",
74
- "react-dom": "^19.0.0",
75
- "storybook": "^10.5.9",
68
+ "eslint": "^10.10.0",
69
+ "jsdom": "^30.0.1",
70
+ "lucide-react": "^1.42.0",
71
+ "prettier": "^3.9.6",
72
+ "radix-ui": "^1.6.7",
73
+ "react": "^19.2.8",
74
+ "react-dom": "^19.2.8",
75
+ "storybook": "^10.6.0",
76
76
  "tailwindcss": "^4.3.3",
77
77
  "tw-animate-css": "^1.4.0",
78
- "typescript": "^5.0.0",
79
- "typescript-eslint": "^8.0.0",
80
- "vite": "^7.3.2",
81
- "vitest": "^3.0.0"
78
+ "typescript": "^6.0.3",
79
+ "typescript-eslint": "^8.69.0",
80
+ "vite": "^8.2.2",
81
+ "vitest": "^5.0.0"
82
82
  },
83
83
  "scripts": {
84
84
  "build": "tsc -p tsconfig.build.json && pnpm run build:css",
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect, afterEach } from "vitest";
2
- import { cleanup, fireEvent, render, screen } from "@testing-library/react";
2
+ import { cleanup, render, screen } from "@testing-library/react";
3
3
  import { CapabilityStatus } from "../capability-status.tsx";
4
4
  import { TooltipProvider } from "../tooltip.tsx";
5
5
 
@@ -59,8 +59,9 @@ describe("CapabilityStatus", () => {
59
59
  />,
60
60
  );
61
61
 
62
- const trigger = screen.getByRole("note");
63
- fireEvent.focus(trigger);
62
+ // Radix only opens the tooltip for focus that actually moves the active
63
+ // element, so a synthetic focus event is not enough here.
64
+ screen.getByRole("note").focus();
64
65
 
65
66
  const tooltip = await screen.findByRole("tooltip");
66
67
  expect(tooltip).toHaveTextContent("HEIC uploads will be rejected with 415 — check Dockerfile");
@@ -112,4 +112,58 @@ describe("TablePagination", () => {
112
112
  render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
113
113
  expect(screen.getByLabelText("per page")).toBeInTheDocument();
114
114
  });
115
+
116
+ describe("changing the page size", () => {
117
+ // Radix Select relies on pointer capture and scrollIntoView, which jsdom does not implement.
118
+ beforeEach(() => {
119
+ Element.prototype.hasPointerCapture ??= () => false;
120
+ Element.prototype.releasePointerCapture ??= () => undefined;
121
+ Element.prototype.scrollIntoView ??= () => undefined;
122
+ });
123
+
124
+ function selectPageSize(size: string) {
125
+ fireEvent.keyDown(screen.getByLabelText("per page"), { key: "Enter" });
126
+ fireEvent.click(screen.getByRole("option", { name: size }));
127
+ }
128
+
129
+ it("stores the new size and resets to the first page", () => {
130
+ const onPageChange = vi.fn();
131
+ const onPageSizeChange = vi.fn();
132
+ render(
133
+ <TablePagination
134
+ {...defaultProps}
135
+ page={1}
136
+ onPageChange={onPageChange}
137
+ onPageSizeChange={onPageSizeChange}
138
+ totalElements={35}
139
+ totalPages={2}
140
+ />,
141
+ );
142
+ selectPageSize("50");
143
+ expect(onPageSizeChange).toHaveBeenCalledWith(50);
144
+ expect(localStorageMock.setItem).toHaveBeenCalledWith("pageSize.test", "50");
145
+ expect(onPageChange).toHaveBeenCalledWith(0);
146
+ });
147
+
148
+ it("still changes the size when storage throws", () => {
149
+ localStorageMock.setItem.mockImplementationOnce(() => {
150
+ throw new DOMException("The operation is insecure.", "SecurityError");
151
+ });
152
+ const onPageChange = vi.fn();
153
+ const onPageSizeChange = vi.fn();
154
+ render(
155
+ <TablePagination
156
+ {...defaultProps}
157
+ page={1}
158
+ onPageChange={onPageChange}
159
+ onPageSizeChange={onPageSizeChange}
160
+ totalElements={35}
161
+ totalPages={2}
162
+ />,
163
+ );
164
+ selectPageSize("50");
165
+ expect(onPageSizeChange).toHaveBeenCalledWith(50);
166
+ expect(onPageChange).toHaveBeenCalledWith(0);
167
+ });
168
+ });
115
169
  });
@@ -1,5 +1,5 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { render, screen, waitFor } from "@testing-library/react";
1
+ import { describe, it, expect, vi, afterEach } from "vitest";
2
+ import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
3
3
  import { TagMultiSelect } from "../tag-multi-select.tsx";
4
4
  import type { TagOption, TagMultiSelectTranslations } from "../../types/index.ts";
5
5
 
@@ -14,6 +14,21 @@ const mockTags: TagOption[] = [
14
14
  { value: "3", label: "Lead", color: "#0000FF" },
15
15
  ];
16
16
 
17
+ afterEach(() => {
18
+ cleanup();
19
+ });
20
+
21
+ // The listbox is portalled and only mounted while the combobox is open, so the
22
+ // empty message is not in the DOM until the popup is opened. ArrowDown is the
23
+ // combobox open interaction that works without layout, which jsdom lacks.
24
+ async function openPopup() {
25
+ const input = screen.getByPlaceholderText("Choose tags...");
26
+ fireEvent.keyDown(input, { key: "ArrowDown" });
27
+ await waitFor(() => {
28
+ expect(input).toHaveAttribute("aria-expanded", "true");
29
+ });
30
+ }
31
+
17
32
  describe("TagMultiSelect", () => {
18
33
  it("calls loadTags on mount", async () => {
19
34
  const loadTags = vi.fn().mockResolvedValue(mockTags);
@@ -44,9 +59,7 @@ describe("TagMultiSelect", () => {
44
59
  />,
45
60
  );
46
61
 
47
- const inputs = screen.getAllByPlaceholderText("Choose tags...");
48
- expect(inputs.length).toBeGreaterThanOrEqual(1);
49
- expect(inputs[0]).toBeInTheDocument();
62
+ expect(screen.getByPlaceholderText("Choose tags...")).toBeInTheDocument();
50
63
  });
51
64
 
52
65
  it("shows empty message when no tags loaded", async () => {
@@ -62,9 +75,11 @@ describe("TagMultiSelect", () => {
62
75
  );
63
76
 
64
77
  await waitFor(() => {
65
- const msgs = screen.getAllByText("No tags available");
66
- expect(msgs.length).toBeGreaterThanOrEqual(1);
78
+ expect(loadTags).toHaveBeenCalled();
67
79
  });
80
+ await openPopup();
81
+
82
+ expect(screen.getByText("No tags available")).toBeInTheDocument();
68
83
  });
69
84
 
70
85
  it("handles loadTags failure without crashing", async () => {
@@ -82,9 +97,9 @@ describe("TagMultiSelect", () => {
82
97
  await waitFor(() => {
83
98
  expect(loadTags).toHaveBeenCalled();
84
99
  });
100
+ await openPopup();
85
101
 
86
- const msgs = screen.getAllByText("No tags available");
87
- expect(msgs.length).toBeGreaterThanOrEqual(1);
102
+ expect(screen.getByText("No tags available")).toBeInTheDocument();
88
103
  });
89
104
 
90
105
  it("renders selected tags as chips with correct colors", async () => {
@@ -120,10 +135,8 @@ describe("TagMultiSelect", () => {
120
135
  );
121
136
 
122
137
  await waitFor(() => {
123
- const chips = screen.getAllByText("Bad Color");
124
- const chipEl = chips.find((el) => el.closest("[data-slot='combobox-chip']"));
125
- expect(chipEl).toBeTruthy();
126
- expect(chipEl!.closest("[data-slot='combobox-chip']")).toHaveStyle({
138
+ const chipEl = screen.getByText("Bad Color");
139
+ expect(chipEl.closest("[data-slot='combobox-chip']")).toHaveStyle({
127
140
  backgroundColor: "#6B7280",
128
141
  });
129
142
  });
@@ -73,7 +73,7 @@ function Calendar({
73
73
  : "flex h-8 items-center gap-1 rounded-md pr-1 pl-2 text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground",
74
74
  defaultClassNames.caption_label,
75
75
  ),
76
- table: "w-full border-collapse",
76
+ month_grid: cn("w-full border-collapse", defaultClassNames.month_grid),
77
77
  weekdays: cn("flex", defaultClassNames.weekdays),
78
78
  weekday: cn(
79
79
  "flex-1 rounded-md text-[0.8rem] font-normal text-muted-foreground select-none",
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { Button } from "./button.tsx";
4
4
  import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.tsx";
5
+ import { safeStorage } from "../lib/safe-storage.ts";
5
6
 
6
7
  export type PaginationTranslations = {
7
8
  readonly perPage: string;
@@ -45,7 +46,7 @@ export function TablePagination({
45
46
  onValueChange={(v) => {
46
47
  const n = Number(v);
47
48
  onPageSizeChange(n);
48
- localStorage.setItem(storageKey, v);
49
+ safeStorage.set(storageKey, v);
49
50
  onPageChange(0);
50
51
  }}
51
52
  >
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  import { createContext, useContext, useEffect, useState, useCallback, useMemo } from "react";
4
+ import { safeStorage } from "../lib/safe-storage.ts";
4
5
 
5
6
  export type Language = "de" | "en";
6
7
 
@@ -16,13 +17,9 @@ interface LanguageContextValue<T = Record<string, unknown>> {
16
17
  const LanguageContext = createContext<LanguageContextValue<any> | null>(null);
17
18
 
18
19
  function detectLanguage(): Language {
19
- try {
20
- const stored = localStorage.getItem(STORAGE_KEY);
21
- if (stored === "de" || stored === "en") {
22
- return stored;
23
- }
24
- } catch {
25
- // localStorage not available
20
+ const stored = safeStorage.get(STORAGE_KEY);
21
+ if (stored === "de" || stored === "en") {
22
+ return stored;
26
23
  }
27
24
 
28
25
  if (typeof navigator !== "undefined") {
@@ -63,20 +60,12 @@ export function LanguageProvider<T>({
63
60
  const setLanguage = useCallback((lang: Language) => {
64
61
  setLanguageState(lang);
65
62
  document.documentElement.lang = lang;
66
- try {
67
- localStorage.setItem(STORAGE_KEY, lang);
68
- } catch {
69
- // localStorage not available
70
- }
63
+ safeStorage.set(STORAGE_KEY, lang);
71
64
  }, []);
72
65
 
73
66
  useEffect(() => {
74
67
  if (mounted) {
75
- try {
76
- localStorage.setItem(STORAGE_KEY, language);
77
- } catch {
78
- // localStorage not available
79
- }
68
+ safeStorage.set(STORAGE_KEY, language);
80
69
  }
81
70
  }, [language, mounted]);
82
71
 
package/src/index.ts CHANGED
@@ -195,6 +195,7 @@ export type {
195
195
 
196
196
  // Utilities
197
197
  export { cn } from "./lib/utils.ts";
198
+ export { safeStorage } from "./lib/safe-storage.ts";
198
199
 
199
200
  // i18n
200
201
  export { LanguageProvider, useTranslations, useLanguage } from "./i18n/language-context.tsx";
@@ -0,0 +1,62 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { safeStorage } from "../safe-storage.ts";
3
+
4
+ const throwingStorage = {
5
+ getItem: () => {
6
+ throw new DOMException("The operation is insecure.", "SecurityError");
7
+ },
8
+ setItem: () => {
9
+ throw new DOMException("The quota has been exceeded.", "QuotaExceededError");
10
+ },
11
+ removeItem: () => {
12
+ throw new DOMException("The operation is insecure.", "SecurityError");
13
+ },
14
+ };
15
+
16
+ afterEach(() => {
17
+ vi.unstubAllGlobals();
18
+ window.localStorage.clear();
19
+ });
20
+
21
+ describe("safeStorage", () => {
22
+ it("reads back a stored value", () => {
23
+ safeStorage.set("key", "value");
24
+ expect(safeStorage.get("key")).toBe("value");
25
+ });
26
+
27
+ it("returns null for a key that is not stored", () => {
28
+ expect(safeStorage.get("missing")).toBeNull();
29
+ });
30
+
31
+ it("removes a stored value", () => {
32
+ safeStorage.set("key", "value");
33
+ safeStorage.remove("key");
34
+ expect(safeStorage.get("key")).toBeNull();
35
+ });
36
+
37
+ it("does not throw when every storage call throws", () => {
38
+ vi.stubGlobal("localStorage", throwingStorage);
39
+ expect(safeStorage.get("key")).toBeNull();
40
+ expect(() => safeStorage.set("key", "value")).not.toThrow();
41
+ expect(() => safeStorage.remove("key")).not.toThrow();
42
+ });
43
+
44
+ it("does not throw when localStorage is null", () => {
45
+ vi.stubGlobal("localStorage", null);
46
+ expect(safeStorage.get("key")).toBeNull();
47
+ expect(() => safeStorage.set("key", "value")).not.toThrow();
48
+ expect(() => safeStorage.remove("key")).not.toThrow();
49
+ });
50
+
51
+ it("does not throw when accessing localStorage itself throws", () => {
52
+ const getter = vi.spyOn(window, "localStorage", "get").mockImplementation(() => {
53
+ throw new DOMException("The operation is insecure.", "SecurityError");
54
+ });
55
+ try {
56
+ expect(safeStorage.get("key")).toBeNull();
57
+ expect(() => safeStorage.set("key", "value")).not.toThrow();
58
+ } finally {
59
+ getter.mockRestore();
60
+ }
61
+ });
62
+ });
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Access to `window.localStorage` that never throws.
3
+ *
4
+ * Storage can be missing (server rendering, `dom.storage.enabled = false`) or throw on access
5
+ * (site data blocked in hardened browsers such as LibreWolf, quota exceeded). Persisted values in
6
+ * this library are only conveniences, so every failure falls back to "nothing stored".
7
+ */
8
+ function storage(): Storage | null {
9
+ try {
10
+ return typeof window === "undefined" ? null : window.localStorage;
11
+ } catch {
12
+ return null;
13
+ }
14
+ }
15
+
16
+ export const safeStorage = {
17
+ /** Returns the stored value, or `null` if nothing is stored or storage is unavailable. */
18
+ get(key: string): string | null {
19
+ try {
20
+ return storage()?.getItem(key) ?? null;
21
+ } catch {
22
+ return null;
23
+ }
24
+ },
25
+
26
+ /** Stores the value; does nothing if storage is unavailable. */
27
+ set(key: string, value: string): void {
28
+ try {
29
+ storage()?.setItem(key, value);
30
+ } catch {
31
+ // Storage unavailable or full: the value is simply not persisted.
32
+ }
33
+ },
34
+
35
+ /** Removes the value; does nothing if storage is unavailable. */
36
+ remove(key: string): void {
37
+ try {
38
+ storage()?.removeItem(key);
39
+ } catch {
40
+ // Storage unavailable: nothing to remove.
41
+ }
42
+ },
43
+ } as const;