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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/dist/bin.mjs +1 -1
  2. package/dist/index.d.mts +4 -4
  3. package/dist/index.mjs +1 -1
  4. package/dist/{io-Bybgct-f.mjs → io-C_8_JerZ.mjs} +368 -1039
  5. package/package.json +13 -5
  6. package/templates/astro/js/base/README.md +93 -0
  7. package/templates/astro/js/base/_gitignore +5 -0
  8. package/templates/astro/js/base/astro.config.mjs +23 -0
  9. package/templates/astro/js/base/src/components/About.jsx +24 -0
  10. package/templates/astro/js/base/src/components/Counter.jsx +26 -0
  11. package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
  12. package/templates/astro/js/base/src/components/Header.jsx +99 -0
  13. package/templates/astro/js/base/src/components/Home.jsx +32 -0
  14. package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
  15. package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
  16. package/templates/astro/js/base/src/data/api.js +84 -0
  17. package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
  18. package/templates/astro/js/base/src/pages/404.astro +12 -0
  19. package/templates/astro/js/base/src/pages/about.astro +12 -0
  20. package/templates/astro/js/base/src/pages/index.astro +23 -0
  21. package/templates/astro/js/base/src/theme/Island.jsx +89 -0
  22. package/templates/astro/js/base/src/theme/scheme.js +32 -0
  23. package/templates/astro/js/base/vitest.config.js +12 -0
  24. package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
  25. package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
  26. package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
  27. package/templates/astro/js/sample-data/src/data/items.js +65 -0
  28. package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
  29. package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
  30. package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
  31. package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
  32. package/templates/astro/js/with/msw/_env +3 -0
  33. package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
  34. package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
  35. package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
  36. package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
  37. package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
  38. package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
  39. package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
  40. package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
  41. package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
  42. package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
  43. package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
  44. package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
  45. package/templates/astro/ts/base/README.md +93 -0
  46. package/templates/astro/ts/base/_gitignore +5 -0
  47. package/templates/astro/ts/base/astro.config.mjs +23 -0
  48. package/templates/astro/ts/base/src/components/About.tsx +24 -0
  49. package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
  50. package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
  51. package/templates/astro/ts/base/src/components/Header.tsx +99 -0
  52. package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
  53. package/templates/astro/ts/base/src/components/Home.tsx +33 -0
  54. package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
  55. package/templates/astro/ts/base/src/data/api.ts +92 -0
  56. package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
  57. package/templates/astro/ts/base/src/pages/404.astro +12 -0
  58. package/templates/astro/ts/base/src/pages/about.astro +12 -0
  59. package/templates/astro/ts/base/src/pages/index.astro +23 -0
  60. package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
  61. package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
  62. package/templates/astro/ts/base/tsconfig.json +9 -0
  63. package/templates/astro/ts/base/vitest.config.ts +12 -0
  64. package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
  65. package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
  66. package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
  67. package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
  68. package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
  69. package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
  70. package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
  71. package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
  72. package/templates/astro/ts/with/msw/_env +3 -0
  73. package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
  74. package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
  75. package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
  76. package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
  77. package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
  78. package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
  79. package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
  80. package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
  81. package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
  82. package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
  83. package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
  84. package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
  85. package/templates/templates.md +66 -0
  86. package/templates/vike/js/base/README.md +87 -0
  87. package/templates/vike/js/base/_gitignore +4 -0
  88. package/templates/vike/js/base/src/data/api.js +76 -0
  89. package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
  90. package/templates/vike/js/base/src/pages/+config.js +12 -0
  91. package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
  92. package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
  93. package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
  94. package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
  95. package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
  96. package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
  97. package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
  98. package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
  99. package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
  100. package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
  101. package/templates/vike/js/base/vite.config.js +14 -0
  102. package/templates/vike/js/base/vitest.config.js +13 -0
  103. package/templates/vike/js/sample-data/src/data/items.js +58 -0
  104. package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
  105. package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
  106. package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
  107. package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
  108. package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
  109. package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
  110. package/templates/vike/js/spa/src/pages/+config.js +12 -0
  111. package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +25 -0
  112. package/templates/vike/js/spa/src/pages/+onRenderHtml.jsx +16 -0
  113. package/templates/vike/js/spa/src/pages/about/+Page.jsx +14 -0
  114. package/templates/vike/js/ssg/src/pages/+config.js +11 -0
  115. package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
  116. package/templates/vike/js/ssr/src/pages/+config.js +12 -0
  117. package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
  118. package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
  119. package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
  120. package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
  121. package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
  122. package/templates/vike/js/with/msw/_env +3 -0
  123. package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
  124. package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
  125. package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
  126. package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
  127. package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
  128. package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
  129. package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
  130. package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
  131. package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
  132. package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
  133. package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
  134. package/templates/vike/ts/base/README.md +87 -0
  135. package/templates/vike/ts/base/_gitignore +4 -0
  136. package/templates/vike/ts/base/src/data/api.ts +84 -0
  137. package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
  138. package/templates/vike/ts/base/src/pages/+config.ts +14 -0
  139. package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
  140. package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
  141. package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
  142. package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
  143. package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
  144. package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
  145. package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
  146. package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
  147. package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
  148. package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
  149. package/templates/vike/ts/base/src/vike.d.ts +20 -0
  150. package/templates/vike/ts/base/tsconfig.json +18 -0
  151. package/templates/vike/ts/base/vite.config.ts +14 -0
  152. package/templates/vike/ts/base/vitest.config.ts +13 -0
  153. package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
  154. package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
  155. package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
  156. package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
  157. package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
  158. package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
  159. package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
  160. package/templates/vike/ts/spa/src/pages/+config.ts +14 -0
  161. package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +27 -0
  162. package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +17 -0
  163. package/templates/vike/ts/spa/src/pages/about/+Page.tsx +14 -0
  164. package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
  165. package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
  166. package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
  167. package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
  168. package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
  169. package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
  170. package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
  171. package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
  172. package/templates/vike/ts/with/msw/_env +3 -0
  173. package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
  174. package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
  175. package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
  176. package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
  177. package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
  178. package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
  179. package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
  180. package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
  181. package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
  182. package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
  183. package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
@@ -0,0 +1,27 @@
1
+ import { Button } from '@soroush.tech/design-system/Button'
2
+ import { Flex } from '@soroush.tech/design-system/Flex'
3
+ import { Typography } from '@soroush.tech/design-system/Typography'
4
+ import { withIsland } from '../theme/Island'
5
+ import { useDispatch, useSelector } from 'react-redux'
6
+ import { incremented, type RootState } from '../state/store'
7
+
8
+ /**
9
+ * A count, held in the Redux store, src/state/store, and the button that adds to it.
10
+ * An island of its own, which renders in the browser only: the count it shows is the store's,
11
+ * not the HTML's.
12
+ */
13
+ function Counter() {
14
+ const count = useSelector((state: RootState) => state.count)
15
+ const dispatch = useDispatch()
16
+ const increment = () => dispatch(incremented())
17
+ return (
18
+ <Flex flexDirection="row" alignItems="center" gap={2}>
19
+ <Button size="sm" variant="outlined" onClick={increment}>
20
+ Count
21
+ </Button>
22
+ <Typography>Count: {count}</Typography>
23
+ </Flex>
24
+ )
25
+ }
26
+
27
+ export default withIsland(Counter)
@@ -0,0 +1,19 @@
1
+ import { configureStore, createSlice } from '@reduxjs/toolkit'
2
+
3
+ /** The home page's count: a slice of the store. */
4
+ const countSlice = createSlice({
5
+ name: 'count',
6
+ initialState: 0,
7
+ reducers: {
8
+ incremented: (count) => count + 1,
9
+ },
10
+ })
11
+
12
+ export const { incremented } = countSlice.actions
13
+
14
+ /** The app's Redux store, which every island's Island provides: one store, for all of them. */
15
+ export const store = configureStore({ reducer: { count: countSlice.reducer } })
16
+ // @if ts
17
+
18
+ export type RootState = ReturnType<typeof store.getState>
19
+ // @endif
@@ -0,0 +1,13 @@
1
+ import { QueryClient } from '@tanstack/react-query'
2
+
3
+ let browserClient: QueryClient | undefined
4
+
5
+ /**
6
+ * The query client of an island. In the browser every island gets the same one, so they share its
7
+ * cache; on the server each render gets its own, so no request sees another's data.
8
+ */
9
+ export function getQueryClient() {
10
+ if (typeof window === 'undefined') return new QueryClient()
11
+ browserClient ??= new QueryClient()
12
+ return browserClient
13
+ }
@@ -0,0 +1,24 @@
1
+ import { Button } from '@soroush.tech/design-system/Button'
2
+ import { Flex } from '@soroush.tech/design-system/Flex'
3
+ import { Typography } from '@soroush.tech/design-system/Typography'
4
+ import { withIsland } from '../theme/Island'
5
+ import { useCounter } from '../state/counter'
6
+
7
+ /**
8
+ * A count, held in a zustand store, src/state/counter, and the button that adds to it.
9
+ * An island of its own, which renders in the browser only: the count it shows is the store's,
10
+ * not the HTML's.
11
+ */
12
+ function Counter() {
13
+ const { count, increment } = useCounter()
14
+ return (
15
+ <Flex flexDirection="row" alignItems="center" gap={2}>
16
+ <Button size="sm" variant="outlined" onClick={increment}>
17
+ Count
18
+ </Button>
19
+ <Typography>Count: {count}</Typography>
20
+ </Flex>
21
+ )
22
+ }
23
+
24
+ export default withIsland(Counter)
@@ -0,0 +1,19 @@
1
+ import { create } from 'zustand'
2
+
3
+ interface CounterState {
4
+ count: number
5
+ increment: () => void
6
+ }
7
+
8
+ /** The home page's count: a zustand store, which any component can read. */
9
+ // @if ts
10
+ export const useCounter = create<CounterState>()((set) => ({
11
+ count: 0,
12
+ increment: () => set((state) => ({ count: state.count + 1 })),
13
+ }))
14
+ // @else
15
+ export const useCounter = create((set) => ({
16
+ count: 0,
17
+ increment: () => set((state) => ({ count: state.count + 1 })),
18
+ }))
19
+ // @endif
@@ -0,0 +1,66 @@
1
+ # The starters' templates
2
+
3
+ `create` writes an app from these files. Each framework has a folder, and each language a folder
4
+ in it: `<framework>/ts/` is written by hand, and `<framework>/js/` is made from it - by the
5
+ package's build and its tests - and kept out of git.
6
+
7
+ ## Layers
8
+
9
+ A framework's TypeScript folder holds layers, laid in this order, each over the ones before it:
10
+
11
+ 1. `base/`: the isomorphic app, with no library and no sample data.
12
+ 2. `<rendering>/`: the files a rendering writes in place of the base's, such as `ssr/`.
13
+ 3. `sample-data/`: the items pages.
14
+ 4. `with/<library>/`: a library's own files, in the order `create` offers the libraries.
15
+
16
+ A file a later layer has takes the place of the same file below it. A file left empty once its
17
+ markers are applied is not written, so a whole file can sit inside one `@if`.
18
+
19
+ ## Markers
20
+
21
+ A line of its own, in the comment the file's language has, keeps or drops the lines up to the
22
+ next marker:
23
+
24
+ ```tsx
25
+ // @if tanstack-query
26
+ import { useQuery } from '@tanstack/react-query'
27
+ // @elif swr
28
+ import useSWR from 'swr'
29
+ // @else
30
+ import { useState } from 'react'
31
+ // @endif
32
+ ```
33
+
34
+ - In JSX and in an `.astro` page: `{/* @if msw */}` ... `{/* @endif */}`. In Markdown:
35
+ `<!-- @if msw -->`.
36
+ - A condition holds names, `!`, `&&`, `||` and parentheses. The names are the app's rendering
37
+ (`isomorphic`, `ssr`, `spa`, `ssg`), its libraries (`tanstack-query`, `msw`, ...),
38
+ `sample-data`, and `ts` or `js`.
39
+ - **Every branch is whole** - a statement, a JSX element, a list item - so the file parses with
40
+ all its branches in it: the formatter reads it, and so does the JavaScript build. A name
41
+ declared in two branches is fine; a tag opened in one branch and closed in another is not.
42
+ - **What only TypeScript has goes in `// @if ts`** when taking the types out would leave something
43
+ behind: the doc comment over an interface, the blank line before a type. `// @if ts` and
44
+ `// @else` also hold two forms of a statement whose JavaScript is not its TypeScript stripped.
45
+
46
+ ## Placeholders
47
+
48
+ `__TITLE__` (the app's title), `__TITLE_STRING__` (the title, quoted for a string in single
49
+ quotes) and `__SCHEME__` (`light` or `dark`). A path a text names is written as TypeScript's
50
+ (`src/theme/themes.ts`): the JavaScript build turns it to `.js`.
51
+
52
+ ## Names npm would drop
53
+
54
+ `_gitignore` is written as `.gitignore`, and `_env` as `.env`.
55
+
56
+ ## The JavaScript templates
57
+
58
+ `pnpm gen:templates` makes `<framework>/js/` from `<framework>/ts/`: it takes the types out of each
59
+ `.ts` and `.tsx` file and of an `.astro` file's frontmatter (`ts-blank-space`), drops a line that
60
+ held only types, turns the paths a text names and the file names to `.js` and `.jsx`, leaves out
61
+ `tsconfig.json` and `.d.ts` files, and formats the result. `pnpm build` runs it before the code is
62
+ built, so the published package holds both languages, and so do the tests, in their global setup.
63
+
64
+ Lint skips the templates, since a template is not a program until its markers are applied; the
65
+ formatter reads the TypeScript ones. The apps they write are checked as the fixtures in
66
+ `type-tests/create-*`.
@@ -0,0 +1,87 @@
1
+ # __TITLE__
2
+
3
+ A [Vike](https://vike.dev) app on the [@soroush.tech design system](https://docs.soroush.tech):
4
+ <!-- @if isomorphic -->
5
+ prerendered at build time with its styles in the HTML, then moving between pages with no reload.
6
+ <!-- @elif ssr -->
7
+ every page rendered on the server, on each request, with its styles in the HTML, then moving between pages with no reload.
8
+ <!-- @elif spa -->
9
+ a single-page app: each page is an HTML shell from the build, rendered in the browser, which moves between pages with no reload.
10
+ <!-- @else -->
11
+ every page prerendered to HTML at build time, with its styles, and no JavaScript in the browser.
12
+ <!-- @endif -->
13
+
14
+ - `npm run dev` starts it, `npm run build` builds it, and `npm run preview` serves the build.
15
+ <!-- @if ssr -->
16
+ - `npm start` runs the server the build wrote, on `PORT` (3000 by default), on any Node host.
17
+ <!-- @endif -->
18
+ - `src/pages/` holds the pages: a folder per route, each with its `+Page`.
19
+ - `src/pages/+Layout.jsx` is the frame every page renders in, inside the app's providers.
20
+ - `src/theme/themes.js` holds the light and the dark theme: make them your own.
21
+ <!-- @if isomorphic -->
22
+ - In `src/pages/`, `+onRenderHtml` and `+onRenderClient` render the pages, with the styles each page uses in its HTML.
23
+ <!-- @elif ssr -->
24
+ - In `src/pages/`, `+onRenderHtml` renders each page on the server and `+onRenderClient` takes it over in the browser, with the styles each page uses in its HTML.
25
+ <!-- @elif spa -->
26
+ - In `src/pages/`, `+onRenderHtml` writes the HTML shell of each page, and `+onRenderClient` renders the page in it.
27
+ <!-- @else -->
28
+ - In `src/pages/`, `+onRenderHtml` renders the pages to HTML, with the styles each page uses.
29
+ <!-- @endif -->
30
+ <!-- @if sample-data -->
31
+ - `src/pages/items/` lists the sample items of `src/data/items.js`, with a page for each.
32
+ <!-- @if ssr -->
33
+ Their `+data` hooks read the data on each request: make them fetch your own.
34
+ <!-- @else -->
35
+ Their `+data` hooks read the data at build time: make them fetch your own.
36
+ <!-- @endif -->
37
+ <!-- @endif -->
38
+ <!-- @if !ssg && !zustand && !jotai && !redux-toolkit -->
39
+ - `src/pages/index/Counter.jsx` counts the presses of its button, in its own state.
40
+ <!-- @endif -->
41
+ <!-- @if tanstack-query && react-hook-form && sample-data -->
42
+ - [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. The items pages start on their build-time data, then query it in the browser.
43
+ <!-- @elif tanstack-query && react-hook-form -->
44
+ - [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.
45
+ <!-- @elif tanstack-query && sample-data -->
46
+ - [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. The items pages start on their build-time data, then query it in the browser.
47
+ <!-- @elif tanstack-query -->
48
+ - [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.
49
+ <!-- @endif -->
50
+ <!-- @if swr && react-hook-form && sample-data -->
51
+ - [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. The items pages start on their build-time data, then fetch it in the browser.
52
+ <!-- @elif swr && react-hook-form -->
53
+ - [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.
54
+ <!-- @elif swr && sample-data -->
55
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.js` with SWR. The items pages start on their build-time data, then fetch it in the browser.
56
+ <!-- @elif swr -->
57
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.js` with SWR.
58
+ <!-- @endif -->
59
+ <!-- @if msw && react-hook-form && sample-data -->
60
+ - [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.
61
+ <!-- @elif msw && react-hook-form -->
62
+ - [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.
63
+ <!-- @elif msw && sample-data -->
64
+ - [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.
65
+ <!-- @elif msw -->
66
+ - [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.
67
+ <!-- @endif -->
68
+ <!-- @if zustand -->
69
+ - [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.
70
+ <!-- @elif jotai -->
71
+ - [Jotai](https://jotai.org): `src/state/scheme.js` holds the color scheme and `src/state/counter.js` the home page's count, in atoms.
72
+ <!-- @elif redux-toolkit -->
73
+ - [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 `PageShell` provides.
74
+ <!-- @endif -->
75
+ <!-- @if react-hook-form -->
76
+ - [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): `/form` is a form whose values a Zod schema checks.
77
+ <!-- @endif -->
78
+ <!-- @if ssg -->
79
+ - [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.
80
+ <!-- @elif msw -->
81
+ - [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.
82
+ <!-- @else -->
83
+ - [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.
84
+ <!-- @endif -->
85
+
86
+ Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
87
+ https://docs.soroush.tech/design-system/getting-started/installation/#fonts
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ dist/
3
+ *.log
4
+ .DS_Store
@@ -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,183 @@
1
+ // @if !ssg && (tanstack-query || (!zustand && !jotai && !redux-toolkit))
2
+ import { useState } from 'react'
3
+ // @else
4
+ // @endif
5
+ // @if !ssg
6
+ import { Button } from '@soroush.tech/design-system/Button'
7
+ // @endif
8
+ import { Flex } from '@soroush.tech/design-system/Flex'
9
+ import { Link } from '@soroush.tech/design-system/Link'
10
+ import { Typography } from '@soroush.tech/design-system/Typography'
11
+ import { View } from '@soroush.tech/design-system/View'
12
+ // @if tanstack-query
13
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
14
+ // @endif
15
+ // @if zustand
16
+ import { useScheme } from '../state/scheme'
17
+ // @elif jotai
18
+ import { useAtom } from 'jotai'
19
+ import { schemeAtom } from '../state/scheme'
20
+ // @elif redux-toolkit
21
+ import { useDispatch, useSelector } from 'react-redux'
22
+ import { schemeChanged } from '../state/store'
23
+ // @endif
24
+ import { Providers } from '../theme/Providers'
25
+
26
+ /**
27
+ // @if ssg
28
+ * The frame every page renders in, inside the app's providers. A page has no JavaScript in the
29
+ * browser, so it is drawn in the default scheme, which src/theme/Providers.js sets.
30
+ // @elif zustand || jotai
31
+ * The frame every page renders in. The app's providers live here, in the app itself, so the
32
+ * scheme, held in src/state/scheme.js, can switch the theme.
33
+ // @elif redux-toolkit
34
+ * The frame every page renders in. The app's providers live here, in the app itself, so the
35
+ * scheme, held in src/state/store.js, can switch the theme.
36
+ // @else
37
+ * The frame every page renders in. The app's providers live here, in the app itself, so the
38
+ * scheme it holds can switch the theme.
39
+ // @endif
40
+ */
41
+ export default function Layout({ children }) {
42
+ // @if tanstack-query
43
+ const [queryClient] = useState(() => new QueryClient())
44
+ // @endif
45
+ // @if zustand
46
+ const { scheme, setScheme } = useScheme()
47
+ // @elif jotai
48
+ const [scheme, setScheme] = useAtom(schemeAtom)
49
+ // @elif redux-toolkit
50
+ const scheme = useSelector((state) => state.scheme)
51
+ const dispatch = useDispatch()
52
+ const setScheme = (next) => dispatch(schemeChanged(next))
53
+ // @elif !ssg
54
+ const [scheme, setScheme] = useState('__SCHEME__')
55
+ // @endif
56
+ // @if !ssg
57
+ const otherScheme = scheme === 'light' ? 'dark' : 'light'
58
+ // @endif
59
+ // @if ssg
60
+ return (
61
+ <Providers>
62
+ <Flex
63
+ as="header"
64
+ flexDirection="row"
65
+ alignItems="center"
66
+ justifyContent="space-between"
67
+ gap={2}
68
+ px={3}
69
+ py={2}
70
+ bg="paper"
71
+ >
72
+ <Typography variant="h6" as="span">
73
+ __TITLE__
74
+ </Typography>
75
+ <Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
76
+ <Link href="/" underline="hover">
77
+ Home
78
+ </Link>
79
+ <Link href="/about" underline="hover">
80
+ About
81
+ </Link>
82
+ {/* @if sample-data */}
83
+ <Link href="/items" underline="hover">
84
+ Items
85
+ </Link>
86
+ {/* @endif */}
87
+ </Flex>
88
+ </Flex>
89
+ <View as="main" px={3} py={4}>
90
+ {children}
91
+ </View>
92
+ </Providers>
93
+ )
94
+ // @elif tanstack-query
95
+ return (
96
+ <QueryClientProvider client={queryClient}>
97
+ <Providers scheme={scheme}>
98
+ <Flex
99
+ as="header"
100
+ flexDirection="row"
101
+ alignItems="center"
102
+ justifyContent="space-between"
103
+ gap={2}
104
+ px={3}
105
+ py={2}
106
+ bg="paper"
107
+ >
108
+ <Typography variant="h6" as="span">
109
+ __TITLE__
110
+ </Typography>
111
+ <Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
112
+ <Link href="/" underline="hover">
113
+ Home
114
+ </Link>
115
+ <Link href="/about" underline="hover">
116
+ About
117
+ </Link>
118
+ {/* @if sample-data */}
119
+ <Link href="/items" underline="hover">
120
+ Items
121
+ </Link>
122
+ {/* @endif */}
123
+ {/* @if react-hook-form */}
124
+ <Link href="/form" underline="hover">
125
+ Form
126
+ </Link>
127
+ {/* @endif */}
128
+ <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
129
+ {otherScheme}
130
+ </Button>
131
+ </Flex>
132
+ </Flex>
133
+ <View as="main" px={3} py={4}>
134
+ {children}
135
+ </View>
136
+ </Providers>
137
+ </QueryClientProvider>
138
+ )
139
+ // @else
140
+ return (
141
+ <Providers scheme={scheme}>
142
+ <Flex
143
+ as="header"
144
+ flexDirection="row"
145
+ alignItems="center"
146
+ justifyContent="space-between"
147
+ gap={2}
148
+ px={3}
149
+ py={2}
150
+ bg="paper"
151
+ >
152
+ <Typography variant="h6" as="span">
153
+ __TITLE__
154
+ </Typography>
155
+ <Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
156
+ <Link href="/" underline="hover">
157
+ Home
158
+ </Link>
159
+ <Link href="/about" underline="hover">
160
+ About
161
+ </Link>
162
+ {/* @if sample-data */}
163
+ <Link href="/items" underline="hover">
164
+ Items
165
+ </Link>
166
+ {/* @endif */}
167
+ {/* @if react-hook-form */}
168
+ <Link href="/form" underline="hover">
169
+ Form
170
+ </Link>
171
+ {/* @endif */}
172
+ <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
173
+ {otherScheme}
174
+ </Button>
175
+ </Flex>
176
+ </Flex>
177
+ <View as="main" px={3} py={4}>
178
+ {children}
179
+ </View>
180
+ </Providers>
181
+ )
182
+ // @endif
183
+ }
@@ -0,0 +1,12 @@
1
+ // The settings of every page: https://vike.dev/config
2
+ export default {
3
+ // Isomorphic: every page is HTML at build time, then the app moves between pages in the
4
+ // browser, with no reload.
5
+ prerender: true,
6
+ clientRouting: true,
7
+ hydrationCanBeAborted: true,
8
+ // +Layout is a setting of this app's own: the component every page renders inside.
9
+ meta: {
10
+ Layout: { env: { server: true, client: true } },
11
+ },
12
+ }
@@ -0,0 +1,39 @@
1
+ // @if !ssg
2
+ import { createRoot, hydrateRoot } from 'react-dom/client'
3
+ // @if msw
4
+ import { startMocks } from '../mocks/start'
5
+ // @else
6
+ // @endif
7
+ import { PageShell } from './PageShell'
8
+
9
+ let root
10
+ // @if msw
11
+ const mocks = startMocks()
12
+ // @endif
13
+
14
+ /** Takes over the page the server drew, then draws each page the app moves to. */
15
+ // @if msw
16
+ export const onRenderClient = async (pageContext) => {
17
+ await mocks
18
+ const page = <PageShell pageContext={pageContext} />
19
+ const container = document.getElementById('root')
20
+ if (pageContext.isHydration) {
21
+ root = hydrateRoot(container, page)
22
+ } else {
23
+ root ??= createRoot(container)
24
+ root.render(page)
25
+ }
26
+ }
27
+ // @else
28
+ export const onRenderClient = (pageContext) => {
29
+ const page = <PageShell pageContext={pageContext} />
30
+ const container = document.getElementById('root')
31
+ if (pageContext.isHydration) {
32
+ root = hydrateRoot(container, page)
33
+ } else {
34
+ root ??= createRoot(container)
35
+ root.render(page)
36
+ }
37
+ }
38
+ // @endif
39
+ // @endif
@@ -0,0 +1,27 @@
1
+ import createEmotionServer from '@emotion/server/create-instance'
2
+ import { renderToString } from 'react-dom/server'
3
+ import { styleCache } from '@soroush.tech/design-system/engine'
4
+ import { dangerouslySkipEscape, escapeInject } from 'vike/server'
5
+ import { PageShell } from './PageShell'
6
+
7
+ /**
8
+ * Renders the page to HTML - at build time, as the app is prerendered - with the styles it uses
9
+ * in the head, so it is drawn styled before any script runs.
10
+ */
11
+ export const onRenderHtml = (pageContext) => {
12
+ const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(styleCache)
13
+ const html = renderToString(<PageShell pageContext={pageContext} />)
14
+ const styles = constructStyleTagsFromChunks(extractCriticalToChunks(html))
15
+ return escapeInject`<!doctype html>
16
+ <html lang="en">
17
+ <head>
18
+ <meta charset="UTF-8" />
19
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
20
+ <title>__TITLE__</title>
21
+ ${dangerouslySkipEscape(styles)}
22
+ </head>
23
+ <body>
24
+ <div id="root">${dangerouslySkipEscape(html)}</div>
25
+ </body>
26
+ </html>`
27
+ }
@@ -0,0 +1,47 @@
1
+ import { StrictMode } from 'react'
2
+ import { CacheProvider, styleCache } from '@soroush.tech/design-system/engine'
3
+ // @if redux-toolkit
4
+ import { Provider as StoreProvider } from 'react-redux'
5
+ // @endif
6
+ // @if redux-toolkit
7
+ import { store } from '../state/store'
8
+ // @endif
9
+ import { PageContextProvider } from './usePageContext'
10
+
11
+ /**
12
+ * The page inside its +Layout. The Emotion cache is the design system's, so the styles the page
13
+ * uses are found where it is drawn, and every component of the page can read its pageContext.
14
+ */
15
+ export function PageShell({ pageContext }) {
16
+ const { Page, config } = pageContext
17
+ const { Layout } = config
18
+ return (
19
+ <StrictMode>
20
+ <CacheProvider value={styleCache}>
21
+ {/* @if redux-toolkit */}
22
+ <StoreProvider store={store}>
23
+ <PageContextProvider pageContext={pageContext}>
24
+ {Layout ? (
25
+ <Layout>
26
+ <Page />
27
+ </Layout>
28
+ ) : (
29
+ <Page />
30
+ )}
31
+ </PageContextProvider>
32
+ </StoreProvider>
33
+ {/* @else */}
34
+ <PageContextProvider pageContext={pageContext}>
35
+ {Layout ? (
36
+ <Layout>
37
+ <Page />
38
+ </Layout>
39
+ ) : (
40
+ <Page />
41
+ )}
42
+ </PageContextProvider>
43
+ {/* @endif */}
44
+ </CacheProvider>
45
+ </StrictMode>
46
+ )
47
+ }