@wwchen/stack 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +79 -2
- package/blocks/AGENTS.md +77 -0
- package/blocks/CLAUDE.md +113 -0
- package/blocks/env.example +34 -0
- package/blocks/gitignore +18 -0
- package/configs/biome.json +45 -0
- package/configs/tsconfig.next.json +22 -0
- package/dist/auth/better-auth/index.d.ts +316 -0
- package/dist/auth/better-auth/index.js +237 -0
- package/dist/auth/better-auth/tables.d.ts +628 -0
- package/dist/auth/better-auth/tables.js +74 -0
- package/dist/auth/better-auth/users.d.ts +27 -0
- package/dist/auth/better-auth/users.js +128 -0
- package/dist/auth/index.d.ts +58 -0
- package/dist/auth/index.js +120 -0
- package/dist/auth/proxy.d.ts +14 -0
- package/dist/auth/proxy.js +50 -0
- package/dist/auth/safe-next.d.ts +17 -0
- package/dist/auth/safe-next.js +36 -0
- package/dist/auth/types.d.ts +47 -0
- package/dist/auth/types.js +5 -0
- package/dist/cli/context.d.ts +38 -0
- package/dist/cli/context.js +43 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +95 -0
- package/dist/cli/neon.d.ts +97 -0
- package/dist/cli/neon.js +334 -0
- package/dist/cli/sync-blocks.d.ts +16 -0
- package/dist/cli/sync-blocks.js +97 -0
- package/dist/cli/user-create.d.ts +2 -0
- package/dist/cli/user-create.js +35 -0
- package/dist/cli/vercel-build.d.ts +4 -0
- package/dist/cli/vercel-build.js +72 -0
- package/dist/configs/vitest.d.ts +4 -0
- package/dist/configs/vitest.js +50 -0
- package/dist/connections/crypto.d.ts +8 -0
- package/dist/connections/crypto.js +50 -0
- package/dist/connections/index.d.ts +63 -0
- package/dist/connections/index.js +212 -0
- package/dist/connections/providers.d.ts +30 -0
- package/dist/connections/providers.js +47 -0
- package/dist/connections/table.d.ts +156 -0
- package/dist/connections/table.js +29 -0
- package/dist/db/explaining-pool.d.ts +16 -0
- package/dist/db/explaining-pool.js +62 -0
- package/dist/db/index.d.ts +21 -0
- package/dist/db/index.js +36 -0
- package/dist/db/migration-status.d.ts +8 -0
- package/dist/db/migration-status.js +41 -0
- package/dist/db/target.d.ts +26 -0
- package/dist/db/target.js +72 -0
- package/dist/db/testing/index.d.ts +27 -0
- package/dist/db/testing/index.js +71 -0
- package/dist/db/testing/isolate.d.ts +1 -0
- package/dist/db/testing/isolate.js +4 -0
- package/dist/db/testing/pglite.d.ts +21 -0
- package/dist/db/testing/pglite.js +71 -0
- package/dist/env/define-env.d.ts +37 -0
- package/dist/env/define-env.js +134 -0
- package/dist/env/index.d.ts +3 -0
- package/dist/env/index.js +3 -0
- package/dist/env/load-env-file.d.ts +15 -0
- package/dist/env/load-env-file.js +52 -0
- package/dist/env/stack-env.d.ts +40 -0
- package/dist/env/stack-env.js +60 -0
- package/dist/env/testing.d.ts +8 -0
- package/dist/env/testing.js +16 -0
- package/dist/http/client-ip.d.ts +12 -0
- package/dist/http/client-ip.js +28 -0
- package/dist/http/guards.d.ts +17 -0
- package/dist/http/guards.js +57 -0
- package/dist/http/rate-limit.d.ts +34 -0
- package/dist/http/rate-limit.js +79 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/jobs/index.d.ts +37 -0
- package/dist/jobs/index.js +51 -0
- package/dist/mail/index.d.ts +45 -0
- package/dist/mail/index.js +97 -0
- package/dist/next/api.d.ts +71 -0
- package/dist/next/api.js +118 -0
- package/dist/next/config.d.ts +8 -0
- package/dist/next/config.js +104 -0
- package/dist/next/cron.d.ts +14 -0
- package/dist/next/cron.js +40 -0
- package/dist/next/health.d.ts +19 -0
- package/dist/next/health.js +36 -0
- package/dist/next/index.d.ts +5 -0
- package/dist/next/index.js +4 -0
- package/dist/testing/next.d.ts +14 -0
- package/dist/testing/next.js +39 -0
- package/dist/ui/app.css +56 -0
- package/dist/ui/globals.css +152 -0
- package/dist/ui/icons.d.ts +17 -0
- package/dist/ui/icons.js +104 -0
- package/dist/ui/index.d.ts +33 -0
- package/dist/ui/index.js +41 -0
- package/dist/ui/theme.d.ts +18 -0
- package/dist/ui/theme.js +24 -0
- package/package.json +134 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { bearerMatches } from "../http/guards.js";
|
|
2
|
+
import { apiError } from "./api.js";
|
|
3
|
+
/**
|
|
4
|
+
* A scheduled job's route, called by Vercel Cron with `Authorization: Bearer $CRON_SECRET`.
|
|
5
|
+
* `CRON_SECRET` triggers scheduled jobs and nothing else: with none configured every request is
|
|
6
|
+
* refused (an empty bearer is not accepted), and the response is whatever `fn` returned, which
|
|
7
|
+
* must be counts. No row data, no ids, no messages.
|
|
8
|
+
*
|
|
9
|
+
* A job that returns `alerting: true` answers 500 with the same counts, so an uptime monitor calling
|
|
10
|
+
* it alerts: a multi-person job sets it while someone's failure is new (`@wwchen/stack/jobs`).
|
|
11
|
+
*
|
|
12
|
+
* `export const GET = cronRoute(tick, { secret: env.CRON_SECRET })`.
|
|
13
|
+
*/
|
|
14
|
+
export function cronRoute(fn, opts) {
|
|
15
|
+
return async (req) => {
|
|
16
|
+
if (!bearerMatches(req, opts.secret)) {
|
|
17
|
+
// A refused call writes nothing anywhere, so without this line a missing or wrong secret
|
|
18
|
+
// looks exactly like a cron that never fired. Never logs what was presented.
|
|
19
|
+
console.warn(JSON.stringify({
|
|
20
|
+
level: "warn",
|
|
21
|
+
msg: "cron refused",
|
|
22
|
+
reason: opts.secret ? "bad secret" : "CRON_SECRET not set",
|
|
23
|
+
}));
|
|
24
|
+
return apiError(401, "unauthenticated", "Invalid cron credentials.");
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const counts = await fn(req);
|
|
28
|
+
const alerting = counts?.alerting === true;
|
|
29
|
+
return Response.json(counts ?? {}, {
|
|
30
|
+
status: alerting ? 500 : 200,
|
|
31
|
+
headers: { "cache-control": "no-store" },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
// A short-lived function can be frozen before a logger drains, so this is the one line.
|
|
36
|
+
console.error("[cron] job failed", error);
|
|
37
|
+
return apiError(500, "internal_error", "Something went wrong.");
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
2
|
+
/**
|
|
3
|
+
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a
|
|
4
|
+
* database blip must not make an uptime check report a healthy web process as down.
|
|
5
|
+
*
|
|
6
|
+
* Without the token the answer is `{ ok: true }` and the database is never touched, so an
|
|
7
|
+
* anonymous caller cannot make the app query it. With `Authorization: Bearer $HEALTH_TOKEN` it
|
|
8
|
+
* also reports `db` and `migrations`. `HEALTH_TOKEN` is its own secret, never `CRON_SECRET`.
|
|
9
|
+
*
|
|
10
|
+
* `export const GET = healthRoute({ db, token: env.HEALTH_TOKEN })`, with `dynamic =
|
|
11
|
+
* "force-dynamic"`. `withStack` traces `./drizzle/**` into this route so the migrations folder
|
|
12
|
+
* exists in a deployment.
|
|
13
|
+
*/
|
|
14
|
+
export declare function healthRoute(opts: {
|
|
15
|
+
db?: NodePgDatabase<any>;
|
|
16
|
+
/** Defaults to `drizzle/` under the working directory. */
|
|
17
|
+
migrationsFolder?: string;
|
|
18
|
+
token?: string;
|
|
19
|
+
}): (req: Request) => Promise<Response>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
|
+
import { migrationStatus } from "../db/migration-status.js";
|
|
4
|
+
import { bearerMatches } from "../http/guards.js";
|
|
5
|
+
/**
|
|
6
|
+
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a
|
|
7
|
+
* database blip must not make an uptime check report a healthy web process as down.
|
|
8
|
+
*
|
|
9
|
+
* Without the token the answer is `{ ok: true }` and the database is never touched, so an
|
|
10
|
+
* anonymous caller cannot make the app query it. With `Authorization: Bearer $HEALTH_TOKEN` it
|
|
11
|
+
* also reports `db` and `migrations`. `HEALTH_TOKEN` is its own secret, never `CRON_SECRET`.
|
|
12
|
+
*
|
|
13
|
+
* `export const GET = healthRoute({ db, token: env.HEALTH_TOKEN })`, with `dynamic =
|
|
14
|
+
* "force-dynamic"`. `withStack` traces `./drizzle/**` into this route so the migrations folder
|
|
15
|
+
* exists in a deployment.
|
|
16
|
+
*/
|
|
17
|
+
export function healthRoute(opts) {
|
|
18
|
+
const migrationsFolder = opts.migrationsFolder ?? path.join(process.cwd(), "drizzle");
|
|
19
|
+
// No-store: a cached liveness answer is not a liveness answer.
|
|
20
|
+
const headers = { "cache-control": "no-store" };
|
|
21
|
+
return async (req) => {
|
|
22
|
+
if (!bearerMatches(req, opts.token))
|
|
23
|
+
return Response.json({ ok: true }, { headers });
|
|
24
|
+
const { db } = opts;
|
|
25
|
+
if (!db)
|
|
26
|
+
return Response.json({ ok: true, db: "unknown", migrations: "unknown" }, { headers });
|
|
27
|
+
try {
|
|
28
|
+
await db.execute(sql `select 1`);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return Response.json({ ok: true, db: "down", migrations: "unknown" }, { headers });
|
|
32
|
+
}
|
|
33
|
+
const migrations = await migrationStatus(db, migrationsFolder).catch(() => "unknown");
|
|
34
|
+
return Response.json({ ok: true, db: "up", migrations }, { headers });
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { ApiHandlerOptions, BodySchema } from "./api.ts";
|
|
2
|
+
export { ApiError, apiError, apiHandler, NotFoundError, PathIdError, readJson, requirePathId, } from "./api.ts";
|
|
3
|
+
export { withStack } from "./config.ts";
|
|
4
|
+
export { cronRoute } from "./cron.ts";
|
|
5
|
+
export { healthRoute } from "./health.ts";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` inside a real Next request scope in its server-action phase — what Next sets up around
|
|
3
|
+
* a `"use server"` function — so `headers()` reads `headers` and `cookies().set()` works. Returns
|
|
4
|
+
* the `Set-Cookie` lines the scope would send, which is how a test sees what Better Auth's
|
|
5
|
+
* `nextCookies()` wrote. Built from Next's own internals (next is pinned exactly).
|
|
6
|
+
*
|
|
7
|
+
* `redirect()` and `notFound()` throw as they do in Next; `thrownBy` reads where they pointed.
|
|
8
|
+
*/
|
|
9
|
+
export declare function inServerAction<T>(headers: HeadersInit, fn: () => Promise<T>): Promise<{
|
|
10
|
+
result: T;
|
|
11
|
+
setCookies: string[];
|
|
12
|
+
}>;
|
|
13
|
+
/** What a `redirect()` or `notFound()` thrown by `fn` means: `redirect:<url>` or `http:<status>`. */
|
|
14
|
+
export declare function thrownBy(fn: () => Promise<unknown>): Promise<string>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// `@wwchen/stack/testing/next`: test helpers, for a stack app's tests and the package's own. Not
|
|
2
|
+
// for app code. Built on Next's internals, which move with the exact `next` the stack pins, so they
|
|
3
|
+
// ship here and an app takes a fix with the stack rather than keeping a copy.
|
|
4
|
+
import { workAsyncStorage } from "next/dist/server/app-render/work-async-storage.external.js";
|
|
5
|
+
import { workUnitAsyncStorage } from "next/dist/server/app-render/work-unit-async-storage.external.js";
|
|
6
|
+
import { createRequestStoreForAPI } from "next/dist/server/async-storage/request-store.js";
|
|
7
|
+
/**
|
|
8
|
+
* Runs `fn` inside a real Next request scope in its server-action phase — what Next sets up around
|
|
9
|
+
* a `"use server"` function — so `headers()` reads `headers` and `cookies().set()` works. Returns
|
|
10
|
+
* the `Set-Cookie` lines the scope would send, which is how a test sees what Better Auth's
|
|
11
|
+
* `nextCookies()` wrote. Built from Next's own internals (next is pinned exactly).
|
|
12
|
+
*
|
|
13
|
+
* `redirect()` and `notFound()` throw as they do in Next; `thrownBy` reads where they pointed.
|
|
14
|
+
*/
|
|
15
|
+
export async function inServerAction(headers, fn) {
|
|
16
|
+
let setCookies = [];
|
|
17
|
+
const store = createRequestStoreForAPI({ headers: Object.fromEntries(new Headers(headers)) }, new URL("http://localhost:3000/sign-in"), { tags: [], expirationsByCacheKind: new Map() }, (cookies) => {
|
|
18
|
+
setCookies = cookies;
|
|
19
|
+
}, undefined, undefined);
|
|
20
|
+
const result = await workAsyncStorage.run({ route: "/sign-in" }, () => workUnitAsyncStorage.run(store, fn));
|
|
21
|
+
return { result, setCookies };
|
|
22
|
+
}
|
|
23
|
+
/** What a `redirect()` or `notFound()` thrown by `fn` means: `redirect:<url>` or `http:<status>`. */
|
|
24
|
+
export async function thrownBy(fn) {
|
|
25
|
+
try {
|
|
26
|
+
await fn();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
const digest = error.digest;
|
|
30
|
+
if (typeof digest === "string" && digest.startsWith("NEXT_REDIRECT;")) {
|
|
31
|
+
return `redirect:${digest.split(";")[2]}`;
|
|
32
|
+
}
|
|
33
|
+
if (typeof digest === "string" && digest.startsWith("NEXT_HTTP_ERROR_FALLBACK;")) {
|
|
34
|
+
return `http:${digest.split(";")[1]}`;
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
throw new Error("expected a redirect or notFound, but nothing was thrown");
|
|
39
|
+
}
|
package/dist/ui/app.css
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The bridge onto LDS (`@lew-ds/lds`, imported in globals.css): the only place the package adjusts
|
|
3
|
+
* it. Each rule says which gap it fills.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Fonts. The product theme names 'Inter', which LDS does not ship as a file. An app that loads
|
|
8
|
+
* Inter through `next/font` exposes it as `--font-inter` under a generated family name, so the
|
|
9
|
+
* theme's font roles point there; without it they fall back to the system font.
|
|
10
|
+
* `--th-mono` stays the theme's ui-monospace.
|
|
11
|
+
*/
|
|
12
|
+
.theme-product {
|
|
13
|
+
--th-display: var(--font-inter, Inter), system-ui, sans-serif;
|
|
14
|
+
--th-body: var(--font-inter, Inter), system-ui, sans-serif;
|
|
15
|
+
--th-ui: var(--th-display);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* Two roles LDS spells only inside a component, given a name so a layout utility can use them: the
|
|
20
|
+
* soft tint (`.emph-soft`'s fill and ink, for a selected nav item) and the error ink (an
|
|
21
|
+
* `lds-field__error`'s colour on the red ramp).
|
|
22
|
+
*/
|
|
23
|
+
:root {
|
|
24
|
+
--app-tint: var(--c-100);
|
|
25
|
+
--app-tint-fg: var(--c-800);
|
|
26
|
+
--app-danger: var(--red-700);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.mode-dark {
|
|
30
|
+
--app-tint: var(--c-800);
|
|
31
|
+
--app-tint-fg: var(--c-100);
|
|
32
|
+
--app-danger: var(--red-200);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Tooltip line breaks. LDS's bubble collapses whitespace, so a label listing several items, one to
|
|
37
|
+
* a line, would run together. `pre-line` keeps the label's newlines and still wraps long lines; a
|
|
38
|
+
* label without one reads as before.
|
|
39
|
+
*/
|
|
40
|
+
.lds-tooltip__bubble {
|
|
41
|
+
white-space: pre-line;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
* No zoom on focus. iOS Safari zooms the page into any field whose text is under 16px and leaves
|
|
46
|
+
* it zoomed after; LDS's fields take the 13px body. On a touch screen they take 16px, so focusing a
|
|
47
|
+
* field leaves the page where it was. Pinch zoom stays: the viewport is never locked
|
|
48
|
+
* (`maximum-scale` would break the 200% zoom rule). A desktop keeps LDS's size.
|
|
49
|
+
*/
|
|
50
|
+
@media (pointer: coarse) {
|
|
51
|
+
.lds-field input,
|
|
52
|
+
.lds-field textarea,
|
|
53
|
+
.lds-field select {
|
|
54
|
+
font-size: max(16px, var(--text-body));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The design system is LDS (Lew Design System), product theme: the `@lew-ds/lds` package.
|
|
3
|
+
* LDS paints components through its own class names (`lds-btn`, `lds-tag`, `lds-table`, …), which
|
|
4
|
+
* `@lew-ds/lds-react`'s components emit. Tailwind is kept for LAYOUT, and every colour, radius
|
|
5
|
+
* and shadow utility below resolves to an LDS role, so there is one palette, not two.
|
|
6
|
+
*
|
|
7
|
+
* Import this file FIRST in the app's stylesheet, then Tailwind:
|
|
8
|
+
*
|
|
9
|
+
* @import "@wwchen/stack/ui/globals.css";
|
|
10
|
+
* @import "tailwindcss";
|
|
11
|
+
*
|
|
12
|
+
* The layer order below must be the first `@layer` statement Tailwind's own sees: LDS sits after
|
|
13
|
+
* Tailwind's preflight (so it styles bare buttons and links) and before `components` and
|
|
14
|
+
* `utilities` (so a layout utility on an LDS element still wins). LDS's root tokens therefore also
|
|
15
|
+
* win over any Tailwind default that shares their name (`--radius-lg`, `--shadow-sm`,
|
|
16
|
+
* `--leading-tight`): LDS is the system.
|
|
17
|
+
*/
|
|
18
|
+
@layer theme, base, lds, components, utilities;
|
|
19
|
+
|
|
20
|
+
@import "@lew-ds/lds/css/palette" layer(lds);
|
|
21
|
+
@import "@lew-ds/lds/css/lds" layer(lds);
|
|
22
|
+
@import "@lew-ds/lds/css/themes/product" layer(lds);
|
|
23
|
+
@import "./app.css" layer(lds);
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* Dark mode is LDS's `mode-dark` class, which the SERVER writes onto <html> from the theme cookie
|
|
27
|
+
* beside `theme-product` (see `htmlClassName`): never a media query and never a client-side flip,
|
|
28
|
+
* so the first response is already correct.
|
|
29
|
+
*/
|
|
30
|
+
@custom-variant dark (&:where(.mode-dark, .mode-dark *));
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* `inline`: each utility emits the LDS variable itself (`color: var(--text)`), so it re-resolves
|
|
34
|
+
* wherever an LDS emphasis or mode class re-points the role, the way LDS's own components do.
|
|
35
|
+
*/
|
|
36
|
+
@theme inline {
|
|
37
|
+
/* ---- surfaces, borders and text: LDS's object-colour roles ---- */
|
|
38
|
+
--color-bg: var(--surface-page);
|
|
39
|
+
--color-surface: var(--surface-base);
|
|
40
|
+
--color-surface-raised: var(--surface-raised);
|
|
41
|
+
--color-sunken: var(--surface-sunken);
|
|
42
|
+
/* The 1px hairline that separates sections, and the quieter one inside a bounded panel. */
|
|
43
|
+
--color-border: var(--border);
|
|
44
|
+
--color-border-subdued: var(--border-subdued);
|
|
45
|
+
--color-fg: var(--text);
|
|
46
|
+
/* LDS has two text tiers: text and text-subdued. */
|
|
47
|
+
--color-fg-subdued: var(--text-subdued);
|
|
48
|
+
/* Glyphs and dashed tracks. NEVER body text. */
|
|
49
|
+
--color-icon: var(--icon);
|
|
50
|
+
--color-accent: var(--text-accent);
|
|
51
|
+
--color-link: var(--link);
|
|
52
|
+
/* LDS's soft emphasis (c-100 on c-800, mirrored in dark): a selected nav item. */
|
|
53
|
+
--color-tint: var(--app-tint);
|
|
54
|
+
--color-tint-fg: var(--app-tint-fg);
|
|
55
|
+
--color-danger: var(--app-danger);
|
|
56
|
+
|
|
57
|
+
/* ---- type: LDS product's scale (Inter; metadata in the theme's mono) ---- */
|
|
58
|
+
--font-sans: var(--th-body);
|
|
59
|
+
--font-mono: var(--th-mono);
|
|
60
|
+
|
|
61
|
+
--text-micro: var(--size-0);
|
|
62
|
+
--text-micro--line-height: var(--leading-normal);
|
|
63
|
+
--text-secondary: var(--size-1);
|
|
64
|
+
--text-secondary--line-height: var(--leading-normal);
|
|
65
|
+
--text-small: var(--size-2);
|
|
66
|
+
--text-small--line-height: var(--leading-normal);
|
|
67
|
+
/* LDS's body (13px under theme-product). Named `copy` because LDS owns `--text-body`. */
|
|
68
|
+
--text-copy: var(--text-body);
|
|
69
|
+
--text-copy--line-height: var(--leading-body);
|
|
70
|
+
--text-emphasis: var(--size-3);
|
|
71
|
+
--text-emphasis--line-height: var(--leading-normal);
|
|
72
|
+
--text-emphasis--font-weight: var(--weight-semibold);
|
|
73
|
+
--text-brand: var(--size-4);
|
|
74
|
+
--text-brand--line-height: var(--leading-tight);
|
|
75
|
+
--text-brand--font-weight: var(--weight-semibold);
|
|
76
|
+
--text-section: var(--size-4);
|
|
77
|
+
--text-section--line-height: var(--leading-tight);
|
|
78
|
+
--text-section--font-weight: var(--weight-semibold);
|
|
79
|
+
/* The page h1: LDS's title (19px under theme-product). */
|
|
80
|
+
--text-headline: var(--text-title);
|
|
81
|
+
--text-headline--line-height: var(--leading-tight);
|
|
82
|
+
--text-headline--font-weight: var(--weight-semibold);
|
|
83
|
+
|
|
84
|
+
/* ---- spacing: LDS's 4px scale ---- */
|
|
85
|
+
--spacing: var(--space-1);
|
|
86
|
+
|
|
87
|
+
/* ---- radius and elevation: LDS product ---- */
|
|
88
|
+
/* Panels take the theme's base radius (10px), not LDS's 28px card radius: a dense table frame. */
|
|
89
|
+
--radius-card: var(--radius);
|
|
90
|
+
--radius-control: var(--pill);
|
|
91
|
+
--shadow-panel: var(--shadow-base);
|
|
92
|
+
|
|
93
|
+
/* ---- layout ---- */
|
|
94
|
+
--container-page: 1280px;
|
|
95
|
+
--container-form: 960px;
|
|
96
|
+
|
|
97
|
+
/* ---- motion ---- */
|
|
98
|
+
--ease-out-ui: var(--ease-standard);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
:root {
|
|
102
|
+
color-scheme: light;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
:root.mode-dark {
|
|
106
|
+
color-scheme: dark;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@layer base {
|
|
110
|
+
/*
|
|
111
|
+
* The root stays 14px (LDS's size-3): every rem-based utility (the container widths) is measured
|
|
112
|
+
* from it, and LDS's own sizes are px. Text takes LDS's 13px body from the `text-copy` on <body>,
|
|
113
|
+
* not from here.
|
|
114
|
+
*/
|
|
115
|
+
html {
|
|
116
|
+
font-size: var(--size-3);
|
|
117
|
+
line-height: var(--leading-body);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
body {
|
|
121
|
+
background-color: var(--surface-page);
|
|
122
|
+
color: var(--text);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/* A visible focus ring at >= 3:1, on everything, in both themes: LDS's 2px accent outline. */
|
|
126
|
+
:focus-visible {
|
|
127
|
+
outline: 2px solid var(--text-accent);
|
|
128
|
+
outline-offset: 2px;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/* Money and count figures are tabular everywhere: misaligned digits in a column are a bug. */
|
|
132
|
+
[data-numeric] {
|
|
133
|
+
font-variant-numeric: tabular-nums;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
* Reduced motion, deliberately OUTSIDE every layer: unlayered CSS wins over `@layer utilities`
|
|
139
|
+
* whatever the specificity, so this needs no `!important` to switch off a `duration-*` utility. It
|
|
140
|
+
* covers LDS's own hover, focus and press transitions too.
|
|
141
|
+
*/
|
|
142
|
+
@media (prefers-reduced-motion: reduce) {
|
|
143
|
+
[class*="duration-"],
|
|
144
|
+
[class*="lds-"] {
|
|
145
|
+
transition-duration: 0s;
|
|
146
|
+
animation-duration: 0s;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.lds-btn:active {
|
|
150
|
+
transform: none;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's icon sprite holds the app's icons plus what LDS's components draw on their own. A
|
|
3
|
+
* glyph a component names but the sprite lacks renders as nothing, with no error anywhere, so this
|
|
4
|
+
* reads every glyph name the app's source hands an LDS component and checks it is in the sprite.
|
|
5
|
+
*/
|
|
6
|
+
export interface IconReport {
|
|
7
|
+
/** Named in source, in neither the app's icons nor LDS's component icons. */
|
|
8
|
+
unknown: string[];
|
|
9
|
+
/** In the app's icons, named nowhere in source. */
|
|
10
|
+
unused: string[];
|
|
11
|
+
/** In the app's icons, not a glyph `@lew-ds/open-icons` ships. */
|
|
12
|
+
notInOpenIcons: string[];
|
|
13
|
+
}
|
|
14
|
+
export declare function checkIcons(opts: {
|
|
15
|
+
srcDir: string;
|
|
16
|
+
appIcons: readonly string[];
|
|
17
|
+
}): IconReport;
|
package/dist/ui/icons.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { COMPONENT_ICONS } from "@lew-ds/lds";
|
|
4
|
+
import * as lds from "@lew-ds/lds-react";
|
|
5
|
+
import names from "@lew-ds/open-icons/names.json" with { type: "json" };
|
|
6
|
+
/** The components `@lew-ds/lds-react` exports: only their icon props name glyphs. */
|
|
7
|
+
const LDS_COMPONENTS = new Set(Object.keys(lds).filter((name) => /^[A-Z]/.test(name)));
|
|
8
|
+
/** The icon props of an LDS element; `name` counts only on `<Icon>`. */
|
|
9
|
+
const ICON_PROP = /(?<![\w-])(iconStart|iconEnd|icon|name)=/g;
|
|
10
|
+
/** A string that could be a glyph name: lower-case words joined by hyphens. */
|
|
11
|
+
const GLYPH = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
12
|
+
/** A file that imports the design system, where `icon: "…"` in an object is an option's glyph. */
|
|
13
|
+
const IMPORTS_LDS = /from "(?:@wwchen\/stack\/ui|@lew-ds\/lds-react)"/;
|
|
14
|
+
/** The text of a `{…}` attribute value starting at `open`, braces balanced. */
|
|
15
|
+
function braced(text, open) {
|
|
16
|
+
let depth = 0;
|
|
17
|
+
for (let i = open; i < text.length; i++) {
|
|
18
|
+
if (text[i] === "{")
|
|
19
|
+
depth++;
|
|
20
|
+
if (text[i] === "}" && --depth === 0)
|
|
21
|
+
return text.slice(open + 1, i);
|
|
22
|
+
}
|
|
23
|
+
return text.slice(open + 1);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Whether the tag opened at `from` is still open at `to`: no `>` outside braces and quotes between
|
|
27
|
+
* them. A `=>` or `>` inside a `{…}` value or a string does not close it.
|
|
28
|
+
*/
|
|
29
|
+
function tagOpenAt(text, from, to) {
|
|
30
|
+
let depth = 0;
|
|
31
|
+
let quote = "";
|
|
32
|
+
for (let i = from; i < to; i++) {
|
|
33
|
+
const ch = text[i];
|
|
34
|
+
if (quote) {
|
|
35
|
+
if (ch === quote)
|
|
36
|
+
quote = "";
|
|
37
|
+
}
|
|
38
|
+
else if (ch === '"' || ch === "'")
|
|
39
|
+
quote = ch;
|
|
40
|
+
else if (ch === "{")
|
|
41
|
+
depth++;
|
|
42
|
+
else if (ch === "}")
|
|
43
|
+
depth--;
|
|
44
|
+
else if (ch === ">" && depth === 0)
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/** The name of the tag whose opening `<…>` the prop at `at` sits inside, or undefined. */
|
|
50
|
+
function enclosingTag(text, at) {
|
|
51
|
+
const opens = [...text.slice(0, at).matchAll(/<([A-Za-z]\w*)/g)];
|
|
52
|
+
for (const open of opens.reverse()) {
|
|
53
|
+
if (tagOpenAt(text, open.index ?? 0, at))
|
|
54
|
+
return open[1];
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Every glyph name a JSX source hands an LDS component: a literal (`iconStart="edit"`) or any
|
|
60
|
+
* string in an expression (`iconStart={open ? "chevron-up" : "chevron-down"}`), except an
|
|
61
|
+
* attribute value nested inside it (`type="button"` on an element passed as the icon). Also an
|
|
62
|
+
* `icon: "…"` in an object literal (a SegmentedControl option), in a file that imports LDS.
|
|
63
|
+
*/
|
|
64
|
+
function glyphNamesIn(text) {
|
|
65
|
+
const found = [];
|
|
66
|
+
for (const match of text.matchAll(ICON_PROP)) {
|
|
67
|
+
const at = match.index ?? 0;
|
|
68
|
+
const owner = enclosingTag(text, at);
|
|
69
|
+
if (!owner || !LDS_COMPONENTS.has(owner))
|
|
70
|
+
continue;
|
|
71
|
+
if (match[1] === "name" && owner !== "Icon")
|
|
72
|
+
continue;
|
|
73
|
+
const start = at + match[0].length;
|
|
74
|
+
if (text[start] === '"') {
|
|
75
|
+
found.push(text.slice(start + 1, text.indexOf('"', start + 1)));
|
|
76
|
+
}
|
|
77
|
+
else if (text[start] === "{") {
|
|
78
|
+
for (const literal of braced(text, start).matchAll(/(=)?"([^"]*)"/g)) {
|
|
79
|
+
if (!literal[1] && GLYPH.test(literal[2] ?? ""))
|
|
80
|
+
found.push(literal[2]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (IMPORTS_LDS.test(text)) {
|
|
85
|
+
for (const option of text.matchAll(/\bicon:\s*"([^"]+)"/g))
|
|
86
|
+
found.push(option[1]);
|
|
87
|
+
}
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
function tsxFiles(dir) {
|
|
91
|
+
return readdirSync(dir, { recursive: true, encoding: "utf8" })
|
|
92
|
+
.filter((file) => file.endsWith(".tsx"))
|
|
93
|
+
.map((file) => join(dir, file));
|
|
94
|
+
}
|
|
95
|
+
export function checkIcons(opts) {
|
|
96
|
+
const inSprite = new Set([...opts.appIcons, ...COMPONENT_ICONS]);
|
|
97
|
+
const used = new Set(tsxFiles(opts.srcDir).flatMap((f) => glyphNamesIn(readFileSync(f, "utf8"))));
|
|
98
|
+
const shipped = new Set(names);
|
|
99
|
+
return {
|
|
100
|
+
unknown: [...used].filter((name) => !inSprite.has(name)),
|
|
101
|
+
unused: opts.appIcons.filter((name) => !used.has(name)),
|
|
102
|
+
notInOpenIcons: opts.appIcons.filter((name) => !shipped.has(name)),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type ButtonProps, type IconButtonProps, type LinkProps, type RowProps } from "@lew-ds/lds-react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* The design system, as an app imports it: `@lew-ds/lds-react` (server components, but for the
|
|
5
|
+
* five stateful ones, which carry their own `"use client"`), with one thing bound. Every component
|
|
6
|
+
* that can take an `href` (Button, IconButton, Link, Row) renders it through `next/link`, so moving
|
|
7
|
+
* between pages is a client navigation rather than a full reload. LDS takes the router's link as
|
|
8
|
+
* `as`; a call site can still pass its own.
|
|
9
|
+
*
|
|
10
|
+
* Import every LDS component from here, never from the package directly, so no link forgets `as`.
|
|
11
|
+
* This module has no `"use client"`: a server component can import it, and so can a client one.
|
|
12
|
+
*/
|
|
13
|
+
export * from "@lew-ds/lds-react";
|
|
14
|
+
export type { Theme } from "./theme.ts";
|
|
15
|
+
export { htmlClassName, resolveTheme, THEME_COOKIE } from "./theme.ts";
|
|
16
|
+
export declare function Button(props: ButtonProps): import("react").JSX.Element;
|
|
17
|
+
/** IconButton renders the package's own Button inside its Tooltip, so it needs the binding too. */
|
|
18
|
+
export declare function IconButton(props: IconButtonProps): import("react").JSX.Element;
|
|
19
|
+
/**
|
|
20
|
+
* LDS's Link renders an anchor even with no `href` (a placeholder link), which `next/link` refuses:
|
|
21
|
+
* the router's link only stands in when there is somewhere to go.
|
|
22
|
+
*/
|
|
23
|
+
export declare function Link(props: LinkProps): import("react").JSX.Element;
|
|
24
|
+
export declare function Row(props: RowProps): import("react").JSX.Element;
|
|
25
|
+
/**
|
|
26
|
+
* The root of an app's `<body>`: the inline icon sprite, then the page. Masked glyphs draw blank
|
|
27
|
+
* from an external sprite in Chromium, so the sprite is in the document. `icons` is the app's own
|
|
28
|
+
* glyphs (see `checkIcons`); LDS adds the ones its components draw.
|
|
29
|
+
*/
|
|
30
|
+
export declare function StackRoot({ icons, children }: {
|
|
31
|
+
icons: readonly string[];
|
|
32
|
+
children: ReactNode;
|
|
33
|
+
}): import("react").JSX.Element;
|
package/dist/ui/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button as LdsButton, IconButton as LdsIconButton, Link as LdsLink, Row as LdsRow, } from "@lew-ds/lds-react";
|
|
3
|
+
import { IconSprite } from "@lew-ds/lds-react/icon-sprite";
|
|
4
|
+
import NextLink from "next/link.js";
|
|
5
|
+
/**
|
|
6
|
+
* The design system, as an app imports it: `@lew-ds/lds-react` (server components, but for the
|
|
7
|
+
* five stateful ones, which carry their own `"use client"`), with one thing bound. Every component
|
|
8
|
+
* that can take an `href` (Button, IconButton, Link, Row) renders it through `next/link`, so moving
|
|
9
|
+
* between pages is a client navigation rather than a full reload. LDS takes the router's link as
|
|
10
|
+
* `as`; a call site can still pass its own.
|
|
11
|
+
*
|
|
12
|
+
* Import every LDS component from here, never from the package directly, so no link forgets `as`.
|
|
13
|
+
* This module has no `"use client"`: a server component can import it, and so can a client one.
|
|
14
|
+
*/
|
|
15
|
+
export * from "@lew-ds/lds-react";
|
|
16
|
+
export { htmlClassName, resolveTheme, THEME_COOKIE } from "./theme.js";
|
|
17
|
+
export function Button(props) {
|
|
18
|
+
return _jsx(LdsButton, { as: NextLink, ...props });
|
|
19
|
+
}
|
|
20
|
+
/** IconButton renders the package's own Button inside its Tooltip, so it needs the binding too. */
|
|
21
|
+
export function IconButton(props) {
|
|
22
|
+
return _jsx(LdsIconButton, { as: NextLink, ...props });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* LDS's Link renders an anchor even with no `href` (a placeholder link), which `next/link` refuses:
|
|
26
|
+
* the router's link only stands in when there is somewhere to go.
|
|
27
|
+
*/
|
|
28
|
+
export function Link(props) {
|
|
29
|
+
return _jsx(LdsLink, { as: props.href === undefined ? undefined : NextLink, ...props });
|
|
30
|
+
}
|
|
31
|
+
export function Row(props) {
|
|
32
|
+
return _jsx(LdsRow, { as: NextLink, ...props });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The root of an app's `<body>`: the inline icon sprite, then the page. Masked glyphs draw blank
|
|
36
|
+
* from an external sprite in Chromium, so the sprite is in the document. `icons` is the app's own
|
|
37
|
+
* glyphs (see `checkIcons`); LDS adds the ones its components draw.
|
|
38
|
+
*/
|
|
39
|
+
export function StackRoot({ icons, children }) {
|
|
40
|
+
return (_jsxs(_Fragment, { children: [_jsx(IconSprite, { icons: icons }), children] }));
|
|
41
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type Theme = "light" | "dark";
|
|
2
|
+
export declare const THEME_COOKIE = "theme";
|
|
3
|
+
/**
|
|
4
|
+
* The classes LDS needs on `<html>`: the product theme (its ramps, type, radii and shadows) and the
|
|
5
|
+
* mode its dark rules key on (`.mode-dark .lds-tag`). Pure, so a root layout and Storybook write
|
|
6
|
+
* the same thing. `extra` is appended (an app's `next/font` class).
|
|
7
|
+
*/
|
|
8
|
+
export declare function htmlClassName(theme: Theme, extra?: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* The server-side theme read: the `theme` cookie, `light` unless it says `dark`. Dark mode is
|
|
11
|
+
* correct in the FIRST server response, before any client script runs, because the root layout
|
|
12
|
+
* calls this during render and writes the class onto `<html>`.
|
|
13
|
+
*
|
|
14
|
+
* `next/headers.js` is imported on call, not at the top: `@wwchen/stack/ui` also re-exports the
|
|
15
|
+
* design system's components, and a client component importing a Button from it must not pull
|
|
16
|
+
* `next/headers` into the client bundle.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveTheme(): Promise<Theme>;
|
package/dist/ui/theme.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const THEME_COOKIE = "theme";
|
|
2
|
+
/**
|
|
3
|
+
* The classes LDS needs on `<html>`: the product theme (its ramps, type, radii and shadows) and the
|
|
4
|
+
* mode its dark rules key on (`.mode-dark .lds-tag`). Pure, so a root layout and Storybook write
|
|
5
|
+
* the same thing. `extra` is appended (an app's `next/font` class).
|
|
6
|
+
*/
|
|
7
|
+
export function htmlClassName(theme, extra) {
|
|
8
|
+
const base = `theme-product mode-${theme}`;
|
|
9
|
+
return extra ? `${base} ${extra}` : base;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The server-side theme read: the `theme` cookie, `light` unless it says `dark`. Dark mode is
|
|
13
|
+
* correct in the FIRST server response, before any client script runs, because the root layout
|
|
14
|
+
* calls this during render and writes the class onto `<html>`.
|
|
15
|
+
*
|
|
16
|
+
* `next/headers.js` is imported on call, not at the top: `@wwchen/stack/ui` also re-exports the
|
|
17
|
+
* design system's components, and a client component importing a Button from it must not pull
|
|
18
|
+
* `next/headers` into the client bundle.
|
|
19
|
+
*/
|
|
20
|
+
export async function resolveTheme() {
|
|
21
|
+
const { cookies } = await import("next/headers.js");
|
|
22
|
+
const store = await cookies();
|
|
23
|
+
return store.get(THEME_COOKIE)?.value === "dark" ? "dark" : "light";
|
|
24
|
+
}
|