@wwchen/stack 0.3.0 → 0.5.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 +4 -3
- package/blocks/AGENTS.md +4 -1
- package/blocks/CLAUDE.md +3 -0
- package/configs/biome.json +2 -1
- package/dist/cli/index.js +3 -2
- package/dist/cli/neon.d.ts +4 -0
- package/dist/cli/neon.js +23 -0
- package/dist/cli/user-create.js +17 -12
- package/dist/storybook/index.d.ts +98 -0
- package/dist/storybook/index.js +72 -0
- package/package.json +16 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Shared building blocks for Next.js 16 apps on Neon, Better Auth, Resend and lew-ds. Apps are normally created from the Copier template in [wwchen/bootstrap-stack](https://github.com/wwchen/bootstrap-stack), which wires everything below; this README is the package's reference.
|
|
4
4
|
|
|
5
|
-
Requires Node 24. The peer dependencies are exact pins: `next`, `react`, `react-dom`, `drizzle-orm`, `@lew-ds/lds`, `@lew-ds/lds-react`, `@lew-ds/open-icons`. `@electric-sql/pglite` and `@electric-sql/pglite-socket` are optional peers for tests and local development.
|
|
5
|
+
Requires Node 24. The peer dependencies are exact pins: `next`, `react`, `react-dom`, `drizzle-orm`, `@lew-ds/lds`, `@lew-ds/lds-react`, `@lew-ds/open-icons`. `@electric-sql/pglite` and `@electric-sql/pglite-socket` are optional peers for tests and local development, and `storybook` and `@storybook/nextjs-vite` for an app's Storybook.
|
|
6
6
|
|
|
7
7
|
## Entry points
|
|
8
8
|
|
|
@@ -19,6 +19,7 @@ Requires Node 24. The peer dependencies are exact pins: `next`, `react`, `react-
|
|
|
19
19
|
| `@wwchen/stack/connections` | connected accounts (not sign-in): `connectionTable`, `createConnections` (start and callback route handlers, `token`, `list`, `connected`, `disconnect`), the `foursquare` provider, AES-256-GCM token encryption under `TOKEN_ENCRYPTION_KEY` |
|
|
20
20
|
| `@wwchen/stack/jobs` | multi-person scheduled jobs: `failingSince` and `isNewFailure` (alert on a failure's first 24 hours only), `mapLimit` |
|
|
21
21
|
| `@wwchen/stack/ui`, `/ui/icons`, `/ui/globals.css`, `/ui/app.css` | lew-ds wiring, theme cookie, icon sprite and its usage check, and the CSS partials |
|
|
22
|
+
| `@wwchen/stack/storybook` | an app's Storybook set up as its root layout sets up a page: `stackPreview({ icons, fontVariable, bodyClassName })` (the `<html>` theme classes with a light/dark toolbar, the icon sprite, a logging stand-in for the App Router's `useRouter()`, the viewports), and `PHONE` and `SMALL_PHONE` story globals |
|
|
22
23
|
| `@wwchen/stack/configs/tsconfig.next.json`, `/configs/biome.json`, `/configs/vitest` | shared tool configs, consumed with `extends` or an import |
|
|
23
24
|
| `@wwchen/stack/testing/next` | tests only: `inServerAction` runs a function in a real Next server-action scope, `thrownBy` reads a `redirect()`/`notFound()` |
|
|
24
25
|
|
|
@@ -58,8 +59,8 @@ The callback lands on `next` with `?connection=<provider>&result=<code>`, where
|
|
|
58
59
|
| `stack neon reset-preview [--set-vercel]` | re-cuts the shared `preview` branch from `seed`; `--set-vercel` writes its URLs as Vercel's sensitive Preview `DATABASE_URL`/`DATABASE_URL_UNPOOLED` |
|
|
59
60
|
| `stack vercel-build` | Vercel's build command: migrate the deployment's database, then `next build` |
|
|
60
61
|
| `stack doctor` | read-only audit of the linked Vercel project (framework, Neon integration, shared secrets, database variables); exits 1 on an error |
|
|
61
|
-
| `stack user:create <email> [--admin]` | adds a user through the app's `src/lib/auth.ts` |
|
|
62
|
-
| `stack user:role <email> <admin\|user
|
|
62
|
+
| `stack user:create <email> [--admin] [--preview]` | adds a user through the app's `src/lib/auth.ts`; `--preview` adds your own email to the shared `preview` branch so you can sign in on a preview (`docs/runbook.md`, "Signing in on a preview") |
|
|
63
|
+
| `stack user:role <email> <admin\|user> [--preview]` | changes a user's role through the app's `src/lib/auth.ts`; never leaves the app without an admin |
|
|
63
64
|
| `stack sync-blocks [--check]` | refreshes the stack blocks in `AGENTS.md`, `CLAUDE.md`, `.gitignore`, `.env.example` |
|
|
64
65
|
|
|
65
66
|
`NEON_PROJECT_ID` comes from the environment; nothing in the package names a project or domain.
|
package/blocks/AGENTS.md
CHANGED
|
@@ -13,9 +13,11 @@
|
|
|
13
13
|
| Typecheck | `pnpm typecheck` |
|
|
14
14
|
| Lint / format | `pnpm lint` · `pnpm lint:fix` |
|
|
15
15
|
| Tests | `pnpm test` · one file: `pnpm test tests/unit/<name>.test.ts` |
|
|
16
|
+
| Storybook | `pnpm storybook` (http://localhost:6006) · every story built, as CI does: `pnpm storybook:build` |
|
|
16
17
|
| Migrate | `pnpm db:migrate` (your dev branch) |
|
|
17
18
|
| Rebuild the `seed` branch (fake data; every dev and preview branch is cut from it) | `pnpm exec stack neon seed` |
|
|
18
19
|
| Add a user | `stack user:create <email> [--admin]` (on your dev branch; production: the runbook's "First admin") |
|
|
20
|
+
| Sign in on a preview | `stack user:create <your email> --admin --preview` (your own email only; `stack neon reset-preview` drops it) |
|
|
19
21
|
| Change a user's role | `stack user:role <email> <admin\|user>` |
|
|
20
22
|
| Check the Vercel project's setup | `stack doctor` (read-only; exits 1 on an error) |
|
|
21
23
|
| Refresh the stack blocks in this file | `stack sync-blocks` (`--check` in CI) |
|
|
@@ -44,7 +46,8 @@ The design system is LDS (Lew Design System), product theme: `@lew-ds/lds`, `@le
|
|
|
44
46
|
- Never hand-roll a pill, button, input, table or banner. If LDS lacks something, fix it upstream in LDS, never at a call site.
|
|
45
47
|
- LDS paints through its own class names; the app never restyles them. The app's own `app.css` is the only place it fills a gap.
|
|
46
48
|
- Tailwind is for layout only. Its colour, type, radius and shadow utilities resolve to LDS roles through the stack's `globals.css`. No raw hex or px in a component.
|
|
47
|
-
- A design change lands in the component and its stories in the same change.
|
|
49
|
+
- A design change lands in the component and its stories (narrow and wide) in the same change, and carries to every component that shows the same thing.
|
|
50
|
+
- Stories live beside their component as `*.stories.tsx` and render the real components from fixture data; a story never hand-builds a look. `PHONE` (390px) and `SMALL_PHONE` (320px) from `@wwchen/stack/storybook` open a story narrow, and the toolbar switches light and dark.
|
|
48
51
|
|
|
49
52
|
## Database
|
|
50
53
|
|
package/blocks/CLAUDE.md
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
| Typecheck | `pnpm typecheck` |
|
|
14
14
|
| Lint / format | `pnpm lint` · `pnpm lint:fix` |
|
|
15
15
|
| Tests | `pnpm test` · one file: `pnpm test tests/unit/<name>.test.ts` |
|
|
16
|
+
| Storybook | `pnpm storybook` (http://localhost:6006) · every story built, as CI does: `pnpm storybook:build` |
|
|
16
17
|
| DB generate / apply migration | `pnpm db:generate` · `pnpm db:migrate` |
|
|
17
18
|
| Seed the shared `seed` branch | `stack neon seed` (runs the app's `db/seed.ts`) |
|
|
18
19
|
| Reset the shared preview branch | `stack neon reset-preview` (`--set-vercel` also writes its URLs into Vercel's Preview variables) |
|
|
19
20
|
| Check the Vercel project's setup | `stack doctor` (read-only; exits 1 on an error) |
|
|
20
21
|
| Add a user / an admin | `stack user:create <email> [--admin]` (on your dev branch; production: the runbook's "First admin") |
|
|
22
|
+
| Sign in on a preview | `stack user:create <your email> --admin --preview` (your own email only; `stack neon reset-preview` drops it) |
|
|
21
23
|
| Change a user's role | `stack user:role <email> <admin\|user>` (keeps at least one admin) |
|
|
22
24
|
| Refresh the stack blocks in this file | `stack sync-blocks` (`--check` in CI) |
|
|
23
25
|
|
|
@@ -54,6 +56,7 @@ The design system is LDS (Lew Design System), product theme: `@lew-ds/lds`, `@le
|
|
|
54
56
|
- Layouts respond to the width a component has (container queries), never to the device.
|
|
55
57
|
- Theme is a cookie (`theme-product mode-*`) read and written through the stack's `/ui` helpers.
|
|
56
58
|
- A design change lands in the component and its stories (narrow and wide) in the same change, and carries to every component that shows the same thing.
|
|
59
|
+
- Stories live beside their component as `*.stories.tsx` and render the real components from fixture data; a story never hand-builds a look. `PHONE` (390px) and `SMALL_PHONE` (320px) from `@wwchen/stack/storybook` open a story narrow, and the toolbar switches light and dark.
|
|
57
60
|
|
|
58
61
|
## Database rules
|
|
59
62
|
|
package/configs/biome.json
CHANGED
package/dist/cli/index.js
CHANGED
|
@@ -20,8 +20,9 @@ const USAGE = `usage: stack <command>
|
|
|
20
20
|
--set-vercel writes its URLs into Vercel's Preview variables
|
|
21
21
|
vercel-build Vercel's build command: migrate, then next build
|
|
22
22
|
doctor audit the linked Vercel project's setup (read-only)
|
|
23
|
-
user:create <email> [--admin] add a user through the app's auth
|
|
24
|
-
|
|
23
|
+
user:create <email> [--admin] add a user through the app's auth; --preview: on the shared
|
|
24
|
+
preview branch (your own email only, to sign in on a preview)
|
|
25
|
+
user:role <email> <admin|user> change a user's role through the app's auth; --preview as above
|
|
25
26
|
sync-blocks [--check] refresh the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore, .env.example`;
|
|
26
27
|
/** The testable entry: every outside effect arrives through `deps`; the exit code comes back. */
|
|
27
28
|
export async function runCli(argv, deps) {
|
package/dist/cli/neon.d.ts
CHANGED
|
@@ -44,6 +44,10 @@ export declare function withNeon(cli: Cli, [command, ...args]: string[]): Promis
|
|
|
44
44
|
* DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
45
45
|
*/
|
|
46
46
|
export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
47
|
+
/**
|
|
48
|
+
* The shared `preview` branch's `branchEnv`, for `user:create --preview` and `user:role --preview`: previews hold only `db/seed.ts`'s fake users, so this is how a developer adds their own email to sign in on one. `--preview` names the database, so DATABASE_URL or ENV_FILE set as well is refused rather than overridden, as is a `preview` that is the default branch or answers with production's endpoint.
|
|
49
|
+
*/
|
|
50
|
+
export declare function previewBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
47
51
|
/**
|
|
48
52
|
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
49
53
|
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
package/dist/cli/neon.js
CHANGED
|
@@ -148,6 +148,29 @@ export async function devBranchEnv(cli) {
|
|
|
148
148
|
cli.error(`[neon] ${name}`);
|
|
149
149
|
return env;
|
|
150
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* The shared `preview` branch's `branchEnv`, for `user:create --preview` and `user:role --preview`: previews hold only `db/seed.ts`'s fake users, so this is how a developer adds their own email to sign in on one. `--preview` names the database, so DATABASE_URL or ENV_FILE set as well is refused rather than overridden, as is a `preview` that is the default branch or answers with production's endpoint.
|
|
153
|
+
*/
|
|
154
|
+
export async function previewBranchEnv(cli) {
|
|
155
|
+
if (cli.env.DATABASE_URL || cli.env.ENV_FILE) {
|
|
156
|
+
throw new CliError("--preview names the database itself; unset DATABASE_URL and ENV_FILE.");
|
|
157
|
+
}
|
|
158
|
+
const branches = await listBranches(cli);
|
|
159
|
+
const production = defaultBranch(branches);
|
|
160
|
+
const branch = branches.find((b) => b.name === PREVIEW);
|
|
161
|
+
if (!branch) {
|
|
162
|
+
throw new CliError(`no ${PREVIEW} branch yet; run \`stack neon reset-preview\` first.`);
|
|
163
|
+
}
|
|
164
|
+
const env = await branchEnv(cli, branch.id, production);
|
|
165
|
+
const productionEndpoint = endpointOf(env.PRODUCTION_DATABASE_HOST);
|
|
166
|
+
if (branch.default ||
|
|
167
|
+
endpointOf(env.DATABASE_URL) === productionEndpoint ||
|
|
168
|
+
endpointOf(env.DATABASE_URL_UNPOOLED) === productionEndpoint) {
|
|
169
|
+
throw new CliError(`${PREVIEW} is production's database; refusing to change it.`);
|
|
170
|
+
}
|
|
171
|
+
cli.error(`[neon] ${PREVIEW}`);
|
|
172
|
+
return env;
|
|
173
|
+
}
|
|
151
174
|
/**
|
|
152
175
|
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
153
176
|
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
package/dist/cli/user-create.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { CliError, importApp, loadAppEnv } from "./context.js";
|
|
2
|
-
import { devBranchEnv } from "./neon.js";
|
|
2
|
+
import { devBranchEnv, PREVIEW, previewBranchEnv } from "./neon.js";
|
|
3
|
+
const PREVIEW_FLAG = "--preview";
|
|
3
4
|
/**
|
|
4
|
-
* The app's `auth.users`, with the database every command here uses: the dev branch's connection (when nothing else names a database) wins over `.env`, which fills in only what is still unset. The app's modules read process.env, so this sets it before importing them.
|
|
5
|
+
* The app's `auth.users`, with the database every command here uses: the dev branch's connection (when nothing else names a database), or the `preview` branch's with `--preview`, wins over `.env`, which fills in only what is still unset. The app's modules read process.env, so this sets it before importing them.
|
|
5
6
|
*/
|
|
6
|
-
async function appUsers(cli) {
|
|
7
|
-
Object.assign(cli.env, await devBranchEnv(cli));
|
|
7
|
+
async function appUsers(cli, preview) {
|
|
8
|
+
Object.assign(cli.env, preview ? await previewBranchEnv(cli) : await devBranchEnv(cli));
|
|
8
9
|
loadAppEnv(cli);
|
|
9
10
|
const auth = (await importApp(cli, "src/lib/auth.ts")).auth;
|
|
10
11
|
if (!auth?.users)
|
|
@@ -13,17 +14,20 @@ async function appUsers(cli) {
|
|
|
13
14
|
}
|
|
14
15
|
export async function userCreate(cli, args) {
|
|
15
16
|
const admin = args.includes("--admin");
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const preview = args.includes(PREVIEW_FLAG);
|
|
18
|
+
const [email, ...rest] = args.filter((arg) => arg !== "--admin" && arg !== PREVIEW_FLAG);
|
|
19
|
+
if (!email || rest.length > 0) {
|
|
20
|
+
cli.error("usage: stack user:create <email> [--admin] [--preview]");
|
|
19
21
|
return 2;
|
|
20
22
|
}
|
|
21
|
-
const users = await appUsers(cli);
|
|
23
|
+
const users = await appUsers(cli, preview);
|
|
22
24
|
if (!users.add)
|
|
23
25
|
throw new CliError("src/lib/auth.ts must export `auth` (a StackAuth).");
|
|
24
26
|
try {
|
|
25
27
|
const user = await users.add(email, admin ? "admin" : "user");
|
|
26
|
-
cli.log(
|
|
28
|
+
cli.log(preview
|
|
29
|
+
? `created ${user.email}${admin ? " as an admin" : ""} on ${PREVIEW}: sign in on a preview with a magic link. \`stack neon reset-preview\` drops it.`
|
|
30
|
+
: `created ${user.email}${admin ? " as an admin" : ""}. They sign in with a magic link or Google.`);
|
|
27
31
|
return 0;
|
|
28
32
|
}
|
|
29
33
|
catch (error) {
|
|
@@ -39,12 +43,13 @@ const isRole = (value) => ROLES.includes(value);
|
|
|
39
43
|
* `stack user:role <email> <admin|user>`. The email is matched as it is stored (trimmed, lower-cased). The rule that the app always keeps an admin is `setRole`'s, enforced in its own transaction: its refusal is reported as is, and nothing changes.
|
|
40
44
|
*/
|
|
41
45
|
export async function userRole(cli, args) {
|
|
42
|
-
const
|
|
46
|
+
const preview = args.includes(PREVIEW_FLAG);
|
|
47
|
+
const [email, role, ...rest] = args.filter((arg) => arg !== PREVIEW_FLAG);
|
|
43
48
|
if (!email || !isRole(role) || rest.length > 0) {
|
|
44
|
-
cli.error("usage: stack user:role <email> <admin|user>");
|
|
49
|
+
cli.error("usage: stack user:role <email> <admin|user> [--preview]");
|
|
45
50
|
return 2;
|
|
46
51
|
}
|
|
47
|
-
const users = await appUsers(cli);
|
|
52
|
+
const users = await appUsers(cli, preview);
|
|
48
53
|
if (!users.list || !users.setRole) {
|
|
49
54
|
throw new CliError("src/lib/auth.ts must export `auth` (a StackAuth).");
|
|
50
55
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Preview } from "@storybook/nextjs-vite";
|
|
2
|
+
import { type AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime.js";
|
|
3
|
+
import { type Theme } from "../ui/theme.ts";
|
|
4
|
+
/**
|
|
5
|
+
* An app's Storybook, set up the way its root layout sets up a page: the same classes on `<html>` (theme, mode and font), the same inline icon sprite, and a router for `useRouter()`. An app's `.storybook/preview.tsx` imports its own stylesheet and font and spreads `stackPreview(...)`.
|
|
6
|
+
*
|
|
7
|
+
* Storybook's packages are optional peers: only an app's Storybook imports this module.
|
|
8
|
+
*/
|
|
9
|
+
export type StackPreviewOptions = {
|
|
10
|
+
/** The app's `APP_ICONS`: the glyphs its components name, inlined as the root layout does. */
|
|
11
|
+
icons: readonly string[];
|
|
12
|
+
/** The `next/font` class the root layout passes to `htmlClassName` (`inter.variable`). */
|
|
13
|
+
fontVariable?: string;
|
|
14
|
+
/** The classes the root layout puts on `<body>`, added to Storybook's own `<body>` beside its layout classes. */
|
|
15
|
+
bodyClassName?: string;
|
|
16
|
+
};
|
|
17
|
+
/** The toolbar's light/dark switch, as a story global. Anything but `"dark"` is light. */
|
|
18
|
+
export declare function storyTheme(value: unknown): Theme;
|
|
19
|
+
/**
|
|
20
|
+
* Writes what the root layout writes on the server. Storybook has no request, so the theme comes from the toolbar instead of the cookie, and the classes are set on the live document. `<html>` is the stack's alone; `<body>` also carries Storybook's layout classes, so the app's are added beside them.
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyStoryTheme(document: {
|
|
23
|
+
documentElement: {
|
|
24
|
+
className: string;
|
|
25
|
+
};
|
|
26
|
+
body: {
|
|
27
|
+
classList: {
|
|
28
|
+
add(...tokens: string[]): void;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}, theme: Theme, options?: Pick<StackPreviewOptions, "fontVariable" | "bodyClassName">): void;
|
|
32
|
+
/**
|
|
33
|
+
* The router `useRouter()` reads in a story. Storybook's own app-router mode does not render under Next 16.4 (its RedirectBoundary reads a context it never sets), so stories get Next's context directly. A navigation or refresh is logged to the browser console instead of happening.
|
|
34
|
+
*/
|
|
35
|
+
export declare const storyRouter: AppRouterInstance;
|
|
36
|
+
/**
|
|
37
|
+
* The viewport toolbar: a current phone (390 × 844, the width phone layouts are checked at), the 320px floor every page must fit with no horizontal scroll, then Storybook's minimal set. Storybook's own 320px entry is the stack's `smallPhone`, so it is listed once, under a name the stack owns.
|
|
38
|
+
*/
|
|
39
|
+
export declare const STORY_VIEWPORTS: {
|
|
40
|
+
readonly mobile2: {
|
|
41
|
+
readonly name: "Large mobile";
|
|
42
|
+
readonly styles: {
|
|
43
|
+
readonly height: "896px";
|
|
44
|
+
readonly width: "414px";
|
|
45
|
+
};
|
|
46
|
+
readonly type: "mobile";
|
|
47
|
+
};
|
|
48
|
+
readonly tablet: {
|
|
49
|
+
readonly name: "Tablet";
|
|
50
|
+
readonly styles: {
|
|
51
|
+
readonly height: "1112px";
|
|
52
|
+
readonly width: "834px";
|
|
53
|
+
};
|
|
54
|
+
readonly type: "tablet";
|
|
55
|
+
};
|
|
56
|
+
readonly desktop: {
|
|
57
|
+
readonly name: "Desktop";
|
|
58
|
+
readonly styles: {
|
|
59
|
+
readonly height: "1024px";
|
|
60
|
+
readonly width: "1280px";
|
|
61
|
+
};
|
|
62
|
+
readonly type: "desktop";
|
|
63
|
+
};
|
|
64
|
+
readonly phone: {
|
|
65
|
+
readonly name: "Phone";
|
|
66
|
+
readonly styles: {
|
|
67
|
+
readonly width: "390px";
|
|
68
|
+
readonly height: "844px";
|
|
69
|
+
};
|
|
70
|
+
readonly type: "mobile";
|
|
71
|
+
};
|
|
72
|
+
readonly smallPhone: {
|
|
73
|
+
readonly name: "Small phone";
|
|
74
|
+
readonly styles: {
|
|
75
|
+
readonly height: "568px";
|
|
76
|
+
readonly width: "320px";
|
|
77
|
+
};
|
|
78
|
+
readonly type: "mobile";
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/** A story's `globals` that open it at a phone's width: `export const Narrow = { globals: PHONE }`. */
|
|
82
|
+
export declare const PHONE: {
|
|
83
|
+
readonly viewport: {
|
|
84
|
+
readonly value: "phone";
|
|
85
|
+
readonly isRotated: false;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/** A story's `globals` that open it at the 320px floor: `export const Narrowest = { globals: SMALL_PHONE }`. */
|
|
89
|
+
export declare const SMALL_PHONE: {
|
|
90
|
+
readonly viewport: {
|
|
91
|
+
readonly value: "smallPhone";
|
|
92
|
+
readonly isRotated: false;
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The stack's part of an app's `.storybook/preview.tsx`: the theme toolbar (light and dark), the viewports, and a decorator that renders every story inside the root layout's set-up. Spread it, then add the app's own parameters or decorators.
|
|
97
|
+
*/
|
|
98
|
+
export declare function stackPreview(options: StackPreviewOptions): Preview;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { AppRouterContext, } from "next/dist/shared/lib/app-router-context.shared-runtime.js";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
import { MINIMAL_VIEWPORTS } from "storybook/viewport";
|
|
5
|
+
import { StackRoot } from "../ui/index.js";
|
|
6
|
+
import { htmlClassName } from "../ui/theme.js";
|
|
7
|
+
/** The toolbar's light/dark switch, as a story global. Anything but `"dark"` is light. */
|
|
8
|
+
export function storyTheme(value) {
|
|
9
|
+
return value === "dark" ? "dark" : "light";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Writes what the root layout writes on the server. Storybook has no request, so the theme comes from the toolbar instead of the cookie, and the classes are set on the live document. `<html>` is the stack's alone; `<body>` also carries Storybook's layout classes, so the app's are added beside them.
|
|
13
|
+
*/
|
|
14
|
+
export function applyStoryTheme(document, theme, options = {}) {
|
|
15
|
+
document.documentElement.className = htmlClassName(theme, options.fontVariable);
|
|
16
|
+
const body = options.bodyClassName?.split(/\s+/).filter(Boolean) ?? [];
|
|
17
|
+
if (body.length > 0)
|
|
18
|
+
document.body.classList.add(...body);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The router `useRouter()` reads in a story. Storybook's own app-router mode does not render under Next 16.4 (its RedirectBoundary reads a context it never sets), so stories get Next's context directly. A navigation or refresh is logged to the browser console instead of happening.
|
|
22
|
+
*/
|
|
23
|
+
export const storyRouter = {
|
|
24
|
+
refresh: () => console.info("[storybook] router.refresh()"),
|
|
25
|
+
push: (href) => console.info("[storybook] router.push", href),
|
|
26
|
+
replace: (href) => console.info("[storybook] router.replace", href),
|
|
27
|
+
back: () => console.info("[storybook] router.back()"),
|
|
28
|
+
forward: () => console.info("[storybook] router.forward()"),
|
|
29
|
+
prefetch: () => { },
|
|
30
|
+
bfcacheId: "story",
|
|
31
|
+
};
|
|
32
|
+
const { mobile1, ...minimalViewports } = MINIMAL_VIEWPORTS;
|
|
33
|
+
/**
|
|
34
|
+
* The viewport toolbar: a current phone (390 × 844, the width phone layouts are checked at), the 320px floor every page must fit with no horizontal scroll, then Storybook's minimal set. Storybook's own 320px entry is the stack's `smallPhone`, so it is listed once, under a name the stack owns.
|
|
35
|
+
*/
|
|
36
|
+
export const STORY_VIEWPORTS = {
|
|
37
|
+
phone: { name: "Phone", styles: { width: "390px", height: "844px" }, type: "mobile" },
|
|
38
|
+
smallPhone: { name: "Small phone", styles: mobile1.styles, type: "mobile" },
|
|
39
|
+
...minimalViewports,
|
|
40
|
+
};
|
|
41
|
+
/** A story's `globals` that open it at a phone's width: `export const Narrow = { globals: PHONE }`. */
|
|
42
|
+
export const PHONE = { viewport: { value: "phone", isRotated: false } };
|
|
43
|
+
/** A story's `globals` that open it at the 320px floor: `export const Narrowest = { globals: SMALL_PHONE }`. */
|
|
44
|
+
export const SMALL_PHONE = { viewport: { value: "smallPhone", isRotated: false } };
|
|
45
|
+
function StoryRoot({ theme, options, children, }) {
|
|
46
|
+
const { fontVariable, bodyClassName } = options;
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
applyStoryTheme(document, theme, { fontVariable, bodyClassName });
|
|
49
|
+
}, [theme, fontVariable, bodyClassName]);
|
|
50
|
+
return (_jsx(AppRouterContext.Provider, { value: storyRouter, children: _jsx(StackRoot, { icons: options.icons, children: children }) }));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The stack's part of an app's `.storybook/preview.tsx`: the theme toolbar (light and dark), the viewports, and a decorator that renders every story inside the root layout's set-up. Spread it, then add the app's own parameters or decorators.
|
|
54
|
+
*/
|
|
55
|
+
export function stackPreview(options) {
|
|
56
|
+
return {
|
|
57
|
+
parameters: {
|
|
58
|
+
layout: "padded",
|
|
59
|
+
viewport: { options: STORY_VIEWPORTS },
|
|
60
|
+
},
|
|
61
|
+
globalTypes: {
|
|
62
|
+
theme: {
|
|
63
|
+
description: "Light or dark",
|
|
64
|
+
toolbar: { title: "Theme", icon: "mirror", items: ["light", "dark"], dynamicTitle: true },
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
initialGlobals: { theme: "light" },
|
|
68
|
+
decorators: [
|
|
69
|
+
(Story, context) => (_jsx(StoryRoot, { theme: storyTheme(context.globals.theme), options: options, children: _jsx(Story, {}) })),
|
|
70
|
+
],
|
|
71
|
+
};
|
|
72
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwchen/stack",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Shared Next.js, Neon, Better Auth, Resend and lew-ds building blocks.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -79,6 +79,10 @@
|
|
|
79
79
|
},
|
|
80
80
|
"./ui/globals.css": "./dist/ui/globals.css",
|
|
81
81
|
"./ui/app.css": "./dist/ui/app.css",
|
|
82
|
+
"./storybook": {
|
|
83
|
+
"types": "./dist/storybook/index.d.ts",
|
|
84
|
+
"default": "./dist/storybook/index.js"
|
|
85
|
+
},
|
|
82
86
|
"./configs/tsconfig.next.json": "./configs/tsconfig.next.json",
|
|
83
87
|
"./configs/biome.json": "./configs/biome.json",
|
|
84
88
|
"./configs/vitest": {
|
|
@@ -108,10 +112,12 @@
|
|
|
108
112
|
"@lew-ds/lds": "1.2.0",
|
|
109
113
|
"@lew-ds/lds-react": "2.0.0",
|
|
110
114
|
"@lew-ds/open-icons": "1.1.0",
|
|
115
|
+
"@storybook/nextjs-vite": "10.6.2",
|
|
111
116
|
"drizzle-orm": "0.45.4",
|
|
112
117
|
"next": "16.4.6",
|
|
113
118
|
"react": "19.3.1",
|
|
114
|
-
"react-dom": "19.3.1"
|
|
119
|
+
"react-dom": "19.3.1",
|
|
120
|
+
"storybook": "10.6.2"
|
|
115
121
|
},
|
|
116
122
|
"peerDependenciesMeta": {
|
|
117
123
|
"@electric-sql/pglite": {
|
|
@@ -119,16 +125,24 @@
|
|
|
119
125
|
},
|
|
120
126
|
"@electric-sql/pglite-socket": {
|
|
121
127
|
"optional": true
|
|
128
|
+
},
|
|
129
|
+
"@storybook/nextjs-vite": {
|
|
130
|
+
"optional": true
|
|
131
|
+
},
|
|
132
|
+
"storybook": {
|
|
133
|
+
"optional": true
|
|
122
134
|
}
|
|
123
135
|
},
|
|
124
136
|
"devDependencies": {
|
|
125
137
|
"@electric-sql/pglite": "0.5.10",
|
|
126
138
|
"@electric-sql/pglite-socket": "0.2.13",
|
|
139
|
+
"@storybook/nextjs-vite": "10.6.2",
|
|
127
140
|
"@types/node": "^24.19.2",
|
|
128
141
|
"@types/pg": "^8.23.1",
|
|
129
142
|
"@types/react": "19.3.0",
|
|
130
143
|
"@types/react-dom": "19.3.0",
|
|
131
144
|
"drizzle-kit": "0.31.11",
|
|
145
|
+
"storybook": "10.6.2",
|
|
132
146
|
"tailwindcss": "4.3.3",
|
|
133
147
|
"typescript": "~6.0.3",
|
|
134
148
|
"vitest": "5.0.5"
|