@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,109 @@
1
+ import { Flex } from '@soroush.tech/design-system/Flex'
2
+ import { Link } from '@soroush.tech/design-system/Link'
3
+ import { Typography } from '@soroush.tech/design-system/Typography'
4
+ // @if tanstack-query
5
+ import { useQuery } from '@tanstack/react-query'
6
+ import { fetchItems } from '../data/api'
7
+ // @elif swr
8
+ import useSWR from 'swr'
9
+ import { fetchItems } from '../data/api'
10
+ // @endif
11
+ import type { Item } from '../data/items'
12
+ import { withIsland } from '../theme/Island'
13
+ import { StatusChip } from './StatusChip'
14
+
15
+ // @if ssr
16
+ /** The list of items, as src/pages/items/index.astro reads it on each request. */
17
+ // @else
18
+ /** The list of items, as src/pages/items/index.astro reads it at build time. */
19
+ // @endif
20
+ // @if tanstack-query
21
+ function ItemList({
22
+ items: initialItems,
23
+ }: {
24
+ readonly items: Pick<Item, 'id' | 'title' | 'status'>[]
25
+ }) {
26
+ // It starts on the items read with the page, then queries them again in the browser.
27
+ const { data: items } = useQuery({
28
+ queryKey: ['items'],
29
+ queryFn: fetchItems,
30
+ initialData: initialItems,
31
+ })
32
+ return (
33
+ <Flex gap={2} px={3} py={4}>
34
+ <Typography variant="h2">Items</Typography>
35
+ <Typography color="secondary">
36
+ {/* @if ssr */}
37
+ Sample data from src/data/items.ts, which this page reads on each request.
38
+ {/* @else */}
39
+ Sample data from src/data/items.ts, which this page reads at build time.
40
+ {/* @endif */}
41
+ </Typography>
42
+ <Flex gap={1}>
43
+ {items.map((item) => (
44
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
45
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
46
+ <StatusChip status={item.status} />
47
+ </Flex>
48
+ ))}
49
+ </Flex>
50
+ </Flex>
51
+ )
52
+ }
53
+ // @elif swr
54
+ function ItemList({
55
+ items: initialItems,
56
+ }: {
57
+ readonly items: Pick<Item, 'id' | 'title' | 'status'>[]
58
+ }) {
59
+ // It starts on the items read with the page, then fetches them again in the browser.
60
+ const { data: items = initialItems } = useSWR('items', fetchItems, {
61
+ fallbackData: initialItems,
62
+ })
63
+ return (
64
+ <Flex gap={2} px={3} py={4}>
65
+ <Typography variant="h2">Items</Typography>
66
+ <Typography color="secondary">
67
+ {/* @if ssr */}
68
+ Sample data from src/data/items.ts, which this page reads on each request.
69
+ {/* @else */}
70
+ Sample data from src/data/items.ts, which this page reads at build time.
71
+ {/* @endif */}
72
+ </Typography>
73
+ <Flex gap={1}>
74
+ {items.map((item) => (
75
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
76
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
77
+ <StatusChip status={item.status} />
78
+ </Flex>
79
+ ))}
80
+ </Flex>
81
+ </Flex>
82
+ )
83
+ }
84
+ // @else
85
+ function ItemList({ items }: { readonly items: Pick<Item, 'id' | 'title' | 'status'>[] }) {
86
+ return (
87
+ <Flex gap={2} px={3} py={4}>
88
+ <Typography variant="h2">Items</Typography>
89
+ <Typography color="secondary">
90
+ {/* @if ssr */}
91
+ Sample data from src/data/items.ts, which this page reads on each request.
92
+ {/* @else */}
93
+ Sample data from src/data/items.ts, which this page reads at build time.
94
+ {/* @endif */}
95
+ </Typography>
96
+ <Flex gap={1}>
97
+ {items.map((item) => (
98
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
99
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
100
+ <StatusChip status={item.status} />
101
+ </Flex>
102
+ ))}
103
+ </Flex>
104
+ </Flex>
105
+ )
106
+ }
107
+ // @endif
108
+
109
+ export default withIsland(ItemList)
@@ -0,0 +1,9 @@
1
+ import { Chip } from '@soroush.tech/design-system/Chip'
2
+ import type { Item } from '../data/items'
3
+
4
+ const COLORS = { todo: 'default', doing: 'primary', done: 'success' } as const
5
+
6
+ /** An item's status, in its palette color. */
7
+ export function StatusChip({ status }: { readonly status: Item['status'] }) {
8
+ return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
9
+ }
@@ -0,0 +1,71 @@
1
+ // @if ts
2
+ /** One sample item. */
3
+ export interface Item {
4
+ id: string
5
+ title: string
6
+ summary: string
7
+ status: 'todo' | 'doing' | 'done'
8
+ }
9
+
10
+ // @endif
11
+ // @if ssr
12
+ /**
13
+ * Sample data, so the items pages have something to show: the pages in src/pages/items read it
14
+ * on each request. Make them fetch your own data instead, and remove this file.
15
+ */
16
+ // @else
17
+ /**
18
+ * Sample data, so the items pages have something to show: the pages in src/pages/items read it
19
+ * at build time. Make them fetch your own data instead, and remove this file.
20
+ */
21
+ // @endif
22
+ export const items: readonly Item[] = [
23
+ {
24
+ id: 'theme',
25
+ title: 'Make the theme your own',
26
+ summary: 'Change the colors and fonts of both themes in src/theme/themes.ts.',
27
+ status: 'doing',
28
+ },
29
+ {
30
+ id: 'fonts',
31
+ title: 'Load the fonts',
32
+ summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
33
+ status: 'todo',
34
+ },
35
+ {
36
+ id: 'pages',
37
+ title: 'Add a page',
38
+ summary: 'An .astro file in src/pages is a new route: it renders your components.',
39
+ status: 'todo',
40
+ },
41
+ {
42
+ id: 'data',
43
+ title: 'Read your own data',
44
+ // @if ssr
45
+ summary: 'The items pages read this file on each request. Make them fetch from your API.',
46
+ // @else
47
+ summary: 'The items pages read this file at build time. Make them fetch from your API.',
48
+ // @endif
49
+ status: 'todo',
50
+ },
51
+ {
52
+ id: 'build',
53
+ title: 'Build it',
54
+ // @if ssr
55
+ summary: 'The server renders every page, these items among them, as HTML with its styles.',
56
+ // @else
57
+ summary: 'The build writes every page, these items among them, as HTML with its styles.',
58
+ // @endif
59
+ status: 'done',
60
+ },
61
+ {
62
+ id: 'deploy',
63
+ title: 'Deploy it',
64
+ // @if ssr
65
+ summary: 'npm start runs the server the build wrote, on any Node host.',
66
+ // @else
67
+ summary: 'The build is static files in dist, which any static host serves.',
68
+ // @endif
69
+ status: 'todo',
70
+ },
71
+ ]
@@ -0,0 +1,39 @@
1
+ ---
2
+ import ItemDetails from '../../components/ItemDetails'
3
+ import Layout from '../../layouts/Layout.astro'
4
+ // @if ssr
5
+ import { items } from '../../data/items'
6
+
7
+ // The item of the id in the URL, read on each request, or the 404 page for an id with no item.
8
+ const item = items.find(({ id }) => id === Astro.params.id)
9
+ if (!item) return Astro.rewrite('/404')
10
+ // @elif ts
11
+ import type { GetStaticPaths, InferGetStaticPropsType } from 'astro'
12
+ import { items } from '../../data/items'
13
+
14
+ /** The pages this route writes at build time: one per item. */
15
+ export const getStaticPaths = (() =>
16
+ items.map((item) => ({ params: { id: item.id }, props: { item } }))) satisfies GetStaticPaths
17
+
18
+ type Props = InferGetStaticPropsType<typeof getStaticPaths>
19
+
20
+ const { item } = Astro.props
21
+ // @else
22
+ import { items } from '../../data/items'
23
+
24
+ /** The pages this route writes at build time: one per item. */
25
+ export function getStaticPaths() {
26
+ return items.map((item) => ({ params: { id: item.id }, props: { item } }))
27
+ }
28
+
29
+ const { item } = Astro.props
30
+ // @endif
31
+ ---
32
+
33
+ <Layout>
34
+ {/* @if ssg */}
35
+ <ItemDetails item={item} />
36
+ {/* @else */}
37
+ <ItemDetails item={item} client:load />
38
+ {/* @endif */}
39
+ </Layout>
@@ -0,0 +1,20 @@
1
+ ---
2
+ import ItemList from '../../components/ItemList'
3
+ import Layout from '../../layouts/Layout.astro'
4
+ import { items } from '../../data/items'
5
+
6
+ // @if ssr
7
+ // What the list shows of each item, read on each request.
8
+ // @else
9
+ // What the list shows of each item, read at build time.
10
+ // @endif
11
+ const summaries = items.map(({ id, title, status }) => ({ id, title, status }))
12
+ ---
13
+
14
+ <Layout>
15
+ {/* @if ssg */}
16
+ <ItemList items={summaries} />
17
+ {/* @else */}
18
+ <ItemList items={summaries} client:load />
19
+ {/* @endif */}
20
+ </Layout>
@@ -0,0 +1,26 @@
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 { useAtom } from 'jotai'
6
+ import { countAtom } from '../state/counter'
7
+
8
+ /**
9
+ * A count, held in a Jotai atom, src/state/counter, 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, setCount] = useAtom(countAtom)
15
+ const increment = () => setCount(count + 1)
16
+ return (
17
+ <Flex flexDirection="row" alignItems="center" gap={2}>
18
+ <Button size="sm" variant="outlined" onClick={increment}>
19
+ Count
20
+ </Button>
21
+ <Typography>Count: {count}</Typography>
22
+ </Flex>
23
+ )
24
+ }
25
+
26
+ export default withIsland(Counter)
@@ -0,0 +1,4 @@
1
+ import { atom } from 'jotai'
2
+
3
+ /** The home page's count: a Jotai atom, which any component can read. */
4
+ export const countAtom = atom(0)
@@ -0,0 +1,3 @@
1
+ # The mock API in src/mocks answers in the browser while this is true. Set it to false once
2
+ # the app's own API answers. Astro hands the browser only the variables named PUBLIC_.
3
+ PUBLIC_MSW=true
@@ -0,0 +1,5 @@
1
+ import { setupWorker } from 'msw/browser'
2
+ import { handlers } from './handlers'
3
+
4
+ /** The mock API in the browser: a service worker answers the requests the handlers match. */
5
+ export const worker = setupWorker(...handlers)
@@ -0,0 +1,24 @@
1
+ import { http, HttpResponse } from 'msw'
2
+ // @if sample-data
3
+ import { items } from '../data/items'
4
+ // @endif
5
+
6
+ /** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
7
+ export const handlers = [
8
+ http.get('/api/greeting', () => HttpResponse.json({ message: 'Hello from the mock API' })),
9
+ // @if react-hook-form
10
+ http.post<never, { name: string }>('/api/messages', async ({ request }) => {
11
+ const { name } = await request.json()
12
+ return HttpResponse.json({ message: `Thanks, ${name}: the mock API took your message.` })
13
+ }),
14
+ // @endif
15
+ // @if sample-data
16
+ http.get('/api/items', () =>
17
+ HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
18
+ ),
19
+ http.get('/api/items/:id', ({ params }) => {
20
+ const item = items.find(({ id }) => id === params.id)
21
+ return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
22
+ }),
23
+ // @endif
24
+ ]
@@ -0,0 +1,5 @@
1
+ import { setupServer } from 'msw/node'
2
+ import { handlers } from './handlers'
3
+
4
+ /** The mock API in Node, for the tests: it answers the requests the handlers match. */
5
+ export const server = setupServer(...handlers)
@@ -0,0 +1,7 @@
1
+ import { afterAll, afterEach, beforeAll } from 'vitest'
2
+ import { server } from './server'
3
+
4
+ // The mock API answers every request a test makes; one it has no handler for fails the test.
5
+ beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
6
+ afterEach(() => server.resetHandlers())
7
+ afterAll(() => server.close())
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The mock API in the browser, started once, while PUBLIC_MSW, in .env, is true: src/data/api waits
3
+ * for it before each request. On the server, and in the tests, where msw's Node server answers,
4
+ * nothing starts. Set PUBLIC_MSW to false once the app's own API answers.
5
+ */
6
+ export const mocks = startMocks()
7
+
8
+ async function startMocks() {
9
+ const isBrowser = typeof window !== 'undefined' && import.meta.env.MODE !== 'test'
10
+ if (!isBrowser || import.meta.env.PUBLIC_MSW !== 'true') return
11
+ const { worker } = await import('./browser')
12
+ // A request the mock API has no handler for - a page the router fetches - goes to the network.
13
+ await worker.start({ onUnhandledFrame: 'bypass' })
14
+ }
@@ -0,0 +1,317 @@
1
+ // @if !tanstack-query && !swr
2
+ import { useState } from 'react'
3
+ // @endif
4
+ import { zodResolver } from '@hookform/resolvers/zod'
5
+ // @if tanstack-query
6
+ import { useMutation } from '@tanstack/react-query'
7
+ // @endif
8
+ import { Controller, useForm } from 'react-hook-form'
9
+ // @if swr
10
+ import useSWRMutation from 'swr/mutation'
11
+ // @endif
12
+ import { z } from 'zod'
13
+ import { Button } from '@soroush.tech/design-system/Button'
14
+ import { Flex } from '@soroush.tech/design-system/Flex'
15
+ import { TextInput } from '@soroush.tech/design-system/TextInput'
16
+ import { Typography } from '@soroush.tech/design-system/Typography'
17
+ import { withIsland } from '../theme/Island'
18
+ // @if tanstack-query || swr || msw
19
+ import { sendMessage } from '../data/api'
20
+ // @endif
21
+
22
+ /** What the form takes, and what it says when a value will not do. */
23
+ const schema = z.object({
24
+ name: z.string().trim().min(1, 'Your name, please.'),
25
+ email: z.email('An email address, please.'),
26
+ })
27
+
28
+ type Values = z.infer<typeof schema>
29
+
30
+ // @if tanstack-query
31
+ /** A form whose values a Zod schema checks; once they pass, it sends them with a mutation of TanStack Query. */
32
+ function Form() {
33
+ const mutation = useMutation({ mutationFn: sendMessage })
34
+ const { control, handleSubmit } = useForm<Values>({
35
+ resolver: zodResolver(schema),
36
+ defaultValues: { name: '', email: '' },
37
+ })
38
+ return (
39
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
40
+ <Typography variant="h2">Form</Typography>
41
+ <Typography color="secondary">
42
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
43
+ </Typography>
44
+ <form noValidate onSubmit={handleSubmit((values) => mutation.mutate(values))}>
45
+ <Flex gap={2} alignItems="flex-start">
46
+ <Controller
47
+ name="name"
48
+ control={control}
49
+ render={({ field, fieldState }) => (
50
+ <Flex gap={1}>
51
+ <TextInput
52
+ name={field.name}
53
+ value={field.value}
54
+ onChange={field.onChange}
55
+ placeholder="Name"
56
+ error={fieldState.invalid}
57
+ inputProps={{
58
+ 'aria-label': 'Name',
59
+ autoComplete: 'name',
60
+ onBlur: field.onBlur,
61
+ }}
62
+ />
63
+ <Typography variant="caption" color="error">
64
+ {fieldState.error?.message}
65
+ </Typography>
66
+ </Flex>
67
+ )}
68
+ />
69
+ <Controller
70
+ name="email"
71
+ control={control}
72
+ render={({ field, fieldState }) => (
73
+ <Flex gap={1}>
74
+ <TextInput
75
+ name={field.name}
76
+ value={field.value}
77
+ onChange={field.onChange}
78
+ type="email"
79
+ placeholder="Email"
80
+ error={fieldState.invalid}
81
+ inputProps={{
82
+ 'aria-label': 'Email',
83
+ autoComplete: 'email',
84
+ onBlur: field.onBlur,
85
+ }}
86
+ />
87
+ <Typography variant="caption" color="error">
88
+ {fieldState.error?.message}
89
+ </Typography>
90
+ </Flex>
91
+ )}
92
+ />
93
+ <Button type="submit">Send</Button>
94
+ </Flex>
95
+ </form>
96
+ {mutation.data ? <Typography>{mutation.data.message}</Typography> : null}
97
+ </Flex>
98
+ )
99
+ }
100
+ // @elif swr
101
+ /** A form whose values a Zod schema checks; once they pass, it sends them with SWR's mutation. */
102
+ function Form() {
103
+ const { trigger, data } = useSWRMutation('messages', (_key: string, { arg }: { arg: Values }) =>
104
+ sendMessage(arg)
105
+ )
106
+ const { control, handleSubmit } = useForm<Values>({
107
+ resolver: zodResolver(schema),
108
+ defaultValues: { name: '', email: '' },
109
+ })
110
+ return (
111
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
112
+ <Typography variant="h2">Form</Typography>
113
+ <Typography color="secondary">
114
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
115
+ </Typography>
116
+ <form noValidate onSubmit={handleSubmit((values) => trigger(values))}>
117
+ <Flex gap={2} alignItems="flex-start">
118
+ <Controller
119
+ name="name"
120
+ control={control}
121
+ render={({ field, fieldState }) => (
122
+ <Flex gap={1}>
123
+ <TextInput
124
+ name={field.name}
125
+ value={field.value}
126
+ onChange={field.onChange}
127
+ placeholder="Name"
128
+ error={fieldState.invalid}
129
+ inputProps={{
130
+ 'aria-label': 'Name',
131
+ autoComplete: 'name',
132
+ onBlur: field.onBlur,
133
+ }}
134
+ />
135
+ <Typography variant="caption" color="error">
136
+ {fieldState.error?.message}
137
+ </Typography>
138
+ </Flex>
139
+ )}
140
+ />
141
+ <Controller
142
+ name="email"
143
+ control={control}
144
+ render={({ field, fieldState }) => (
145
+ <Flex gap={1}>
146
+ <TextInput
147
+ name={field.name}
148
+ value={field.value}
149
+ onChange={field.onChange}
150
+ type="email"
151
+ placeholder="Email"
152
+ error={fieldState.invalid}
153
+ inputProps={{
154
+ 'aria-label': 'Email',
155
+ autoComplete: 'email',
156
+ onBlur: field.onBlur,
157
+ }}
158
+ />
159
+ <Typography variant="caption" color="error">
160
+ {fieldState.error?.message}
161
+ </Typography>
162
+ </Flex>
163
+ )}
164
+ />
165
+ <Button type="submit">Send</Button>
166
+ </Flex>
167
+ </form>
168
+ {data ? <Typography>{data.message}</Typography> : null}
169
+ </Flex>
170
+ )
171
+ }
172
+ // @elif msw
173
+ /** A form whose values a Zod schema checks; once they pass, it sends them to the API. */
174
+ function Form() {
175
+ const [answer, setAnswer] = useState<string | null>(null)
176
+ const { control, handleSubmit } = useForm<Values>({
177
+ resolver: zodResolver(schema),
178
+ defaultValues: { name: '', email: '' },
179
+ })
180
+ return (
181
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
182
+ <Typography variant="h2">Form</Typography>
183
+ <Typography color="secondary">
184
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
185
+ </Typography>
186
+ <form
187
+ noValidate
188
+ onSubmit={handleSubmit(async (values) => setAnswer((await sendMessage(values)).message))}
189
+ >
190
+ <Flex gap={2} alignItems="flex-start">
191
+ <Controller
192
+ name="name"
193
+ control={control}
194
+ render={({ field, fieldState }) => (
195
+ <Flex gap={1}>
196
+ <TextInput
197
+ name={field.name}
198
+ value={field.value}
199
+ onChange={field.onChange}
200
+ placeholder="Name"
201
+ error={fieldState.invalid}
202
+ inputProps={{
203
+ 'aria-label': 'Name',
204
+ autoComplete: 'name',
205
+ onBlur: field.onBlur,
206
+ }}
207
+ />
208
+ <Typography variant="caption" color="error">
209
+ {fieldState.error?.message}
210
+ </Typography>
211
+ </Flex>
212
+ )}
213
+ />
214
+ <Controller
215
+ name="email"
216
+ control={control}
217
+ render={({ field, fieldState }) => (
218
+ <Flex gap={1}>
219
+ <TextInput
220
+ name={field.name}
221
+ value={field.value}
222
+ onChange={field.onChange}
223
+ type="email"
224
+ placeholder="Email"
225
+ error={fieldState.invalid}
226
+ inputProps={{
227
+ 'aria-label': 'Email',
228
+ autoComplete: 'email',
229
+ onBlur: field.onBlur,
230
+ }}
231
+ />
232
+ <Typography variant="caption" color="error">
233
+ {fieldState.error?.message}
234
+ </Typography>
235
+ </Flex>
236
+ )}
237
+ />
238
+ <Button type="submit">Send</Button>
239
+ </Flex>
240
+ </form>
241
+ {answer ? <Typography>{answer}</Typography> : null}
242
+ </Flex>
243
+ )
244
+ }
245
+ // @else
246
+ /** A form whose values a Zod schema checks; once they pass, it shows them. */
247
+ function Form() {
248
+ const [sent, setSent] = useState<Values | null>(null)
249
+ const { control, handleSubmit } = useForm<Values>({
250
+ resolver: zodResolver(schema),
251
+ defaultValues: { name: '', email: '' },
252
+ })
253
+ return (
254
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
255
+ <Typography variant="h2">Form</Typography>
256
+ <Typography color="secondary">
257
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
258
+ </Typography>
259
+ <form noValidate onSubmit={handleSubmit(setSent)}>
260
+ <Flex gap={2} alignItems="flex-start">
261
+ <Controller
262
+ name="name"
263
+ control={control}
264
+ render={({ field, fieldState }) => (
265
+ <Flex gap={1}>
266
+ <TextInput
267
+ name={field.name}
268
+ value={field.value}
269
+ onChange={field.onChange}
270
+ placeholder="Name"
271
+ error={fieldState.invalid}
272
+ inputProps={{
273
+ 'aria-label': 'Name',
274
+ autoComplete: 'name',
275
+ onBlur: field.onBlur,
276
+ }}
277
+ />
278
+ <Typography variant="caption" color="error">
279
+ {fieldState.error?.message}
280
+ </Typography>
281
+ </Flex>
282
+ )}
283
+ />
284
+ <Controller
285
+ name="email"
286
+ control={control}
287
+ render={({ field, fieldState }) => (
288
+ <Flex gap={1}>
289
+ <TextInput
290
+ name={field.name}
291
+ value={field.value}
292
+ onChange={field.onChange}
293
+ type="email"
294
+ placeholder="Email"
295
+ error={fieldState.invalid}
296
+ inputProps={{
297
+ 'aria-label': 'Email',
298
+ autoComplete: 'email',
299
+ onBlur: field.onBlur,
300
+ }}
301
+ />
302
+ <Typography variant="caption" color="error">
303
+ {fieldState.error?.message}
304
+ </Typography>
305
+ </Flex>
306
+ )}
307
+ />
308
+ <Button type="submit">Send</Button>
309
+ </Flex>
310
+ </form>
311
+ {sent ? <Typography>Thanks, {sent.name}: the form was valid.</Typography> : null}
312
+ </Flex>
313
+ )
314
+ }
315
+ // @endif
316
+
317
+ export default withIsland(Form)
@@ -0,0 +1,8 @@
1
+ ---
2
+ import Form from '../components/Form'
3
+ import Layout from '../layouts/Layout.astro'
4
+ ---
5
+
6
+ <Layout>
7
+ <Form client:load />
8
+ </Layout>