@soroush.design/cli 0.2.0-alpha.4 → 0.2.0-alpha.5

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.
Files changed (87) hide show
  1. package/dist/bin.mjs +2 -15
  2. package/dist/index.mjs +1 -1
  3. package/dist/{io-C_8_JerZ.mjs → io-CqI2BTaP.mjs} +81 -12
  4. package/package.json +3 -2
  5. package/templates/react-router/js/base/README.md +67 -0
  6. package/templates/react-router/js/base/_gitignore +4 -0
  7. package/templates/{vike/js/spa/src/pages/+onRenderHtml.jsx → react-router/js/base/index.html} +3 -7
  8. package/templates/react-router/js/base/src/components/RouterLink.jsx +12 -0
  9. package/templates/react-router/js/base/src/data/api.js +76 -0
  10. package/templates/react-router/js/base/src/main.jsx +38 -0
  11. package/templates/react-router/js/base/src/pages/ErrorPage.jsx +14 -0
  12. package/templates/react-router/js/base/src/pages/Layout.jsx +141 -0
  13. package/templates/react-router/js/base/src/pages/index/Counter.jsx +18 -0
  14. package/templates/react-router/js/base/src/pages/index/Greeting.jsx +48 -0
  15. package/templates/react-router/js/base/src/pages/index/Page.jsx +29 -0
  16. package/templates/react-router/js/base/src/pages/index/Page.test.jsx +61 -0
  17. package/templates/react-router/js/base/src/routes.jsx +46 -0
  18. package/templates/react-router/js/base/vite.config.js +13 -0
  19. package/templates/react-router/js/base/vitest.config.js +13 -0
  20. package/templates/react-router/js/sample-data/src/data/items.js +55 -0
  21. package/templates/react-router/js/sample-data/src/pages/item/Page.jsx +49 -0
  22. package/templates/react-router/js/sample-data/src/pages/item/loader.js +14 -0
  23. package/templates/react-router/js/sample-data/src/pages/items/Page.jsx +48 -0
  24. package/templates/react-router/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
  25. package/templates/react-router/js/sample-data/src/pages/items/loader.js +11 -0
  26. package/templates/react-router/js/with/jotai/src/pages/index/Counter.jsx +19 -0
  27. package/templates/react-router/js/with/jotai/src/state/counter.js +4 -0
  28. package/templates/react-router/js/with/jotai/src/state/scheme.js +4 -0
  29. package/templates/react-router/js/with/msw/_env +3 -0
  30. package/templates/react-router/js/with/msw/src/mocks/browser.js +5 -0
  31. package/templates/react-router/js/with/msw/src/mocks/handlers.js +24 -0
  32. package/templates/react-router/js/with/msw/src/mocks/server.js +5 -0
  33. package/templates/react-router/js/with/msw/src/mocks/setupTests.js +7 -0
  34. package/templates/react-router/js/with/msw/src/mocks/start.js +10 -0
  35. package/templates/react-router/js/with/react-hook-form/src/pages/form/Page.jsx +294 -0
  36. package/templates/react-router/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
  37. package/templates/react-router/js/with/redux-toolkit/src/state/store.js +27 -0
  38. package/templates/react-router/js/with/zustand/src/pages/index/Counter.jsx +17 -0
  39. package/templates/react-router/js/with/zustand/src/state/counter.js +14 -0
  40. package/templates/react-router/js/with/zustand/src/state/scheme.js +14 -0
  41. package/templates/react-router/ts/base/README.md +67 -0
  42. package/templates/react-router/ts/base/_gitignore +4 -0
  43. package/templates/react-router/ts/base/index.html +12 -0
  44. package/templates/react-router/ts/base/src/components/RouterLink.tsx +12 -0
  45. package/templates/react-router/ts/base/src/data/api.ts +84 -0
  46. package/templates/react-router/ts/base/src/main.tsx +38 -0
  47. package/templates/react-router/ts/base/src/pages/ErrorPage.tsx +14 -0
  48. package/templates/react-router/ts/base/src/pages/Layout.tsx +141 -0
  49. package/templates/react-router/ts/base/src/pages/index/Counter.tsx +18 -0
  50. package/templates/react-router/ts/base/src/pages/index/Greeting.tsx +49 -0
  51. package/templates/react-router/ts/base/src/pages/index/Page.test.tsx +61 -0
  52. package/templates/react-router/ts/base/src/pages/index/Page.tsx +29 -0
  53. package/templates/react-router/ts/base/src/routes.tsx +47 -0
  54. package/templates/react-router/ts/base/tsconfig.json +18 -0
  55. package/templates/react-router/ts/base/vite.config.ts +13 -0
  56. package/templates/react-router/ts/base/vitest.config.ts +13 -0
  57. package/templates/react-router/ts/sample-data/src/data/items.ts +61 -0
  58. package/templates/react-router/ts/sample-data/src/pages/item/Page.tsx +50 -0
  59. package/templates/react-router/ts/sample-data/src/pages/item/loader.ts +14 -0
  60. package/templates/react-router/ts/sample-data/src/pages/items/Page.tsx +49 -0
  61. package/templates/react-router/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
  62. package/templates/react-router/ts/sample-data/src/pages/items/loader.ts +11 -0
  63. package/templates/react-router/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
  64. package/templates/react-router/ts/with/jotai/src/state/counter.ts +4 -0
  65. package/templates/react-router/ts/with/jotai/src/state/scheme.ts +4 -0
  66. package/templates/react-router/ts/with/msw/_env +3 -0
  67. package/templates/react-router/ts/with/msw/src/mocks/browser.ts +5 -0
  68. package/templates/react-router/ts/with/msw/src/mocks/handlers.ts +24 -0
  69. package/templates/react-router/ts/with/msw/src/mocks/server.ts +5 -0
  70. package/templates/react-router/ts/with/msw/src/mocks/setupTests.ts +7 -0
  71. package/templates/react-router/ts/with/msw/src/mocks/start.ts +10 -0
  72. package/templates/react-router/ts/with/react-hook-form/src/pages/form/Page.tsx +298 -0
  73. package/templates/react-router/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
  74. package/templates/react-router/ts/with/redux-toolkit/src/state/store.ts +29 -0
  75. package/templates/react-router/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
  76. package/templates/react-router/ts/with/zustand/src/state/counter.ts +19 -0
  77. package/templates/react-router/ts/with/zustand/src/state/scheme.ts +19 -0
  78. package/templates/templates.md +2 -1
  79. package/templates/vike/js/base/README.md +0 -4
  80. package/templates/vike/ts/base/README.md +0 -4
  81. package/templates/vike/js/spa/src/pages/+config.js +0 -12
  82. package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +0 -25
  83. package/templates/vike/ts/spa/src/pages/+config.ts +0 -14
  84. package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +0 -27
  85. package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +0 -17
  86. /package/templates/{vike/js/spa/src/pages/about/+Page.jsx → react-router/js/base/src/pages/about/Page.jsx} +0 -0
  87. /package/templates/{vike/ts/spa/src/pages/about/+Page.tsx → react-router/ts/base/src/pages/about/Page.tsx} +0 -0
package/dist/bin.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { n as main, r as version$1, t as createNodeIo } from "./io-C_8_JerZ.mjs";
2
+ import { n as main, t as createNodeIo } from "./io-CqI2BTaP.mjs";
3
3
  // ../design-system/package.json
4
4
  var name = "@soroush.tech/design-system";
5
5
  var version = "2.2.0";
@@ -7,23 +7,10 @@ var peerDependencies = {
7
7
  "react": "^18.0.0 || ^19.0.0",
8
8
  "react-dom": "^18.0.0 || ^19.0.0"
9
9
  };
10
- // src/designSystemVersion.ts
11
- /**
12
- * The design system version the cli run on its own installs: the one it was built next to, read
13
- * from the workspace at build time. That is the target version, which a prerelease cli is
14
- * published ahead of, so a prerelease cli asks for the design system prerelease of its own
15
- * channel instead: `2.3.0-alpha.0` from a `0.2.0-alpha.N` cli, which `^` takes to the newest
16
- * alpha, or to `2.3.0` once it is out.
17
- */
18
- function getDesignSystemVersion(designSystemVersion, cliVersion) {
19
- const channel = /-([a-z]+)\.\d+$/.exec(cliVersion)?.[1];
20
- if (!channel || designSystemVersion.includes("-")) return designSystemVersion;
21
- return `${designSystemVersion}-${channel}.0`;
22
- }
23
10
  // src/bin.ts
24
11
  process.exitCode = await main(process.argv.slice(2), createNodeIo(), {
25
12
  name,
26
- version: getDesignSystemVersion(version, version$1),
13
+ version,
27
14
  peerDependencies
28
15
  });
29
16
  export {};
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export { t as createNodeIo, n as default, i as readDesignSystemPackage } from "./io-C_8_JerZ.mjs";
1
+ export { t as createNodeIo, n as default, r as readDesignSystemPackage } from "./io-CqI2BTaP.mjs";
@@ -1121,8 +1121,6 @@ function createTemplateFiles(framework, facts, root = findTemplatesRoot()) {
1121
1121
  }
1122
1122
  return files;
1123
1123
  }
1124
- // package.json
1125
- var version = "0.2.0-alpha.4";
1126
1124
  // src/create/versions.ts
1127
1125
  /** The versions the starters install: the ones this package is tested with. */
1128
1126
  const VERSIONS = {
@@ -1150,6 +1148,7 @@ const VERSIONS = {
1150
1148
  "react-dom": "^19.3.1",
1151
1149
  "react-hook-form": "^7.92.0",
1152
1150
  "react-redux": "^9.3.1",
1151
+ "react-router": "^8.5.1",
1153
1152
  "swr": "^2.5.1",
1154
1153
  "ts-blank-space": "^0.9.0",
1155
1154
  "tsdown": "^0.23.7",
@@ -1195,7 +1194,7 @@ const TEST_PACKAGES = [
1195
1194
  ];
1196
1195
  // src/create/astro/astro.ts
1197
1196
  const TYPESCRIPT_RANGE = VERSIONS["typescript-astro"].replace("npm:typescript@", "");
1198
- function createPackageJson$1({ packageName, isTypeScript, rendering, libraries, version }) {
1197
+ function createPackageJson$2({ packageName, isTypeScript, rendering, libraries, version }) {
1199
1198
  const isServer = rendering === "ssr";
1200
1199
  const { dependencies: libraryPackages, devDependencies: libraryDevPackages } = getLibraryPackages(libraries);
1201
1200
  const packageJson = {
@@ -1238,13 +1237,13 @@ function createPackageJson$1({ packageName, isTypeScript, rendering, libraries,
1238
1237
  return `${JSON.stringify(packageJson, null, 2)}\n`;
1239
1238
  }
1240
1239
  /** The files of the Astro app `facts` describe. */
1241
- function createFiles$1(facts) {
1240
+ function createFiles$2(facts) {
1242
1241
  const { isTypeScript, scheme } = facts;
1243
1242
  const x = isTypeScript ? "ts" : "js";
1244
1243
  return [
1245
1244
  {
1246
1245
  path: "package.json",
1247
- content: createPackageJson$1(facts)
1246
+ content: createPackageJson$2(facts)
1248
1247
  },
1249
1248
  {
1250
1249
  path: `src/theme/themes.${x}`,
@@ -1270,11 +1269,79 @@ const astroStarter = {
1270
1269
  "ssg"
1271
1270
  ],
1272
1271
  hasLibraries: true,
1272
+ createFiles: createFiles$2
1273
+ };
1274
+ // src/create/react-router/react-router.ts
1275
+ function createPackageJson$1({ packageName, isTypeScript, libraries, version }) {
1276
+ const { dependencies: libraryPackages, devDependencies: libraryDevPackages } = getLibraryPackages(libraries);
1277
+ const packageJson = {
1278
+ name: packageName,
1279
+ private: true,
1280
+ version: "0.0.0",
1281
+ type: "module",
1282
+ scripts: {
1283
+ dev: "vite",
1284
+ build: "vite build",
1285
+ preview: "vite preview",
1286
+ ...isTypeScript ? { typecheck: "tsc --noEmit" } : {},
1287
+ test: "vitest run"
1288
+ },
1289
+ dependencies: {
1290
+ "@soroush.tech/design-system": toRange(version),
1291
+ ...pickVersions([
1292
+ ...libraryPackages,
1293
+ "react",
1294
+ "react-dom",
1295
+ "react-router"
1296
+ ])
1297
+ },
1298
+ devDependencies: pickVersions([
1299
+ "@vitejs/plugin-react",
1300
+ ...libraryDevPackages,
1301
+ ...TEST_PACKAGES,
1302
+ "vite",
1303
+ ...isTypeScript ? [
1304
+ "@types/react",
1305
+ "@types/react-dom",
1306
+ "typescript"
1307
+ ] : []
1308
+ ])
1309
+ };
1310
+ return `${JSON.stringify(packageJson, null, 2)}\n`;
1311
+ }
1312
+ /** The files of the React Router app `facts` describe. */
1313
+ function createFiles$1(facts) {
1314
+ const { isTypeScript, scheme } = facts;
1315
+ const x = isTypeScript ? "ts" : "js";
1316
+ return [
1317
+ {
1318
+ path: "package.json",
1319
+ content: createPackageJson$1(facts)
1320
+ },
1321
+ {
1322
+ path: `src/theme/themes.${x}`,
1323
+ content: createThemesSource({ isTypeScript })
1324
+ },
1325
+ {
1326
+ path: `src/theme/Providers.${x}`,
1327
+ content: createProvidersSource({
1328
+ isTypeScript,
1329
+ scheme,
1330
+ themesImport: "./themes"
1331
+ })
1332
+ },
1333
+ ...createTemplateFiles("react-router", facts)
1334
+ ];
1335
+ }
1336
+ const reactRouterStarter = {
1337
+ title: "React Router",
1338
+ label: "React Router: a single-page app, rendered in the browser",
1339
+ renderings: ["spa"],
1340
+ hasLibraries: true,
1273
1341
  createFiles: createFiles$1
1274
1342
  };
1275
1343
  // src/create/vike/vike.ts
1276
1344
  function createPackageJson({ packageName, isTypeScript, rendering, libraries, version }) {
1277
- const renderer = rendering === "spa" ? [] : ["@emotion/server"];
1278
1345
  const { dependencies: libraryPackages, devDependencies: libraryDevPackages } = getLibraryPackages(libraries);
1279
1346
  const packageJson = {
1280
1347
  name: packageName,
@@ -1292,7 +1359,7 @@ function createPackageJson({ packageName, isTypeScript, rendering, libraries, ve
1292
1359
  dependencies: {
1293
1360
  "@soroush.tech/design-system": toRange(version),
1294
1361
  ...pickVersions([
1295
- ...renderer,
1362
+ "@emotion/server",
1296
1363
  ...libraryPackages,
1297
1364
  "react",
1298
1365
  "react-dom",
@@ -1343,7 +1410,6 @@ const vikeStarter = {
1343
1410
  renderings: [
1344
1411
  "isomorphic",
1345
1412
  "ssr",
1346
- "spa",
1347
1413
  "ssg"
1348
1414
  ],
1349
1415
  hasLibraries: true,
@@ -1360,7 +1426,8 @@ const RENDERINGS = [
1360
1426
  /** The starters, by the name `--framework` takes. The first is the default. */
1361
1427
  const STARTERS = {
1362
1428
  vike: vikeStarter,
1363
- astro: astroStarter
1429
+ astro: astroStarter,
1430
+ "react-router": reactRouterStarter
1364
1431
  };
1365
1432
  // src/create/workspace.ts
1366
1433
  /** The layouts, in the order create offers them. The first is the default. */
@@ -1694,7 +1761,8 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
1694
1761
  const offered = RENDERINGS.filter((rendering) => pool.some((framework) => starters[framework].renderings.includes(rendering)));
1695
1762
  if (options.rendering !== void 0 && !offered.includes(options.rendering)) {
1696
1763
  const titles = pool.map((framework) => starters[framework].title).join(" or ");
1697
- io.write(`${titles} does not render ${options.rendering}: it renders ${offered.join(" or ")}.\n`);
1764
+ const pointer = frameworks.filter((framework) => starters[framework].renderings.includes(options.rendering)).map((framework) => ` --framework ${framework} does.`).join("");
1765
+ io.write(`${titles} does not render ${options.rendering}: it renders ${offered.join(" or ")}.${pointer}\n`);
1698
1766
  return 1;
1699
1767
  }
1700
1768
  const rendering = options.rendering ?? (canAsk && offered.length > 1 ? await askRendering(io, offered) : offered[0]);
@@ -1840,7 +1908,8 @@ Options:
1840
1908
  when not given
1841
1909
  --layout <name> workspace (the default): the app in apps/web, with room for more
1842
1910
  apps and services; or standalone: the app alone
1843
- --render <name> how pages render: isomorphic (the default), ssr, spa or ssg
1911
+ --render <name> how pages render: isomorphic (the default), ssr, ssg, or spa, which
1912
+ React Router writes
1844
1913
  --with <names> the libraries to add, by comma: ${LIBRARY_NAMES.join(", ")}
1845
1914
  --scheme <name> the default color scheme: light (the default) or dark
1846
1915
  --sample-data add a list of sample items, and a page for each
@@ -2110,4 +2179,4 @@ function createNodeIo({ stdin = process.stdin, stdout = process.stdout, platform
2110
2179
  }
2111
2180
  };
2112
2181
  }
2113
- export { readDesignSystemPackage as i, main as n, version as r, createNodeIo as t };
2182
+ export { main as n, readDesignSystemPackage as r, createNodeIo as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soroush.design/cli",
3
- "version": "0.2.0-alpha.4",
3
+ "version": "0.2.0-alpha.5",
4
4
  "description": "The command line of the @soroush.tech design system: init sets it up in an existing React app, and create starts a new one, each showing every change first.",
5
5
  "keywords": [
6
6
  "cli",
@@ -63,6 +63,7 @@
63
63
  "react-dom": "^19.3.1",
64
64
  "react-hook-form": "^7.92.0",
65
65
  "react-redux": "^9.3.1",
66
+ "react-router": "^8.5.1",
66
67
  "swr": "^2.5.1",
67
68
  "ts-blank-space": "^0.9.0",
68
69
  "tsdown": "^0.23.7",
@@ -81,7 +82,7 @@
81
82
  "typecheck": "tsc --noEmit",
82
83
  "test": "vitest run",
83
84
  "test:coverage": "vitest run --coverage",
84
- "test:types": "pnpm --filter @soroush.tech/design-system build && tsc -p type-tests/init/tsconfig.json && tsc -p type-tests/tsconfig.create-vike.json && tsc -p type-tests/tsconfig.create-vike-spa.json && tsc -p type-tests/tsconfig.create-vike-ssg.json && tsc -p type-tests/tsconfig.create-vike-ssr.json && tsc -p type-tests/tsconfig.create-vike-libraries.json && tsc -p type-tests/tsconfig.create-vike-jotai.json && tsc -p type-tests/tsconfig.create-vike-more.json && tsc -p type-tests/tsconfig.create-astro.json && tsc -p type-tests/tsconfig.create-astro-ssg.json && tsc -p type-tests/tsconfig.create-astro-ssr.json && tsc -p type-tests/tsconfig.create-astro-libraries.json && tsc -p type-tests/tsconfig.create-astro-more.json",
85
+ "test:types": "pnpm --filter @soroush.tech/design-system build && tsc -p type-tests/init/tsconfig.json && tsc -p type-tests/tsconfig.create-vike.json && tsc -p type-tests/tsconfig.create-vike-ssg.json && tsc -p type-tests/tsconfig.create-vike-ssr.json && tsc -p type-tests/tsconfig.create-vike-libraries.json && tsc -p type-tests/tsconfig.create-vike-jotai.json && tsc -p type-tests/tsconfig.create-vike-more.json && tsc -p type-tests/tsconfig.create-astro.json && tsc -p type-tests/tsconfig.create-astro-ssg.json && tsc -p type-tests/tsconfig.create-astro-ssr.json && tsc -p type-tests/tsconfig.create-astro-libraries.json && tsc -p type-tests/tsconfig.create-astro-more.json && tsc -p type-tests/tsconfig.create-react-router.json && tsc -p type-tests/tsconfig.create-react-router-libraries.json && tsc -p type-tests/tsconfig.create-react-router-more.json && tsc -p type-tests/tsconfig.create-react-router-jotai.json",
85
86
  "gen:templates": "node scripts/gen-templates.mjs"
86
87
  },
87
88
  "module": "./dist/index.mjs"
@@ -0,0 +1,67 @@
1
+ # __TITLE__
2
+
3
+ A single-page app on [React Router](https://reactrouter.com) and the
4
+ [@soroush.tech design system](https://docs.soroush.tech): the browser renders each page, and moves
5
+ between them with no reload.
6
+
7
+ - `npm run dev` starts it, `npm run build` builds it, and `npm run preview` serves the build.
8
+ - `dist/` is static files: any host serves it, once it answers every path with `index.html`, so the
9
+ router shows the page a URL names.
10
+ - `src/routes.jsx` lists the pages by path, and `src/main.jsx` renders the router in `index.html`.
11
+ - `src/pages/` holds the pages, and `src/pages/Layout.jsx` is the frame every page renders in,
12
+ inside the app's providers. `src/components/RouterLink.jsx` is the design system's `Link`, moving
13
+ to a page with no reload.
14
+ - `src/theme/themes.js` holds the light and the dark theme: make them your own.
15
+ <!-- @if sample-data && (tanstack-query || swr) -->
16
+ - `src/pages/items/` lists the sample items of `src/data/items.js`, and `src/pages/item/` shows one: both read them through `src/data/api.js`.
17
+ <!-- @elif sample-data -->
18
+ - `src/pages/items/` lists the sample items of `src/data/items.js`, and `src/pages/item/` shows one: their loaders read the data before the router shows the page. Make them fetch your own.
19
+ <!-- @endif -->
20
+ <!-- @if !zustand && !jotai && !redux-toolkit -->
21
+ - `src/pages/index/Counter.jsx` counts the presses of its button, in its own state.
22
+ <!-- @endif -->
23
+ <!-- @if tanstack-query && react-hook-form && sample-data -->
24
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` and the items pages read `src/data/api.js` with queries, and the form sends its values with a mutation.
25
+ <!-- @elif tanstack-query && react-hook-form -->
26
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` reads `src/data/api.js` with a query, and the form sends its values with a mutation.
27
+ <!-- @elif tanstack-query && sample-data -->
28
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` and the items pages read `src/data/api.js` with queries.
29
+ <!-- @elif tanstack-query -->
30
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` reads `src/data/api.js` with a query.
31
+ <!-- @endif -->
32
+ <!-- @if swr && react-hook-form && sample-data -->
33
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` and the items pages read `src/data/api.js` with SWR, and the form sends its values with a mutation.
34
+ <!-- @elif swr && react-hook-form -->
35
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.js` with SWR, and the form sends its values with a mutation.
36
+ <!-- @elif swr && sample-data -->
37
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` and the items pages read `src/data/api.js` with SWR.
38
+ <!-- @elif swr -->
39
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.js` with SWR.
40
+ <!-- @endif -->
41
+ <!-- @if msw && react-hook-form && sample-data && (tanstack-query || swr) -->
42
+ - [msw](https://mswjs.io): `src/mocks/handlers.js` is a mock API, which answers the greeting, the form, the sample items in the browser while `VITE_MSW` in `.env` is true.
43
+ <!-- @elif msw && react-hook-form -->
44
+ - [msw](https://mswjs.io): `src/mocks/handlers.js` is a mock API, which answers the greeting, the form in the browser while `VITE_MSW` in `.env` is true.
45
+ <!-- @elif msw && sample-data && (tanstack-query || swr) -->
46
+ - [msw](https://mswjs.io): `src/mocks/handlers.js` is a mock API, which answers the greeting, the sample items in the browser while `VITE_MSW` in `.env` is true.
47
+ <!-- @elif msw -->
48
+ - [msw](https://mswjs.io): `src/mocks/handlers.js` is a mock API, which answers the greeting in the browser while `VITE_MSW` in `.env` is true.
49
+ <!-- @endif -->
50
+ <!-- @if zustand -->
51
+ - [zustand](https://zustand.docs.pmnd.rs): `src/state/scheme.js` holds the color scheme and `src/state/counter.js` the home page's count, in stores.
52
+ <!-- @elif jotai -->
53
+ - [Jotai](https://jotai.org): `src/state/scheme.js` holds the color scheme and `src/state/counter.js` the home page's count, in atoms.
54
+ <!-- @elif redux-toolkit -->
55
+ - [Redux Toolkit](https://redux-toolkit.js.org): `src/state/store.js` holds the color scheme and the home page's count, in slices of the store `src/main.jsx` provides.
56
+ <!-- @endif -->
57
+ <!-- @if react-hook-form -->
58
+ - [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): `/form` is a form whose values a Zod schema checks.
59
+ <!-- @endif -->
60
+ <!-- @if msw -->
61
+ - [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.jsx`, which checks the title, the counter and the greeting, which msw's Node server, `src/mocks/server.js`, answers.
62
+ <!-- @else -->
63
+ - [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.jsx`, which checks the title and the counter.
64
+ <!-- @endif -->
65
+
66
+ Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
67
+ https://docs.soroush.tech/design-system/getting-started/installation/#fonts
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ dist/
3
+ *.log
4
+ .DS_Store
@@ -1,8 +1,4 @@
1
- import { escapeInject } from 'vike/server'
2
-
3
- /** The page's HTML shell, from the build: the browser renders the page in #root. */
4
- export const onRenderHtml = () => {
5
- return escapeInject`<!doctype html>
1
+ <!doctype html>
6
2
  <html lang="en">
7
3
  <head>
8
4
  <meta charset="UTF-8" />
@@ -11,6 +7,6 @@ export const onRenderHtml = () => {
11
7
  </head>
12
8
  <body>
13
9
  <div id="root"></div>
10
+ <script type="module" src="/src/main.jsx"></script>
14
11
  </body>
15
- </html>`
16
- }
12
+ </html>
@@ -0,0 +1,12 @@
1
+ import { Link } from '@soroush.tech/design-system/Link'
2
+ import { useHref, useLinkClickHandler } from 'react-router'
3
+
4
+ /**
5
+ * The design system's Link to a page of the app: a click moves to it with no reload, as the
6
+ * router's own Link does, and a click that opens a new tab still does.
7
+ */
8
+ export function RouterLink({ to, ...props }) {
9
+ const href = useHref(to)
10
+ const handleClick = useLinkClickHandler(to)
11
+ return <Link {...props} href={href} onClick={handleClick} />
12
+ }
@@ -0,0 +1,76 @@
1
+ // @if tanstack-query || swr || msw
2
+ // @if ts && sample-data && msw && (tanstack-query || swr)
3
+
4
+ // @elif sample-data && !msw && (tanstack-query || swr)
5
+ import { items } from './items'
6
+
7
+ // @endif
8
+ // @if ts
9
+ /** What the API says to the app. */
10
+ // @if react-hook-form
11
+
12
+ /** What the form sends. */
13
+ // @endif
14
+
15
+ // @endif
16
+ // @if msw
17
+ /** A greeting, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
18
+ export async function fetchGreeting() {
19
+ const response = await fetch('/api/greeting')
20
+ if (!response.ok) throw new Error(`GET /api/greeting answered ${response.status}`)
21
+ return response.json()
22
+ }
23
+ // @else
24
+ /** A greeting, as an API would answer: make it fetch from yours. */
25
+ export function fetchGreeting() {
26
+ return Promise.resolve({ message: 'Hello from src/data/api: make it fetch from your API.' })
27
+ }
28
+ // @endif
29
+ // @if react-hook-form && msw
30
+
31
+ /** Sends the form's values to the API, which answers what it made of them. */
32
+ export async function sendMessage(message) {
33
+ const response = await fetch('/api/messages', {
34
+ method: 'POST',
35
+ headers: { 'Content-Type': 'application/json' },
36
+ body: JSON.stringify(message),
37
+ })
38
+ if (!response.ok) throw new Error(`POST /api/messages answered ${response.status}`)
39
+ return response.json()
40
+ }
41
+ // @elif react-hook-form
42
+
43
+ /** Sends the form's values, as an API would take them: make it post to yours. */
44
+ export function sendMessage(message) {
45
+ return Promise.resolve({ message: `Thanks, ${message.name}: your message was sent.` })
46
+ }
47
+ // @endif
48
+ // @if sample-data && msw && (tanstack-query || swr)
49
+
50
+ /** The items, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
51
+ export async function fetchItems() {
52
+ const response = await fetch('/api/items')
53
+ if (!response.ok) throw new Error(`GET /api/items answered ${response.status}`)
54
+ return response.json()
55
+ }
56
+
57
+ /** One item, from the API. */
58
+ export async function fetchItem(id) {
59
+ const response = await fetch(`/api/items/${id}`)
60
+ if (!response.ok) throw new Error(`GET /api/items/${id} answered ${response.status}`)
61
+ return response.json()
62
+ }
63
+ // @elif sample-data && (tanstack-query || swr)
64
+
65
+ /** The items, as an API would answer: make it fetch from yours. */
66
+ export function fetchItems() {
67
+ return Promise.resolve(items.map(({ id, title, status }) => ({ id, title, status })))
68
+ }
69
+
70
+ /** One item, as an API would answer: make it fetch from yours. */
71
+ export function fetchItem(id) {
72
+ const item = items.find((candidate) => candidate.id === id)
73
+ return item ? Promise.resolve(item) : Promise.reject(new Error(`There is no item ${id}.`))
74
+ }
75
+ // @endif
76
+ // @endif
@@ -0,0 +1,38 @@
1
+ import { StrictMode } from 'react'
2
+ import { createRoot } from 'react-dom/client'
3
+ // @if redux-toolkit
4
+ import { Provider as StoreProvider } from 'react-redux'
5
+ // @endif
6
+ import { createBrowserRouter } from 'react-router'
7
+ import { RouterProvider } from 'react-router/dom'
8
+ // @if msw
9
+ import { startMocks } from './mocks/start'
10
+ // @endif
11
+ import { routes } from './routes'
12
+ // @if redux-toolkit
13
+ import { store } from './state/store'
14
+ // @endif
15
+
16
+ // @if msw
17
+ // The mock API starts first, so it answers the app's first request.
18
+ await startMocks()
19
+
20
+ // @endif
21
+ /** The router: it shows the page the URL names, and moves between pages with no reload. */
22
+ const router = createBrowserRouter(routes)
23
+
24
+ // @if redux-toolkit
25
+ createRoot(document.getElementById('root')).render(
26
+ <StrictMode>
27
+ <StoreProvider store={store}>
28
+ <RouterProvider router={router} />
29
+ </StoreProvider>
30
+ </StrictMode>
31
+ )
32
+ // @else
33
+ createRoot(document.getElementById('root')).render(
34
+ <StrictMode>
35
+ <RouterProvider router={router} />
36
+ </StrictMode>
37
+ )
38
+ // @endif
@@ -0,0 +1,14 @@
1
+ import { Flex } from '@soroush.tech/design-system/Flex'
2
+ import { Typography } from '@soroush.tech/design-system/Typography'
3
+ import { RouterLink } from '../components/RouterLink'
4
+
5
+ /** Shown for a path with no page, and when a page fails to load or to render. */
6
+ export function ErrorPage() {
7
+ return (
8
+ <Flex gap={2} alignItems="flex-start">
9
+ <Typography variant="h2">Page not found</Typography>
10
+ <Typography color="secondary">This page does not exist, or it could not be shown.</Typography>
11
+ <RouterLink to="/">Back home</RouterLink>
12
+ </Flex>
13
+ )
14
+ }
@@ -0,0 +1,141 @@
1
+ // @if tanstack-query || (!zustand && !jotai && !redux-toolkit)
2
+ import { useState } from 'react'
3
+ // @endif
4
+ import { Button } from '@soroush.tech/design-system/Button'
5
+ import { Flex } from '@soroush.tech/design-system/Flex'
6
+ import { Typography } from '@soroush.tech/design-system/Typography'
7
+ import { View } from '@soroush.tech/design-system/View'
8
+ // @if tanstack-query
9
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
10
+ // @endif
11
+ import { Outlet } from 'react-router'
12
+ // @if zustand
13
+ import { useScheme } from '../state/scheme'
14
+ // @elif jotai
15
+ import { useAtom } from 'jotai'
16
+ import { schemeAtom } from '../state/scheme'
17
+ // @elif redux-toolkit
18
+ import { useDispatch, useSelector } from 'react-redux'
19
+ import { schemeChanged } from '../state/store'
20
+ // @endif
21
+ import { RouterLink } from '../components/RouterLink'
22
+ import { Providers } from '../theme/Providers'
23
+
24
+ /**
25
+ // @if zustand || jotai
26
+ * The frame every page renders in: the router draws the page in its Outlet. The app's providers
27
+ * live here, in the app itself, so the scheme, held in src/state/scheme.js, can switch the theme.
28
+ // @elif redux-toolkit
29
+ * The frame every page renders in: the router draws the page in its Outlet. The app's providers
30
+ * live here, in the app itself, so the scheme, held in src/state/store.js, can switch the theme.
31
+ // @else
32
+ * The frame every page renders in: the router draws the page in its Outlet. The app's providers
33
+ * live here, in the app itself, so the scheme it holds can switch the theme.
34
+ // @endif
35
+ */
36
+ export function Layout() {
37
+ // @if tanstack-query
38
+ const [queryClient] = useState(() => new QueryClient())
39
+ // @endif
40
+ // @if zustand
41
+ const { scheme, setScheme } = useScheme()
42
+ // @elif jotai
43
+ const [scheme, setScheme] = useAtom(schemeAtom)
44
+ // @elif redux-toolkit
45
+ const scheme = useSelector((state) => state.scheme)
46
+ const dispatch = useDispatch()
47
+ const setScheme = (next) => dispatch(schemeChanged(next))
48
+ // @else
49
+ const [scheme, setScheme] = useState('__SCHEME__')
50
+ // @endif
51
+ const otherScheme = scheme === 'light' ? 'dark' : 'light'
52
+ // @if tanstack-query
53
+ return (
54
+ <QueryClientProvider client={queryClient}>
55
+ <Providers scheme={scheme}>
56
+ <Flex
57
+ as="header"
58
+ flexDirection="row"
59
+ alignItems="center"
60
+ justifyContent="space-between"
61
+ gap={2}
62
+ px={3}
63
+ py={2}
64
+ bg="paper"
65
+ >
66
+ <Typography variant="h6" as="span">
67
+ __TITLE__
68
+ </Typography>
69
+ <Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
70
+ <RouterLink to="/" underline="hover">
71
+ Home
72
+ </RouterLink>
73
+ <RouterLink to="/about" underline="hover">
74
+ About
75
+ </RouterLink>
76
+ {/* @if sample-data */}
77
+ <RouterLink to="/items" underline="hover">
78
+ Items
79
+ </RouterLink>
80
+ {/* @endif */}
81
+ {/* @if react-hook-form */}
82
+ <RouterLink to="/form" underline="hover">
83
+ Form
84
+ </RouterLink>
85
+ {/* @endif */}
86
+ <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
87
+ {otherScheme}
88
+ </Button>
89
+ </Flex>
90
+ </Flex>
91
+ <View as="main" px={3} py={4}>
92
+ <Outlet />
93
+ </View>
94
+ </Providers>
95
+ </QueryClientProvider>
96
+ )
97
+ // @else
98
+ return (
99
+ <Providers scheme={scheme}>
100
+ <Flex
101
+ as="header"
102
+ flexDirection="row"
103
+ alignItems="center"
104
+ justifyContent="space-between"
105
+ gap={2}
106
+ px={3}
107
+ py={2}
108
+ bg="paper"
109
+ >
110
+ <Typography variant="h6" as="span">
111
+ __TITLE__
112
+ </Typography>
113
+ <Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
114
+ <RouterLink to="/" underline="hover">
115
+ Home
116
+ </RouterLink>
117
+ <RouterLink to="/about" underline="hover">
118
+ About
119
+ </RouterLink>
120
+ {/* @if sample-data */}
121
+ <RouterLink to="/items" underline="hover">
122
+ Items
123
+ </RouterLink>
124
+ {/* @endif */}
125
+ {/* @if react-hook-form */}
126
+ <RouterLink to="/form" underline="hover">
127
+ Form
128
+ </RouterLink>
129
+ {/* @endif */}
130
+ <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
131
+ {otherScheme}
132
+ </Button>
133
+ </Flex>
134
+ </Flex>
135
+ <View as="main" px={3} py={4}>
136
+ <Outlet />
137
+ </View>
138
+ </Providers>
139
+ )
140
+ // @endif
141
+ }
@@ -0,0 +1,18 @@
1
+ import { useState } from 'react'
2
+ import { Button } from '@soroush.tech/design-system/Button'
3
+ import { Flex } from '@soroush.tech/design-system/Flex'
4
+ import { Typography } from '@soroush.tech/design-system/Typography'
5
+
6
+ /** A count, held in the component's own state, and the button that adds to it. */
7
+ export function Counter() {
8
+ const [count, setCount] = useState(0)
9
+ const increment = () => setCount(count + 1)
10
+ return (
11
+ <Flex flexDirection="row" alignItems="center" gap={2}>
12
+ <Button size="sm" variant="outlined" onClick={increment}>
13
+ Count
14
+ </Button>
15
+ <Typography>Count: {count}</Typography>
16
+ </Flex>
17
+ )
18
+ }