@edenapp/solid-kit 0.16.0 → 0.17.1
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/README.md +60 -10
- package/package.json +14 -3
- package/src/KeyboardButton.tsx +4 -3
- package/src/datetime/DateTimeField.css +73 -0
- package/src/datetime/DateTimeField.test.tsx +118 -0
- package/src/datetime/DateTimeField.tsx +288 -0
- package/src/datetime.ts +6 -0
- package/src/dialogs/createDialogs.tsx +9 -7
- package/src/locale.test.ts +40 -0
- package/src/locale.ts +79 -0
- package/src/locales/en.ts +36 -0
- package/src/locales/pl.ts +26 -0
- package/src/notices/NoticeProvider.test.tsx +31 -9
- package/src/notices/NoticeProvider.tsx +2 -4
- package/src/notices/NoticeRegion.tsx +11 -18
- package/src/notices.ts +0 -1
- package/src/sidebar.tsx +5 -4
package/README.md
CHANGED
|
@@ -2,6 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
High-level SolidJS UI utilities for Eden renderer apps.
|
|
4
4
|
|
|
5
|
+
## Language
|
|
6
|
+
|
|
7
|
+
Solid-kit components have built-in text, such as the Cancel button of a dialog
|
|
8
|
+
and the names of icon buttons. They show it in the app's language: the locale
|
|
9
|
+
of the app's `setupI18n` from `@edenapp/babel/solid` (see
|
|
10
|
+
[Translate an app](../../docs/localizing-apps.md)). Without it, they show
|
|
11
|
+
English.
|
|
12
|
+
|
|
13
|
+
Solid-kit includes English and Polish. A regional locale such as `pl-PL` uses
|
|
14
|
+
its language. To add a language or change a string, put it under `solidKit` in
|
|
15
|
+
the app's locale files. Strings the app leaves out use solid-kit's own, then
|
|
16
|
+
English. `SolidKitTranslations` from `@edenapp/solid-kit/locale` lists the
|
|
17
|
+
keys:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
export const de = {
|
|
21
|
+
solidKit: {
|
|
22
|
+
dialogs: { cancel: "Abbrechen" },
|
|
23
|
+
notices: { showMore: "{count} weitere" },
|
|
24
|
+
},
|
|
25
|
+
notes: { welcome: "Willkommen" },
|
|
26
|
+
} as const;
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Dialogs still take `okLabel`, `confirmLabel`, and `cancelLabel` for text that
|
|
30
|
+
belongs to one dialog, such as "Delete".
|
|
31
|
+
|
|
5
32
|
## Sidebars
|
|
6
33
|
|
|
7
34
|
`@edenapp/solid-kit/sidebar` wraps EdenCSS sidebar classes with Solid components.
|
|
@@ -16,7 +43,6 @@ import { FiHome } from "solid-icons/fi";
|
|
|
16
43
|
label="Navigation"
|
|
17
44
|
heading="Places"
|
|
18
45
|
size="compact"
|
|
19
|
-
toggleLabels={{ collapse: "Collapse sidebar", expand: "Expand sidebar" }}
|
|
20
46
|
>
|
|
21
47
|
<SidebarSection>
|
|
22
48
|
<SidebarItem
|
|
@@ -30,7 +56,8 @@ import { FiHome } from "solid-icons/fi";
|
|
|
30
56
|
</Sidebar>
|
|
31
57
|
```
|
|
32
58
|
|
|
33
|
-
Pass
|
|
59
|
+
Pass the sidebar and item labels from your app. The fold button has a
|
|
60
|
+
[built-in name](#language). Items retain their accessible names and
|
|
34
61
|
tooltips when folded. `SidebarSection` accepts an optional `title`;
|
|
35
62
|
`SidebarDivider` separates sections. `SidebarItem` supports `description`,
|
|
36
63
|
`disabled`, and an optional `action` containing a `SidebarItemAction`. Action
|
|
@@ -101,18 +128,13 @@ started in the app, and for problems that the app observes. Use the toaster
|
|
|
101
128
|
(`notification` from `@edenapp/tablets`) for failures that are not owned by
|
|
102
129
|
one app.
|
|
103
130
|
|
|
104
|
-
Wrap the app in `NoticeProvider
|
|
131
|
+
Wrap the app in `NoticeProvider`. Its buttons have
|
|
132
|
+
[built-in text](#language):
|
|
105
133
|
|
|
106
134
|
```tsx
|
|
107
135
|
import { NoticeProvider, useNotices } from "@edenapp/solid-kit/notices";
|
|
108
136
|
|
|
109
|
-
<NoticeProvider
|
|
110
|
-
labels={{
|
|
111
|
-
dismiss: t("common.dismiss"),
|
|
112
|
-
showMore: (hidden) => t("notices.showMore", { count: hidden }),
|
|
113
|
-
showLess: t("notices.showLess"),
|
|
114
|
-
}}
|
|
115
|
-
>
|
|
137
|
+
<NoticeProvider>
|
|
116
138
|
<App />
|
|
117
139
|
</NoticeProvider>
|
|
118
140
|
```
|
|
@@ -152,6 +174,34 @@ dismiss button and clear when their condition is false or their owner is
|
|
|
152
174
|
disposed. Three notices are visible at a time; a button shows the others.
|
|
153
175
|
Screen readers announce danger notices immediately and other notices politely.
|
|
154
176
|
|
|
177
|
+
## Date and time fields
|
|
178
|
+
|
|
179
|
+
`@edenapp/solid-kit/datetime` lets the user choose a date and time by touch.
|
|
180
|
+
`DateTimeField` shows its value on a button. The button opens a floating panel
|
|
181
|
+
with steppers for the day, hour, and minute. Hold a stepper to repeat it. Minutes move in steps of
|
|
182
|
+
`minuteStep` (5 by default), and the field stays between `min` and `max`. A tap
|
|
183
|
+
outside the panel, Escape, scrolling, or resizing closes it. Inside another
|
|
184
|
+
panel, such as a filter popover, these close only the field's panel.
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
import { DateTimeField } from "@edenapp/solid-kit/datetime";
|
|
188
|
+
import { t } from "./i18n";
|
|
189
|
+
|
|
190
|
+
<DateTimeField
|
|
191
|
+
label={t("logs.from")}
|
|
192
|
+
value={from()}
|
|
193
|
+
onChange={setFrom}
|
|
194
|
+
placeholder={t("logs.anyTime")}
|
|
195
|
+
max={new Date()}
|
|
196
|
+
/>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
The field formats its value and shows its text in the
|
|
200
|
+
[app's language](#language).
|
|
201
|
+
|
|
202
|
+
The field's styles are part of this entry point, not EdenCSS, so only apps that
|
|
203
|
+
import it load them. The root `@edenapp/solid-kit` entry does not export it.
|
|
204
|
+
|
|
155
205
|
## Dialogs
|
|
156
206
|
|
|
157
207
|
`@edenapp/solid-kit/dialogs` provides local, in-renderer dialogs with a tiny promise API.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edenapp/solid-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.1",
|
|
4
4
|
"description": "High-level SolidJS UI primitives for Eden apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -11,11 +11,21 @@
|
|
|
11
11
|
"import": "./src/index.ts",
|
|
12
12
|
"default": "./src/index.ts"
|
|
13
13
|
},
|
|
14
|
+
"./datetime": {
|
|
15
|
+
"types": "./src/datetime.ts",
|
|
16
|
+
"import": "./src/datetime.ts",
|
|
17
|
+
"default": "./src/datetime.ts"
|
|
18
|
+
},
|
|
14
19
|
"./dialogs": {
|
|
15
20
|
"types": "./src/dialogs.ts",
|
|
16
21
|
"import": "./src/dialogs.ts",
|
|
17
22
|
"default": "./src/dialogs.ts"
|
|
18
23
|
},
|
|
24
|
+
"./locale": {
|
|
25
|
+
"types": "./src/locale.ts",
|
|
26
|
+
"import": "./src/locale.ts",
|
|
27
|
+
"default": "./src/locale.ts"
|
|
28
|
+
},
|
|
19
29
|
"./notices": {
|
|
20
30
|
"types": "./src/notices.ts",
|
|
21
31
|
"import": "./src/notices.ts",
|
|
@@ -35,7 +45,8 @@
|
|
|
35
45
|
},
|
|
36
46
|
"dependencies": {
|
|
37
47
|
"solid-icons": "^1.2.0",
|
|
38
|
-
"@edenapp/
|
|
48
|
+
"@edenapp/babel": "0.17.1",
|
|
49
|
+
"@edenapp/client": "0.17.1"
|
|
39
50
|
},
|
|
40
51
|
"devDependencies": {
|
|
41
52
|
"@solidjs/testing-library": "^0.8.10",
|
|
@@ -43,7 +54,7 @@
|
|
|
43
54
|
"typescript": "^6.0.3",
|
|
44
55
|
"vite-plugin-solid": "^2.11.14",
|
|
45
56
|
"vitest": "^4.1.10",
|
|
46
|
-
"@edenapp/types": "0.
|
|
57
|
+
"@edenapp/types": "0.17.1"
|
|
47
58
|
},
|
|
48
59
|
"keywords": [
|
|
49
60
|
"eden",
|
package/src/KeyboardButton.tsx
CHANGED
|
@@ -2,9 +2,9 @@ import type { EdenKeyboardState } from "@edenapp/types";
|
|
|
2
2
|
import { BiSolidKeyboard } from "solid-icons/bi";
|
|
3
3
|
import { createSignal, onCleanup, onMount, Show } from "solid-js";
|
|
4
4
|
import "./KeyboardButton.css";
|
|
5
|
+
import { solidKitStrings } from "./locale.js";
|
|
5
6
|
|
|
6
7
|
export interface KeyboardButtonProps {
|
|
7
|
-
label: string;
|
|
8
8
|
class?: string;
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -18,6 +18,7 @@ const defaultKeyboardState: EdenKeyboardState = {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
export function KeyboardButton(props: KeyboardButtonProps) {
|
|
21
|
+
const label = () => solidKitStrings("keyboard").toggle;
|
|
21
22
|
const [keyboardState, setKeyboardState] =
|
|
22
23
|
createSignal<EdenKeyboardState>(defaultKeyboardState);
|
|
23
24
|
let keyboardVisibleAtPointerDown: boolean | null = null;
|
|
@@ -70,8 +71,8 @@ export function KeyboardButton(props: KeyboardButtonProps) {
|
|
|
70
71
|
classList={{ active: keyboardState().visible }}
|
|
71
72
|
onClick={() => void handleToggle()}
|
|
72
73
|
onPointerDown={handlePointerDown}
|
|
73
|
-
title={
|
|
74
|
-
aria-label={
|
|
74
|
+
title={label()}
|
|
75
|
+
aria-label={label()}
|
|
75
76
|
>
|
|
76
77
|
<BiSolidKeyboard />
|
|
77
78
|
</button>
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
.eden-datetime-field {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
gap: var(--eden-space-xs);
|
|
5
|
+
min-width: 0;
|
|
6
|
+
font-family: var(--eden-font-family-base);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.eden-datetime-label {
|
|
10
|
+
font-size: var(--eden-font-size-base);
|
|
11
|
+
color: var(--eden-color-text-secondary);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.eden-datetime-value {
|
|
15
|
+
display: flex;
|
|
16
|
+
align-items: center;
|
|
17
|
+
gap: var(--eden-space-sm);
|
|
18
|
+
text-align: start;
|
|
19
|
+
cursor: pointer;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.eden-datetime-value svg {
|
|
23
|
+
flex: none;
|
|
24
|
+
color: var(--eden-color-text-secondary);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.eden-datetime-value [data-placeholder="true"] {
|
|
28
|
+
color: var(--eden-color-text-muted);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.eden-datetime-panel {
|
|
32
|
+
position: fixed;
|
|
33
|
+
z-index: var(--eden-z-popover);
|
|
34
|
+
font-family: var(--eden-font-family-base);
|
|
35
|
+
box-shadow: var(--eden-shadow-lg);
|
|
36
|
+
display: grid;
|
|
37
|
+
grid-template-columns: max-content auto minmax(6em, 1fr) auto;
|
|
38
|
+
align-items: center;
|
|
39
|
+
gap: var(--eden-space-xs) var(--eden-space-sm);
|
|
40
|
+
padding: var(--eden-space-sm);
|
|
41
|
+
border: 1px solid var(--eden-color-border-medium);
|
|
42
|
+
border-radius: var(--eden-radius-lg);
|
|
43
|
+
background: var(--eden-color-bg-secondary);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.eden-datetime-step {
|
|
47
|
+
display: contents;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.eden-datetime-step-name {
|
|
51
|
+
font-size: var(--eden-font-size-base);
|
|
52
|
+
color: var(--eden-color-text-secondary);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.eden-datetime-step-value {
|
|
56
|
+
text-align: center;
|
|
57
|
+
font-size: var(--eden-font-size-md);
|
|
58
|
+
font-variant-numeric: tabular-nums;
|
|
59
|
+
color: var(--eden-color-text-primary);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* Holding a stepper repeats it; keep the long press from selecting text. */
|
|
63
|
+
.eden-datetime-step .eden-btn {
|
|
64
|
+
touch-action: manipulation;
|
|
65
|
+
user-select: none;
|
|
66
|
+
-webkit-user-select: none;
|
|
67
|
+
-webkit-touch-callout: none;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.eden-datetime-done {
|
|
71
|
+
grid-column: 1 / -1;
|
|
72
|
+
justify-self: end;
|
|
73
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { setupI18n } from "@edenapp/babel/solid";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library";
|
|
3
|
+
import { createSignal } from "solid-js";
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { DateTimeField, stepDateTime } from "./DateTimeField.js";
|
|
6
|
+
|
|
7
|
+
function renderField(initial: Date, bounds: { min?: Date; max?: Date } = {}) {
|
|
8
|
+
const [value, setValue] = createSignal<Date | undefined>(initial);
|
|
9
|
+
render(() => (
|
|
10
|
+
<DateTimeField
|
|
11
|
+
label="From"
|
|
12
|
+
value={value()}
|
|
13
|
+
onChange={setValue}
|
|
14
|
+
placeholder="Any time"
|
|
15
|
+
{...bounds}
|
|
16
|
+
/>
|
|
17
|
+
));
|
|
18
|
+
fireEvent.click(screen.getByLabelText("From"));
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const app = setupI18n({ resources: {} });
|
|
23
|
+
|
|
24
|
+
describe("stepDateTime", () => {
|
|
25
|
+
it("steps minutes onto whole steps, carries into the hour and drops seconds", () => {
|
|
26
|
+
const at = new Date(2026, 10, 25, 14, 57, 30);
|
|
27
|
+
expect(stepDateTime(at, "minute", 1)).toEqual(
|
|
28
|
+
new Date(2026, 10, 25, 15, 0),
|
|
29
|
+
);
|
|
30
|
+
expect(stepDateTime(at, "minute", -1)).toEqual(
|
|
31
|
+
new Date(2026, 10, 25, 14, 55),
|
|
32
|
+
);
|
|
33
|
+
expect(
|
|
34
|
+
stepDateTime(new Date(2026, 10, 25, 0, 0), "minute", -1, {
|
|
35
|
+
minuteStep: 15,
|
|
36
|
+
}),
|
|
37
|
+
).toEqual(new Date(2026, 10, 24, 23, 45));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("DateTimeField", () => {
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
cleanup();
|
|
44
|
+
app.setLocale("en");
|
|
45
|
+
vi.useRealTimers();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("stops at its bounds and disables the step that would pass them", () => {
|
|
49
|
+
const min = new Date(2026, 10, 25, 14, 0);
|
|
50
|
+
const value = renderField(new Date(2026, 10, 25, 14, 30), { min });
|
|
51
|
+
|
|
52
|
+
fireEvent.click(screen.getByRole("button", { name: "Earlier day" }));
|
|
53
|
+
expect(value()).toEqual(min);
|
|
54
|
+
expect(
|
|
55
|
+
(
|
|
56
|
+
screen.getByRole("button", {
|
|
57
|
+
name: "Earlier hour",
|
|
58
|
+
}) as HTMLButtonElement
|
|
59
|
+
).disabled,
|
|
60
|
+
).toBe(true);
|
|
61
|
+
expect(
|
|
62
|
+
(screen.getByRole("button", { name: "Later hour" }) as HTMLButtonElement)
|
|
63
|
+
.disabled,
|
|
64
|
+
).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("switches language with the app's locale while open", () => {
|
|
68
|
+
renderField(new Date(2026, 10, 25, 14, 0));
|
|
69
|
+
expect(screen.getByRole("button", { name: "Done" })).toBeTruthy();
|
|
70
|
+
app.setLocale("pl");
|
|
71
|
+
expect(screen.getByRole("button", { name: "Gotowe" })).toBeTruthy();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("floats until a tap outside or Escape, which stop at its panel", () => {
|
|
75
|
+
renderField(new Date(2026, 10, 25, 14, 0));
|
|
76
|
+
const seen: string[] = [];
|
|
77
|
+
const listen = (event: Event) => seen.push(event.type);
|
|
78
|
+
document.addEventListener("pointerdown", listen);
|
|
79
|
+
document.addEventListener("keydown", listen);
|
|
80
|
+
try {
|
|
81
|
+
// Steppers inside do not count as a tap outside a surrounding panel.
|
|
82
|
+
fireEvent.pointerDown(screen.getByRole("button", { name: "Later hour" }));
|
|
83
|
+
expect(seen).toEqual([]);
|
|
84
|
+
fireEvent.keyDown(document.body, { key: "Escape" });
|
|
85
|
+
expect(screen.queryByRole("button", { name: "Done" })).toBeNull();
|
|
86
|
+
expect(seen).toEqual([]);
|
|
87
|
+
|
|
88
|
+
fireEvent.click(screen.getByLabelText("From"));
|
|
89
|
+
fireEvent.pointerDown(document.body);
|
|
90
|
+
expect(screen.queryByRole("button", { name: "Done" })).toBeNull();
|
|
91
|
+
} finally {
|
|
92
|
+
document.removeEventListener("pointerdown", listen);
|
|
93
|
+
document.removeEventListener("keydown", listen);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("steps once per tap and repeats while held", () => {
|
|
98
|
+
vi.useFakeTimers();
|
|
99
|
+
const value = renderField(new Date(2026, 10, 25, 14, 0));
|
|
100
|
+
const later = screen.getByRole("button", { name: "Later minute" });
|
|
101
|
+
|
|
102
|
+
// A tap fires pointerdown, pointerup and click: one step.
|
|
103
|
+
fireEvent.pointerDown(later);
|
|
104
|
+
fireEvent.pointerUp(window);
|
|
105
|
+
fireEvent.click(later);
|
|
106
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 5));
|
|
107
|
+
vi.advanceTimersByTime(1000);
|
|
108
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 5));
|
|
109
|
+
|
|
110
|
+
// Held: one step now, then repeats after a pause until released.
|
|
111
|
+
fireEvent.pointerDown(later);
|
|
112
|
+
vi.advanceTimersByTime(400 + 3 * 100);
|
|
113
|
+
fireEvent.pointerUp(window);
|
|
114
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 25));
|
|
115
|
+
vi.advanceTimersByTime(1000);
|
|
116
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 25));
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BiRegularCalendar,
|
|
3
|
+
BiRegularMinus,
|
|
4
|
+
BiRegularPlus,
|
|
5
|
+
} from "solid-icons/bi";
|
|
6
|
+
import {
|
|
7
|
+
createEffect,
|
|
8
|
+
createSignal,
|
|
9
|
+
createUniqueId,
|
|
10
|
+
For,
|
|
11
|
+
onCleanup,
|
|
12
|
+
Show,
|
|
13
|
+
} from "solid-js";
|
|
14
|
+
import { Portal } from "solid-js/web";
|
|
15
|
+
import "./DateTimeField.css";
|
|
16
|
+
import { solidKitLocale, solidKitStrings } from "../locale.js";
|
|
17
|
+
|
|
18
|
+
export type DateTimeUnit = "day" | "hour" | "minute";
|
|
19
|
+
|
|
20
|
+
export interface DateTimeFieldProps {
|
|
21
|
+
/** Names the field, such as "From". */
|
|
22
|
+
label: string;
|
|
23
|
+
value: Date | undefined;
|
|
24
|
+
onChange: (value: Date) => void;
|
|
25
|
+
/** Shown while `value` is unset. */
|
|
26
|
+
placeholder: string;
|
|
27
|
+
min?: Date;
|
|
28
|
+
max?: Date;
|
|
29
|
+
/** Minutes moved by one minute step. Defaults to 5. */
|
|
30
|
+
minuteStep?: number;
|
|
31
|
+
class?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const units: readonly DateTimeUnit[] = ["day", "hour", "minute"];
|
|
35
|
+
const repeatDelayMs = 400;
|
|
36
|
+
const repeatEveryMs = 100;
|
|
37
|
+
|
|
38
|
+
/** `time` moved one step of `unit` in `direction`, kept within the bounds. */
|
|
39
|
+
export function stepDateTime(
|
|
40
|
+
time: Date,
|
|
41
|
+
unit: DateTimeUnit,
|
|
42
|
+
direction: 1 | -1,
|
|
43
|
+
options: { minuteStep?: number; min?: Date; max?: Date } = {},
|
|
44
|
+
): Date {
|
|
45
|
+
const next = new Date(time);
|
|
46
|
+
next.setSeconds(0, 0);
|
|
47
|
+
if (unit === "day") next.setDate(next.getDate() + direction);
|
|
48
|
+
else if (unit === "hour") next.setHours(next.getHours() + direction);
|
|
49
|
+
else {
|
|
50
|
+
// Land on a multiple of the step, so 14:57 goes to 15:00 or 14:55.
|
|
51
|
+
const step = Math.max(1, options.minuteStep ?? 5);
|
|
52
|
+
const minutes = next.getMinutes();
|
|
53
|
+
next.setMinutes(
|
|
54
|
+
direction > 0
|
|
55
|
+
? Math.floor(minutes / step) * step + step
|
|
56
|
+
: Math.ceil(minutes / step) * step - step,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
if (options.min && next < options.min) return new Date(options.min);
|
|
60
|
+
if (options.max && next > options.max) return new Date(options.max);
|
|
61
|
+
return next;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A date and time chosen by touch: a button shows the value and opens steppers
|
|
66
|
+
* for the day, hour and minute below it. Holding a stepper repeats it.
|
|
67
|
+
*/
|
|
68
|
+
export function DateTimeField(props: DateTimeFieldProps) {
|
|
69
|
+
const [open, setOpen] = createSignal(false);
|
|
70
|
+
const [place, setPlace] = createSignal<{
|
|
71
|
+
left: number;
|
|
72
|
+
width: number;
|
|
73
|
+
top?: number;
|
|
74
|
+
bottom?: number;
|
|
75
|
+
}>({ left: 0, width: 0 });
|
|
76
|
+
let field: HTMLButtonElement | undefined;
|
|
77
|
+
let panel: HTMLDivElement | undefined;
|
|
78
|
+
const gap = 4;
|
|
79
|
+
// Under the field, or above it when it does not fit below.
|
|
80
|
+
const placePanel = () => {
|
|
81
|
+
if (!field) return;
|
|
82
|
+
const rect = field.getBoundingClientRect();
|
|
83
|
+
const width = Math.min(
|
|
84
|
+
Math.max(rect.width, 300),
|
|
85
|
+
window.innerWidth - 2 * gap,
|
|
86
|
+
);
|
|
87
|
+
const left = Math.min(
|
|
88
|
+
Math.max(gap, rect.left),
|
|
89
|
+
window.innerWidth - width - gap,
|
|
90
|
+
);
|
|
91
|
+
const height = panel?.offsetHeight ?? 0;
|
|
92
|
+
const below = window.innerHeight - rect.bottom - gap;
|
|
93
|
+
setPlace(
|
|
94
|
+
height > below && rect.top - gap > below
|
|
95
|
+
? { left, width, bottom: window.innerHeight - rect.top + gap }
|
|
96
|
+
: { left, width, top: rect.bottom + gap },
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
const toggle = () => {
|
|
100
|
+
if (open()) return setOpen(false);
|
|
101
|
+
placePanel();
|
|
102
|
+
setOpen(true);
|
|
103
|
+
// Measured once shown, to flip it above the field if needed.
|
|
104
|
+
queueMicrotask(placePanel);
|
|
105
|
+
};
|
|
106
|
+
// While open: a tap outside, Escape, a scroll or a resize closes it. Escape
|
|
107
|
+
// and taps inside stop here, so a panel the field sits in stays open.
|
|
108
|
+
createEffect(() => {
|
|
109
|
+
if (!open()) return;
|
|
110
|
+
const outside = (event: PointerEvent) => {
|
|
111
|
+
const target = event.target as Node;
|
|
112
|
+
if (!field?.contains(target) && !panel?.contains(target)) {
|
|
113
|
+
setOpen(false);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const closeOnEscape = (event: KeyboardEvent) => {
|
|
117
|
+
if (event.key !== "Escape") return;
|
|
118
|
+
event.stopPropagation();
|
|
119
|
+
setOpen(false);
|
|
120
|
+
};
|
|
121
|
+
const scroll = (event: Event) => {
|
|
122
|
+
if (!panel?.contains(event.target as Node)) setOpen(false);
|
|
123
|
+
};
|
|
124
|
+
const close = () => setOpen(false);
|
|
125
|
+
document.addEventListener("pointerdown", outside, true);
|
|
126
|
+
window.addEventListener("keydown", closeOnEscape, true);
|
|
127
|
+
window.addEventListener("scroll", scroll, true);
|
|
128
|
+
window.addEventListener("resize", close);
|
|
129
|
+
onCleanup(() => {
|
|
130
|
+
document.removeEventListener("pointerdown", outside, true);
|
|
131
|
+
window.removeEventListener("keydown", closeOnEscape, true);
|
|
132
|
+
window.removeEventListener("scroll", scroll, true);
|
|
133
|
+
window.removeEventListener("resize", close);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
const strings = () => solidKitStrings("datetime");
|
|
137
|
+
const id = `eden-datetime-${createUniqueId()}`;
|
|
138
|
+
const format = (options: Intl.DateTimeFormatOptions, time: Date) =>
|
|
139
|
+
new Intl.DateTimeFormat(solidKitLocale(), options).format(time);
|
|
140
|
+
const bounds = () => ({
|
|
141
|
+
minuteStep: props.minuteStep,
|
|
142
|
+
min: props.min,
|
|
143
|
+
max: props.max,
|
|
144
|
+
});
|
|
145
|
+
// An unset field starts from now, on a whole step, within the bounds.
|
|
146
|
+
const shown = () => {
|
|
147
|
+
if (props.value) return props.value;
|
|
148
|
+
const now = new Date();
|
|
149
|
+
return stepDateTime(
|
|
150
|
+
stepDateTime(now, "minute", 1, bounds()),
|
|
151
|
+
"minute",
|
|
152
|
+
-1,
|
|
153
|
+
bounds(),
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
const step = (unit: DateTimeUnit, direction: 1 | -1) =>
|
|
157
|
+
stepDateTime(shown(), unit, direction, bounds());
|
|
158
|
+
const canStep = (unit: DateTimeUnit, direction: 1 | -1) =>
|
|
159
|
+
step(unit, direction).getTime() !== shown().getTime();
|
|
160
|
+
const text = (unit: DateTimeUnit, time: Date) => {
|
|
161
|
+
if (unit === "day") {
|
|
162
|
+
return format({ weekday: "short", day: "numeric", month: "short" }, time);
|
|
163
|
+
}
|
|
164
|
+
if (unit === "hour") return format({ hour: "numeric" }, time);
|
|
165
|
+
return String(time.getMinutes()).padStart(2, "0");
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const stepper = (unit: DateTimeUnit, direction: 1 | -1) => {
|
|
169
|
+
let delay: ReturnType<typeof setTimeout> | undefined;
|
|
170
|
+
let repeat: ReturnType<typeof setInterval> | undefined;
|
|
171
|
+
let pressed = false;
|
|
172
|
+
const stop = () => {
|
|
173
|
+
clearTimeout(delay);
|
|
174
|
+
clearInterval(repeat);
|
|
175
|
+
window.removeEventListener("pointerup", stop);
|
|
176
|
+
window.removeEventListener("pointercancel", stop);
|
|
177
|
+
};
|
|
178
|
+
const run = () => {
|
|
179
|
+
if (!canStep(unit, direction)) return stop();
|
|
180
|
+
props.onChange(step(unit, direction));
|
|
181
|
+
};
|
|
182
|
+
onCleanup(stop);
|
|
183
|
+
const name = () => strings()[direction > 0 ? "later" : "earlier"][unit];
|
|
184
|
+
return (
|
|
185
|
+
<button
|
|
186
|
+
type="button"
|
|
187
|
+
class="eden-btn eden-btn-ghost eden-btn-square"
|
|
188
|
+
title={name()}
|
|
189
|
+
aria-label={name()}
|
|
190
|
+
disabled={!canStep(unit, direction)}
|
|
191
|
+
on:pointerdown={(event) => {
|
|
192
|
+
if (event.button > 0) return;
|
|
193
|
+
stop();
|
|
194
|
+
pressed = true;
|
|
195
|
+
run();
|
|
196
|
+
// A disabled button gets no pointerup, so listen on the window.
|
|
197
|
+
window.addEventListener("pointerup", stop);
|
|
198
|
+
window.addEventListener("pointercancel", stop);
|
|
199
|
+
delay = setTimeout(() => {
|
|
200
|
+
repeat = setInterval(run, repeatEveryMs);
|
|
201
|
+
}, repeatDelayMs);
|
|
202
|
+
}}
|
|
203
|
+
onPointerLeave={stop}
|
|
204
|
+
onClick={() => {
|
|
205
|
+
// A press already stepped; a click without one comes from the keyboard.
|
|
206
|
+
if (pressed) pressed = false;
|
|
207
|
+
else run();
|
|
208
|
+
}}
|
|
209
|
+
>
|
|
210
|
+
{direction > 0 ? <BiRegularPlus /> : <BiRegularMinus />}
|
|
211
|
+
</button>
|
|
212
|
+
);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
<div class={`eden-datetime-field ${props.class ?? ""}`} data-open={open()}>
|
|
217
|
+
<label class="eden-datetime-label" for={id}>
|
|
218
|
+
{props.label}
|
|
219
|
+
</label>
|
|
220
|
+
<button
|
|
221
|
+
id={id}
|
|
222
|
+
ref={field}
|
|
223
|
+
type="button"
|
|
224
|
+
class="eden-input eden-datetime-value"
|
|
225
|
+
aria-expanded={open()}
|
|
226
|
+
onClick={toggle}
|
|
227
|
+
>
|
|
228
|
+
<BiRegularCalendar />
|
|
229
|
+
<span data-placeholder={!props.value}>
|
|
230
|
+
{props.value
|
|
231
|
+
? format(
|
|
232
|
+
{
|
|
233
|
+
weekday: "short",
|
|
234
|
+
day: "numeric",
|
|
235
|
+
month: "short",
|
|
236
|
+
hour: "numeric",
|
|
237
|
+
minute: "2-digit",
|
|
238
|
+
},
|
|
239
|
+
props.value,
|
|
240
|
+
)
|
|
241
|
+
: props.placeholder}
|
|
242
|
+
</span>
|
|
243
|
+
</button>
|
|
244
|
+
<Show when={open()}>
|
|
245
|
+
<Portal>
|
|
246
|
+
<div
|
|
247
|
+
class="eden-datetime-panel"
|
|
248
|
+
ref={panel}
|
|
249
|
+
style={{
|
|
250
|
+
left: `${place().left}px`,
|
|
251
|
+
width: `${place().width}px`,
|
|
252
|
+
top: place().top === undefined ? undefined : `${place().top}px`,
|
|
253
|
+
bottom:
|
|
254
|
+
place().bottom === undefined
|
|
255
|
+
? undefined
|
|
256
|
+
: `${place().bottom}px`,
|
|
257
|
+
}}
|
|
258
|
+
// Native, not delegated: the steppers' own pointerdown listeners
|
|
259
|
+
// run first, then the event stops before document listeners.
|
|
260
|
+
on:pointerdown={(event) => event.stopPropagation()}
|
|
261
|
+
>
|
|
262
|
+
<For each={units}>
|
|
263
|
+
{(unit) => (
|
|
264
|
+
<div class="eden-datetime-step">
|
|
265
|
+
<span class="eden-datetime-step-name">
|
|
266
|
+
{strings().unit[unit]}
|
|
267
|
+
</span>
|
|
268
|
+
{stepper(unit, -1)}
|
|
269
|
+
<output class="eden-datetime-step-value">
|
|
270
|
+
{text(unit, shown())}
|
|
271
|
+
</output>
|
|
272
|
+
{stepper(unit, 1)}
|
|
273
|
+
</div>
|
|
274
|
+
)}
|
|
275
|
+
</For>
|
|
276
|
+
<button
|
|
277
|
+
type="button"
|
|
278
|
+
class="eden-btn eden-btn-primary eden-datetime-done"
|
|
279
|
+
onClick={() => setOpen(false)}
|
|
280
|
+
>
|
|
281
|
+
{strings().done}
|
|
282
|
+
</button>
|
|
283
|
+
</div>
|
|
284
|
+
</Portal>
|
|
285
|
+
</Show>
|
|
286
|
+
</div>
|
|
287
|
+
);
|
|
288
|
+
}
|
package/src/datetime.ts
ADDED
|
@@ -210,7 +210,7 @@ export const createDialogs = (): DialogController => {
|
|
|
210
210
|
class="eden-btn eden-btn-primary"
|
|
211
211
|
onClick={() => ctx.submit()}
|
|
212
212
|
>
|
|
213
|
-
{options.okLabel ?? "
|
|
213
|
+
{options.okLabel ?? solidKitStrings("dialogs").ok}
|
|
214
214
|
</button>
|
|
215
215
|
),
|
|
216
216
|
});
|
|
@@ -234,7 +234,7 @@ export const createDialogs = (): DialogController => {
|
|
|
234
234
|
footer: (ctx) => (
|
|
235
235
|
<>
|
|
236
236
|
<button type="button" class="eden-btn" onClick={() => ctx.cancel()}>
|
|
237
|
-
{options.cancelLabel ?? "
|
|
237
|
+
{options.cancelLabel ?? solidKitStrings("dialogs").cancel}
|
|
238
238
|
</button>
|
|
239
239
|
<button
|
|
240
240
|
type="button"
|
|
@@ -242,7 +242,7 @@ export const createDialogs = (): DialogController => {
|
|
|
242
242
|
class={confirmButtonClass(tone)}
|
|
243
243
|
onClick={() => ctx.submit(true)}
|
|
244
244
|
>
|
|
245
|
-
{options.confirmLabel ?? "
|
|
245
|
+
{options.confirmLabel ?? solidKitStrings("dialogs").ok}
|
|
246
246
|
</button>
|
|
247
247
|
</>
|
|
248
248
|
),
|
|
@@ -287,7 +287,7 @@ export const createDialogs = (): DialogController => {
|
|
|
287
287
|
footer: (ctx) => (
|
|
288
288
|
<>
|
|
289
289
|
<button type="button" class="eden-btn" onClick={() => ctx.cancel()}>
|
|
290
|
-
{options.cancelLabel ?? "
|
|
290
|
+
{options.cancelLabel ?? solidKitStrings("dialogs").cancel}
|
|
291
291
|
</button>
|
|
292
292
|
<button
|
|
293
293
|
type="button"
|
|
@@ -295,7 +295,7 @@ export const createDialogs = (): DialogController => {
|
|
|
295
295
|
class={confirmButtonClass(tone)}
|
|
296
296
|
onClick={() => ctx.submit()}
|
|
297
297
|
>
|
|
298
|
-
{options.confirmLabel ?? "
|
|
298
|
+
{options.confirmLabel ?? solidKitStrings("dialogs").ok}
|
|
299
299
|
</button>
|
|
300
300
|
</>
|
|
301
301
|
),
|
|
@@ -452,7 +452,7 @@ export const createDialogs = (): DialogController => {
|
|
|
452
452
|
footer: (ctx) => (
|
|
453
453
|
<>
|
|
454
454
|
<button type="button" class="eden-btn" onClick={() => ctx.cancel()}>
|
|
455
|
-
{options.cancelLabel ?? "
|
|
455
|
+
{options.cancelLabel ?? solidKitStrings("dialogs").cancel}
|
|
456
456
|
</button>
|
|
457
457
|
<button
|
|
458
458
|
type="button"
|
|
@@ -461,7 +461,7 @@ export const createDialogs = (): DialogController => {
|
|
|
461
461
|
disabled={!ctx.canSubmit()}
|
|
462
462
|
onClick={() => ctx.submit()}
|
|
463
463
|
>
|
|
464
|
-
{options.confirmLabel ?? "
|
|
464
|
+
{options.confirmLabel ?? solidKitStrings("dialogs").ok}
|
|
465
465
|
</button>
|
|
466
466
|
</>
|
|
467
467
|
),
|
|
@@ -490,3 +490,5 @@ export type {
|
|
|
490
490
|
DialogFormValues,
|
|
491
491
|
FormDialogOptions,
|
|
492
492
|
} from "./types.js";
|
|
493
|
+
|
|
494
|
+
import { solidKitStrings } from "../locale.js";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { setupI18n } from "@edenapp/babel/solid";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { fillString, solidKitStrings } from "./locale.js";
|
|
4
|
+
|
|
5
|
+
const app = setupI18n({
|
|
6
|
+
resources: {
|
|
7
|
+
en: { translation: { solidKit: { dialogs: { cancel: "Back" } } } },
|
|
8
|
+
de: {
|
|
9
|
+
translation: {
|
|
10
|
+
solidKit: {
|
|
11
|
+
dialogs: { cancel: "Abbrechen" },
|
|
12
|
+
sidebar: { expand: "Ausklappen" },
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("solid-kit strings", () => {
|
|
20
|
+
afterEach(() => app.setLocale("en"));
|
|
21
|
+
|
|
22
|
+
it("prefers the app's translation, then solid-kit's, then English", async () => {
|
|
23
|
+
app.setLocale("de-AT");
|
|
24
|
+
await vi.waitFor(() =>
|
|
25
|
+
expect(solidKitStrings("dialogs").cancel).toBe("Abbrechen"),
|
|
26
|
+
);
|
|
27
|
+
expect(solidKitStrings("sidebar").expand).toBe("Ausklappen");
|
|
28
|
+
expect(solidKitStrings("sidebar").collapse).toBe("Collapse sidebar");
|
|
29
|
+
|
|
30
|
+
// The app's English override is not used for Polish, which solid-kit has.
|
|
31
|
+
app.setLocale("pl-PL");
|
|
32
|
+
expect(solidKitStrings("dialogs").cancel).toBe("Anuluj");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("fills named values and leaves unknown ones", () => {
|
|
36
|
+
expect(fillString("{count} more of {name}", { count: 2 })).toBe(
|
|
37
|
+
"2 more of {name}",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
});
|
package/src/locale.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { appI18n } from "@edenapp/babel/solid";
|
|
2
|
+
import { en, type SolidKitStrings } from "./locales/en.js";
|
|
3
|
+
import { pl } from "./locales/pl.js";
|
|
4
|
+
|
|
5
|
+
export type { SolidKitStrings };
|
|
6
|
+
|
|
7
|
+
export type DeepPartial<T> = {
|
|
8
|
+
[K in keyof T]?: T[K] extends string ? T[K] : DeepPartial<T[K]>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The keys an app can put in its own locale files to replace solid-kit's
|
|
13
|
+
* strings, such as `solidKit.dialogs.cancel`.
|
|
14
|
+
*/
|
|
15
|
+
export interface SolidKitTranslations {
|
|
16
|
+
solidKit?: DeepPartial<SolidKitStrings>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const builtIn: Record<string, SolidKitStrings> = { en, pl };
|
|
20
|
+
|
|
21
|
+
/** The app's locale from `@edenapp/babel/solid`, or English without one. */
|
|
22
|
+
export function solidKitLocale(): string {
|
|
23
|
+
return appI18n()?.locale() ?? "en";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One component's strings for the app's locale. Each is the app's translation
|
|
28
|
+
* under `solidKit.`, else solid-kit's for that locale or its language, else
|
|
29
|
+
* English. Read it in a reactive scope to follow locale changes.
|
|
30
|
+
*/
|
|
31
|
+
export function solidKitStrings<G extends keyof SolidKitStrings>(
|
|
32
|
+
group: G,
|
|
33
|
+
): SolidKitStrings[G] {
|
|
34
|
+
const app = appI18n();
|
|
35
|
+
const value = solidKitLocale();
|
|
36
|
+
const own = builtIn[value] ?? builtIn[value.split("-")[0]] ?? en;
|
|
37
|
+
const resolve = (
|
|
38
|
+
english: object,
|
|
39
|
+
local: object | undefined,
|
|
40
|
+
path: string,
|
|
41
|
+
): object =>
|
|
42
|
+
Object.fromEntries(
|
|
43
|
+
Object.entries(english).map(([key, fallback]) => {
|
|
44
|
+
const at = (from: object | undefined) =>
|
|
45
|
+
(from as Record<string, unknown> | undefined)?.[key];
|
|
46
|
+
if (typeof fallback === "object") {
|
|
47
|
+
return [
|
|
48
|
+
key,
|
|
49
|
+
resolve(
|
|
50
|
+
fallback,
|
|
51
|
+
at(local) as object | undefined,
|
|
52
|
+
`${path}.${key}`,
|
|
53
|
+
),
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
return [
|
|
57
|
+
key,
|
|
58
|
+
app?.find(`${path}.${key}`) ??
|
|
59
|
+
(at(local) as string | undefined) ??
|
|
60
|
+
fallback,
|
|
61
|
+
];
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
return resolve(
|
|
65
|
+
en[group],
|
|
66
|
+
own[group],
|
|
67
|
+
`solidKit.${group}`,
|
|
68
|
+
) as SolidKitStrings[G];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** `template` with each `{name}` replaced by its value. */
|
|
72
|
+
export function fillString(
|
|
73
|
+
template: string,
|
|
74
|
+
values: Record<string, string | number>,
|
|
75
|
+
): string {
|
|
76
|
+
return template.replace(/\{(\w+)\}/g, (match, name: string) =>
|
|
77
|
+
name in values ? String(values[name]) : match,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DateTimeUnit } from "../datetime/DateTimeField.js";
|
|
2
|
+
|
|
3
|
+
/** Every string solid-kit components show, grouped by component. */
|
|
4
|
+
export interface SolidKitStrings {
|
|
5
|
+
datetime: {
|
|
6
|
+
unit: Record<DateTimeUnit, string>;
|
|
7
|
+
earlier: Record<DateTimeUnit, string>;
|
|
8
|
+
later: Record<DateTimeUnit, string>;
|
|
9
|
+
done: string;
|
|
10
|
+
};
|
|
11
|
+
dialogs: { ok: string; cancel: string };
|
|
12
|
+
keyboard: { toggle: string };
|
|
13
|
+
notices: { dismiss: string; showMore: string; showLess: string };
|
|
14
|
+
sidebar: { collapse: string; expand: string };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const en: SolidKitStrings = {
|
|
18
|
+
datetime: {
|
|
19
|
+
unit: { day: "Day", hour: "Hour", minute: "Minute" },
|
|
20
|
+
earlier: {
|
|
21
|
+
day: "Earlier day",
|
|
22
|
+
hour: "Earlier hour",
|
|
23
|
+
minute: "Earlier minute",
|
|
24
|
+
},
|
|
25
|
+
later: { day: "Later day", hour: "Later hour", minute: "Later minute" },
|
|
26
|
+
done: "Done",
|
|
27
|
+
},
|
|
28
|
+
dialogs: { ok: "OK", cancel: "Cancel" },
|
|
29
|
+
keyboard: { toggle: "Toggle keyboard" },
|
|
30
|
+
notices: {
|
|
31
|
+
dismiss: "Dismiss",
|
|
32
|
+
showMore: "{count} more",
|
|
33
|
+
showLess: "Show less",
|
|
34
|
+
},
|
|
35
|
+
sidebar: { collapse: "Collapse sidebar", expand: "Expand sidebar" },
|
|
36
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SolidKitStrings } from "./en.js";
|
|
2
|
+
|
|
3
|
+
export const pl: SolidKitStrings = {
|
|
4
|
+
datetime: {
|
|
5
|
+
unit: { day: "Dzień", hour: "Godzina", minute: "Minuta" },
|
|
6
|
+
earlier: {
|
|
7
|
+
day: "Wcześniejszy dzień",
|
|
8
|
+
hour: "Wcześniejsza godzina",
|
|
9
|
+
minute: "Wcześniejsza minuta",
|
|
10
|
+
},
|
|
11
|
+
later: {
|
|
12
|
+
day: "Późniejszy dzień",
|
|
13
|
+
hour: "Późniejsza godzina",
|
|
14
|
+
minute: "Późniejsza minuta",
|
|
15
|
+
},
|
|
16
|
+
done: "Gotowe",
|
|
17
|
+
},
|
|
18
|
+
dialogs: { ok: "OK", cancel: "Anuluj" },
|
|
19
|
+
keyboard: { toggle: "Przełącz klawiaturę" },
|
|
20
|
+
notices: {
|
|
21
|
+
dismiss: "Zamknij",
|
|
22
|
+
showMore: "Więcej: {count}",
|
|
23
|
+
showLess: "Pokaż mniej",
|
|
24
|
+
},
|
|
25
|
+
sidebar: { collapse: "Zwiń pasek boczny", expand: "Rozwiń pasek boczny" },
|
|
26
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { setupI18n } from "@edenapp/babel/solid";
|
|
1
2
|
import {
|
|
2
3
|
cleanup,
|
|
3
4
|
fireEvent,
|
|
@@ -10,14 +11,13 @@ import { afterEach, describe, expect, it } from "vitest";
|
|
|
10
11
|
import type { Notices } from "./createNotices.js";
|
|
11
12
|
import { NoticeProvider, useNotices } from "./NoticeProvider.js";
|
|
12
13
|
|
|
13
|
-
const
|
|
14
|
-
dismiss: "Dismiss",
|
|
15
|
-
showMore: (hidden: number) => `${hidden} more`,
|
|
16
|
-
showLess: "Show less",
|
|
17
|
-
};
|
|
14
|
+
const app = setupI18n({ resources: {} });
|
|
18
15
|
|
|
19
16
|
describe("NoticeProvider", () => {
|
|
20
|
-
afterEach(
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
cleanup();
|
|
19
|
+
app.setLocale("en");
|
|
20
|
+
});
|
|
21
21
|
|
|
22
22
|
it("announces danger as an alert and others politely, lets the user dismiss them, and hides extra notices", () => {
|
|
23
23
|
function Reporter() {
|
|
@@ -32,7 +32,7 @@ describe("NoticeProvider", () => {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
render(() => (
|
|
35
|
-
<NoticeProvider
|
|
35
|
+
<NoticeProvider>
|
|
36
36
|
<Reporter />
|
|
37
37
|
</NoticeProvider>
|
|
38
38
|
));
|
|
@@ -60,7 +60,7 @@ describe("NoticeProvider", () => {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
render(() => (
|
|
63
|
-
<NoticeProvider
|
|
63
|
+
<NoticeProvider>
|
|
64
64
|
<Reporter />
|
|
65
65
|
</NoticeProvider>
|
|
66
66
|
));
|
|
@@ -87,7 +87,7 @@ describe("NoticeProvider", () => {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
render(() => (
|
|
90
|
-
<NoticeProvider
|
|
90
|
+
<NoticeProvider>
|
|
91
91
|
<Condition />
|
|
92
92
|
</NoticeProvider>
|
|
93
93
|
));
|
|
@@ -95,4 +95,26 @@ describe("NoticeProvider", () => {
|
|
|
95
95
|
expect(screen.getByRole("status").textContent).toContain("offline");
|
|
96
96
|
expect(screen.queryByRole("button", { name: "Dismiss" })).toBeNull();
|
|
97
97
|
});
|
|
98
|
+
|
|
99
|
+
it("labels its controls in the app's language", () => {
|
|
100
|
+
app.setLocale("pl");
|
|
101
|
+
function Reporter() {
|
|
102
|
+
const notices = useNotices();
|
|
103
|
+
onMount(() => {
|
|
104
|
+
for (const title of ["a", "b", "c", "d"]) {
|
|
105
|
+
notices.report({ severity: "warning", title });
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
render(() => (
|
|
112
|
+
<NoticeProvider>
|
|
113
|
+
<Reporter />
|
|
114
|
+
</NoticeProvider>
|
|
115
|
+
));
|
|
116
|
+
|
|
117
|
+
expect(screen.getByRole("button", { name: "Więcej: 1" })).toBeTruthy();
|
|
118
|
+
expect(screen.getAllByRole("button", { name: "Zamknij" })).toHaveLength(3);
|
|
119
|
+
});
|
|
98
120
|
});
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { createContext, type JSX, useContext } from "solid-js";
|
|
2
2
|
import { createNotices, type Notices } from "./createNotices.js";
|
|
3
|
-
import {
|
|
3
|
+
import { NoticeRegion } from "./NoticeRegion.js";
|
|
4
4
|
|
|
5
5
|
const NoticesContext = createContext<Notices>();
|
|
6
6
|
|
|
7
7
|
export interface NoticeProviderProps {
|
|
8
|
-
/** Localized labels for the notice controls. */
|
|
9
|
-
labels: NoticeLabels;
|
|
10
8
|
autoHideMs?: number;
|
|
11
9
|
children: JSX.Element;
|
|
12
10
|
}
|
|
@@ -17,7 +15,7 @@ export function NoticeProvider(props: NoticeProviderProps) {
|
|
|
17
15
|
return (
|
|
18
16
|
<NoticesContext.Provider value={notices}>
|
|
19
17
|
{props.children}
|
|
20
|
-
<NoticeRegion notices={notices}
|
|
18
|
+
<NoticeRegion notices={notices} />
|
|
21
19
|
</NoticesContext.Provider>
|
|
22
20
|
);
|
|
23
21
|
}
|
|
@@ -8,17 +8,9 @@ import {
|
|
|
8
8
|
} from "solid-icons/fi";
|
|
9
9
|
import { createEffect, createMemo, createSignal, For, Show } from "solid-js";
|
|
10
10
|
import { Dynamic } from "solid-js/web";
|
|
11
|
+
import { fillString, solidKitStrings } from "../locale.js";
|
|
11
12
|
import type { Notice, NoticeSeverity, Notices } from "./createNotices.js";
|
|
12
13
|
|
|
13
|
-
export interface NoticeLabels {
|
|
14
|
-
/** Accessible name of the dismiss button. */
|
|
15
|
-
dismiss: string;
|
|
16
|
-
/** Text of the button that shows the hidden notices. */
|
|
17
|
-
showMore: (hidden: number) => string;
|
|
18
|
-
/** Text of the button that collapses the list again. */
|
|
19
|
-
showLess: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
14
|
export const NOTICE_MAX_VISIBLE = 3;
|
|
23
15
|
|
|
24
16
|
const severityIcons: Record<NoticeSeverity, IconTypes> = {
|
|
@@ -30,7 +22,7 @@ const severityIcons: Record<NoticeSeverity, IconTypes> = {
|
|
|
30
22
|
|
|
31
23
|
function NoticeItem(props: {
|
|
32
24
|
notice: Notice;
|
|
33
|
-
|
|
25
|
+
dismiss: string;
|
|
34
26
|
onDismiss: (id: string) => void;
|
|
35
27
|
}) {
|
|
36
28
|
// Most screen readers announce an alert inserted into the page. Other
|
|
@@ -60,8 +52,8 @@ function NoticeItem(props: {
|
|
|
60
52
|
<button
|
|
61
53
|
type="button"
|
|
62
54
|
class="eden-btn eden-btn-ghost eden-btn-sm eden-btn-square eden-notice-dismiss"
|
|
63
|
-
aria-label={props.
|
|
64
|
-
title={props.
|
|
55
|
+
aria-label={props.dismiss}
|
|
56
|
+
title={props.dismiss}
|
|
65
57
|
onClick={() => props.onDismiss(props.notice.id)}
|
|
66
58
|
>
|
|
67
59
|
<FiX size={16} aria-hidden="true" />
|
|
@@ -71,11 +63,12 @@ function NoticeItem(props: {
|
|
|
71
63
|
);
|
|
72
64
|
}
|
|
73
65
|
|
|
74
|
-
export function NoticeRegion(props: {
|
|
75
|
-
notices: Notices;
|
|
76
|
-
labels: NoticeLabels;
|
|
77
|
-
}) {
|
|
66
|
+
export function NoticeRegion(props: { notices: Notices }) {
|
|
78
67
|
const [expanded, setExpanded] = createSignal(false);
|
|
68
|
+
const text = () => solidKitStrings("notices");
|
|
69
|
+
const dismiss = () => text().dismiss;
|
|
70
|
+
const showMore = (hidden: number) =>
|
|
71
|
+
fillString(text().showMore, { count: hidden });
|
|
79
72
|
const list = () => props.notices.list();
|
|
80
73
|
const visible = () =>
|
|
81
74
|
expanded() ? list() : list().slice(0, NOTICE_MAX_VISIBLE);
|
|
@@ -102,7 +95,7 @@ export function NoticeRegion(props: {
|
|
|
102
95
|
{(notice) => (
|
|
103
96
|
<NoticeItem
|
|
104
97
|
notice={notice()}
|
|
105
|
-
|
|
98
|
+
dismiss={dismiss()}
|
|
106
99
|
onDismiss={props.notices.dismiss}
|
|
107
100
|
/>
|
|
108
101
|
)}
|
|
@@ -117,7 +110,7 @@ export function NoticeRegion(props: {
|
|
|
117
110
|
aria-expanded={expanded()}
|
|
118
111
|
onClick={() => setExpanded(!expanded())}
|
|
119
112
|
>
|
|
120
|
-
{expanded() ?
|
|
113
|
+
{expanded() ? text().showLess : showMore(hidden())}
|
|
121
114
|
</button>
|
|
122
115
|
</Show>
|
|
123
116
|
</div>
|
package/src/notices.ts
CHANGED
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
|
-
|
|
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;
|