@c9up/nebula 0.2.2 → 0.2.4
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 +8 -7
- package/dist/atoms/Avatar.d.ts +14 -0
- package/dist/atoms/Avatar.js +46 -8
- package/dist/atoms/Checkbox.d.ts +1 -1
- package/dist/atoms/Checkbox.js +1 -1
- package/dist/atoms/Input.d.ts +5 -0
- package/dist/atoms/Input.js +1 -0
- package/dist/cli/add.d.ts +2 -0
- package/dist/cli/add.js +4 -1
- package/dist/cli/diff.d.ts +2 -0
- package/dist/cli/diff.js +4 -1
- package/dist/cli/projectConfig.d.ts +16 -0
- package/dist/cli/projectConfig.js +61 -0
- package/dist/console/AddCommand.js +6 -1
- package/dist/console/DiffCommand.js +6 -1
- package/dist/lib/children.d.ts +13 -8
- package/dist/lib/children.js +5 -0
- package/dist/lib/icons.d.ts +8 -0
- package/dist/lib/icons.js +8 -0
- package/dist/lib/image.js +12 -1
- package/dist/molecules/Breadcrumb.d.ts +11 -2
- package/dist/molecules/Breadcrumb.js +2 -2
- package/dist/molecules/InputOTP.d.ts +14 -0
- package/dist/molecules/InputOTP.js +71 -28
- package/dist/molecules/Pagination.d.ts +34 -2
- package/dist/molecules/Pagination.js +15 -6
- package/dist/molecules/PieceMap.d.ts +72 -0
- package/dist/molecules/PieceMap.js +78 -0
- package/dist/molecules/Resizable.d.ts +2 -0
- package/dist/molecules/Resizable.js +1 -1
- package/dist/molecules/index.d.ts +4 -3
- package/dist/molecules/index.js +2 -1
- package/dist/organisms/Calendar.d.ts +13 -0
- package/dist/organisms/Calendar.js +2 -2
- package/dist/organisms/Carousel.d.ts +8 -0
- package/dist/organisms/Carousel.js +8 -5
- package/dist/organisms/Combobox.d.ts +13 -0
- package/dist/organisms/Combobox.js +5 -2
- package/dist/organisms/Command.js +56 -8
- package/dist/organisms/DataTable.d.ts +23 -1
- package/dist/organisms/DataTable.js +57 -27
- package/dist/organisms/DatePicker.d.ts +7 -0
- package/dist/organisms/DatePicker.js +2 -1
- package/dist/organisms/DateRangePicker.d.ts +2 -0
- package/dist/organisms/DateRangePicker.js +2 -1
- package/dist/organisms/Dialog.d.ts +13 -3
- package/dist/organisms/Dialog.js +12 -5
- package/dist/organisms/Form.d.ts +15 -0
- package/dist/organisms/Form.js +10 -0
- package/dist/organisms/Questionnaire.d.ts +4 -0
- package/dist/organisms/Questionnaire.js +5 -2
- package/dist/organisms/Select.d.ts +25 -4
- package/dist/organisms/Select.js +116 -15
- package/dist/organisms/Sheet.d.ts +2 -0
- package/dist/organisms/Sheet.js +2 -1
- package/dist/organisms/Sidebar.d.ts +10 -2
- package/dist/organisms/Sidebar.js +62 -29
- package/dist/organisms/Sonner.d.ts +243 -0
- package/dist/organisms/Sonner.js +909 -0
- package/dist/organisms/Toaster.d.ts +11 -0
- package/dist/organisms/Toaster.js +34 -9
- package/dist/organisms/Tooltip.d.ts +4 -0
- package/dist/organisms/index.d.ts +8 -2
- package/dist/organisms/index.js +7 -1
- package/dist/primitives/dismissable.d.ts +11 -4
- package/dist/primitives/dismissable.js +8 -6
- package/dist/primitives/floating.d.ts +5 -1
- package/dist/primitives/floating.js +32 -2
- package/dist/primitives/menu.js +2 -1
- package/dist/primitives/modalSurface.js +70 -36
- package/dist/primitives/presence.js +8 -5
- package/dist/templates/SettingsLayout.d.ts +2 -0
- package/dist/templates/SettingsLayout.js +1 -1
- package/nebula.css +2 -2
- package/package.json +3 -3
- package/registry.json +26 -0
- package/src/atoms/Avatar.ts +54 -7
- package/src/atoms/Checkbox.ts +1 -1
- package/src/atoms/Input.ts +6 -0
- package/src/cli/add.ts +7 -1
- package/src/cli/diff.ts +7 -1
- package/src/cli/projectConfig.ts +71 -0
- package/src/console/AddCommand.ts +6 -1
- package/src/console/DiffCommand.ts +6 -1
- package/src/lib/children.ts +15 -7
- package/src/lib/icons.ts +28 -0
- package/src/lib/image.ts +13 -1
- package/src/molecules/Breadcrumb.ts +15 -4
- package/src/molecules/InputOTP.ts +88 -28
- package/src/molecules/Pagination.ts +50 -8
- package/src/molecules/PieceMap.ts +136 -0
- package/src/molecules/Resizable.ts +3 -1
- package/src/molecules/index.ts +17 -0
- package/src/organisms/Calendar.ts +16 -2
- package/src/organisms/Carousel.ts +17 -5
- package/src/organisms/Combobox.ts +18 -2
- package/src/organisms/Command.ts +66 -7
- package/src/organisms/DataTable.ts +95 -30
- package/src/organisms/DatePicker.ts +10 -2
- package/src/organisms/DateRangePicker.ts +4 -2
- package/src/organisms/Dialog.ts +17 -5
- package/src/organisms/Form.ts +21 -0
- package/src/organisms/Questionnaire.ts +10 -2
- package/src/organisms/Select.ts +140 -16
- package/src/organisms/Sheet.ts +5 -1
- package/src/organisms/Sidebar.ts +62 -23
- package/src/organisms/Sonner.ts +1366 -0
- package/src/organisms/Toaster.ts +38 -10
- package/src/organisms/Tooltip.ts +4 -0
- package/src/organisms/index.ts +30 -1
- package/src/primitives/dismissable.ts +16 -8
- package/src/primitives/floating.ts +40 -3
- package/src/primitives/menu.ts +2 -1
- package/src/primitives/modalSurface.ts +77 -40
- package/src/primitives/presence.ts +12 -9
- package/src/templates/SettingsLayout.ts +3 -1
package/src/cli/add.ts
CHANGED
|
@@ -27,6 +27,8 @@ export interface AddOptions {
|
|
|
27
27
|
paths?: Partial<NebulaPaths>;
|
|
28
28
|
/** Overrides both the detected language and the configured one. */
|
|
29
29
|
language?: Language;
|
|
30
|
+
/** The configured language: used when the tree says nothing. */
|
|
31
|
+
defaultLanguage?: Language;
|
|
30
32
|
force?: boolean;
|
|
31
33
|
/** Print what would happen and write nothing. */
|
|
32
34
|
dryRun?: boolean;
|
|
@@ -80,7 +82,11 @@ export function add(options: AddOptions): AddResult {
|
|
|
80
82
|
options.cwd,
|
|
81
83
|
options.paths?.components ?? defaultPaths.components,
|
|
82
84
|
);
|
|
83
|
-
const language =
|
|
85
|
+
const language =
|
|
86
|
+
options.language ??
|
|
87
|
+
detectLanguage(target) ??
|
|
88
|
+
options.defaultLanguage ??
|
|
89
|
+
"js";
|
|
84
90
|
|
|
85
91
|
// The compiled output is what a browser can load. It ships in the package
|
|
86
92
|
// and `prepublishOnly` rebuilds it, so an installed copy always has it;
|
package/src/cli/diff.ts
CHANGED
|
@@ -42,6 +42,8 @@ export interface DiffOptions {
|
|
|
42
42
|
cwd: string;
|
|
43
43
|
paths?: Partial<NebulaPaths>;
|
|
44
44
|
language?: Language;
|
|
45
|
+
/** The configured language: used when the tree says nothing. */
|
|
46
|
+
defaultLanguage?: Language;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
export interface DiffResult {
|
|
@@ -58,7 +60,11 @@ export interface DiffResult {
|
|
|
58
60
|
export function diff(options: DiffOptions): DiffResult {
|
|
59
61
|
const paths = { ...defaultPaths, ...options.paths };
|
|
60
62
|
const target = join(options.cwd, paths.components);
|
|
61
|
-
const language =
|
|
63
|
+
const language =
|
|
64
|
+
options.language ??
|
|
65
|
+
detectLanguage(target) ??
|
|
66
|
+
options.defaultLanguage ??
|
|
67
|
+
"js";
|
|
62
68
|
|
|
63
69
|
const root = packageRoot(join(options.cwd, "package.json"));
|
|
64
70
|
const sourceRoot = join(root, language === "ts" ? "src" : "dist");
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app's `config/nebula.ts`, as the copying commands read it.
|
|
3
|
+
*
|
|
4
|
+
* shadcn's CLI reads `components.json` on every `add` and `diff`; these read
|
|
5
|
+
* the config file the same way. Imported directly rather than taken from the
|
|
6
|
+
* booted application: the console kernel hands the app — and its config —
|
|
7
|
+
* only to a command that starts it, and starting it opens the connections a
|
|
8
|
+
* file copy has no use for, and fails where they are not running.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { pathToFileURL } from "node:url";
|
|
14
|
+
import type { Language, NebulaPaths } from "../config.js";
|
|
15
|
+
|
|
16
|
+
export interface ProjectConfig {
|
|
17
|
+
paths?: Partial<NebulaPaths>;
|
|
18
|
+
language?: Language;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Tried in order; the first that exists is the config. */
|
|
22
|
+
const CANDIDATES = ["nebula.ts", "nebula.js", "nebula.mjs"];
|
|
23
|
+
|
|
24
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
25
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function pathsOf(
|
|
29
|
+
value: unknown,
|
|
30
|
+
file: string,
|
|
31
|
+
): Partial<NebulaPaths> | undefined {
|
|
32
|
+
if (value === undefined) return undefined;
|
|
33
|
+
if (!isRecord(value)) {
|
|
34
|
+
throw new Error(`${file}: \`paths\` must be an object.`);
|
|
35
|
+
}
|
|
36
|
+
const paths: Partial<NebulaPaths> = {};
|
|
37
|
+
for (const key of ["components", "css", "output"] as const) {
|
|
38
|
+
const entry = value[key];
|
|
39
|
+
if (entry === undefined) continue;
|
|
40
|
+
if (typeof entry !== "string" || entry === "") {
|
|
41
|
+
throw new Error(`${file}: \`paths.${key}\` must be a non-empty string.`);
|
|
42
|
+
}
|
|
43
|
+
paths[key] = entry;
|
|
44
|
+
}
|
|
45
|
+
return paths;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function languageOf(value: unknown, file: string): Language | undefined {
|
|
49
|
+
if (value === undefined) return undefined;
|
|
50
|
+
if (value === "ts" || value === "js") return value;
|
|
51
|
+
throw new Error(`${file}: \`language\` must be "ts" or "js".`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The config under `<cwd>/config`, or nothing when the app has none. */
|
|
55
|
+
export async function loadProjectConfig(cwd: string): Promise<ProjectConfig> {
|
|
56
|
+
for (const name of CANDIDATES) {
|
|
57
|
+
const file = join(cwd, "config", name);
|
|
58
|
+
if (!existsSync(file)) continue;
|
|
59
|
+
const label = `config/${name}`;
|
|
60
|
+
const loaded: unknown = await import(pathToFileURL(file).href);
|
|
61
|
+
const config = isRecord(loaded) ? loaded.default : undefined;
|
|
62
|
+
if (!isRecord(config)) {
|
|
63
|
+
throw new Error(`${label} must export its config as the default export.`);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
paths: pathsOf(config.paths, label),
|
|
67
|
+
language: languageOf(config.language, label),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { add } from "../cli/add.js";
|
|
15
|
+
import { loadProjectConfig } from "../cli/projectConfig.js";
|
|
15
16
|
import { argument, flag, type NebulaCommandClass } from "./contract.js";
|
|
16
17
|
|
|
17
18
|
export default class NebulaAddCommand {
|
|
@@ -70,12 +71,16 @@ export default class NebulaAddCommand {
|
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
const cwd = process.cwd();
|
|
75
|
+
const config = await loadProjectConfig(cwd);
|
|
73
76
|
const result = add({
|
|
74
|
-
cwd
|
|
77
|
+
cwd,
|
|
75
78
|
names: this.components,
|
|
76
79
|
force: this.force,
|
|
77
80
|
dryRun: this.dryRun,
|
|
81
|
+
paths: config.paths,
|
|
78
82
|
language: this.ts ? "ts" : this.js ? "js" : undefined,
|
|
83
|
+
defaultLanguage: config.language,
|
|
79
84
|
});
|
|
80
85
|
|
|
81
86
|
const out: string[] = [""];
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { type DiffState, diff } from "../cli/diff.js";
|
|
11
|
+
import { loadProjectConfig } from "../cli/projectConfig.js";
|
|
11
12
|
import { flag, type NebulaCommandClass } from "./contract.js";
|
|
12
13
|
|
|
13
14
|
/** What each state means, and what to do about it. */
|
|
@@ -54,9 +55,13 @@ export default class NebulaDiffCommand {
|
|
|
54
55
|
js = false;
|
|
55
56
|
|
|
56
57
|
async run(): Promise<void> {
|
|
58
|
+
const cwd = process.cwd();
|
|
59
|
+
const config = await loadProjectConfig(cwd);
|
|
57
60
|
const result = diff({
|
|
58
|
-
cwd
|
|
61
|
+
cwd,
|
|
62
|
+
paths: config.paths,
|
|
59
63
|
language: this.ts ? "ts" : this.js ? "js" : undefined,
|
|
64
|
+
defaultLanguage: config.language,
|
|
60
65
|
});
|
|
61
66
|
|
|
62
67
|
if (result.entries.length === 0) {
|
package/src/lib/children.ts
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
* frozen for the lifetime of the node.
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
import type
|
|
16
|
+
import { type Each, isEach, type TemplateResult } from "@c9up/aurora";
|
|
17
17
|
import type { Reactive } from "./props.js";
|
|
18
18
|
|
|
19
19
|
export type Child =
|
|
20
20
|
| TemplateResult
|
|
21
|
+
/** A keyed list from aurora's `each`, which keeps its rows by key. */
|
|
22
|
+
| Each
|
|
21
23
|
| Node
|
|
22
24
|
| string
|
|
23
25
|
| number
|
|
@@ -44,13 +46,16 @@ export type Slot = Reactive<Child>;
|
|
|
44
46
|
* `Parts` names it so the type makes the requirement, rather than a blank
|
|
45
47
|
* surface at runtime.
|
|
46
48
|
*
|
|
47
|
-
* Called ONCE, during setup
|
|
48
|
-
*
|
|
49
|
+
* Called ONCE, during setup. What changes goes in an accessor inside it, and
|
|
50
|
+
* parts built there still reach their provider — Aurora calls the accessor
|
|
51
|
+
* with the providers it was written under — so a list of options can follow a
|
|
52
|
+
* signal:
|
|
49
53
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* there is
|
|
54
|
+
* SelectContent({ children: () => html`${() => roles().map((r) => SelectItem(…))}` })
|
|
55
|
+
*
|
|
56
|
+
* A part that has to be KNOWN at setup is the exception. `DialogTitle` names
|
|
57
|
+
* the panel, and the panel reads whether there is a title before it renders
|
|
58
|
+
* its children, so a title built later in an accessor leaves it unnamed:
|
|
54
59
|
*
|
|
55
60
|
* DialogHeader({ children: html`${DialogTitle(…)}` }) // ✓ a value
|
|
56
61
|
* DialogHeader({ children: () => html`${DialogTitle(…)}` }) // ✗ too late
|
|
@@ -70,5 +75,8 @@ export type Parts = () => Child;
|
|
|
70
75
|
*/
|
|
71
76
|
export function slot(content: Slot | undefined): () => Child {
|
|
72
77
|
if (content === undefined) return () => null;
|
|
78
|
+
// A keyed list is a function too, but it is content, not an accessor for
|
|
79
|
+
// content: handed over as the value, it keeps its rows by key.
|
|
80
|
+
if (isEach(content)) return () => content;
|
|
73
81
|
return typeof content === "function" ? content : () => content;
|
|
74
82
|
}
|
package/src/lib/icons.ts
CHANGED
|
@@ -133,6 +133,34 @@ export const GripVerticalIcon = (props?: IconProps): TemplateResult =>
|
|
|
133
133
|
props,
|
|
134
134
|
);
|
|
135
135
|
|
|
136
|
+
/** Lucide `circle-check` — shadcn's Sonner success icon. */
|
|
137
|
+
export const CircleCheckIcon = (props?: IconProps): TemplateResult =>
|
|
138
|
+
svg(
|
|
139
|
+
html`<circle cx="12" cy="12" r="10" /><path d="m16 9-5.5 5.5L8 12" />`,
|
|
140
|
+
props,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
/** Lucide `info` — shadcn's Sonner info icon. */
|
|
144
|
+
export const InfoIcon = (props?: IconProps): TemplateResult =>
|
|
145
|
+
svg(
|
|
146
|
+
html`<circle cx="12" cy="12" r="10" /><path d="M12 16v-4" /><path d="M12 8h.01" />`,
|
|
147
|
+
props,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
/** Lucide `triangle-alert` — shadcn's Sonner warning icon. */
|
|
151
|
+
export const TriangleAlertIcon = (props?: IconProps): TemplateResult =>
|
|
152
|
+
svg(
|
|
153
|
+
html`<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" /><path d="M12 9v4" /><path d="M12 17h.01" />`,
|
|
154
|
+
props,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
/** Lucide `octagon-x` — shadcn's Sonner error icon. */
|
|
158
|
+
export const OctagonXIcon = (props?: IconProps): TemplateResult =>
|
|
159
|
+
svg(
|
|
160
|
+
html`<path d="m15 9-6 6" /><path d="M2.586 16.726A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2h6.624a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586z" /><path d="m9 9 6 6" />`,
|
|
161
|
+
props,
|
|
162
|
+
);
|
|
163
|
+
|
|
136
164
|
/** The arc Lucide uses for `loader-circle`; spun by the Spinner's animation. */
|
|
137
165
|
export const LoaderIcon = (props?: IconProps): TemplateResult =>
|
|
138
166
|
svg(html`<path d="M21 12a9 9 0 1 1-6.219-8.56" />`, props);
|
package/src/lib/image.ts
CHANGED
|
@@ -351,6 +351,18 @@ export function setImageEndpoint(path: string): void {
|
|
|
351
351
|
endpoint = path;
|
|
352
352
|
}
|
|
353
353
|
|
|
354
|
+
/**
|
|
355
|
+
* A root-relative `src` as the path under the endpoint's public roots.
|
|
356
|
+
*
|
|
357
|
+
* `/photos/hero.png` is how a page names a public file, and the endpoint
|
|
358
|
+
* resolves `src` inside its roots, refusing an absolute path: sent as is,
|
|
359
|
+
* every variant is a 404. Exactly one slash goes, so a protocol-relative
|
|
360
|
+
* `//host/…` stays what it is and is left to an application's own resolver.
|
|
361
|
+
*/
|
|
362
|
+
function relativeToRoots(src: string): string {
|
|
363
|
+
return src.startsWith("/") && !src.startsWith("//") ? src.slice(1) : src;
|
|
364
|
+
}
|
|
365
|
+
|
|
354
366
|
/**
|
|
355
367
|
* The resolver used when an application installs none.
|
|
356
368
|
*
|
|
@@ -360,7 +372,7 @@ export function setImageEndpoint(path: string): void {
|
|
|
360
372
|
*/
|
|
361
373
|
export const defaultImageResolver: ImageUrlResolver = (src, transform) => {
|
|
362
374
|
const params = new URLSearchParams({
|
|
363
|
-
src,
|
|
375
|
+
src: relativeToRoots(src),
|
|
364
376
|
w: String(Math.round(transform.width)),
|
|
365
377
|
});
|
|
366
378
|
if (transform.format !== undefined) params.set("f", transform.format);
|
|
@@ -32,10 +32,15 @@ export interface BreadcrumbProps {
|
|
|
32
32
|
class?: Reactive<string>;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export
|
|
35
|
+
export interface BreadcrumbNavProps extends BreadcrumbProps {
|
|
36
|
+
/** The `<nav>`'s accessible name. Default `"breadcrumb"`. */
|
|
37
|
+
"aria-label"?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const Breadcrumb = component<BreadcrumbNavProps>(
|
|
36
41
|
(props) => html`<nav
|
|
37
42
|
data-slot="breadcrumb"
|
|
38
|
-
aria-label="breadcrumb"
|
|
43
|
+
aria-label="${props["aria-label"] ?? "breadcrumb"}"
|
|
39
44
|
class="${() => cn(read(props.class))}"
|
|
40
45
|
>${slot(props.children)}</nav>`,
|
|
41
46
|
);
|
|
@@ -97,12 +102,18 @@ export const BreadcrumbSeparator = component<BreadcrumbProps>(
|
|
|
97
102
|
>${props.children === undefined ? ChevronRightIcon() : slot(props.children)}</li>`,
|
|
98
103
|
);
|
|
99
104
|
|
|
105
|
+
export interface BreadcrumbEllipsisProps {
|
|
106
|
+
/** The hidden text. Default `"More"`. */
|
|
107
|
+
label?: string;
|
|
108
|
+
class?: Reactive<string>;
|
|
109
|
+
}
|
|
110
|
+
|
|
100
111
|
/** Stands in for ancestors that did not fit. */
|
|
101
|
-
export const BreadcrumbEllipsis = component<
|
|
112
|
+
export const BreadcrumbEllipsis = component<BreadcrumbEllipsisProps>(
|
|
102
113
|
(props) => html`<span
|
|
103
114
|
data-slot="breadcrumb-ellipsis"
|
|
104
115
|
role="presentation"
|
|
105
116
|
aria-hidden="true"
|
|
106
117
|
class="${() => cn("flex size-9 items-center justify-center", read(props.class))}"
|
|
107
|
-
>${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only"
|
|
118
|
+
>${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only">${props.label ?? "More"}</span></span>`,
|
|
108
119
|
);
|
|
@@ -38,7 +38,9 @@ const slotClasses =
|
|
|
38
38
|
|
|
39
39
|
interface InputOTPApi {
|
|
40
40
|
readonly length: number;
|
|
41
|
+
readonly inputMode: "numeric" | "text";
|
|
41
42
|
readonly disabled: () => boolean;
|
|
43
|
+
readonly slotLabel: (position: number, length: number) => string;
|
|
42
44
|
/** Claim the next box's index, in the order the slots are built. */
|
|
43
45
|
claimSlot(): number;
|
|
44
46
|
onInput(index: number, event: Event): void;
|
|
@@ -48,9 +50,24 @@ interface InputOTPApi {
|
|
|
48
50
|
|
|
49
51
|
const InputOTPContext = createContext<InputOTPApi>("InputOTP");
|
|
50
52
|
|
|
53
|
+
/** input-otp's patterns, for the `pattern` prop. */
|
|
54
|
+
export const REGEXP_ONLY_DIGITS = "^\\d+$";
|
|
55
|
+
export const REGEXP_ONLY_CHARS = "^[a-zA-Z]+$";
|
|
56
|
+
export const REGEXP_ONLY_DIGITS_AND_CHARS = "^[a-zA-Z0-9]+$";
|
|
57
|
+
|
|
51
58
|
export interface InputOTPProps {
|
|
52
59
|
/** Number of boxes. Default `6` — and it must match the slots built. */
|
|
53
60
|
length?: number;
|
|
61
|
+
/**
|
|
62
|
+
* What the code may hold, tested against the whole value as input-otp
|
|
63
|
+
* does: a character or a fill that would not match is refused. None by
|
|
64
|
+
* default, as input-otp; `REGEXP_ONLY_DIGITS` for a numeric code.
|
|
65
|
+
*/
|
|
66
|
+
pattern?: RegExp | string;
|
|
67
|
+
/** The on-screen keyboard. Default `"numeric"`. */
|
|
68
|
+
inputMode?: "numeric" | "text";
|
|
69
|
+
/** Each box's accessible name from its position. Default `"Character 2 of 6"`. */
|
|
70
|
+
slotLabel?: (position: number, length: number) => string;
|
|
54
71
|
name?: string;
|
|
55
72
|
disabled?: Reactive<boolean>;
|
|
56
73
|
class?: Reactive<string>;
|
|
@@ -96,9 +113,51 @@ export const InputOTP = component<InputOTPProps>((props) => {
|
|
|
96
113
|
commit(next);
|
|
97
114
|
}
|
|
98
115
|
|
|
116
|
+
const pattern =
|
|
117
|
+
typeof props.pattern === "string"
|
|
118
|
+
? new RegExp(props.pattern)
|
|
119
|
+
: props.pattern;
|
|
120
|
+
|
|
121
|
+
function accepts(next: readonly string[]): boolean {
|
|
122
|
+
const joined = next.join("");
|
|
123
|
+
return pattern === undefined || joined === "" || pattern.test(joined);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Put the boxes back to what the code holds. */
|
|
127
|
+
function reflect(): void {
|
|
128
|
+
const inputs = boxes();
|
|
129
|
+
characters().forEach((character, position) => {
|
|
130
|
+
const input = inputs[position];
|
|
131
|
+
if (input !== undefined) input.value = character;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Share a run of characters out over the boxes from `index`: a paste, and
|
|
137
|
+
* a whole code an autofill or an IME put into one box.
|
|
138
|
+
*/
|
|
139
|
+
function spread(index: number, text: string): void {
|
|
140
|
+
const next = [...characters()];
|
|
141
|
+
let cursor = index;
|
|
142
|
+
for (const character of text) {
|
|
143
|
+
if (cursor >= length) break;
|
|
144
|
+
next[cursor] = character;
|
|
145
|
+
cursor += 1;
|
|
146
|
+
}
|
|
147
|
+
if (accepts(next)) commit(next);
|
|
148
|
+
// The boxes are uncontrolled between keystrokes, so writing the signal
|
|
149
|
+
// alone would leave the characters invisible — or a refused fill in place.
|
|
150
|
+
reflect();
|
|
151
|
+
focusBox(Math.min(cursor, length - 1));
|
|
152
|
+
}
|
|
153
|
+
|
|
99
154
|
provide<InputOTPApi>(InputOTPContext, {
|
|
100
155
|
length,
|
|
156
|
+
inputMode: props.inputMode ?? "numeric",
|
|
101
157
|
disabled: () => readOr(props.disabled, false),
|
|
158
|
+
slotLabel:
|
|
159
|
+
props.slotLabel ??
|
|
160
|
+
((position, total) => `Character ${position} of ${total}`),
|
|
102
161
|
claimSlot() {
|
|
103
162
|
const index = claimed;
|
|
104
163
|
claimed += 1;
|
|
@@ -108,12 +167,31 @@ export const InputOTP = component<InputOTPProps>((props) => {
|
|
|
108
167
|
const target = event.target;
|
|
109
168
|
if (!(target instanceof HTMLInputElement)) return;
|
|
110
169
|
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
170
|
+
const value = target.value;
|
|
171
|
+
// One key pressed in a box already holding a character: the box holds
|
|
172
|
+
// both, and the one just typed is what counts.
|
|
173
|
+
const typed =
|
|
174
|
+
event instanceof InputEvent &&
|
|
175
|
+
event.inputType === "insertText" &&
|
|
176
|
+
event.data !== null &&
|
|
177
|
+
[...event.data].length === 1
|
|
178
|
+
? event.data
|
|
179
|
+
: undefined;
|
|
180
|
+
if ([...value].length > 1 && typed === undefined) {
|
|
181
|
+
// A whole code at once: autofill, a password manager, an IME.
|
|
182
|
+
spread(index, value);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const character = typed ?? value;
|
|
186
|
+
const next = [...characters()];
|
|
187
|
+
next[index] = character;
|
|
188
|
+
if (!accepts(next)) {
|
|
189
|
+
target.value = characters()[index] ?? "";
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
target.value = character;
|
|
193
|
+
commit(next);
|
|
194
|
+
if (character !== "" && index < length - 1) focusBox(index + 1);
|
|
117
195
|
},
|
|
118
196
|
onKeyDown(index, event) {
|
|
119
197
|
if (event.key === "Backspace") {
|
|
@@ -138,25 +216,7 @@ export const InputOTP = component<InputOTPProps>((props) => {
|
|
|
138
216
|
const pasted = event.clipboardData?.getData("text") ?? "";
|
|
139
217
|
if (pasted === "") return;
|
|
140
218
|
event.preventDefault();
|
|
141
|
-
|
|
142
|
-
const next = [...characters()];
|
|
143
|
-
let cursor = index;
|
|
144
|
-
for (const character of pasted) {
|
|
145
|
-
if (cursor >= length) break;
|
|
146
|
-
next[cursor] = character;
|
|
147
|
-
cursor += 1;
|
|
148
|
-
}
|
|
149
|
-
commit(next);
|
|
150
|
-
|
|
151
|
-
// Reflect into the DOM: the boxes are uncontrolled between keystrokes,
|
|
152
|
-
// so writing the signal alone would leave the pasted characters
|
|
153
|
-
// invisible.
|
|
154
|
-
const inputs = boxes();
|
|
155
|
-
next.forEach((character, position) => {
|
|
156
|
-
const input = inputs[position];
|
|
157
|
-
if (input !== undefined) input.value = character;
|
|
158
|
-
});
|
|
159
|
-
focusBox(Math.min(cursor, length - 1));
|
|
219
|
+
spread(index, pasted);
|
|
160
220
|
},
|
|
161
221
|
});
|
|
162
222
|
|
|
@@ -203,10 +263,10 @@ export const InputOTPSlot = component<InputOTPSlotProps>((props) => {
|
|
|
203
263
|
data-slot="input-otp-slot"
|
|
204
264
|
data-otp-index="${index}"
|
|
205
265
|
type="text"
|
|
206
|
-
inputmode="
|
|
266
|
+
inputmode="${otp.inputMode}"
|
|
207
267
|
autocomplete="${index === 0 ? "one-time-code" : "off"}"
|
|
208
|
-
maxlength="
|
|
209
|
-
aria-label="${
|
|
268
|
+
maxlength="${otp.length}"
|
|
269
|
+
aria-label="${otp.slotLabel(index + 1, otp.length)}"
|
|
210
270
|
?disabled="${() => otp.disabled()}"
|
|
211
271
|
class="${() => cn(slotClasses, read(props.class))}"
|
|
212
272
|
@input="${(event: Event) => otp.onInput(index, event)}"
|
|
@@ -79,11 +79,45 @@ export interface PaginationProps {
|
|
|
79
79
|
class?: Reactive<string>;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Every text the pagination writes, in one place.
|
|
84
|
+
*
|
|
85
|
+
* The defaults are upstream's English. An app in another language passes its
|
|
86
|
+
* own: most of these are read only by a screen reader, which would otherwise
|
|
87
|
+
* switch to English in the middle of the page.
|
|
88
|
+
*/
|
|
89
|
+
export interface PaginationLabels {
|
|
90
|
+
/** The `<nav>`'s accessible name. */
|
|
91
|
+
navigation: string;
|
|
92
|
+
/** The visible word beside the previous arrow. */
|
|
93
|
+
previous: string;
|
|
94
|
+
/** The previous link's accessible name. */
|
|
95
|
+
previousPage: string;
|
|
96
|
+
next: string;
|
|
97
|
+
nextPage: string;
|
|
98
|
+
/** The ellipsis's hidden text. */
|
|
99
|
+
morePages: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Frozen: the defaults, not a place to set an app's language from. */
|
|
103
|
+
export const paginationLabels: Readonly<PaginationLabels> = Object.freeze({
|
|
104
|
+
navigation: "pagination",
|
|
105
|
+
previous: "Previous",
|
|
106
|
+
previousPage: "Go to previous page",
|
|
107
|
+
next: "Next",
|
|
108
|
+
nextPage: "Go to next page",
|
|
109
|
+
morePages: "More pages",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
export interface PaginationNavProps extends PaginationProps {
|
|
113
|
+
"aria-label"?: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const Pagination = component<PaginationNavProps>(
|
|
83
117
|
(props) => html`<nav
|
|
84
118
|
data-slot="pagination"
|
|
85
119
|
role="navigation"
|
|
86
|
-
aria-label="
|
|
120
|
+
aria-label="${props["aria-label"] ?? paginationLabels.navigation}"
|
|
87
121
|
class="${() => cn("mx-auto flex w-full justify-center", read(props.class))}"
|
|
88
122
|
>${slot(props.children)}</nav>`,
|
|
89
123
|
);
|
|
@@ -153,18 +187,24 @@ export const PaginationLink = component<PaginationLinkProps>((props) => {
|
|
|
153
187
|
>${slot(props.children)}</a>`;
|
|
154
188
|
});
|
|
155
189
|
|
|
190
|
+
/**
|
|
191
|
+
* `aria-label` replaces the accessible name, as upstream's prop spread lets it.
|
|
192
|
+
* `children` replaces the visible word and keeps the arrow — upstream writes
|
|
193
|
+
* that word in the file, which an app owning its copy can edit and a package
|
|
194
|
+
* cannot.
|
|
195
|
+
*/
|
|
156
196
|
export type PaginationStepProps = Omit<
|
|
157
197
|
PaginationLinkProps,
|
|
158
|
-
"size" | "isActive"
|
|
198
|
+
"size" | "isActive"
|
|
159
199
|
>;
|
|
160
200
|
|
|
161
201
|
export const PaginationPrevious = component<PaginationStepProps>((props) =>
|
|
162
202
|
PaginationLink({
|
|
163
203
|
...props,
|
|
164
204
|
size: "default",
|
|
165
|
-
"aria-label": "
|
|
205
|
+
"aria-label": props["aria-label"] ?? paginationLabels.previousPage,
|
|
166
206
|
class: () => cn("gap-1 px-2.5 sm:pl-2.5", read(props.class)),
|
|
167
|
-
children: html`${ChevronLeftIcon({})}<span class="hidden sm:block"
|
|
207
|
+
children: html`${ChevronLeftIcon({})}<span class="hidden sm:block">${slot(props.children ?? paginationLabels.previous)}</span>`,
|
|
168
208
|
}),
|
|
169
209
|
);
|
|
170
210
|
|
|
@@ -172,13 +212,15 @@ export const PaginationNext = component<PaginationStepProps>((props) =>
|
|
|
172
212
|
PaginationLink({
|
|
173
213
|
...props,
|
|
174
214
|
size: "default",
|
|
175
|
-
"aria-label": "
|
|
215
|
+
"aria-label": props["aria-label"] ?? paginationLabels.nextPage,
|
|
176
216
|
class: () => cn("gap-1 px-2.5 sm:pr-2.5", read(props.class)),
|
|
177
|
-
children: html`<span class="hidden sm:block"
|
|
217
|
+
children: html`<span class="hidden sm:block">${slot(props.children ?? paginationLabels.next)}</span>${ChevronRightIcon({})}`,
|
|
178
218
|
}),
|
|
179
219
|
);
|
|
180
220
|
|
|
181
221
|
export interface PaginationEllipsisProps {
|
|
222
|
+
/** The hidden text. Default `"More pages"`. */
|
|
223
|
+
label?: string;
|
|
182
224
|
class?: Reactive<string>;
|
|
183
225
|
}
|
|
184
226
|
|
|
@@ -188,5 +230,5 @@ export const PaginationEllipsis = component<PaginationEllipsisProps>(
|
|
|
188
230
|
data-slot="pagination-ellipsis"
|
|
189
231
|
aria-hidden="true"
|
|
190
232
|
class="${() => cn("flex size-9 items-center justify-center", read(props.class))}"
|
|
191
|
-
>${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only"
|
|
233
|
+
>${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only">${props.label ?? paginationLabels.morePages}</span></span>`,
|
|
192
234
|
);
|