@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.
- package/README.md +109 -2
- package/package.json +25 -4
- package/src/KeyboardButton.tsx +4 -3
- package/src/datetime/DateTimeField.css +69 -0
- package/src/datetime/DateTimeField.test.tsx +95 -0
- package/src/datetime/DateTimeField.tsx +196 -0
- package/src/datetime.ts +6 -0
- package/src/dialogs/createDialogs.tsx +9 -7
- package/src/index.ts +1 -0
- 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 +120 -0
- package/src/notices/NoticeProvider.tsx +29 -0
- package/src/notices/NoticeRegion.tsx +118 -0
- package/src/notices/createNotices.test.ts +167 -0
- package/src/notices/createNotices.ts +217 -0
- package/src/notices.ts +21 -0
- 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
|
|
@@ -93,6 +120,86 @@ a stop. `run()` remains pending until cleanup finishes, then rejects with an
|
|
|
93
120
|
`AbortError` when cancelled. Filesystem copies remove incomplete destinations and
|
|
94
121
|
restore overwritten files before reaching the `cancelled` status.
|
|
95
122
|
|
|
123
|
+
## Notices
|
|
124
|
+
|
|
125
|
+
`@edenapp/solid-kit/notices` shows notices inside the app window, at the top
|
|
126
|
+
centre below the app frame. Use them for failures of actions that the user
|
|
127
|
+
started in the app, and for problems that the app observes. Use the toaster
|
|
128
|
+
(`notification` from `@edenapp/tablets`) for failures that are not owned by
|
|
129
|
+
one app.
|
|
130
|
+
|
|
131
|
+
Wrap the app in `NoticeProvider`. Its buttons have
|
|
132
|
+
[built-in text](#language):
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
import { NoticeProvider, useNotices } from "@edenapp/solid-kit/notices";
|
|
136
|
+
|
|
137
|
+
<NoticeProvider>
|
|
138
|
+
<App />
|
|
139
|
+
</NoticeProvider>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Report notices with `useNotices()`:
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
const notices = useNotices();
|
|
146
|
+
|
|
147
|
+
// A failure of an action. It stays until the user dismisses it.
|
|
148
|
+
notices.report({
|
|
149
|
+
severity: "danger",
|
|
150
|
+
title: t("offsets.writeFailed"),
|
|
151
|
+
detail: reason,
|
|
152
|
+
key: "offset:G54:X",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Run an action. When it throws, the error is logged and reported.
|
|
156
|
+
const file = await notices.attempt(() => openFile(path), {
|
|
157
|
+
title: t("program.openFailed"),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// A problem that lasts while the condition is true. Call it in a component
|
|
161
|
+
// body, not after an `await` or in an event handler.
|
|
162
|
+
notices.condition(() => !connected(), {
|
|
163
|
+
severity: "warning",
|
|
164
|
+
title: t("connection.lost"),
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Severities match notification types: `danger`, `warning`, `info`, and
|
|
169
|
+
`success`. Danger and warning notices stay until the user dismisses them; info
|
|
170
|
+
and success notices hide after four seconds. A report with the `key` of a shown
|
|
171
|
+
notice replaces it in place and shows a count. Notices are listed most severe first;
|
|
172
|
+
within a severity, condition notices come first. Condition notices have no
|
|
173
|
+
dismiss button and clear when their condition is false or their owner is
|
|
174
|
+
disposed. Three notices are visible at a time; a button shows the others.
|
|
175
|
+
Screen readers announce danger notices immediately and other notices politely.
|
|
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 steppers for the
|
|
181
|
+
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`.
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import { DateTimeField } from "@edenapp/solid-kit/datetime";
|
|
186
|
+
import { t } from "./i18n";
|
|
187
|
+
|
|
188
|
+
<DateTimeField
|
|
189
|
+
label={t("logs.from")}
|
|
190
|
+
value={from()}
|
|
191
|
+
onChange={setFrom}
|
|
192
|
+
placeholder={t("logs.anyTime")}
|
|
193
|
+
max={new Date()}
|
|
194
|
+
/>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The field formats its value and shows its text in the
|
|
198
|
+
[app's language](#language).
|
|
199
|
+
|
|
200
|
+
The field's styles are part of this entry point, not EdenCSS, so only apps that
|
|
201
|
+
import it load them. The root `@edenapp/solid-kit` entry does not export it.
|
|
202
|
+
|
|
96
203
|
## Dialogs
|
|
97
204
|
|
|
98
205
|
`@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.0",
|
|
4
4
|
"description": "High-level SolidJS UI primitives for Eden apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -11,11 +11,26 @@
|
|
|
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
|
+
},
|
|
29
|
+
"./notices": {
|
|
30
|
+
"types": "./src/notices.ts",
|
|
31
|
+
"import": "./src/notices.ts",
|
|
32
|
+
"default": "./src/notices.ts"
|
|
33
|
+
},
|
|
19
34
|
"./sidebar": {
|
|
20
35
|
"types": "./src/sidebar.tsx",
|
|
21
36
|
"import": "./src/sidebar.tsx",
|
|
@@ -30,11 +45,16 @@
|
|
|
30
45
|
},
|
|
31
46
|
"dependencies": {
|
|
32
47
|
"solid-icons": "^1.2.0",
|
|
33
|
-
"@edenapp/
|
|
48
|
+
"@edenapp/babel": "0.17.0",
|
|
49
|
+
"@edenapp/client": "0.17.0"
|
|
34
50
|
},
|
|
35
51
|
"devDependencies": {
|
|
52
|
+
"@solidjs/testing-library": "^0.8.10",
|
|
53
|
+
"jsdom": "^30.0.1",
|
|
36
54
|
"typescript": "^6.0.3",
|
|
37
|
-
"
|
|
55
|
+
"vite-plugin-solid": "^2.11.14",
|
|
56
|
+
"vitest": "^4.1.10",
|
|
57
|
+
"@edenapp/types": "0.17.0"
|
|
38
58
|
},
|
|
39
59
|
"keywords": [
|
|
40
60
|
"eden",
|
|
@@ -51,6 +71,7 @@
|
|
|
51
71
|
},
|
|
52
72
|
"scripts": {
|
|
53
73
|
"build": "tsc --noEmit",
|
|
54
|
-
"clean": "rm -rf dist"
|
|
74
|
+
"clean": "rm -rf dist",
|
|
75
|
+
"test": "vitest run"
|
|
55
76
|
}
|
|
56
77
|
}
|
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,69 @@
|
|
|
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
|
+
display: grid;
|
|
33
|
+
grid-template-columns: max-content auto minmax(6em, 1fr) auto;
|
|
34
|
+
align-items: center;
|
|
35
|
+
gap: var(--eden-space-xs) var(--eden-space-sm);
|
|
36
|
+
padding: var(--eden-space-sm);
|
|
37
|
+
border: 1px solid var(--eden-color-border-light);
|
|
38
|
+
border-radius: var(--eden-radius-md);
|
|
39
|
+
background: var(--eden-color-surface-secondary);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.eden-datetime-step {
|
|
43
|
+
display: contents;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.eden-datetime-step-name {
|
|
47
|
+
font-size: var(--eden-font-size-base);
|
|
48
|
+
color: var(--eden-color-text-secondary);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.eden-datetime-step-value {
|
|
52
|
+
text-align: center;
|
|
53
|
+
font-size: var(--eden-font-size-md);
|
|
54
|
+
font-variant-numeric: tabular-nums;
|
|
55
|
+
color: var(--eden-color-text-primary);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* Holding a stepper repeats it; keep the long press from selecting text. */
|
|
59
|
+
.eden-datetime-step .eden-btn {
|
|
60
|
+
touch-action: manipulation;
|
|
61
|
+
user-select: none;
|
|
62
|
+
-webkit-user-select: none;
|
|
63
|
+
-webkit-touch-callout: none;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.eden-datetime-done {
|
|
67
|
+
grid-column: 1 / -1;
|
|
68
|
+
justify-self: end;
|
|
69
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
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("steps once per tap and repeats while held", () => {
|
|
75
|
+
vi.useFakeTimers();
|
|
76
|
+
const value = renderField(new Date(2026, 10, 25, 14, 0));
|
|
77
|
+
const later = screen.getByRole("button", { name: "Later minute" });
|
|
78
|
+
|
|
79
|
+
// A tap fires pointerdown, pointerup and click: one step.
|
|
80
|
+
fireEvent.pointerDown(later);
|
|
81
|
+
fireEvent.pointerUp(window);
|
|
82
|
+
fireEvent.click(later);
|
|
83
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 5));
|
|
84
|
+
vi.advanceTimersByTime(1000);
|
|
85
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 5));
|
|
86
|
+
|
|
87
|
+
// Held: one step now, then repeats after a pause until released.
|
|
88
|
+
fireEvent.pointerDown(later);
|
|
89
|
+
vi.advanceTimersByTime(400 + 3 * 100);
|
|
90
|
+
fireEvent.pointerUp(window);
|
|
91
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 25));
|
|
92
|
+
vi.advanceTimersByTime(1000);
|
|
93
|
+
expect(value()).toEqual(new Date(2026, 10, 25, 14, 25));
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BiRegularCalendar,
|
|
3
|
+
BiRegularMinus,
|
|
4
|
+
BiRegularPlus,
|
|
5
|
+
} from "solid-icons/bi";
|
|
6
|
+
import { createSignal, createUniqueId, For, onCleanup, Show } from "solid-js";
|
|
7
|
+
import "./DateTimeField.css";
|
|
8
|
+
import { solidKitLocale, solidKitStrings } from "../locale.js";
|
|
9
|
+
|
|
10
|
+
export type DateTimeUnit = "day" | "hour" | "minute";
|
|
11
|
+
|
|
12
|
+
export interface DateTimeFieldProps {
|
|
13
|
+
/** Names the field, such as "From". */
|
|
14
|
+
label: string;
|
|
15
|
+
value: Date | undefined;
|
|
16
|
+
onChange: (value: Date) => void;
|
|
17
|
+
/** Shown while `value` is unset. */
|
|
18
|
+
placeholder: string;
|
|
19
|
+
min?: Date;
|
|
20
|
+
max?: Date;
|
|
21
|
+
/** Minutes moved by one minute step. Defaults to 5. */
|
|
22
|
+
minuteStep?: number;
|
|
23
|
+
class?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const units: readonly DateTimeUnit[] = ["day", "hour", "minute"];
|
|
27
|
+
const repeatDelayMs = 400;
|
|
28
|
+
const repeatEveryMs = 100;
|
|
29
|
+
|
|
30
|
+
/** `time` moved one step of `unit` in `direction`, kept within the bounds. */
|
|
31
|
+
export function stepDateTime(
|
|
32
|
+
time: Date,
|
|
33
|
+
unit: DateTimeUnit,
|
|
34
|
+
direction: 1 | -1,
|
|
35
|
+
options: { minuteStep?: number; min?: Date; max?: Date } = {},
|
|
36
|
+
): Date {
|
|
37
|
+
const next = new Date(time);
|
|
38
|
+
next.setSeconds(0, 0);
|
|
39
|
+
if (unit === "day") next.setDate(next.getDate() + direction);
|
|
40
|
+
else if (unit === "hour") next.setHours(next.getHours() + direction);
|
|
41
|
+
else {
|
|
42
|
+
// Land on a multiple of the step, so 14:57 goes to 15:00 or 14:55.
|
|
43
|
+
const step = Math.max(1, options.minuteStep ?? 5);
|
|
44
|
+
const minutes = next.getMinutes();
|
|
45
|
+
next.setMinutes(
|
|
46
|
+
direction > 0
|
|
47
|
+
? Math.floor(minutes / step) * step + step
|
|
48
|
+
: Math.ceil(minutes / step) * step - step,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (options.min && next < options.min) return new Date(options.min);
|
|
52
|
+
if (options.max && next > options.max) return new Date(options.max);
|
|
53
|
+
return next;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A date and time chosen by touch: a button shows the value and opens steppers
|
|
58
|
+
* for the day, hour and minute below it. Holding a stepper repeats it.
|
|
59
|
+
*/
|
|
60
|
+
export function DateTimeField(props: DateTimeFieldProps) {
|
|
61
|
+
const [open, setOpen] = createSignal(false);
|
|
62
|
+
const strings = () => solidKitStrings("datetime");
|
|
63
|
+
const id = `eden-datetime-${createUniqueId()}`;
|
|
64
|
+
const format = (options: Intl.DateTimeFormatOptions, time: Date) =>
|
|
65
|
+
new Intl.DateTimeFormat(solidKitLocale(), options).format(time);
|
|
66
|
+
const bounds = () => ({
|
|
67
|
+
minuteStep: props.minuteStep,
|
|
68
|
+
min: props.min,
|
|
69
|
+
max: props.max,
|
|
70
|
+
});
|
|
71
|
+
// An unset field starts from now, on a whole step, within the bounds.
|
|
72
|
+
const shown = () => {
|
|
73
|
+
if (props.value) return props.value;
|
|
74
|
+
const now = new Date();
|
|
75
|
+
return stepDateTime(
|
|
76
|
+
stepDateTime(now, "minute", 1, bounds()),
|
|
77
|
+
"minute",
|
|
78
|
+
-1,
|
|
79
|
+
bounds(),
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
const step = (unit: DateTimeUnit, direction: 1 | -1) =>
|
|
83
|
+
stepDateTime(shown(), unit, direction, bounds());
|
|
84
|
+
const canStep = (unit: DateTimeUnit, direction: 1 | -1) =>
|
|
85
|
+
step(unit, direction).getTime() !== shown().getTime();
|
|
86
|
+
const text = (unit: DateTimeUnit, time: Date) => {
|
|
87
|
+
if (unit === "day") {
|
|
88
|
+
return format({ weekday: "short", day: "numeric", month: "short" }, time);
|
|
89
|
+
}
|
|
90
|
+
if (unit === "hour") return format({ hour: "numeric" }, time);
|
|
91
|
+
return String(time.getMinutes()).padStart(2, "0");
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const stepper = (unit: DateTimeUnit, direction: 1 | -1) => {
|
|
95
|
+
let delay: ReturnType<typeof setTimeout> | undefined;
|
|
96
|
+
let repeat: ReturnType<typeof setInterval> | undefined;
|
|
97
|
+
let pressed = false;
|
|
98
|
+
const stop = () => {
|
|
99
|
+
clearTimeout(delay);
|
|
100
|
+
clearInterval(repeat);
|
|
101
|
+
window.removeEventListener("pointerup", stop);
|
|
102
|
+
window.removeEventListener("pointercancel", stop);
|
|
103
|
+
};
|
|
104
|
+
const run = () => {
|
|
105
|
+
if (!canStep(unit, direction)) return stop();
|
|
106
|
+
props.onChange(step(unit, direction));
|
|
107
|
+
};
|
|
108
|
+
onCleanup(stop);
|
|
109
|
+
const name = () => strings()[direction > 0 ? "later" : "earlier"][unit];
|
|
110
|
+
return (
|
|
111
|
+
<button
|
|
112
|
+
type="button"
|
|
113
|
+
class="eden-btn eden-btn-ghost eden-btn-square"
|
|
114
|
+
title={name()}
|
|
115
|
+
aria-label={name()}
|
|
116
|
+
disabled={!canStep(unit, direction)}
|
|
117
|
+
onPointerDown={(event) => {
|
|
118
|
+
if (event.button > 0) return;
|
|
119
|
+
stop();
|
|
120
|
+
pressed = true;
|
|
121
|
+
run();
|
|
122
|
+
// A disabled button gets no pointerup, so listen on the window.
|
|
123
|
+
window.addEventListener("pointerup", stop);
|
|
124
|
+
window.addEventListener("pointercancel", stop);
|
|
125
|
+
delay = setTimeout(() => {
|
|
126
|
+
repeat = setInterval(run, repeatEveryMs);
|
|
127
|
+
}, repeatDelayMs);
|
|
128
|
+
}}
|
|
129
|
+
onPointerLeave={stop}
|
|
130
|
+
onClick={() => {
|
|
131
|
+
// A press already stepped; a click without one comes from the keyboard.
|
|
132
|
+
if (pressed) pressed = false;
|
|
133
|
+
else run();
|
|
134
|
+
}}
|
|
135
|
+
>
|
|
136
|
+
{direction > 0 ? <BiRegularPlus /> : <BiRegularMinus />}
|
|
137
|
+
</button>
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div class={`eden-datetime-field ${props.class ?? ""}`} data-open={open()}>
|
|
143
|
+
<label class="eden-datetime-label" for={id}>
|
|
144
|
+
{props.label}
|
|
145
|
+
</label>
|
|
146
|
+
<button
|
|
147
|
+
id={id}
|
|
148
|
+
type="button"
|
|
149
|
+
class="eden-input eden-datetime-value"
|
|
150
|
+
aria-expanded={open()}
|
|
151
|
+
onClick={() => setOpen(!open())}
|
|
152
|
+
>
|
|
153
|
+
<BiRegularCalendar />
|
|
154
|
+
<span data-placeholder={!props.value}>
|
|
155
|
+
{props.value
|
|
156
|
+
? format(
|
|
157
|
+
{
|
|
158
|
+
weekday: "short",
|
|
159
|
+
day: "numeric",
|
|
160
|
+
month: "short",
|
|
161
|
+
hour: "numeric",
|
|
162
|
+
minute: "2-digit",
|
|
163
|
+
},
|
|
164
|
+
props.value,
|
|
165
|
+
)
|
|
166
|
+
: props.placeholder}
|
|
167
|
+
</span>
|
|
168
|
+
</button>
|
|
169
|
+
<Show when={open()}>
|
|
170
|
+
<div class="eden-datetime-panel">
|
|
171
|
+
<For each={units}>
|
|
172
|
+
{(unit) => (
|
|
173
|
+
<div class="eden-datetime-step">
|
|
174
|
+
<span class="eden-datetime-step-name">
|
|
175
|
+
{strings().unit[unit]}
|
|
176
|
+
</span>
|
|
177
|
+
{stepper(unit, -1)}
|
|
178
|
+
<output class="eden-datetime-step-value">
|
|
179
|
+
{text(unit, shown())}
|
|
180
|
+
</output>
|
|
181
|
+
{stepper(unit, 1)}
|
|
182
|
+
</div>
|
|
183
|
+
)}
|
|
184
|
+
</For>
|
|
185
|
+
<button
|
|
186
|
+
type="button"
|
|
187
|
+
class="eden-btn eden-btn-primary eden-datetime-done"
|
|
188
|
+
onClick={() => setOpen(false)}
|
|
189
|
+
>
|
|
190
|
+
{strings().done}
|
|
191
|
+
</button>
|
|
192
|
+
</div>
|
|
193
|
+
</Show>
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
}
|
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";
|