@edenapp/solid-kit 0.15.2 → 0.17.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.
@@ -0,0 +1,217 @@
1
+ import type { NotificationType } from "@edenapp/types";
2
+ import {
3
+ type Accessor,
4
+ createEffect,
5
+ createMemo,
6
+ createSignal,
7
+ getOwner,
8
+ onCleanup,
9
+ untrack,
10
+ } from "solid-js";
11
+
12
+ export type NoticeSeverity = NotificationType;
13
+
14
+ export interface NoticeContent {
15
+ severity: NoticeSeverity;
16
+ title: string;
17
+ detail?: string;
18
+ }
19
+
20
+ export interface NoticeReport extends NoticeContent {
21
+ /** A report with the key of a shown notice replaces it and increments its count. */
22
+ key?: string;
23
+ }
24
+
25
+ export interface Notice extends NoticeContent {
26
+ id: string;
27
+ kind: "event" | "condition";
28
+ key?: string;
29
+ count: number;
30
+ }
31
+
32
+ export interface NoticeAttemptOptions {
33
+ title: string;
34
+ key?: string;
35
+ severity?: "danger" | "warning";
36
+ }
37
+
38
+ export interface Notices {
39
+ /**
40
+ * Shown notices, most severe first. Within a severity, conditions come
41
+ * first, then events, most recently added first. A keyed repeat keeps its
42
+ * notice's place.
43
+ */
44
+ readonly list: Accessor<readonly Notice[]>;
45
+ report(notice: NoticeReport): string;
46
+ dismiss(id: string): void;
47
+ /**
48
+ * Shows a notice while `when` is true. The operator cannot dismiss it. Call
49
+ * inside a reactive owner, such as a component body; the notice is removed
50
+ * when the owner is disposed. Throws when called without an owner, for
51
+ * example after an `await` or in an event handler.
52
+ */
53
+ condition(
54
+ when: Accessor<boolean>,
55
+ content: NoticeContent | Accessor<NoticeContent>,
56
+ ): void;
57
+ /**
58
+ * Runs an action. When it throws, logs the error, reports a notice with the
59
+ * error message as detail, and resolves to `undefined`.
60
+ */
61
+ attempt<T>(
62
+ action: () => Promise<T>,
63
+ options: NoticeAttemptOptions,
64
+ ): Promise<T | undefined>;
65
+ }
66
+
67
+ export interface CreateNoticesOptions {
68
+ /** Time after which info and success notices hide. */
69
+ autoHideMs?: number;
70
+ }
71
+
72
+ export const DEFAULT_NOTICE_AUTO_HIDE_MS = 4_000;
73
+
74
+ /** The more urgent a severity, the earlier its notices are shown. */
75
+ const SEVERITY_RANK: Record<NoticeSeverity, number> = {
76
+ danger: 0,
77
+ warning: 1,
78
+ info: 2,
79
+ success: 3,
80
+ };
81
+
82
+ const hidesBySelf = (severity: NoticeSeverity) =>
83
+ severity === "info" || severity === "success";
84
+
85
+ const contentOf = (content: NoticeContent): NoticeContent => ({
86
+ severity: content.severity,
87
+ title: content.title,
88
+ detail: content.detail,
89
+ });
90
+
91
+ export function errorDetail(error: unknown): string {
92
+ return error instanceof Error ? error.message : String(error);
93
+ }
94
+
95
+ /** Creates notice state owned by the current reactive owner. */
96
+ export function createNotices(options: CreateNoticesOptions = {}): Notices {
97
+ const autoHideMs = options.autoHideMs ?? DEFAULT_NOTICE_AUTO_HIDE_MS;
98
+ const [events, setEvents] = createSignal<readonly Notice[]>([]);
99
+ const [conditions, setConditions] = createSignal<readonly Notice[]>([]);
100
+ const timers = new Map<string, ReturnType<typeof setTimeout>>();
101
+ let disposed = false;
102
+ let nextId = 0;
103
+ const createId = () => `eden-notice-${nextId++}`;
104
+
105
+ const clearTimer = (id: string) => {
106
+ const timer = timers.get(id);
107
+ if (timer === undefined) return;
108
+ clearTimeout(timer);
109
+ timers.delete(id);
110
+ };
111
+
112
+ const dismiss = (id: string) => {
113
+ clearTimer(id);
114
+ setEvents((list) => list.filter((notice) => notice.id !== id));
115
+ };
116
+
117
+ const report = (input: NoticeReport) => {
118
+ // A report after the owner is disposed has nowhere to show.
119
+ if (disposed) return createId();
120
+ // Untracked, so that a caller inside a reactive computation does not
121
+ // depend on the notices it reports and re-run on its own report.
122
+ const existing =
123
+ input.key === undefined
124
+ ? undefined
125
+ : untrack(events).find((notice) => notice.key === input.key);
126
+ const notice: Notice = {
127
+ ...contentOf(input),
128
+ id: existing?.id ?? createId(),
129
+ kind: "event",
130
+ key: input.key,
131
+ count: (existing?.count ?? 0) + 1,
132
+ };
133
+
134
+ // A repeat replaces its notice in place, so the notices around it, and
135
+ // the one under the pointer or keyboard focus, do not move.
136
+ setEvents((list) =>
137
+ existing
138
+ ? list.map((shown) => (shown.id === notice.id ? notice : shown))
139
+ : [notice, ...list],
140
+ );
141
+ clearTimer(notice.id);
142
+ if (hidesBySelf(notice.severity)) {
143
+ timers.set(
144
+ notice.id,
145
+ setTimeout(() => dismiss(notice.id), autoHideMs),
146
+ );
147
+ }
148
+ return notice.id;
149
+ };
150
+
151
+ const removeCondition = (id: string) =>
152
+ setConditions((list) => list.filter((notice) => notice.id !== id));
153
+
154
+ const condition: Notices["condition"] = (when, content) => {
155
+ if (!getOwner()) {
156
+ throw new Error(
157
+ "notices.condition must be called inside a reactive owner, such as a component body",
158
+ );
159
+ }
160
+ const id = createId();
161
+ const read = typeof content === "function" ? content : () => content;
162
+
163
+ createEffect(() => {
164
+ if (!when()) {
165
+ removeCondition(id);
166
+ return;
167
+ }
168
+ const notice: Notice = {
169
+ ...contentOf(read()),
170
+ id,
171
+ kind: "condition",
172
+ count: 1,
173
+ };
174
+ setConditions((list) =>
175
+ list.some((shown) => shown.id === id)
176
+ ? list.map((shown) => (shown.id === id ? notice : shown))
177
+ : [...list, notice],
178
+ );
179
+ });
180
+ onCleanup(() => removeCondition(id));
181
+ };
182
+
183
+ const attempt = async <T>(
184
+ action: () => Promise<T>,
185
+ attemptOptions: NoticeAttemptOptions,
186
+ ): Promise<T | undefined> => {
187
+ try {
188
+ return await action();
189
+ } catch (error) {
190
+ console.error(attemptOptions.title, error);
191
+ report({
192
+ severity: attemptOptions.severity ?? "danger",
193
+ title: attemptOptions.title,
194
+ detail: errorDetail(error),
195
+ key: attemptOptions.key,
196
+ });
197
+ return undefined;
198
+ }
199
+ };
200
+
201
+ onCleanup(() => {
202
+ disposed = true;
203
+ for (const timer of timers.values()) clearTimeout(timer);
204
+ timers.clear();
205
+ });
206
+
207
+ // Array.prototype.sort is stable, so each severity keeps conditions first
208
+ // and events most recently added first.
209
+ const list = createMemo(() =>
210
+ [...conditions(), ...events()].sort(
211
+ (left, right) =>
212
+ SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity],
213
+ ),
214
+ );
215
+
216
+ return { list, report, dismiss, condition, attempt };
217
+ }
package/src/notices.ts ADDED
@@ -0,0 +1,21 @@
1
+ export {
2
+ type CreateNoticesOptions,
3
+ createNotices,
4
+ DEFAULT_NOTICE_AUTO_HIDE_MS,
5
+ errorDetail,
6
+ type Notice,
7
+ type NoticeAttemptOptions,
8
+ type NoticeContent,
9
+ type NoticeReport,
10
+ type NoticeSeverity,
11
+ type Notices,
12
+ } from "./notices/createNotices.js";
13
+ export {
14
+ NoticeProvider,
15
+ type NoticeProviderProps,
16
+ useNotices,
17
+ } from "./notices/NoticeProvider.js";
18
+ export {
19
+ NOTICE_MAX_VISIBLE,
20
+ NoticeRegion,
21
+ } from "./notices/NoticeRegion.js";
package/src/sidebar.tsx CHANGED
@@ -9,13 +9,13 @@ import {
9
9
  Show,
10
10
  splitProps,
11
11
  } from "solid-js";
12
+ import { solidKitStrings } from "./locale.js";
12
13
 
13
14
  export interface SidebarProps extends JSX.HTMLAttributes<HTMLElement> {
14
15
  label: string;
15
16
  heading?: JSX.Element;
16
17
  footer?: JSX.Element;
17
18
  size?: "default" | "compact" | "wide";
18
- toggleLabels: { collapse: string; expand: string };
19
19
  collapsed?: boolean;
20
20
  defaultCollapsed?: boolean;
21
21
  /** Automatically fold below this viewport width until the user toggles. Set 0 to disable. */
@@ -29,7 +29,6 @@ export function Sidebar(props: SidebarProps) {
29
29
  "heading",
30
30
  "footer",
31
31
  "size",
32
- "toggleLabels",
33
32
  "collapsed",
34
33
  "defaultCollapsed",
35
34
  "collapseBelow",
@@ -45,8 +44,10 @@ export function Sidebar(props: SidebarProps) {
45
44
  const collapsed = () => local.collapsed ?? manualCollapsed() ?? narrow();
46
45
  const contentId = `eden-sidebar-${createUniqueId()}`;
47
46
  const footer = children(() => local.footer);
48
- const toggleLabel = () =>
49
- collapsed() ? local.toggleLabels.expand : local.toggleLabels.collapse;
47
+ const toggleLabel = () => {
48
+ const labels = solidKitStrings("sidebar");
49
+ return collapsed() ? labels.expand : labels.collapse;
50
+ };
50
51
 
51
52
  createEffect(() => {
52
53
  const breakpoint = local.collapseBelow ?? 760;